Changeset 1639 for trunk/docx4j/src


Ignore:
Timestamp:
08/12/11 03:38:34 (10 months ago)
Author:
jharrop
Message:

Change static OpenDoPEHandler design to instance-based design, with objective of making it thread-safe.

Location:
trunk/docx4j/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java

    r1583 r1639  
    6767public class OpenDoPEHandler { 
    6868         
    69         private static Logger log = Logger.getLogger(OpenDoPEHandler.class);             
     69        private static Logger log = Logger.getLogger(OpenDoPEHandler.class);     
     70         
     71        public OpenDoPEHandler(WordprocessingMLPackage wordMLPackage) throws Docx4JException { 
     72 
     73                this.wordMLPackage = wordMLPackage; 
     74                 
     75                MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();                     
     76                if (wordMLPackage.getMainDocumentPart().getXPathsPart()==null) { 
     77                        throw new Docx4JException("OpenDoPE XPaths part missing"); 
     78                } else { 
     79                        xPaths = wordMLPackage.getMainDocumentPart().getXPathsPart().getJaxbElement(); 
     80                        log.debug( XmlUtils.marshaltoString(xPaths, true, true)); 
     81                } 
     82                if (wordMLPackage.getMainDocumentPart().getConditionsPart()!=null) { 
     83                        conditions = wordMLPackage.getMainDocumentPart().getConditionsPart().getJaxbElement(); 
     84                        log.debug( XmlUtils.marshaltoString(conditions, true, true)); 
     85                } 
     86                if (wordMLPackage.getMainDocumentPart().getComponentsPart()!=null) { 
     87                        components = wordMLPackage.getMainDocumentPart().getComponentsPart().getJaxbElement(); 
     88                        log.debug( XmlUtils.marshaltoString(components, true, true)); 
     89                } 
     90 
     91                shallowTraversor = new ShallowTraversor();               
     92                shallowTraversor.wordMLPackage = wordMLPackage;  
     93        } 
     94         
     95        private WordprocessingMLPackage wordMLPackage; 
     96        private ShallowTraversor shallowTraversor; 
    7097         
    7198        public final static String BINDING_ROLE_REPEAT = "od:repeat"; 
     
    82109         */ 
    83110                 
    84                 private static org.opendope.conditions.Conditions conditions; 
    85                 private static org.opendope.xpaths.Xpaths xPaths; 
    86                 private static org.opendope.components.Components components; 
    87     private static boolean removeSdtCellsOnFailedCondition; 
     111                private  org.opendope.conditions.Conditions conditions; 
     112                private  org.opendope.xpaths.Xpaths xPaths; 
     113                private  org.opendope.components.Components components; 
     114                 
     115    private  boolean removeSdtCellsOnFailedCondition; 
    88116 
    89117  /** 
     
    100128   * @param removeSdtCellsOnFailedCondition The new value for the cell removal flag. 
    101129   */ 
    102     public static void setRemoveSdtCellsOnFailedCondition(boolean removeSdtCellsOnFailedCondition) { 
    103       OpenDoPEHandler.removeSdtCellsOnFailedCondition = removeSdtCellsOnFailedCondition; 
     130    public void setRemoveSdtCellsOnFailedCondition(boolean removeSdtCellsOnFailedCondition) { 
     131      this.removeSdtCellsOnFailedCondition = removeSdtCellsOnFailedCondition; 
    104132    } 
    105133                 
     
    146174                 * @throws Docx4JException 
    147175                 */ 
    148                 public static WordprocessingMLPackage preprocess(WordprocessingMLPackage wordMLPackage) throws Docx4JException { 
    149  
    150                         MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();                     
    151                         if (wordMLPackage.getMainDocumentPart().getXPathsPart()==null) { 
    152                                 throw new Docx4JException("OpenDoPE XPaths part missing"); 
    153                         } else { 
    154                                 xPaths = wordMLPackage.getMainDocumentPart().getXPathsPart().getJaxbElement(); 
    155                                 log.debug( XmlUtils.marshaltoString(xPaths, true, true)); 
    156                         } 
    157                         if (wordMLPackage.getMainDocumentPart().getConditionsPart()!=null) { 
    158                                 conditions = wordMLPackage.getMainDocumentPart().getConditionsPart().getJaxbElement(); 
    159                                 log.debug( XmlUtils.marshaltoString(conditions, true, true)); 
    160                         } 
    161                         if (wordMLPackage.getMainDocumentPart().getComponentsPart()!=null) { 
    162                                 components = wordMLPackage.getMainDocumentPart().getComponentsPart().getJaxbElement(); 
    163                                 log.debug( XmlUtils.marshaltoString(components, true, true)); 
    164                         } 
    165  
    166                         shallowTraversor.wordMLPackage = wordMLPackage;  
    167                          
     176                public WordprocessingMLPackage preprocess() throws Docx4JException { 
     177 
    168178                        do { 
    169179                                // A component can apply in both the main document part, 
     
    194204                } 
    195205                 
    196                 private static boolean justGotAComponent = false; 
     206                private boolean justGotAComponent = false; 
    197207                 
    198208                private static DocxFetcher docxFetcher; 
     
    204214                } 
    205215                 
    206                 private static Set<ContentAccessor> getParts(WordprocessingMLPackage srcPackage) { 
     216                private Set<ContentAccessor> getParts(WordprocessingMLPackage srcPackage) { 
    207217                         
    208218                        Set<ContentAccessor> partList = new HashSet<ContentAccessor>();                  
     
    224234                } 
    225235 
    226                 private static WordprocessingMLPackage fetchComponents(WordprocessingMLPackage srcPackage, 
     236                private WordprocessingMLPackage fetchComponents(WordprocessingMLPackage srcPackage, 
    227237                                ContentAccessor contentAccessor) throws Docx4JException { 
    228238                         
     
    412422                } 
    413423                 
    414         public static void makeContinuous(SectPr sectPr) { 
     424        public void makeContinuous(SectPr sectPr) { 
    415425                 
    416426                if (sectPr==null) { 
     
    440450                 
    441451                 
    442         private static PartName getNewPartName(String prefix, String suffix, RelationshipsPart rp) throws InvalidFormatException { 
     452        private PartName getNewPartName(String prefix, String suffix, RelationshipsPart rp) throws InvalidFormatException { 
    443453                 
    444454                PartName proposed = null; 
     
    459469        } 
    460470                 
    461                 public static void extensionMissing(Exception e) { 
     471                public void extensionMissing(Exception e) { 
    462472                        log.error("\n" + e.getClass().getName() + ": " + e.getMessage() + "\n"); 
    463473                        log.error("* You don't appear to have the MergeDocx paid extension,"); 
     
    502512                 
    503513                 
    504                 static ShallowTraversor shallowTraversor = new ShallowTraversor(); 
    505514                 
    506515                /** 
    507516                 * This traversor duplicates the repeats, and removes false conditonals 
    508517                 */ 
    509                 private static class ShallowTraversor implements TraversalUtil.Callback { 
    510  
    511                         private static Logger log = Logger.getLogger(ShallowTraversor.class);            
     518                private class ShallowTraversor implements TraversalUtil.Callback { 
     519 
     520//                      private static Logger log = Logger.getLogger(ShallowTraversor.class);            
    512521                         
    513522                        WordprocessingMLPackage wordMLPackage; 
     
    612621                 * @return 
    613622                 */ 
    614                 private static List<Object> processBindingRoleIfAny(WordprocessingMLPackage wordMLPackage, 
     623                private List<Object> processBindingRoleIfAny(WordprocessingMLPackage wordMLPackage, 
    615624                                Object sdt)  
    616625                { 
     
    775784   * @return The "eventually empty" node list to replace the content of <code>sdt</code>. 
    776785   */ 
    777   private static List<Object> eventuallyEmptyList(final Object sdt) { 
     786  private List<Object> eventuallyEmptyList(final Object sdt) { 
    778787 
    779788    final boolean sdtIsCell = sdt instanceof CTSdtCell; 
     
    806815  } 
    807816                 
    808     private static Object obtainParent(Object sdt) { 
     817    private Object obtainParent(Object sdt) { 
    809818      if (! (sdt instanceof Child ))  
    810819        throw new IllegalArgumentException("Object of class "+ sdt.getClass().getName() + " is not a Child"); 
     
    813822    } 
    814823     
    815     private static int countContentChildren(final Object tc) { 
     824    private int countContentChildren(final Object tc) { 
    816825      final List<Object> selfAndSiblings = obtainChildren(tc); 
    817826      int contentChildCount = 0; 
     
    822831    } 
    823832     
    824     private static List<Object> obtainChildren(Object element) { 
     833    private List<Object> obtainChildren(Object element) { 
    825834      if (element instanceof ContentAccessor ) 
    826835        return ((ContentAccessor) element).getContent(); 
     
    830839    } 
    831840 
    832                 public static org.opendope.xpaths.Xpaths.Xpath getXPathFromCondition(Condition c) { 
     841                public org.opendope.xpaths.Xpaths.Xpath getXPathFromCondition(Condition c) { 
    833842                         
    834843                        org.opendope.conditions.Xpathref xpathRef = c.getXpathref(); 
     
    842851                } 
    843852                 
    844                 private static List<Object>  processRepeat(Object sdt, 
     853                private List<Object>  processRepeat(Object sdt, 
    845854                                Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts, 
    846855                                XPathsPart xPathsPart) { 
     
    916925                 
    917926                 
    918                 private static List<Object> cloneRepeatSdt(Object sdt, 
    919                                 String xpathBase, int numRepeats) { 
    920                          
    921                         List<Object> newContent = new ArrayList<Object>(); 
    922                                                                  
    923                         SdtPr sdtPr = getSdtPr(sdt); 
    924                          
    925                         log.debug(XmlUtils.marshaltoString(sdtPr, true, true)); 
    926                          
    927 //                      CTDataBinding binding = (CTDataBinding)XmlUtils.unwrap(sdtPr.getDataBinding()); 
    928                         CTDataBinding binding = sdtPr.getDataBinding(); 
    929                          
    930                          
    931       for (int i = 0; i < numRepeats; i++) { 
    932    
    933         // the first sdt is just copied to the output, the rest has a removed repeat value and no binding 
    934         if (i > 0) { 
    935    
    936           // This needs to be done only once, as we're operating on this same object tree. 
    937           if (i == 1) { 
    938    
    939             emptyRepeatTagValue(sdtPr.getTag()); 
    940    
    941             if (binding != null) { 
    942               sdtPr.getRPrOrAliasOrLock().remove(binding); 
    943             } 
    944    
    945             // Change ID 
    946             sdtPr.setId(); 
    947           } 
    948         } 
    949    
    950         // Clone 
    951         newContent.add( 
    952             XmlUtils.deepCopy(sdt)                       
    953           ); 
    954       }  
    955        
    956                 return newContent; 
    957                 } 
    958                  
    959                 private static void emptyRepeatTagValue(final Tag tag)  { 
    960                    
    961                   final String tagVal = tag.getVal(); 
    962       final Pattern stripRepeatArgPattern = Pattern.compile("(.*od:repeat=)([^&]*)(.*)"); 
    963       final Matcher stripPatternMatcher = stripRepeatArgPattern.matcher(tagVal); 
    964       if (! stripPatternMatcher.matches()) { 
    965         log.fatal("Cannot find repeat tag in sdtPr/tag while processing repeat, sth's very wrong with " + tagVal); 
    966         return; 
    967       } 
    968       final String emptyRepeatValue = stripPatternMatcher.group(1) + stripPatternMatcher.group(3); 
    969       tag.setVal(emptyRepeatValue); 
    970                 } 
    971                  
    972                 static class DeepTraversor implements TraversalUtil.Callback { 
    973  
    974                         private static Logger log = Logger.getLogger(DeepTraversor.class); 
     927        private List<Object> cloneRepeatSdt(Object sdt, String xpathBase, 
     928                        int numRepeats) { 
     929 
     930                List<Object> newContent = new ArrayList<Object>(); 
     931 
     932                SdtPr sdtPr = getSdtPr(sdt); 
     933 
     934                log.debug(XmlUtils.marshaltoString(sdtPr, true, true)); 
     935 
     936                // CTDataBinding binding = 
     937                // (CTDataBinding)XmlUtils.unwrap(sdtPr.getDataBinding()); 
     938                CTDataBinding binding = sdtPr.getDataBinding(); 
     939 
     940                for (int i = 0; i < numRepeats; i++) { 
     941 
     942                        // the first sdt is just copied to the output, the rest has a 
     943                        // removed repeat value and no binding 
     944                        if (i > 0) { 
     945 
     946                                // This needs to be done only once, as we're operating on this 
     947                                // same object tree. 
     948                                if (i == 1) { 
     949 
     950                                        emptyRepeatTagValue(sdtPr.getTag()); 
     951 
     952                                        if (binding != null) { 
     953                                                sdtPr.getRPrOrAliasOrLock().remove(binding); 
     954                                        } 
     955 
     956                                        // Change ID 
     957                                        sdtPr.setId(); 
     958                                } 
     959                        } 
     960 
     961                        // Clone 
     962                        newContent.add(XmlUtils.deepCopy(sdt)); 
     963                } 
     964 
     965                return newContent; 
     966        } 
     967                 
     968        private void emptyRepeatTagValue(final Tag tag) { 
     969 
     970                final String tagVal = tag.getVal(); 
     971                final Pattern stripRepeatArgPattern = Pattern 
     972                                .compile("(.*od:repeat=)([^&]*)(.*)"); 
     973                final Matcher stripPatternMatcher = stripRepeatArgPattern 
     974                                .matcher(tagVal); 
     975                if (!stripPatternMatcher.matches()) { 
     976                        log.fatal("Cannot find repeat tag in sdtPr/tag while processing repeat, sth's very wrong with " 
     977                                        + tagVal); 
     978                        return; 
     979                } 
     980                final String emptyRepeatValue = stripPatternMatcher.group(1) 
     981                                + stripPatternMatcher.group(3); 
     982                tag.setVal(emptyRepeatValue); 
     983        } 
     984                 
     985                class DeepTraversor implements TraversalUtil.Callback { 
     986 
     987//                      private static Logger log = Logger.getLogger(DeepTraversor.class); 
    975988 
    976989                        int index = 0; 
     
    10301043                } 
    10311044                 
    1032                 private static void processDescendantBindings(Object sdt, 
     1045                private void processDescendantBindings(Object sdt, 
    10331046                                String xpathBase, int index) { 
    10341047                         
     
    11571170                } 
    11581171                 
    1159     private static org.opendope.xpaths.Xpaths.Xpath createNewXPathObject(String newPath,  
     1172    private org.opendope.xpaths.Xpaths.Xpath createNewXPathObject(String newPath,  
    11601173                                org.opendope.xpaths.Xpaths.Xpath xpathObj, int index) { 
    11611174                         
     
    12261239//              } 
    12271240                 
    1228                 private static List<Node> xpathGetNodes(Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts, 
     1241                private List<Node> xpathGetNodes(Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts, 
    12291242                                String storeItemId, String xpath, String prefixMappings) { 
    12301243                         
  • trunk/docx4j/src/main/java/org/docx4j/samples/ContentControlBindingExtensions.java

    r1477 r1639  
    9494 
    9595                // Process conditionals and repeats 
    96                 OpenDoPEHandler.preprocess(wordMLPackage); 
     96                OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage); 
     97                odh.preprocess(); 
    9798                System.out.println( 
    9899                                XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true) 
  • trunk/docx4j/src/test/java/org/docx4j/model/datastorage/EndToEndTest.java

    r1587 r1639  
    6060                 
    6161                // Process conditionals and repeats 
    62                 OpenDoPEHandler.preprocess(wordMLPackage); 
     62                OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage); 
     63                odh.preprocess(); 
    6364 
    6465                // Since the JAXB binding stuff seems to remember 
     
    173174                 
    174175                // Process conditionals and repeats 
    175                 OpenDoPEHandler.preprocess(wordMLPackage); 
     176                OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage); 
     177                odh.preprocess(); 
    176178 
    177179                // Since the JAXB binding stuff seems to remember 
Note: See TracChangeset for help on using the changeset viewer.