Ignore:
Timestamp:
01/31/11 11:29:26 (16 months ago)
Author:
jharrop
Message:

AlteredParts?: identify parts in this pkg which are new or altered (compared to pkg2).

Location:
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/CustomXmlDataStoragePart.java

    r1310 r1399  
    173173        } 
    174174 
     175    public boolean isContentEqual(Part other) throws Docx4JException { 
     176         
     177        throw new Docx4JException("Not implemented"); 
     178    } 
    175179         
    176180         
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/JaxbXmlPart.java

    r1389 r1399  
    2121 
    2222 
     23import java.io.ByteArrayOutputStream; 
     24import java.io.InputStream; 
     25import java.io.PipedInputStream; 
     26import java.io.PipedOutputStream; 
     27 
    2328import javax.xml.bind.JAXBContext; 
    2429import javax.xml.bind.JAXBException; 
     
    3035import org.docx4j.jaxb.Context; 
    3136import org.docx4j.jaxb.NamespacePrefixMapperUtils; 
     37import org.docx4j.openpackaging.exceptions.Docx4JException; 
    3238import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
     39import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 
    3340 
    3441/** OPC Parts are either XML, or binary (or text) documents. 
     
    251258        } 
    252259 
    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    } 
    254305         
    255306} 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/Part.java

    r1378 r1399  
    2626import org.docx4j.openpackaging.Base; 
    2727import org.docx4j.openpackaging.contenttype.ContentType; 
     28import org.docx4j.openpackaging.exceptions.Docx4JException; 
    2829import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
    2930import org.docx4j.openpackaging.packages.OpcPackage; 
     
    223224        } 
    224225 
     226    public abstract boolean isContentEqual(Part other) throws Docx4JException; 
    225227 
    226228} 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/BinaryPart.java

    r1300 r1399  
    3030 
    3131import org.apache.commons.io.IOUtils; 
     32import org.docx4j.openpackaging.exceptions.Docx4JException; 
    3233import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
    3334import org.docx4j.openpackaging.parts.ExternalTarget; 
     35import org.docx4j.openpackaging.parts.JaxbXmlPart; 
    3436import org.docx4j.openpackaging.parts.Part; 
    3537import org.docx4j.openpackaging.parts.PartName; 
     
    173175        } 
    174176         
    175     public boolean isContentEqual(BinaryPart other) { 
     177    public boolean isContentEqual(Part other) throws Docx4JException { 
     178         
     179        if (!(other instanceof BinaryPart)) 
     180                return false; 
    176181         
    177182        ByteBuffer thisBB = getBuffer(); 
    178         ByteBuffer thatBB = other.getBuffer(); 
     183        ByteBuffer thatBB = ((BinaryPart)other).getBuffer(); 
    179184         
    180185        return thisBB.equals(thatBB); 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/XmlPart.java

    r1397 r1399  
    2222 
    2323 
     24import java.io.ByteArrayOutputStream; 
    2425import java.io.InputStream; 
    2526import java.util.HashMap; 
     
    2930 
    3031import javax.xml.XMLConstants; 
     32import javax.xml.bind.JAXBException; 
    3133import javax.xml.namespace.NamespaceContext; 
    3234import javax.xml.parsers.DocumentBuilder; 
     
    210212        } 
    211213         
     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    } 
    212225                 
    213226} 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/relationships/RelationshipsPart.java

    r1386 r1399  
    5353import java.net.URI; 
    5454import java.net.URISyntaxException; 
     55import java.util.ArrayList; 
     56import java.util.List; 
    5557 
    5658import javax.xml.bind.JAXBException; 
     
    6567import org.docx4j.openpackaging.contenttype.ContentTypeManager; 
    6668import org.docx4j.openpackaging.contenttype.ContentTypes; 
     69import org.docx4j.openpackaging.exceptions.Docx4JException; 
    6770import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
    6871import org.docx4j.openpackaging.exceptions.InvalidOperationException; 
     
    7275import org.docx4j.openpackaging.parts.Part; 
    7376import org.docx4j.openpackaging.parts.PartName; 
     77import org.docx4j.openpackaging.parts.XmlPart; 
     78import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 
    7479import org.docx4j.relationships.Relationship; 
    7580import org.docx4j.relationships.Relationships; 
     
    8994        // implements Iterable<Relationship> { 
    9095 
    91         private static Logger logger = Logger.getLogger(RelationshipsPart.class); 
     96        private static Logger log = Logger.getLogger(RelationshipsPart.class); 
    9297         
    9398        /* Example: 
     
    306311        public Part getPart(Relationship r ) { 
    307312                 
    308                 log.debug(" source is  " + sourceP.getPartName().toString() ); 
     313//              log.debug(" source is  " + sourceP.getPartName().toString() ); 
    309314        // eg rId1 points to fonts/font1.odttf 
    310315                 
     
    403408                } 
    404409                nextId = highestId+1;            
    405                 logger.debug("nextId reset to : " + nextId); 
     410                log.debug("nextId reset to : " + nextId); 
    406411                 
    407412        } 
     
    850855     
    851856 
     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         
    852951         
    853952} 
Note: See TracChangeset for help on using the changeset viewer.