001    /*
002     * Copyright (c) 2007 Henri Sivonen
003     * Copyright (c) 2008 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    import org.xml.sax.helpers.AttributesImpl;
032    
033    /**
034     * An element.
035     * @version $Id$
036     * @author hsivonen
037     */
038    public final class Element extends ParentNode {
039    
040        /**
041         * The namespace URI.
042         */
043        private final String uri;
044    
045        /**
046         * The local name.
047         */
048        private final String localName;
049    
050        /**
051         * The qualified name.
052         */
053        private final String qName;
054    
055        /**
056         * The attributes.
057         */
058        private final Attributes attributes;
059    
060        /**
061         * The namespace prefix mappings.
062         */
063        private final List<PrefixMapping> prefixMappings;
064    
065        /**
066         * The contructor.
067         * @param locator the locator.
068         * @param uri the namespace URI
069         * @param localName the local name
070         * @param qName the qualified name
071         * @param atts the attributes
072         * @param retainAttributes <code>true</code> to retain the attributes instead of copying
073         * @param prefixMappings the prefix mappings
074         */
075        public Element(Locator locator, String uri, String localName, String qName,
076                Attributes atts, boolean retainAttributes,
077                List<PrefixMapping> prefixMappings) {
078            super(locator);
079            this.uri = uri;
080            this.localName = localName;
081            this.qName = qName;
082            if (retainAttributes) {
083                this.attributes = atts;
084            } else {
085                this.attributes = new AttributesImpl(atts);
086            }
087            this.prefixMappings = prefixMappings;
088        }
089    
090        /**
091         * 
092         * @see nu.validator.saxtree.Node#visit(nu.validator.saxtree.TreeParser)
093         */
094        @Override
095        void visit(TreeParser treeParser) throws SAXException {
096            if (prefixMappings != null) {
097                for (PrefixMapping mapping : prefixMappings) {
098                    treeParser.startPrefixMapping(mapping.getPrefix(),
099                            mapping.getUri(), this);
100                }
101            }
102            treeParser.startElement(uri, localName, qName, attributes, this);
103        }
104    
105        /**
106         * @see nu.validator.saxtree.Node#revisit(nu.validator.saxtree.TreeParser)
107         */
108        @Override
109        void revisit(TreeParser treeParser) throws SAXException {
110            treeParser.endElement(uri, localName, qName, endLocator);
111            if (prefixMappings != null) {
112                for (PrefixMapping mapping : prefixMappings) {
113                    treeParser.endPrefixMapping(mapping.getPrefix(), endLocator);
114                }
115            }
116        }
117    
118        /**
119         * Returns the attributes.
120         * 
121         * @return the attributes
122         */
123        public Attributes getAttributes() {
124            return attributes;
125        }
126    
127        /**
128         * Returns the localName.
129         * 
130         * @return the localName
131         */
132        public String getLocalName() {
133            return localName;
134        }
135    
136        /**
137         * Returns the prefixMappings.
138         * 
139         * @return the prefixMappings
140         */
141        public List<PrefixMapping> getPrefixMappings() {
142            return prefixMappings;
143        }
144    
145        /**
146         * Returns the qName.
147         * 
148         * @return the qName
149         */
150        public String getQName() {
151            return qName;
152        }
153    
154        /**
155         * Returns the uri.
156         * 
157         * @return the uri
158         */
159        public String getUri() {
160            return uri;
161        }
162    
163        /**
164         * 
165         * @see nu.validator.saxtree.Node#getNodeType()
166         */
167        @Override
168        public NodeType getNodeType() {
169            return NodeType.ELEMENT;
170        }
171    
172    }