Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2011 Plutext Pty Ltd and individual contributors.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.docx4j.model.datastorage;
 import static org.docx4j.XmlUtils.getTransformerTemplate;
 import static org.docx4j.XmlUtils.marshaltoW3CDomDocument;
 import static org.docx4j.XmlUtils.prepareJAXBResult;
 import static org.docx4j.XmlUtils.transform;
 import static org.docx4j.model.datastorage.RemovalHandler.Quantifier.ALL;
 import static org.docx4j.model.datastorage.RemovalHandler.Quantifier.NAMED;
 import java.util.Map;
Tool to remove Structured Document Tags from an OpenXML document part.

This tool removes SDTs tagged with a certain quantifier from the document part. It supports to remove any set of od:xpath, od:repeat and od:condition tags.


  • As it does text processing and no real namespace qualification exist, the quantifiers must be prefixed exactly be "od", not any other prefix.
  • In case of qualified removal (in effect not RemovalHandler.Quantifier.ALL), bindings containing more than one qualifier are not supported, that is, when you tag od:repeat=/this&od:xpath=/that, the SDT is removed whenever you specify to remove either repeat or bind tags.

Karsten Tinnefeld
$Revision: $ $Date: $
 public class RemovalHandler {
         private static final String templateFile = RemovalHandler.class.getName()
                         + ".xslt";
         private final Templates removalTemplate;

Initializes the removal handler. This tool is thread safe and should be reused, as initialization is relatively expensive.
         public RemovalHandler() {
                 final InputStream templateStream = getClass().getClassLoader()
                 final Source templateSource = new StreamSource(templateStream);
                 try {
                         this. = getTransformerTemplate(templateSource);
                 } catch (TransformerConfigurationException e) {
                         throw new IllegalStateException(
                                         "Error instantiating SDT removal stylesheet"e);
         public void removeSDTs(WordprocessingMLPackage wordMLPackage,
                        final Quantifier quantifierfinal String... keysthrows Docx4JException {
                // A component can apply in both the main document part,
                // and in headers/footers. See further
                removeSDTs(wordMLPackage.getMainDocumentPart(), quantifierkeys);
                // Add headers/footers
                RelationshipsPart rp = wordMLPackage.getMainDocumentPart()
                for (Relationship r : rp.getRelationships().getRelationship()) {
                        if (r.getType().equals(.)) {
                                removeSDTs((HeaderPartrp.getPart(r), quantifierkeys);
                        } else if (r.getType().equals(.)) {
                                removeSDTs((FooterPartrp.getPart(r), quantifierkeys);

Removes Structured Document Tags from a document part, preserving their contents. In case key "empty" is specified, value bindings (xpath) are removed only if they have void contents (e.g. the XML points nowhere).

part The document part to modify (in situ).
quantifier The quantifier regarding which kinds of parts are to be removed.
keys In case of RemovalHandler.Quantifier.NAMED, quantifier names. All strings expect "xpath", "condition", "repeat", "empty" are ignored.
org.docx4j.openpackaging.exceptions.Docx4JException In case any transformation error occurs.
        public void removeSDTs(final JaxbXmlPart<? extends Objectpart,
                        final Quantifier quantifierfinal String... keys)
                        throws Docx4JException {
                final Map<StringObjectparameters = new HashMap<StringObject>();
                parameters.put("all"quantifier == );
                if (quantifier == )
                        parameters.put("types", ArrayUtils.toString(keys));
                final Document partDOM = marshaltoW3CDomDocument(part.getJaxbElement());
                final JAXBResult result = prepareJAXBResult(.);
                try {
                } catch (JAXBException e) {
                        throw new Docx4JException(
                                        "Error unmarshalling document part for SDT removal"e);

A quantifier specifying kinds of SDTs.
        public static enum Quantifier {

Every SDT shall be removed.

Named SDTs shall be removed, the names given separately.

The default SDTs shall be removed, that is, condition and repeat.
New to GrepCode? Check out our FAQ X