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
Files:
5 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 
  • trunk/docx4j/src/main/java/org/docx4j/samples/PartsList.java

    r1232 r1648  
    2929import org.docx4j.XmlUtils; 
    3030import org.docx4j.openpackaging.contenttype.ContentTypeManager; 
     31import org.docx4j.openpackaging.io.SaveToZipFile; 
    3132import org.docx4j.openpackaging.parts.Part; 
    3233import org.docx4j.openpackaging.parts.JaxbXmlPart; 
     
    5354//                      + "/sample-docs/test-docs/header-footer/header_sections_some-linked.xml"; 
    5455//       inputfilepath = System.getProperty("user.dir") + "/sample-docs/xlsx/pivot.xlsm"; 
    55                 inputfilepath = System.getProperty("user.dir") + "/sample-docs/sample-docx.xml"; 
     56                inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.xml"; 
    5657        // inputfilepath = System.getProperty("user.dir") + "/sample-docs/pptx/lines.pptx"; 
    5758                } 
     
    7172                 
    7273                System.out.println(sb.toString()); 
     74                 
     75//              SaveToZipFile saver = new SaveToZipFile(opcPackage); 
     76//              saver.save(System.getProperty("user.dir") + "/out.docx"); 
     77                 
    7378        } 
    7479         
     
    115120                // TODO: order by rel id 
    116121                 
     122//              if (rp.getRelationships().getRelationship().size()==0) { 
     123//                      System.out.println("In rels part .. empty"); 
     124//              } 
     125                 
    117126                for ( Relationship r : rp.getRelationships().getRelationship() ) { 
    118127                         
     
    139148                        } 
    140149                        handled.put(part, part); 
    141                         if (part.getRelationshipsPart()==null) { 
     150                        if (part.getRelationshipsPart(false)==null) { 
    142151                                // sb.append(".. no rels" );                                             
    143152                        } else { 
    144                                 traverseRelationships(wordMLPackage, part.getRelationshipsPart(), sb, indent + "    "); 
     153                                traverseRelationships(wordMLPackage, part.getRelationshipsPart(false), sb, indent + "    "); 
    145154                        } 
    146155                                         
Note: See TracChangeset for help on using the changeset viewer.