001/*
002 * Copyright (c) 2004 World Wide Web Consortium,
003 *
004 * (Massachusetts Institute of Technology, European Research Consortium for
005 * Informatics and Mathematics, Keio University). All Rights Reserved. This
006 * work is distributed under the W3C(r) Software License [1] in the hope that
007 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
008 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
009 *
010 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
011 */
012
013package org.w3c.dom.ls;
014
015import org.w3c.dom.Node;
016import org.w3c.dom.Element;
017
018/**
019 *  <code>LSParserFilter</code>s provide applications the ability to examine
020 * nodes as they are being constructed while parsing. As each node is
021 * examined, it may be modified or removed, or the entire parse may be
022 * terminated early.
023 * <p> At the time any of the filter methods are called by the parser, the
024 * owner Document and DOMImplementation objects exist and are accessible.
025 * The document element is never passed to the <code>LSParserFilter</code>
026 * methods, i.e. it is not possible to filter out the document element.
027 * <code>Document</code>, <code>DocumentType</code>, <code>Notation</code>,
028 * <code>Entity</code>, and <code>Attr</code> nodes are never passed to the
029 * <code>acceptNode</code> method on the filter. The child nodes of an
030 * <code>EntityReference</code> node are passed to the filter if the
031 * parameter "<a href='http://www.w3.org/TR/DOM-Level-3-Core/core.html#parameter-entities'>
032 * entities</a>" is set to <code>false</code>. Note that, as described by the parameter "<a href='http://www.w3.org/TR/DOM-Level-3-Core/core.html#parameter-entities'>
033 * entities</a>", unexpanded entity reference nodes are never discarded and are always
034 * passed to the filter.
035 * <p> All validity checking while parsing a document occurs on the source
036 * document as it appears on the input stream, not on the DOM document as it
037 * is built in memory. With filters, the document in memory may be a subset
038 * of the document on the stream, and its validity may have been affected by
039 * the filtering.
040 * <p> All default attributes must be present on elements when the elements
041 * are passed to the filter methods. All other default content must be
042 * passed to the filter methods.
043 * <p> DOM applications must not raise exceptions in a filter. The effect of
044 * throwing exceptions from a filter is DOM implementation dependent.
045 * <p>See also the <a href='http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407'>Document Object Model (DOM) Level 3 Load
046and Save Specification</a>.
047 */
048public interface LSParserFilter {
049    // Constants returned by startElement and acceptNode
050    /**
051     * Accept the node.
052     */
053    public static final short FILTER_ACCEPT             = 1;
054    /**
055     * Reject the node and its children.
056     */
057    public static final short FILTER_REJECT             = 2;
058    /**
059     * Skip this single node. The children of this node will still be
060     * considered.
061     */
062    public static final short FILTER_SKIP               = 3;
063    /**
064     *  Interrupt the normal processing of the document.
065     */
066    public static final short FILTER_INTERRUPT          = 4;
067
068    /**
069     *  The parser will call this method after each <code>Element</code> start
070     * tag has been scanned, but before the remainder of the
071     * <code>Element</code> is processed. The intent is to allow the
072     * element, including any children, to be efficiently skipped. Note that
073     * only element nodes are passed to the <code>startElement</code>
074     * function.
075     * <br>The element node passed to <code>startElement</code> for filtering
076     * will include all of the Element's attributes, but none of the
077     * children nodes. The Element may not yet be in place in the document
078     * being constructed (it may not have a parent node.)
079     * <br>A <code>startElement</code> filter function may access or change
080     * the attributes for the Element. Changing Namespace declarations will
081     * have no effect on namespace resolution by the parser.
082     * <br>For efficiency, the Element node passed to the filter may not be
083     * the same one as is actually placed in the tree if the node is
084     * accepted. And the actual node (node object identity) may be reused
085     * during the process of reading in and filtering a document.
086     * @param elementArg The newly encountered element. At the time this
087     *   method is called, the element is incomplete - it will have its
088     *   attributes, but no children.
089     * @return
090     * <ul>
091     * <li> <code>FILTER_ACCEPT</code> if the <code>Element</code> should
092     *   be included in the DOM document being built.
093     * </li>
094     * <li>
095     *   <code>FILTER_REJECT</code> if the <code>Element</code> and all of
096     *   its children should be rejected.
097     * </li>
098     * <li> <code>FILTER_SKIP</code> if the
099     *   <code>Element</code> should be skipped. All of its children are
100     *   inserted in place of the skipped <code>Element</code> node.
101     * </li>
102     * <li>
103     *   <code>FILTER_INTERRUPT</code> if the filter wants to stop the
104     *   processing of the document. Interrupting the processing of the
105     *   document does no longer guarantee that the resulting DOM tree is
106     *   XML well-formed. The <code>Element</code> is rejected.
107     * </li>
108     * </ul> Returning
109     *   any other values will result in unspecified behavior.
110     */
111    public short startElement(Element elementArg);
112
113    /**
114     * This method will be called by the parser at the completion of the
115     * parsing of each node. The node and all of its descendants will exist
116     * and be complete. The parent node will also exist, although it may be
117     * incomplete, i.e. it may have additional children that have not yet
118     * been parsed. Attribute nodes are never passed to this function.
119     * <br>From within this method, the new node may be freely modified -
120     * children may be added or removed, text nodes modified, etc. The state
121     * of the rest of the document outside this node is not defined, and the
122     * affect of any attempt to navigate to, or to modify any other part of
123     * the document is undefined.
124     * <br>For validating parsers, the checks are made on the original
125     * document, before any modification by the filter. No validity checks
126     * are made on any document modifications made by the filter.
127     * <br>If this new node is rejected, the parser might reuse the new node
128     * and any of its descendants.
129     * @param nodeArg The newly constructed element. At the time this method
130     *   is called, the element is complete - it has all of its children
131     *   (and their children, recursively) and attributes, and is attached
132     *   as a child to its parent.
133     * @return
134     * <ul>
135     * <li> <code>FILTER_ACCEPT</code> if this <code>Node</code> should
136     *   be included in the DOM document being built.
137     * </li>
138     * <li>
139     *   <code>FILTER_REJECT</code> if the <code>Node</code> and all of its
140     *   children should be rejected.
141     * </li>
142     * <li> <code>FILTER_SKIP</code> if the
143     *   <code>Node</code> should be skipped and the <code>Node</code>
144     *   should be replaced by all the children of the <code>Node</code>.
145     * </li>
146     * <li>
147     *   <code>FILTER_INTERRUPT</code> if the filter wants to stop the
148     *   processing of the document. Interrupting the processing of the
149     *   document does no longer guarantee that the resulting DOM tree is
150     *   XML well-formed. The <code>Node</code> is accepted and will be the
151     *   last completely parsed node.
152     * </li>
153     * </ul>
154     */
155    public short acceptNode(Node nodeArg);
156
157    /**
158     *  Tells the <code>LSParser</code> what types of nodes to show to the
159     * method <code>LSParserFilter.acceptNode</code>. If a node is not shown
160     * to the filter using this attribute, it is automatically included in
161     * the DOM document being built. See <code>NodeFilter</code> for
162     * definition of the constants. The constants <code>SHOW_ATTRIBUTE</code>
163     * , <code>SHOW_DOCUMENT</code>, <code>SHOW_DOCUMENT_TYPE</code>,
164     * <code>SHOW_NOTATION</code>, <code>SHOW_ENTITY</code>, and
165     * <code>SHOW_DOCUMENT_FRAGMENT</code> are meaningless here. Those nodes
166     * will never be passed to <code>LSParserFilter.acceptNode</code>.
167     * <br> The constants used here are defined in [<a href='http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113'>DOM Level 2 Traversal and      Range</a>]
168     * .
169     */
170    public int getWhatToShow();
171
172}