Changeset 1399 for trunk/docx4j/src/main/java/org/docx4j/openpackaging
- Timestamp:
- 01/31/11 11:29:26 (16 months ago)
- Location:
- trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts
- Files:
-
- 1 added
- 6 edited
-
CustomXmlDataStoragePart.java (modified) (1 diff)
-
JaxbXmlPart.java (modified) (3 diffs)
-
Part.java (modified) (2 diffs)
-
WordprocessingML/BinaryPart.java (modified) (2 diffs)
-
XmlPart.java (modified) (3 diffs)
-
relationships/AlteredParts.java (added)
-
relationships/RelationshipsPart.java (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/CustomXmlDataStoragePart.java
r1310 r1399 173 173 } 174 174 175 public boolean isContentEqual(Part other) throws Docx4JException { 176 177 throw new Docx4JException("Not implemented"); 178 } 175 179 176 180 -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/JaxbXmlPart.java
r1389 r1399 21 21 22 22 23 import java.io.ByteArrayOutputStream; 24 import java.io.InputStream; 25 import java.io.PipedInputStream; 26 import java.io.PipedOutputStream; 27 23 28 import javax.xml.bind.JAXBContext; 24 29 import javax.xml.bind.JAXBException; … … 30 35 import org.docx4j.jaxb.Context; 31 36 import org.docx4j.jaxb.NamespacePrefixMapperUtils; 37 import org.docx4j.openpackaging.exceptions.Docx4JException; 32 38 import org.docx4j.openpackaging.exceptions.InvalidFormatException; 39 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 33 40 34 41 /** OPC Parts are either XML, or binary (or text) documents. … … 251 258 } 252 259 253 260 public boolean isContentEqual(Part other) throws Docx4JException { 261 262 if (!(other instanceof JaxbXmlPart)) 263 return false; 264 265 /* Implementation notes: Either we implement 266 * a notion of equality for all content trees 267 * (ie wml, dml etc), or we marshal to something 268 * and compare that. 269 * 270 * Let's take the marshal approach. 271 * 272 * Question becomes, what is it most efficient 273 * to marshal to? 274 * 275 * Looking at JAXB, probably SAX or a stream. 276 * 277 * Then we want an equality test for one of those, 278 * which returns as soon as inequality is established. 279 * 280 * diffx contains a method boolean equivalent(InputStream xml1, InputStream xml2) 281 * which will do what we want. 282 * 283 * We marshal to an output stream, then need a 284 * way to get an input stream from that. 285 * 286 * Since for now I'm just going to use a byte array for that 287 * (though pipes would be more efficient and possibly worth it for large 288 * MainDocumentPart - calling code could just assume that part is different 289 * though?), 290 * I'll just test the equality of the byte arrays 291 * and be done with it. 292 */ 293 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 294 ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); 295 try { 296 marshal(baos); 297 ((JaxbXmlPart)other).marshal(baos2); 298 } catch (JAXBException e) { 299 throw new Docx4JException("Error marshalling parts", e); 300 } 301 302 return java.util.Arrays.equals(baos.toByteArray(), baos2.toByteArray()); 303 304 } 254 305 255 306 } -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/Part.java
r1378 r1399 26 26 import org.docx4j.openpackaging.Base; 27 27 import org.docx4j.openpackaging.contenttype.ContentType; 28 import org.docx4j.openpackaging.exceptions.Docx4JException; 28 29 import org.docx4j.openpackaging.exceptions.InvalidFormatException; 29 30 import org.docx4j.openpackaging.packages.OpcPackage; … … 223 224 } 224 225 226 public abstract boolean isContentEqual(Part other) throws Docx4JException; 225 227 226 228 } -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/BinaryPart.java
r1300 r1399 30 30 31 31 import org.apache.commons.io.IOUtils; 32 import org.docx4j.openpackaging.exceptions.Docx4JException; 32 33 import org.docx4j.openpackaging.exceptions.InvalidFormatException; 33 34 import org.docx4j.openpackaging.parts.ExternalTarget; 35 import org.docx4j.openpackaging.parts.JaxbXmlPart; 34 36 import org.docx4j.openpackaging.parts.Part; 35 37 import org.docx4j.openpackaging.parts.PartName; … … 173 175 } 174 176 175 public boolean isContentEqual(BinaryPart other) { 177 public boolean isContentEqual(Part other) throws Docx4JException { 178 179 if (!(other instanceof BinaryPart)) 180 return false; 176 181 177 182 ByteBuffer thisBB = getBuffer(); 178 ByteBuffer thatBB = other.getBuffer();183 ByteBuffer thatBB = ((BinaryPart)other).getBuffer(); 179 184 180 185 return thisBB.equals(thatBB); -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/XmlPart.java
r1397 r1399 22 22 23 23 24 import java.io.ByteArrayOutputStream; 24 25 import java.io.InputStream; 25 26 import java.util.HashMap; … … 29 30 30 31 import javax.xml.XMLConstants; 32 import javax.xml.bind.JAXBException; 31 33 import javax.xml.namespace.NamespaceContext; 32 34 import javax.xml.parsers.DocumentBuilder; … … 210 212 } 211 213 214 public boolean isContentEqual(Part other) throws Docx4JException { 215 216 if (!(other instanceof XmlPart)) 217 return false; 218 219 Document doc1 = getDocument(); 220 Document doc2 = ((XmlPart)other).getDocument(); 221 222 return doc1.isEqualNode(doc2); 223 224 } 212 225 213 226 } -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/relationships/RelationshipsPart.java
r1386 r1399 53 53 import java.net.URI; 54 54 import java.net.URISyntaxException; 55 import java.util.ArrayList; 56 import java.util.List; 55 57 56 58 import javax.xml.bind.JAXBException; … … 65 67 import org.docx4j.openpackaging.contenttype.ContentTypeManager; 66 68 import org.docx4j.openpackaging.contenttype.ContentTypes; 69 import org.docx4j.openpackaging.exceptions.Docx4JException; 67 70 import org.docx4j.openpackaging.exceptions.InvalidFormatException; 68 71 import org.docx4j.openpackaging.exceptions.InvalidOperationException; … … 72 75 import org.docx4j.openpackaging.parts.Part; 73 76 import org.docx4j.openpackaging.parts.PartName; 77 import org.docx4j.openpackaging.parts.XmlPart; 78 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 74 79 import org.docx4j.relationships.Relationship; 75 80 import org.docx4j.relationships.Relationships; … … 89 94 // implements Iterable<Relationship> { 90 95 91 private static Logger log ger= Logger.getLogger(RelationshipsPart.class);96 private static Logger log = Logger.getLogger(RelationshipsPart.class); 92 97 93 98 /* Example: … … 306 311 public Part getPart(Relationship r ) { 307 312 308 log.debug(" source is " + sourceP.getPartName().toString() );313 // log.debug(" source is " + sourceP.getPartName().toString() ); 309 314 // eg rId1 points to fonts/font1.odttf 310 315 … … 403 408 } 404 409 nextId = highestId+1; 405 log ger.debug("nextId reset to : " + nextId);410 log.debug("nextId reset to : " + nextId); 406 411 407 412 } … … 850 855 851 856 857 /** 858 * Identify the rels in this relationships part which aren't 859 * in the other 860 * @param otherRP the other RelationshipsPart 861 */ 862 public List<Relationship> uniqueToThis(RelationshipsPart otherRP) { 863 864 List<Relationship> results = new ArrayList<Relationship>(); 865 866 for ( Relationship r : jaxbElement.getRelationship() ) { 867 if (getRelationshipByTarget(otherRP, r.getTarget())==null ) { 868 log.debug("Unique: " + r.getTarget() ); 869 results.add(r); 870 } 871 } 872 return results; 873 } 874 875 /** 876 * Identify the rels in this relationships part which aren't 877 * in the other 878 * @param otherRP the other RelationshipsPart 879 */ 880 public List<Relationship> uniqueToOther(RelationshipsPart otherRP) { 881 882 List<Relationship> results = new ArrayList<Relationship>(); 883 884 for ( Relationship r : otherRP.jaxbElement.getRelationship() ) { 885 if (getRelationshipByTarget(this, r.getTarget())==null ) { 886 results.add(r); 887 } 888 } 889 return results; 890 } 891 892 /** 893 * Identify rels common to both parts, but where rels have different 894 * content. 895 * @param otherRP 896 * @return 897 */ 898 public List<Relationship> differingContent(RelationshipsPart otherRP) throws Docx4JException { 899 900 List<Relationship> results = new ArrayList<Relationship>(); 901 902 for ( Relationship r : jaxbElement.getRelationship() ) { 903 Relationship otherR = getRelationshipByTarget(otherRP, r.getTarget()); 904 if (otherR!=null ) { 905 906 if (r.getTargetMode() !=null && r.getTargetMode().equals("External") ) { 907 if (otherR.getTargetMode() !=null && otherR.getTargetMode().equals("External")) { 908 // Usual case 909 if (!r.getTarget().equals(otherR.getTarget()) ) { 910 // Should never happen, since we matched on target 911 throw new Docx4JException("broken logic!"); 912 //results.add(r); 913 //log.debug("External: " + r.getTarget() ); 914 } 915 916 } else { 917 // eg an image changed from embedded to linked 918 results.add(r); 919 log.debug("External: " + r.getTarget() ); 920 } 921 continue; 922 } 923 924 // Compare content 925 Part thisPart = this.getPart(r); 926 Part otherPart = otherRP.getPart(otherR); 927 if (!thisPart.isContentEqual( otherPart)) { 928 results.add(r); 929 log.debug("Different: " + r.getTarget() ); 930 } 931 } 932 } 933 return results; 934 } 935 936 public static Relationship getRelationshipByTarget(RelationshipsPart rp, String relativeTarget) { 937 938 for ( Relationship r : rp.jaxbElement.getRelationship() ) { 939 if (r.getTarget().equals(relativeTarget) ) { 940 return r; 941 } 942 } 943 return null; 944 } 945 946 public boolean isContentEqual(Part other) throws Docx4JException { 947 948 throw new Docx4JException("Not implemented"); 949 } 950 852 951 853 952 }
Note: See TracChangeset
for help on using the changeset viewer.
