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