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.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.xml.sax.Attributes;
39  import org.xml.sax.SAXException;
40  import org.xml.sax.XMLReader;
41  import org.xml.sax.helpers.AttributesImpl;
42  import org.xml.sax.helpers.XMLFilterImpl;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  
47  
48  /**
49   * A base abstract filter that can be subclassed in order to easily implement removal of properties based on their
50   * name/content.
51   * @author fgiust
52   * @version $Revision: 32667 $ ($Author: gjoseph $)
53   */
54  public abstract class SkipNodePropertyFilter extends XMLFilterImpl {
55  
56      /**
57       * Logger.
58       */
59      protected Logger log = LoggerFactory.getLogger(getClass());
60  
61      protected String lastNodeName;
62  
63      protected boolean skipProperty;
64  
65      protected boolean invalue;
66  
67      private List elementBuffer = new ArrayList();
68  
69      /**
70       * Instantiates a new filter.
71       * @param parent wrapped XMLReader
72       */
73      public SkipNodePropertyFilter(XMLReader parent) {
74          super(parent);
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      public void endElement(String uri, String localName, String qName) throws SAXException {
81  
82          if (skipProperty) {
83              if ("sv:property".equals(qName)) {
84                  skipProperty = false;
85                  invalue = false;
86                  elementBuffer.clear();
87              }
88              return;
89          }
90  
91          super.endElement(uri, localName, qName);
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public void characters(char[] ch, int start, int length) throws SAXException {
98          if (!skipProperty) {
99              super.characters(ch, start, length);
100         }
101         else {
102             if (invalue) {
103                 invalue = false;
104                 // Arrays.copyOfRange(ch, start, start + length)
105                 char[] range = new char[length];
106                 System.arraycopy(ch, start, range, 0, length);
107                 String textContent = new String(range);
108 
109                 // skip only if filter() say so
110                 boolean skip = filter(textContent, lastNodeName);
111 
112                 if (!skip) {
113                     while (!elementBuffer.isEmpty()) {
114                         BufferedElement be = (BufferedElement) elementBuffer.remove(0);
115                         super.startElement(be.getUri(), be.getLocalName(), be.getQName(), be.getAtts());
116                     }
117                     super.characters(ch, start, length);
118                     skipProperty = false;
119                 }
120             }
121 
122         }
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
129 
130         String svname = atts.getValue("sv:name");
131         if ("sv:node".equals(qName)) {
132             lastNodeName = svname;
133         }
134         else if ("sv:property".equals(qName) && (getFilteredPropertyName().equals(svname))) {
135             elementBuffer.add(new BufferedElement(uri, localName, qName, new AttributesImpl(atts)));
136 
137             skipProperty = true;
138             invalue = false;
139         }
140         else if (skipProperty && "sv:value".equals(qName)) {
141             elementBuffer.add(new BufferedElement(uri, localName, qName, new AttributesImpl(atts)));
142             invalue = true;
143         }
144 
145         if (skipProperty) {
146             return;
147         }
148         super.startElement(uri, localName, qName, atts);
149 
150     }
151 
152     /**
153      * Implement this method to specify the name of the property you want to filter.
154      * @return filtered property name
155      */
156     protected abstract String getFilteredPropertyName();
157 
158     /**
159      * Implement this method to specificy if a given property (given its value and the parent node name) should be
160      * removed.
161      * @param propertyValue property value
162      * @param parentNodeName parent node name
163      * @return <code>true</code> if this property should be removed
164      */
165     protected abstract boolean filter(String propertyValue, String parentNodeName);
166 
167     public static class BufferedElement {
168 
169         private String uri;
170 
171         private String localName;
172 
173         private String qName;
174 
175         private Attributes atts;
176 
177         /**
178          * @param atts
179          * @param localName
180          * @param name
181          * @param uri
182          */
183         public BufferedElement(String uri, String localName, String qName, Attributes atts) {
184             this.atts = atts;
185             this.localName = localName;
186             this.qName = qName;
187             this.uri = uri;
188         }
189 
190         /**
191          * Returns the uri.
192          * @return the uri
193          */
194         public String getUri() {
195             return uri;
196         }
197 
198         /**
199          * Sets the uri.
200          * @param uri the uri to set
201          */
202         public void setUri(String uri) {
203             this.uri = uri;
204         }
205 
206         /**
207          * Returns the localName.
208          * @return the localName
209          */
210         public String getLocalName() {
211             return localName;
212         }
213 
214         /**
215          * Sets the localName.
216          * @param localName the localName to set
217          */
218         public void setLocalName(String localName) {
219             this.localName = localName;
220         }
221 
222         /**
223          * Returns the qName.
224          * @return the qName
225          */
226         public String getQName() {
227             return qName;
228         }
229 
230         /**
231          * Sets the qName.
232          * @param name the qName to set
233          */
234         public void setQName(String name) {
235             qName = name;
236         }
237 
238         /**
239          * Returns the atts.
240          * @return the atts
241          */
242         public Attributes getAtts() {
243             return atts;
244         }
245 
246         /**
247          * Sets the atts.
248          * @param atts the atts to set
249          */
250         public void setAtts(Attributes atts) {
251             this.atts = atts;
252         }
253     }
254 }