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.xml.sax.Attributes;
37  import org.xml.sax.SAXException;
38  import org.xml.sax.XMLReader;
39  import org.xml.sax.helpers.AttributesImpl;
40  import org.xml.sax.helpers.XMLFilterImpl;
41  
42  /**
43   * SAX filter, strips version information from a JCR XML (system view).
44   */
45  public class MagnoliaV2Filter extends XMLFilterImpl {
46  
47      /**
48       * if != 0 we are in the middle of a filtered element.
49       */
50      private int inMetadataElement;
51  
52      private boolean skipNode;
53  
54      private boolean skipProperty;
55  
56      private boolean skipWs;
57  
58      /**
59       * Instantiates a new version filter.
60       *
61       * @param parent wrapped XMLReader
62       */
63      public MagnoliaV2Filter(XMLReader parent) {
64          super(parent);
65      }
66  
67      /**
68       * @see org.xml.sax.helpers.XMLFilterImpl#endElement(String, String, String)
69       */
70      @Override
71      public void endElement(String uri, String localName, String qName) throws SAXException {
72  
73          if (inMetadataElement > 0) {
74              inMetadataElement--;
75          }
76  
77          if (skipNode && "sv:node".equals(qName)) {
78              skipNode = false;
79              skipWs = true; // skip additional whitespace after skipped tag
80              return;
81          }
82  
83          if (skipProperty) {
84              if ("sv:property".equals(qName)) {
85                  skipProperty = false;
86                  skipWs = true; // skip additional whitespace after skipped tag
87              }
88              return;
89          }
90  
91          super.endElement(uri, localName, qName);
92      }
93  
94      /**
95       * @see org.xml.sax.helpers.XMLFilterImpl#characters(char[], int, int)
96       */
97      @Override
98      public void characters(char[] ch, int start, int length) throws SAXException {
99          // do not emit text inside of skipped elements
100         if (!(skipNode || skipProperty)) {
101             while (skipWs && length > 0
102                     && Character.isWhitespace(ch[start])) {
103                 start++;
104                 length--;
105             }
106             if (length > 0) {
107                 super.characters(ch, start, length);
108             }
109         }
110         skipWs = false;
111     }
112 
113     /**
114      * @see org.xml.sax.helpers.XMLFilterImpl#startElement(String, String, String, Attributes)
115      */
116     @Override
117     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
118 
119         if (inMetadataElement > 0) {
120             inMetadataElement++;
121         }
122 
123         String svname = atts.getValue("sv:name");
124 
125         if ("sv:node".equals(qName) && "MetaData".equals(svname)) {
126             inMetadataElement++;
127         }
128 
129         if (inMetadataElement > 0) {
130             // MAGNOILA-2653 skip nested elements (sv:value)
131             if (skipProperty) {
132                 return;
133             }
134             // remove
135             // <sv:node sv:name="jcr:content">
136             if ("sv:node".equals(qName) && "jcr:content".equals(svname)) {
137                 skipNode = true;
138                 return;
139             }
140             if ("sv:property".equals(qName)
141                     && ("sequenceposition".equals(svname) || "jcr:primaryType".equals(svname) || "jcr:isCheckedOut"
142                     .equals(svname))) {
143                 skipProperty = true;
144                 return;
145             }
146             if ("sv:property".equals(qName)
147                     && ("Data".equals(svname) || "template".equals(svname) || "authorid".equals(svname) || "title"
148                     .equals(svname))) {
149                 atts = new AttributesImpl();
150                 ((AttributesImpl) atts).addAttribute(uri, "name", "sv:name", uri, "mgnl:" + svname);
151                 ((AttributesImpl) atts).addAttribute(uri, "type", "sv:type", uri, "String");
152             } else if ("sv:property".equals(qName) && ("creationdate".equals(svname) || "lastmodified".equals(svname))) {
153                 atts = new AttributesImpl();
154                 ((AttributesImpl) atts).addAttribute(uri, "name", "sv:name", uri, "mgnl:" + svname);
155                 ((AttributesImpl) atts).addAttribute(uri, "type", "sv:type", uri, "Date");
156             }
157 
158         }
159 
160         super.startElement(uri, localName, qName, atts);
161 
162         if ("sv:node".equals(qName) && "MetaData".equals(svname)) {
163 
164             // add:
165             // <sv:property sv:name="jcr:primaryType" sv:type="Name">
166             // <sv:value>nt:unstructured</sv:value>
167             // </sv:property>
168 
169             String atturi = atts.getURI(0);
170             AttributesImpl atts2 = new AttributesImpl();
171             atts2.addAttribute(uri, "name", "sv:name", atturi, "jcr:primaryType");
172             atts2.addAttribute(uri, "type", "sv:type", atturi, "Name");
173 
174             super.startElement(uri, "property", "sv:property", atts2);
175             super.startElement(uri, "value", "sv:value", new AttributesImpl());
176             super.characters(new char[]{'m', 'g', 'n', 'l', ':', 'm', 'e', 't', 'a', 'D', 'a', 't', 'a'}, 0, 13);
177             super.endElement(uri, "value", "sv:value");
178             super.endElement(uri, "property", "sv:property");
179         }
180     }
181 }