Ignore:
Timestamp:
09/02/11 03:35:43 (9 months ago)
Author:
jharrop
Message:

getRelationshipsPart() will create it if it doesn't exist already, similar to behaviour of JAXB lists.

Location:
trunk/docx4j/src/main/java/org/docx4j/openpackaging
Files:
4 edited

Legend:

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

    r1176 r1648  
    4949         * so see Part for definition of a corresponding field.) 
    5050         */ 
    51         protected RelationshipsPart relationships; 
    52  
    53         /** 
    54          * Get the relationship part 
     51        public RelationshipsPart relationships; 
     52 
     53        /** 
     54         * Get the relationship part. 
     55         * From 2.7.1, the part will be created if it 
     56         * doesn't exist.  (SaveToZipFile will only 
     57         * save it if it contains rels)  
    5558         *  
    5659         * @return The relationship part name. 
    5760         */ 
    5861        public RelationshipsPart getRelationshipsPart() { 
     62                return getRelationshipsPart(true); 
     63        } 
     64         
     65        /** 
     66         * Get the relationship part. 
     67         * @since 2.7.1 
     68         *  
     69         * @param create  whether to create it if it doesn't exist 
     70         * @return 
     71         */ 
     72        public RelationshipsPart getRelationshipsPart(boolean createIfAbsent) { 
    5973//                      throws InvalidOperationException { 
    6074                if (this instanceof org.docx4j.openpackaging.parts.relationships.RelationshipsPart) { 
     
    6377                        //throw new InvalidOperationException(); 
    6478                        return null; 
    65                 } else { 
    66                         return relationships; 
    67                 } 
     79                }  
     80                 
     81                if (relationships == null  
     82                                && createIfAbsent) { 
     83                        relationships = RelationshipsPart.createRelationshipsPartForPart(this); 
     84                } 
     85                 
     86                return relationships; 
    6887        } 
    6988 
     
    194213                        throw new InvalidFormatException("You should add your part to the target part, not the target part's relationships part."); 
    195214                } 
    196  
    197                 // Create RelationshipsPart for this part if necessary 
    198                 if (this.getRelationshipsPart() == null )  
    199                         RelationshipsPart.createRelationshipsPartForPart(this); 
    200215                 
    201216                // Now add the targetpart to the relationships 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/LoadFromZipNG.java

    r1555 r1648  
    467467                        Part part) 
    468468        throws Docx4JException, InvalidFormatException { 
     469                 
    469470                RelationshipsPart rrp = null; 
    470471                // recurse via this parts relationships, if it has any 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/SaveToZipFile.java

    r1604 r1648  
    413413                 
    414414                // recurse via this parts relationships, if it has any 
    415                 if (part.getRelationshipsPart()!= null ) { 
    416                         RelationshipsPart rrp = part.getRelationshipsPart(); 
    417                         log.debug("Found relationships " + rrp.getPartName() ); 
    418                         String relPart = PartName.getRelationshipsPartName(resolvedPartUri); 
    419                         log.debug("Cf constructed name " + relPart ); 
    420                          
    421                         //deprecatedSaveRawXmlPart(out, relPart, rrp.getDocument() ); 
    422                         // 2008 06 12 - try this neater method 
    423                         saveRawXmlPart(out, rrp, relPart ); 
    424                          
    425                         addPartsFromRelationships(out, rrp ); 
    426                 } else { 
    427                         log.debug("No relationships for " + resolvedPartUri );                                   
    428                 } 
     415                RelationshipsPart rrp = part.getRelationshipsPart(false); //don't create 
     416                if (rrp!= null ) { 
     417                         
     418                        //log.debug("Found relationships " + rrp.getPartName() ); 
     419                         
     420                        // Only save it if it actually has rels in it 
     421                        if (rrp.getRelationships().getRelationship().size()>0) { 
     422                                 
     423                                String relPart = PartName.getRelationshipsPartName(resolvedPartUri); 
     424                                //log.debug("Cf constructed name " + relPart ); 
     425                                 
     426                                saveRawXmlPart(out, rrp, relPart ); 
     427                                 
     428                                addPartsFromRelationships(out, rrp ); 
     429                        } 
     430                }  
     431//              else { 
     432//                      log.debug("No relationships for " + resolvedPartUri );                                   
     433//              } 
    429434        } 
    430435         
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/relationships/RelationshipsPart.java

    r1488 r1648  
    200200                        Base sourcePart) { 
    201201 
    202                 if (sourcePart.getRelationshipsPart() != null) 
    203                         return sourcePart.getRelationshipsPart(); 
     202                if (sourcePart.relationships != null) 
     203                        return sourcePart.relationships; 
    204204 
    205205                RelationshipsPart rp = null; 
     
    207207                        rp = new RelationshipsPart(sourcePart); 
    208208                } catch (InvalidFormatException e) { 
    209                         // TODO Auto-generated catch block 
    210                         e.printStackTrace(); 
     209                        // shouldn't happen 
     210                        log.error(e); 
    211211                } 
    212212                rp.setPackage(sourcePart.getPackage()); 
    213  
    214                 // sourcePart.setRelationships(rp); 
    215213 
    216214                // Make sure content manager knows how to handle .rels 
Note: See TracChangeset for help on using the changeset viewer.