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
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * 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.

Restrictions:

  • 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.

Author(s):
Karsten Tinnefeld
Version:
$Revision: $ $Date: $
 
 public class RemovalHandler {
 
         private static final String templateFile = RemovalHandler.class.getName()
                         .replace('.''/')
                         + ".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()
                                 .getResourceAsStream();
                 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
                // http://forums.opendope.org/Support-components-in-headers-footers-tp2964174p2964174.html
                removeSDTs(wordMLPackage.getMainDocumentPart(), quantifierkeys);
                // Add headers/footers
                RelationshipsPart rp = wordMLPackage.getMainDocumentPart()
                                .getRelationshipsPart();
                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).

Parameters:
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.
Throws:
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(.);
                transform(partDOMparametersresult);
                try {
                        part.setJaxbElement(result);
                } 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.
                ALL,

                
Named SDTs shall be removed, the names given separately.
                NAMED,

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