View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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   * @deprecated since 5.4.4. Should not be needed anymore.
48   */
49  @Deprecated
50  public class MetadataUuidFilter extends XMLFilterImpl {
51  
52      /**
53       * if != 0 we are in the middle of a filtered element.
54       */
55      private int inMetadataElement;
56  
57      private boolean skipProperty;
58  
59      private boolean inSkippedNs;
60  
61      private String[] validNs = new String[]{"sv", "xsi"};
62  
63      private boolean removeUnwantedNamespaces;
64  
65      /**
66       * Instantiates a new MetadataUuidFilter filter.
67       *
68       * @param parent wrapped XMLReader
69       * @param removeUnwantedNamespaces if set, non standard JCR name spaces will be removed
70       */
71      public MetadataUuidFilter(XMLReader parent, boolean removeUnwantedNamespaces) {
72          super(parent);
73          this.removeUnwantedNamespaces = removeUnwantedNamespaces;
74      }
75  
76      @Override
77      public void endPrefixMapping(String prefix) throws SAXException {
78          if (!inSkippedNs) {
79              super.endPrefixMapping(prefix);
80          }
81          inSkippedNs = false;
82  
83      }
84  
85      /**
86       * Skip unwanted name spaces, see MAGNOLIA-2756.
87       */
88      @Override
89      public void startPrefixMapping(String prefix, String uri) throws SAXException {
90          if (!removeUnwantedNamespaces || ArrayUtils.contains(validNs, prefix)) {
91              super.startPrefixMapping(prefix, uri);
92          } else {
93              inSkippedNs = true;
94          }
95      }
96  
97      @Override
98      public void endElement(String uri, String localName, String qName) throws SAXException {
99  
100         if (inMetadataElement > 0) {
101             inMetadataElement--;
102         }
103 
104         if (skipProperty) {
105             if ("sv:property".equals(qName)) {
106                 skipProperty = false;
107             }
108             return;
109         }
110 
111         super.endElement(uri, localName, qName);
112     }
113 
114     @Override
115     public void characters(char[] ch, int start, int length) throws SAXException {
116         if (!skipProperty) {
117             super.characters(ch, start, length);
118         }
119     }
120 
121     @Override
122     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
123 
124         if (inMetadataElement > 0) {
125             inMetadataElement++;
126         }
127 
128         String svname = atts.getValue("sv:name");
129 
130         if ("sv:node".equals(qName) && "MetaData".equals(svname)) {
131             inMetadataElement++;
132         }
133 
134         if (inMetadataElement > 0) {
135 
136             if ("sv:property".equals(qName) && ("jcr:uuid".equals(svname))) {
137                 skipProperty = true;
138             }
139 
140             if (skipProperty) {
141                 return;
142             }
143         }
144 
145         super.startElement(uri, localName, qName, atts);
146 
147     }
148 }