1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
50
51
52
53
54 public abstract class SkipNodePropertyFilter extends XMLFilterImpl {
55
56
57
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
71
72
73 public SkipNodePropertyFilter(XMLReader parent) {
74 super(parent);
75 }
76
77
78
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
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
105 char[] range = new char[length];
106 System.arraycopy(ch, start, range, 0, length);
107 String textContent = new String(range);
108
109
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
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
154
155
156 protected abstract String getFilteredPropertyName();
157
158
159
160
161
162
163
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
179
180
181
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
192
193
194 public String getUri() {
195 return uri;
196 }
197
198
199
200
201
202 public void setUri(String uri) {
203 this.uri = uri;
204 }
205
206
207
208
209
210 public String getLocalName() {
211 return localName;
212 }
213
214
215
216
217
218 public void setLocalName(String localName) {
219 this.localName = localName;
220 }
221
222
223
224
225
226 public String getQName() {
227 return qName;
228 }
229
230
231
232
233
234 public void setQName(String name) {
235 qName = name;
236 }
237
238
239
240
241
242 public Attributes getAtts() {
243 return atts;
244 }
245
246
247
248
249
250 public void setAtts(Attributes atts) {
251 this.atts = atts;
252 }
253 }
254 }