View Javadoc
1   /**
2    * This file Copyright (c) 2003-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.importexport.filters;
35  
36  import org.apache.commons.lang3.ArrayUtils;
37  import org.xml.sax.Attributes;
38  import org.xml.sax.SAXException;
39  import org.xml.sax.XMLReader;
40  import org.xml.sax.helpers.XMLFilterImpl;
41  
42  /**
43   * A simple filter that strips jcr:uuid properties in MetaData nodes. Needed due to MAGNOLIA-1650 uuids in MetaData
44   * nodes are changed during import. It also supports removal of non-standards namespaces, by setting the
45   * <code>removeUnwantedNamespaces</code> flag in the constructor.
46   */
47  public class MetadataUuidFilter extends XMLFilterImpl {
48  
49      /**
50       * if != 0 we are in the middle of a filtered element.
51       */
52      private int inMetadataElement;
53  
54      private boolean skipProperty;
55  
56      private boolean inSkippedNs;
57  
58      private String[] validNs = new String[]{"sv", "xsi"};
59  
60      private boolean removeUnwantedNamespaces;
61  
62      /**
63       * Instantiates a new MetadataUuidFilter filter.
64       *
65       * @param parent wrapped XMLReader
66       * @param removeUnwantedNamespaces if set, non standard JCR name spaces will be removed
67       */
68      public MetadataUuidFilter(XMLReader parent, boolean removeUnwantedNamespaces) {
69          super(parent);
70          this.removeUnwantedNamespaces = removeUnwantedNamespaces;
71      }
72  
73      @Override
74      public void endPrefixMapping(String prefix) throws SAXException {
75          if (!inSkippedNs) {
76              super.endPrefixMapping(prefix);
77          }
78          inSkippedNs = false;
79  
80      }
81  
82      /**
83       * Skip unwanted name spaces, see MAGNOLIA-2756.
84       */
85      @Override
86      public void startPrefixMapping(String prefix, String uri) throws SAXException {
87          if (!removeUnwantedNamespaces || ArrayUtils.contains(validNs, prefix)) {
88              super.startPrefixMapping(prefix, uri);
89          } else {
90              inSkippedNs = true;
91          }
92      }
93  
94      @Override
95      public void endElement(String uri, String localName, String qName) throws SAXException {
96  
97          if (inMetadataElement > 0) {
98              inMetadataElement--;
99          }
100 
101         if (skipProperty) {
102             if ("sv:property".equals(qName)) {
103                 skipProperty = false;
104             }
105             return;
106         }
107 
108         super.endElement(uri, localName, qName);
109     }
110 
111     @Override
112     public void characters(char[] ch, int start, int length) throws SAXException {
113         if (!skipProperty) {
114             super.characters(ch, start, length);
115         }
116     }
117 
118     @Override
119     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
120 
121         if (inMetadataElement > 0) {
122             inMetadataElement++;
123         }
124 
125         String svname = atts.getValue("sv:name");
126 
127         if ("sv:node".equals(qName) && "MetaData".equals(svname)) {
128             inMetadataElement++;
129         }
130 
131         if (inMetadataElement > 0) {
132 
133             if ("sv:property".equals(qName) && ("jcr:uuid".equals(svname))) {
134                 skipProperty = true;
135             }
136 
137             if (skipProperty) {
138                 return;
139             }
140         }
141 
142         super.startElement(uri, localName, qName, atts);
143 
144     }
145 }