001    /*
002     * Copyright (c) 2007 Henri Sivonen
003     * Copyright (c) 2007-2009 Mozilla Foundation
004     *
005     * Permission is hereby granted, free of charge, to any person obtaining a 
006     * copy of this software and associated documentation files (the "Software"), 
007     * to deal in the Software without restriction, including without limitation 
008     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
009     * and/or sell copies of the Software, and to permit persons to whom the 
010     * Software is furnished to do so, subject to the following conditions:
011     *
012     * The above copyright notice and this permission notice shall be included in 
013     * all copies or substantial portions of the Software.
014     *
015     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
016     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
017     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
018     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
019     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
020     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
021     * DEALINGS IN THE SOFTWARE.
022     */
023    
024    package nu.validator.saxtree;
025    
026    import java.util.List;
027    
028    import org.xml.sax.Attributes;
029    import org.xml.sax.Locator;
030    import org.xml.sax.SAXException;
031    
032    /**
033     * The common node superclass.
034     * @version $Id$
035     * @author hsivonen
036     */
037    public abstract class Node implements Locator {
038    
039        /**
040         * The system id.
041         */
042        private final String systemId;
043        
044        /**
045         * The public id.
046         */
047        private final String publicId;
048        
049        /**
050         * The column.
051         */
052        private final int column;
053        
054        /**
055         * The line.
056         */
057        private final int line;
058    
059        /**
060         * The next sibling.
061         */
062        private Node nextSibling = null;
063        
064        /**
065         * The parent.
066         */
067        private ParentNode parentNode = null;
068    
069        /**
070         * The constructor.
071         * 
072         * @param locator the locator
073         */
074        Node(Locator locator) {
075            if (locator == null) {
076                this.systemId = null;
077                this.publicId = null;
078                this.column = -1;
079                this.line = -1;
080            } else {
081                this.systemId = locator.getSystemId();
082                this.publicId = locator.getPublicId();
083                this.column = locator.getColumnNumber();
084                this.line = locator.getLineNumber();
085            }
086        }
087        
088        /**
089         * 
090         * @see org.xml.sax.Locator#getColumnNumber()
091         */
092        public int getColumnNumber() {
093            return column;
094        }
095    
096        /**
097         * 
098         * @see org.xml.sax.Locator#getLineNumber()
099         */
100        public int getLineNumber() {
101            return line;
102        }
103    
104        /**
105         * 
106         * @see org.xml.sax.Locator#getPublicId()
107         */
108        public String getPublicId() {
109            return publicId;
110        }
111    
112        /**
113         * 
114         * @see org.xml.sax.Locator#getSystemId()
115         */
116        public String getSystemId() {
117            return systemId;
118        }
119    
120        /**
121         * Visit the node.
122         * 
123         * @param treeParser the visitor
124         * @throws SAXException if stuff goes wrong
125         */
126        abstract void visit(TreeParser treeParser) throws SAXException;
127        
128        /**
129         * Revisit the node.
130         * 
131         * @param treeParser the visitor
132         * @throws SAXException if stuff goes wrong
133         */
134        void revisit(TreeParser treeParser) throws SAXException {
135            return;
136        }
137        
138        /**
139         * Return the first child.
140         * @return the first child
141         */
142        public Node getFirstChild() {
143            return null;
144        }
145    
146        /**
147         * Returns the nextSibling.
148         * 
149         * @return the nextSibling
150         */
151        public final Node getNextSibling() {
152            return nextSibling;
153        }
154        
155        /**
156         * Returns the previous sibling
157         * @return the previous sibling
158         */
159        public final Node getPreviousSibling() {
160            Node prev = null;
161            Node next = parentNode.getFirstChild();
162            for(;;) {
163                if (this == next) {
164                    return prev;
165                }
166                prev = next;
167                next = next.nextSibling;
168            }
169        }
170    
171        /**
172         * Sets the nextSibling.
173         * 
174         * @param nextSibling the nextSibling to set
175         */
176        void setNextSibling(Node nextSibling) {
177            this.nextSibling = nextSibling;
178        }
179        
180        
181        /**
182         * Returns the parentNode.
183         * 
184         * @return the parentNode
185         */
186        public final ParentNode getParentNode() {
187            return parentNode;
188        }
189        
190        /**
191         * Sets the parentNode.
192         * 
193         * @param parentNode the parentNode to set
194         */
195        void setParentNode(ParentNode parentNode) {
196            this.parentNode = parentNode;
197        }
198        
199        /**
200         * Return the node type.
201         * @return the node type
202         */
203        public abstract NodeType getNodeType();
204        
205        // Subclass-specific accessors that are hoisted here to 
206        // avoid casting.
207        
208        /**
209         * Detach this node from its parent.
210         */
211        public void detach() {
212            if (parentNode != null) {
213                parentNode.removeChild(this);
214                parentNode = null;
215            }
216        }
217        
218        /**
219         * Returns the name.
220         * 
221         * @return the name
222         */
223        public String getName() {
224            throw new UnsupportedOperationException();
225        }
226    
227        /**
228         * Returns the publicIdentifier.
229         * 
230         * @return the publicIdentifier
231         */
232        public String getPublicIdentifier() {
233            throw new UnsupportedOperationException();
234        }
235    
236        /**
237         * Returns the systemIdentifier.
238         * 
239         * @return the systemIdentifier
240         */
241        public String getSystemIdentifier() {
242            throw new UnsupportedOperationException();
243        }
244    
245        /**
246         * Returns the attributes.
247         * 
248         * @return the attributes
249         */
250        public Attributes getAttributes() {
251            throw new UnsupportedOperationException();
252        }
253    
254        /**
255         * Returns the localName.
256         * 
257         * @return the localName
258         */
259        public String getLocalName() {
260            throw new UnsupportedOperationException();
261        }
262    
263        /**
264         * Returns the prefixMappings.
265         * 
266         * @return the prefixMappings
267         */
268        public List<PrefixMapping> getPrefixMappings() {
269            throw new UnsupportedOperationException();
270        }
271    
272        /**
273         * Returns the qName.
274         * 
275         * @return the qName
276         */
277        public String getQName() {
278            throw new UnsupportedOperationException();
279        }
280    
281        /**
282         * Returns the uri.
283         * 
284         * @return the uri
285         */
286        public String getUri() {
287            throw new UnsupportedOperationException();
288        }
289    
290        /**
291         * Returns the data.
292         * 
293         * @return the data
294         */
295        public String getData() {
296            throw new UnsupportedOperationException();
297        }
298    
299        /**
300         * Returns the target.
301         * 
302         * @return the target
303         */
304        public String getTarget() {
305            throw new UnsupportedOperationException();
306        }
307    }