001 /*
002 * Copyright (c) 2007 Henri Sivonen
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a
005 * copy of this software and associated documentation files (the "Software"),
006 * to deal in the Software without restriction, including without limitation
007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
008 * and/or sell copies of the Software, and to permit persons to whom the
009 * Software is furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020 * DEALINGS IN THE SOFTWARE.
021 */
022
023 package nu.validator.saxtree;
024
025 import java.util.List;
026
027 import org.xml.sax.Attributes;
028 import org.xml.sax.Locator;
029 import org.xml.sax.SAXException;
030 import org.xml.sax.helpers.AttributesImpl;
031
032 public final class Element extends ParentNode {
033
034 private final String uri;
035
036 private final String localName;
037
038 private final String qName;
039
040 private final Attributes attributes;
041
042 private final List<PrefixMapping> prefixMappings;
043
044 public Element(Locator locator, String uri, String localName, String qName,
045 Attributes atts, boolean retainAttributes,
046 List<PrefixMapping> prefixMappings) {
047 super(locator);
048 this.uri = uri;
049 this.localName = localName;
050 this.qName = qName;
051 if (retainAttributes) {
052 this.attributes = atts;
053 } else {
054 this.attributes = new AttributesImpl(atts);
055 }
056 this.prefixMappings = prefixMappings;
057 }
058
059 @Override
060 void visit(TreeParser treeParser) throws SAXException {
061 if (prefixMappings != null) {
062 for (PrefixMapping mapping : prefixMappings) {
063 treeParser.startPrefixMapping(mapping.getPrefix(),
064 mapping.getUri(), this);
065 }
066 }
067 treeParser.startElement(uri, localName, qName, attributes, this);
068 }
069
070 /**
071 * @throws SAXException
072 * @see nu.validator.saxtree.Node#revisit(nu.validator.saxtree.TreeParser)
073 */
074 @Override
075 void revisit(TreeParser treeParser) throws SAXException {
076 treeParser.endElement(uri, localName, qName, endLocator);
077 if (prefixMappings != null) {
078 for (PrefixMapping mapping : prefixMappings) {
079 treeParser.endPrefixMapping(mapping.getPrefix(), endLocator);
080 }
081 }
082 }
083
084 /**
085 * Returns the attributes.
086 *
087 * @return the attributes
088 */
089 public Attributes getAttributes() {
090 return attributes;
091 }
092
093 /**
094 * Returns the localName.
095 *
096 * @return the localName
097 */
098 public String getLocalName() {
099 return localName;
100 }
101
102 /**
103 * Returns the prefixMappings.
104 *
105 * @return the prefixMappings
106 */
107 public List<PrefixMapping> getPrefixMappings() {
108 return prefixMappings;
109 }
110
111 /**
112 * Returns the qName.
113 *
114 * @return the qName
115 */
116 public String getQName() {
117 return qName;
118 }
119
120 /**
121 * Returns the uri.
122 *
123 * @return the uri
124 */
125 public String getUri() {
126 return uri;
127 }
128
129 @Override
130 public NodeType getNodeType() {
131 return NodeType.ELEMENT;
132 }
133
134 }