Ignore:
Timestamp:
01/11/08 10:04:42 (4 years ago)
Author:
jharrop
Message:

Simplify addition of a new part to the package with convenience method addTargetPart
Change RelationshipsPart? constructor to determine its part name from the name of the source part.

File:
1 edited

Legend:

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

    r112 r118  
    2121 
    2222 
     23import org.apache.log4j.Logger; 
     24import org.docx4j.openpackaging.Base; 
    2325import org.docx4j.openpackaging.parts.DocPropsCorePart; 
    2426import org.docx4j.openpackaging.parts.DocPropsExtendedPart; 
     
    6365         */ 
    6466         
     67        protected static Logger log = Logger.getLogger(WordprocessingMLPackage.class); 
    6568                 
    6669         
     
    7881                if (relationshipType.equals(Namespaces.PROPERTIES_CORE)) { 
    7982                        docPropsCorePart = (DocPropsCorePart)part; 
     83                        log.info("Set shortcut for docPropsCorePart"); 
    8084                        return true;                     
    8185                } else if (relationshipType.equals(Namespaces.PROPERTIES_EXTENDED)) { 
    8286                        docPropsExtendedPart = (DocPropsExtendedPart)part; 
     87                        log.info("Set shortcut for docPropsExtendedPart"); 
    8388                        return true;                     
    8489                } else if (relationshipType.equals(Namespaces.DOCUMENT)) { 
    8590                        mainDoc = (MainDocumentPart)part; 
     91                        log.info("Set shortcut for mainDoc"); 
    8692                        return true;                     
    8793                } else {         
     
    98104                                 
    99105                // Create a package 
    100                 WordprocessingMLPackage p = new WordprocessingMLPackage(); 
     106                WordprocessingMLPackage wmlPack = new WordprocessingMLPackage(); 
    101107 
    102108                // Add a ContentTypeManager to it 
    103109                ContentTypeManager ctm = new ContentTypeManagerImpl(); 
    104                 p.setContentTypeManager(ctm); 
     110                wmlPack.setContentTypeManager(ctm); 
     111                        // TODO - move this to constructor? 
     112 
     113 
     114                // Create main document part 
     115                Part wordDocumentPart = new org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart();          
    105116                 
    106  
    107117                // Create main document part content 
    108118                org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); 
     
    122132                org.docx4j.wml.Document wmlDocumentEl = factory.createDocument(); 
    123133                wmlDocumentEl.setBody(body); 
    124                  
    125  
    126                 // Create main document part 
    127                 Part corePart = new org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart(); 
    128                  
     134                                 
    129135                // Put the content in the part 
    130                 ((org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)corePart).setJaxbElement(wmlDocumentEl); 
     136                ((org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)wordDocumentPart).setJaxbElement(wmlDocumentEl); 
     137                                                 
     138                // Add the main document part to the package relationships 
     139                // (creating it if necessary) 
     140                wmlPack.addTargetPart(wordDocumentPart); 
    131141                                 
    132                 // Make getMainDocumentPart() work 
    133                 p.setPartShortcut(corePart, corePart.getRelationshipType()); 
    134                  
    135                 // Create the PackageRelationships part  
    136                 RelationshipsPart rp = new RelationshipsPart( new PartName("/_rels/.rels"), p ); 
    137                  
    138                 // Make sure content manager knows how to handle .rels 
    139                 ctm.addDefaultContentType("rels", org.docx4j.openpackaging.contenttype.ContentTypes.RELATIONSHIPS_PART); 
    140                  
    141                 // Add it to the package 
    142                 p.setRelationships(rp); 
    143                         // TODO - this should happen automatically? 
    144                                  
    145                 // Add the main document part to the package relationships 
    146                 // and to [Content_Types].xml 
    147                 rp.addPart(corePart, p.getContentTypeManager()); 
    148                  
    149                  
    150142                // Create a styles part 
    151143                Part stylesPart = new org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart(); 
     
    154146                                        .unmarshalDefaultStyles(); 
    155147                         
    156                         // Now we need to add it the main document part's relationships 
    157                         RelationshipsPart docRels = new RelationshipsPart( new PartName("/word/_rels/document.xml.rels"), corePart ); 
    158                                 // TODO - should construct the relationships part name from the source part name 
    159                         corePart.setRelationships(docRels); 
    160                         // TODO - this should happen automatically? 
    161                         docRels.addPart(stylesPart, p.getContentTypeManager());                  
     148                        // Add the styles part to the main document part relationships 
     149                        // (creating it if necessary) 
     150                        wordDocumentPart.addTargetPart(stylesPart); // NB - add it to main doc part, not package!                        
    162151                         
    163152                } catch (Exception e) { 
     
    166155                } 
    167156                // Return the new package 
    168                 return p; 
    169  
     157                return wmlPack; 
    170158                 
    171159        } 
    172  
    173160         
    174161} 
Note: See TracChangeset for help on using the changeset viewer.