1 /* FilteredSAXEventSink.java --
2 Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.dom.ls;
40 import java.util.LinkedList;
41 import org.w3c.dom.Attr;
42 import org.w3c.dom.Element;
43 import org.w3c.dom.Node;
44 import org.w3c.dom.Text;
45 import org.w3c.dom.ls.LSParserFilter;
46 import org.w3c.dom.traversal.NodeFilter;
47 import org.xml.sax.Attributes;
48 import org.xml.sax.SAXException;
51 * A SAX event sink that calls out to a parser filter in order to decide
52 * whether to insert nodes into the tree.
54 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
56 class FilteredSAXEventSink
60 final LSParserFilter filter;
64 * Stack of elements to insert.
69 * Corresponding stack of filter decisions about the nodes.
74 * True when rejecting child nodes.
78 FilteredSAXEventSink(LSParserFilter filter)
81 whatToShow = filter.getWhatToShow();
84 public void startDocument()
91 nodes = new LinkedList();
92 decisions = new LinkedList();
94 super.startDocument();
97 public void endDocument()
106 switch (getDecision(ctx, false))
108 case LSParserFilter.FILTER_REJECT:
118 public void startElement(String uri, String localName, String qName,
122 if (rejecting || interrupted)
126 Element element = createElement(uri, localName, qName, atts);
129 short decision = getDecision(element, true);
130 nodes.addLast(element);
131 decisions.addLast(new Short(decision));
135 case LSParserFilter.FILTER_REJECT:
138 case LSParserFilter.FILTER_INTERRUPT:
144 protected Attr createAttr(Attributes atts, int index)
146 Attr attr = super.createAttr(atts, index);
147 short decision = getDecision(attr, false);
150 case LSParserFilter.FILTER_REJECT:
152 case LSParserFilter.FILTER_INTERRUPT:
159 public void endElement(String uri, String localName, String qName)
162 if (rejecting || interrupted)
166 super.endElement(uri, localName, qName);
168 Element element = (Element) nodes.removeLast();
169 Node parent = nodes.isEmpty() ? doc : (Node) nodes.getLast();
171 short decision = ((Short) decisions.removeLast()).shortValue();
174 case LSParserFilter.FILTER_SKIP:
175 // Add all children of element to parent
176 for (Node child = element.getFirstChild(); child != null;
177 child = child.getNextSibling())
179 parent.insertBefore(child, element);
182 case LSParserFilter.FILTER_REJECT:
186 decision = getDecision(element, false);
189 case LSParserFilter.FILTER_ACCEPT:
190 parent.appendChild(element);
192 case LSParserFilter.FILTER_INTERRUPT:
198 public void characters(char[] c, int off, int len)
201 if (rejecting || interrupted)
205 Text text = createText(c, off, len);
206 short decision = getDecision(text, false);
209 case LSParserFilter.FILTER_ACCEPT:
210 ctx.appendChild(text);
212 case LSParserFilter.FILTER_INTERRUPT:
218 public void processingInstruction(String target, String data)
221 if (rejecting || interrupted || inDTD)
225 Node pi = createProcessingInstruction(target, data);
226 short decision = getDecision(pi, false);
229 case LSParserFilter.FILTER_ACCEPT:
232 case LSParserFilter.FILTER_INTERRUPT:
238 public void startDTD(String name, String publicId, String systemId)
245 Node doctype = createDocumentType(name, publicId, systemId);
248 nodes.addLast(doctype);
249 decisions.addLast(new Short(LSParserFilter.FILTER_ACCEPT));
259 Node doctype = (Node) nodes.removeLast();
260 decisions.removeLast();
263 short decision = getDecision(doctype, false);
266 case LSParserFilter.FILTER_ACCEPT:
267 ctx.appendChild(doctype);
269 case LSParserFilter.FILTER_INTERRUPT:
275 public void comment(char[] c, int off, int len)
278 if (rejecting || interrupted || inDTD)
282 Node comment = createComment(c, off, len);
283 short decision = getDecision(comment, false);
286 case LSParserFilter.FILTER_ACCEPT:
287 ctx.appendChild(comment);
289 case LSParserFilter.FILTER_INTERRUPT:
297 short getDecision(Node node, boolean start)
299 boolean show = (whatToShow == NodeFilter.SHOW_ALL);
302 switch (node.getNodeType())
304 case Node.ATTRIBUTE_NODE:
305 show = ((whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0);
308 show = ((whatToShow & NodeFilter.SHOW_TEXT) != 0);
310 case Node.CDATA_SECTION_NODE:
311 show = ((whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0);
313 case Node.ELEMENT_NODE:
314 show = ((whatToShow & NodeFilter.SHOW_ELEMENT) != 0);
316 case Node.COMMENT_NODE:
317 show = ((whatToShow & NodeFilter.SHOW_COMMENT) != 0);
319 case Node.DOCUMENT_NODE:
320 show = ((whatToShow & NodeFilter.SHOW_DOCUMENT) != 0);
322 case Node.PROCESSING_INSTRUCTION_NODE:
323 show = ((whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0);
325 case Node.DOCUMENT_FRAGMENT_NODE:
326 show = ((whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0);
328 case Node.DOCUMENT_TYPE_NODE:
329 show = ((whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0);
331 case Node.ENTITY_REFERENCE_NODE:
332 show = ((whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0);
334 case Node.ENTITY_NODE:
335 show = ((whatToShow & NodeFilter.SHOW_ENTITY) != 0);
337 case Node.NOTATION_NODE:
338 show = ((whatToShow & NodeFilter.SHOW_NOTATION) != 0);
344 return LSParserFilter.FILTER_ACCEPT;
348 return filter.startElement((Element) node);
350 return filter.acceptNode(node);