View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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.lang.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   * @author fgiust
48   * @version $Revision: $ ($Author: $)
49   */
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       * @param parent wrapped XMLReader
68       * @deprecated since 4.2.2 (MAGNOLIA-2960)
69       */
70      public MetadataUuidFilter(XMLReader parent) {
71          super(parent);
72      }
73  
74      /**
75       * Instantiates a new MetadataUuidFilter filter.
76       * @param parent wrapped XMLReader
77       * @param removeUnwantedNamespaces if set, non standard JCR name spaces will be removed
78       */
79      public MetadataUuidFilter(XMLReader parent, boolean removeUnwantedNamespaces) {
80          super(parent);
81          this.removeUnwantedNamespaces = removeUnwantedNamespaces;
82      }
83  
84      @Override
85      public void endPrefixMapping(String prefix) throws SAXException {
86          if (!inSkippedNs) {
87              super.endPrefixMapping(prefix);
88          }
89          inSkippedNs = false;
90  
91      }
92  
93      /**
94       * Skip unwanted name spaces, see MAGNOLIA-2756.
95       */
96      @Override
97      public void startPrefixMapping(String prefix, String uri) throws SAXException {
98          if (!removeUnwantedNamespaces || ArrayUtils.contains(validNs, prefix)) {
99              super.startPrefixMapping(prefix, uri);
100         }
101         else {
102             inSkippedNs = true;
103         }
104     }
105 
106     @Override
107     public void endElement(String uri, String localName, String qName) throws SAXException {
108 
109         if (inMetadataElement > 0) {
110             inMetadataElement--;
111         }
112 
113         if (skipProperty) {
114             if ("sv:property".equals(qName)) {
115                 skipProperty = false;
116             }
117             return;
118         }
119 
120         super.endElement(uri, localName, qName);
121     }
122 
123     @Override
124     public void characters(char[] ch, int start, int length) throws SAXException {
125         if (!skipProperty) {
126             super.characters(ch, start, length);
127         }
128     }
129 
130     @Override
131     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
132 
133         if (inMetadataElement > 0) {
134             inMetadataElement++;
135         }
136 
137         String svname = atts.getValue("sv:name");
138 
139         if ("sv:node".equals(qName) && "MetaData".equals(svname)) {
140             inMetadataElement++;
141         }
142 
143         if (inMetadataElement > 0) {
144 
145             if ("sv:property".equals(qName) && ("jcr:uuid".equals(svname))) {
146                 skipProperty = true;
147             }
148 
149             if (skipProperty) {
150                 return;
151             }
152         }
153 
154         super.startElement(uri, localName, qName, atts);
155 
156     }
157 }