Changeset 1639 for trunk/docx4j/src
- Timestamp:
- 08/12/11 03:38:34 (10 months ago)
- Location:
- trunk/docx4j/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java
r1583 r1639 67 67 public class OpenDoPEHandler { 68 68 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; 70 97 71 98 public final static String BINDING_ROLE_REPEAT = "od:repeat"; … … 82 109 */ 83 110 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; 88 116 89 117 /** … … 100 128 * @param removeSdtCellsOnFailedCondition The new value for the cell removal flag. 101 129 */ 102 public staticvoid setRemoveSdtCellsOnFailedCondition(boolean removeSdtCellsOnFailedCondition) {103 OpenDoPEHandler.removeSdtCellsOnFailedCondition = removeSdtCellsOnFailedCondition;130 public void setRemoveSdtCellsOnFailedCondition(boolean removeSdtCellsOnFailedCondition) { 131 this.removeSdtCellsOnFailedCondition = removeSdtCellsOnFailedCondition; 104 132 } 105 133 … … 146 174 * @throws Docx4JException 147 175 */ 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 168 178 do { 169 179 // A component can apply in both the main document part, … … 194 204 } 195 205 196 private staticboolean justGotAComponent = false;206 private boolean justGotAComponent = false; 197 207 198 208 private static DocxFetcher docxFetcher; … … 204 214 } 205 215 206 private staticSet<ContentAccessor> getParts(WordprocessingMLPackage srcPackage) {216 private Set<ContentAccessor> getParts(WordprocessingMLPackage srcPackage) { 207 217 208 218 Set<ContentAccessor> partList = new HashSet<ContentAccessor>(); … … 224 234 } 225 235 226 private staticWordprocessingMLPackage fetchComponents(WordprocessingMLPackage srcPackage,236 private WordprocessingMLPackage fetchComponents(WordprocessingMLPackage srcPackage, 227 237 ContentAccessor contentAccessor) throws Docx4JException { 228 238 … … 412 422 } 413 423 414 public staticvoid makeContinuous(SectPr sectPr) {424 public void makeContinuous(SectPr sectPr) { 415 425 416 426 if (sectPr==null) { … … 440 450 441 451 442 private staticPartName getNewPartName(String prefix, String suffix, RelationshipsPart rp) throws InvalidFormatException {452 private PartName getNewPartName(String prefix, String suffix, RelationshipsPart rp) throws InvalidFormatException { 443 453 444 454 PartName proposed = null; … … 459 469 } 460 470 461 public staticvoid extensionMissing(Exception e) {471 public void extensionMissing(Exception e) { 462 472 log.error("\n" + e.getClass().getName() + ": " + e.getMessage() + "\n"); 463 473 log.error("* You don't appear to have the MergeDocx paid extension,"); … … 502 512 503 513 504 static ShallowTraversor shallowTraversor = new ShallowTraversor();505 514 506 515 /** 507 516 * This traversor duplicates the repeats, and removes false conditonals 508 517 */ 509 private staticclass 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); 512 521 513 522 WordprocessingMLPackage wordMLPackage; … … 612 621 * @return 613 622 */ 614 private staticList<Object> processBindingRoleIfAny(WordprocessingMLPackage wordMLPackage,623 private List<Object> processBindingRoleIfAny(WordprocessingMLPackage wordMLPackage, 615 624 Object sdt) 616 625 { … … 775 784 * @return The "eventually empty" node list to replace the content of <code>sdt</code>. 776 785 */ 777 private staticList<Object> eventuallyEmptyList(final Object sdt) {786 private List<Object> eventuallyEmptyList(final Object sdt) { 778 787 779 788 final boolean sdtIsCell = sdt instanceof CTSdtCell; … … 806 815 } 807 816 808 private staticObject obtainParent(Object sdt) {817 private Object obtainParent(Object sdt) { 809 818 if (! (sdt instanceof Child )) 810 819 throw new IllegalArgumentException("Object of class "+ sdt.getClass().getName() + " is not a Child"); … … 813 822 } 814 823 815 private staticint countContentChildren(final Object tc) {824 private int countContentChildren(final Object tc) { 816 825 final List<Object> selfAndSiblings = obtainChildren(tc); 817 826 int contentChildCount = 0; … … 822 831 } 823 832 824 private staticList<Object> obtainChildren(Object element) {833 private List<Object> obtainChildren(Object element) { 825 834 if (element instanceof ContentAccessor ) 826 835 return ((ContentAccessor) element).getContent(); … … 830 839 } 831 840 832 public staticorg.opendope.xpaths.Xpaths.Xpath getXPathFromCondition(Condition c) {841 public org.opendope.xpaths.Xpaths.Xpath getXPathFromCondition(Condition c) { 833 842 834 843 org.opendope.conditions.Xpathref xpathRef = c.getXpathref(); … … 842 851 } 843 852 844 private staticList<Object> processRepeat(Object sdt,853 private List<Object> processRepeat(Object sdt, 845 854 Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts, 846 855 XPathsPart xPathsPart) { … … 916 925 917 926 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); 975 988 976 989 int index = 0; … … 1030 1043 } 1031 1044 1032 private staticvoid processDescendantBindings(Object sdt,1045 private void processDescendantBindings(Object sdt, 1033 1046 String xpathBase, int index) { 1034 1047 … … 1157 1170 } 1158 1171 1159 private staticorg.opendope.xpaths.Xpaths.Xpath createNewXPathObject(String newPath,1172 private org.opendope.xpaths.Xpaths.Xpath createNewXPathObject(String newPath, 1160 1173 org.opendope.xpaths.Xpaths.Xpath xpathObj, int index) { 1161 1174 … … 1226 1239 // } 1227 1240 1228 private staticList<Node> xpathGetNodes(Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts,1241 private List<Node> xpathGetNodes(Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts, 1229 1242 String storeItemId, String xpath, String prefixMappings) { 1230 1243 -
trunk/docx4j/src/main/java/org/docx4j/samples/ContentControlBindingExtensions.java
r1477 r1639 94 94 95 95 // Process conditionals and repeats 96 OpenDoPEHandler.preprocess(wordMLPackage); 96 OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage); 97 odh.preprocess(); 97 98 System.out.println( 98 99 XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true) -
trunk/docx4j/src/test/java/org/docx4j/model/datastorage/EndToEndTest.java
r1587 r1639 60 60 61 61 // Process conditionals and repeats 62 OpenDoPEHandler.preprocess(wordMLPackage); 62 OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage); 63 odh.preprocess(); 63 64 64 65 // Since the JAXB binding stuff seems to remember … … 173 174 174 175 // Process conditionals and repeats 175 OpenDoPEHandler.preprocess(wordMLPackage); 176 OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage); 177 odh.preprocess(); 176 178 177 179 // Since the JAXB binding stuff seems to remember
Note: See TracChangeset
for help on using the changeset viewer.
