Changeset 1732 for trunk/docx4j/src/main


Ignore:
Timestamp:
12/17/11 14:35:42 (5 months ago)
Author:
jharrop
Message:

MERGEFIELD processing - proof of concept

Location:
trunk/docx4j/src/main/java/org/docx4j/model/fields
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/model/fields/FieldLocator.java

    r1727 r1732  
    1818         * it will still be listed just once 
    1919         */ 
    20         List<P> starts = new ArrayList<P>(); 
    21          
     20        List<P> starts = new ArrayList<P>();     
     21        public List<P> getStarts() { 
     22                return starts; 
     23        } 
     24 
    2225        P currentP; 
    2326        int depth=0; 
  • trunk/docx4j/src/main/java/org/docx4j/model/fields/FieldRef.java

    r1731 r1732  
    305305         
    306306        private JAXBElement<Text> instrText; 
    307         public JAXBElement<Text> getInstrText() { 
    308                 return instrText; 
     307//      public JAXBElement<Text> getInstrText() { 
     308//              return instrText; 
     309//      } 
     310        public String getInstr() { 
     311                return instrText.getValue().getValue(); 
    309312        } 
    310313        public void setInstrText(JAXBElement<Text> instrText) { 
  • trunk/docx4j/src/main/java/org/docx4j/model/fields/FieldsPreprocessor.java

    r1731 r1732  
    100100        } 
    101101         
    102 //      public static boolean supported(String type) { 
    103 //               
    104 //              if (type.contains("MERGEFIELD")) { 
    105 //                      return true; 
    106 //              } else { 
    107 //                      return false; 
    108 //              } 
    109 //      } 
    110102         
    111103        public static P canonicalise(P p, List<FieldRef> fieldRefs) { 
     
    267259        }        
    268260         
    269         public static void main(String[] args) throws Exception { 
    270  
    271                 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load( 
    272                                 new java.io.File( 
    273                                                 System.getProperty("user.dir") + "/mergefield1.docx")); 
    274                  
    275                 complexifyFields(wordMLPackage.getMainDocumentPart() ); 
    276                  
    277                 System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)); 
    278                  
    279                 // canonicalise 
    280                 FieldLocator fl = new FieldLocator(); 
    281                 new TraversalUtil(wordMLPackage.getMainDocumentPart().getContent(), fl); 
    282                 log.info("Found " + fl.starts.size() + " fields "); 
    283                  
    284                 List<FieldRef> fieldRefs = new ArrayList<FieldRef>(); 
    285                  
    286                 for( P p : fl.starts) { 
    287                         int index = ((ContentAccessor)p.getParent()).getContent().indexOf(p); 
    288                         P newP = canonicalise(p, fieldRefs); 
    289                         System.out.println("NewP length: " + newP.getContent().size() ); 
    290                         ((ContentAccessor)p.getParent()).getContent().set(index, newP); 
    291                 } 
    292                  
    293                 // Prove we can put something into the results 
    294                 int counter = 0; 
    295                 for (FieldRef fr : fieldRefs) { 
    296                         fr.setResult("Result" + counter); 
    297                         counter++; 
    298                          
    299                         // If doing an actual mail merge, the begin-separate run is removed, as is the end run 
    300                         fr.getParent().getContent().remove(fr.getBeginRun()); 
    301                         fr.getParent().getContent().remove(fr.getEndRun());                      
    302                 } 
    303                  
    304                 System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)); 
    305                  
    306                 wordMLPackage.save(new java.io.File( 
    307                                 System.getProperty("user.dir") + "/mergefield1-OUT.docx") ); 
    308                  
    309                  
    310         } 
    311261} 
Note: See TracChangeset for help on using the changeset viewer.