View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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  /**
44   * A simple filter that strips jcr:uuid properties in MetaData nodes. Needed due to MAGNOLIA-1650 uuids in MetaData
45   * nodes are changed during import. It also supports removal of non-standards namespaces, by setting the
46   * <code>removeUnwantedNamespaces</code> flag in the constructor.
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
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 namespaces will be removed
78       */
79      public MetadataUuidFilter(XMLReader parent, boolean removeUnwantedNamespaces) {
80          super(parent);
81          this.removeUnwantedNamespaces = removeUnwantedNamespaces;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      public void endPrefixMapping(String prefix) throws SAXException {
88          if (!inSkippedNs) {
89              super.endPrefixMapping(prefix);
90          }
91          inSkippedNs = false;
92  
93      }
94  
95      /**
96       * Skip unwanted namespaces, see MAGNOLIA-2756
97       */
98      public void startPrefixMapping(String prefix, String uri) throws SAXException {
99          if (!removeUnwantedNamespaces || ArrayUtils.contains(validNs, prefix)) {
100             super.startPrefixMapping(prefix, uri);
101         }
102         else {
103             inSkippedNs = true;
104         }
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     public void endElement(String uri, String localName, String qName) throws SAXException {
111 
112         if (inMetadataElement > 0) {
113             inMetadataElement--;
114         }
115 
116         if (skipProperty) {
117             if ("sv:property".equals(qName)) {
118                 skipProperty = false;
119             }
120             return;
121         }
122 
123         super.endElement(uri, localName, qName);
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     public void characters(char[] ch, int start, int length) throws SAXException {
130         if (!skipProperty) {
131             super.characters(ch, start, length);
132         }
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
139 
140         if (inMetadataElement > 0) {
141             inMetadataElement++;
142         }
143 
144         String svname = atts.getValue("sv:name");
145 
146         if ("sv:node".equals(qName) && "MetaData".equals(svname)) {
147             inMetadataElement++;
148         }
149 
150         if (inMetadataElement > 0) {
151 
152             if ("sv:property".equals(qName) && ("jcr:uuid".equals(svname))) {
153                 skipProperty = true;
154             }
155 
156             if (skipProperty) {
157                 return;
158             }
159         }
160 
161         super.startElement(uri, localName, qName, atts);
162 
163     }
164 }