001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.xml;
039:
040: import java.util.Collections;
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.Map;
044:
045: import org.apache.commons.collections.map.ListOrderedMap;
046:
047: import com.gargoylesoftware.htmlunit.Page;
048: import com.gargoylesoftware.htmlunit.html.DomNamespaceNode;
049:
050: /**
051: * An XML element.
052: *
053: * @version $Revision: 2132 $
054: * @author Ahmed Ashour
055: */
056: public class XmlElement extends DomNamespaceNode {
057:
058: private static final long serialVersionUID = -8119109851558707854L;
059:
060: /** Constant meaning that the specified attribute was not defined. */
061: public static final String ATTRIBUTE_NOT_DEFINED = new String("");
062:
063: /** The map holding the namespaces, keyed by URI. */
064: private Map namespaces_ = new HashMap();
065:
066: /** The map holding the attributes, keyed by name. */
067: private Map/* String, XmlAttr*/attributes_;
068:
069: /**
070: * Create an instance of a DOM node that can have a namespace.
071: *
072: * @param namespaceURI the URI that identifies an XML namespace.
073: * @param qualifiedName The qualified name of the element type to instantiate.
074: * @param page The page that contains this element.
075: * @param attributes The attributes of this element.
076: */
077: protected XmlElement(final String namespaceURI,
078: final String qualifiedName, final Page page,
079: final Map/* String, XmlAttr*/attributes) {
080: super (namespaceURI, qualifiedName, page);
081: attributes_ = attributes;
082: for (final Iterator values = attributes.values().iterator(); values
083: .hasNext();) {
084: final XmlAttr attr = (XmlAttr) values.next();
085: attr.setParentNode(this );
086: }
087: }
088:
089: /**
090: * {@inheritDoc}
091: */
092: public short getNodeType() {
093: return org.w3c.dom.Node.ELEMENT_NODE;
094: }
095:
096: /**
097: * @return The same value as returned by {@link #getTagName()},
098: */
099: public String getNodeName() {
100: return getTagName();
101: }
102:
103: /**
104: * Return the tag name of this element.
105: * @return the tag name of this element.
106: */
107: public String getTagName() {
108: if (getNamespaceURI() == null) {
109: return getLocalName();
110: } else {
111: return getQualifiedName();
112: }
113: }
114:
115: /**
116: * Return the value of the specified attribute or an empty string. If the
117: * result is an empty string then it will be {@link #ATTRIBUTE_NOT_DEFINED}
118: *
119: * @param attributeName the name of the attribute
120: * @return The value of the attribute or {@link #ATTRIBUTE_NOT_DEFINED}
121: */
122: public final String getAttributeValue(final String attributeName) {
123: final XmlAttr attr = (XmlAttr) attributes_.get(attributeName);
124:
125: if (attr != null) {
126: return attr.getNodeValue();
127: } else {
128: return ATTRIBUTE_NOT_DEFINED;
129: }
130: }
131:
132: /**
133: * Returns the map holding the attributes, keyed by name.
134: * @return the attributes map.
135: */
136: public Map getAttributes() {
137: return attributes_;
138: }
139:
140: /**
141: * Set the value of the attribute specified by name.
142: *
143: * @param attributeName the name of the attribute
144: * @param attributeValue The value of the attribute
145: */
146: public final void setAttribute(final String attributeName,
147: final String attributeValue) {
148: setAttributeValue(null, attributeName, attributeValue);
149: }
150:
151: /**
152: * Set the value of the attribute specified by namespace and qualified name.
153: *
154: * @param namespaceURI the URI that identifies an XML namespace.
155: * @param qualifiedName The qualified name (prefix:local) of the attribute.
156: * @param attributeValue The value of the attribute
157: */
158: public final void setAttributeNS(final String namespaceURI,
159: final String qualifiedName, final String attributeValue) {
160: setAttributeValue(namespaceURI, qualifiedName, attributeValue);
161: }
162:
163: /**
164: * Set the value of the specified attribute.
165: *
166: * @param attributeName the name of the attribute
167: * @param attributeValue The value of the attribute
168: */
169: public final void setAttributeValue(final String attributeName,
170: final String attributeValue) {
171: setAttributeValue(null, attributeName, attributeValue);
172: }
173:
174: /**
175: * Set the value of the specified attribute.
176: *
177: * @param namespaceURI the URI that identifies an XML namespace.
178: * @param qualifiedName The qualified name of the attribute
179: * @param attributeValue The value of the attribute
180: */
181: public final void setAttributeValue(final String namespaceURI,
182: final String qualifiedName, final String attributeValue) {
183: final String value = attributeValue;
184:
185: if (attributes_ == Collections.EMPTY_MAP) {
186: attributes_ = createAttributeMap(1);
187: }
188: final XmlAttr newAttr = addAttributeToMap(
189: (XmlPage) getNativePage(), attributes_, namespaceURI,
190: qualifiedName, value);
191: if (namespaceURI != null) {
192: namespaces_.put(namespaceURI, newAttr.getPrefix());
193: }
194: attributes_.put(newAttr.getName(), newAttr);
195: }
196:
197: /**
198: * Removes an attribute specified by name from this element.
199: * @param attributeName the attribute attributeName
200: */
201: public final void removeAttribute(final String attributeName) {
202: attributes_.remove(attributeName.toLowerCase());
203: }
204:
205: /**
206: * Removes an attribute specified by namespace and local name from this element.
207: * @param namespaceURI the URI that identifies an XML namespace.
208: * @param localName The name within the namespace.
209: */
210: public final void removeAttributeNS(final String namespaceURI,
211: final String localName) {
212: removeAttribute(getQualifiedName(namespaceURI, localName));
213: }
214:
215: /**
216: * Return the qualified name (prefix:local) for the namespace and local name.
217: *
218: * @param namespaceURI the URI that identifies an XML namespace.
219: * @param localName The name within the namespace.
220: * @return The qualified name or just local name if the namespace is not fully defined.
221: */
222: private String getQualifiedName(final String namespaceURI,
223: final String localName) {
224: final String qualifiedName;
225: if (namespaceURI != null) {
226: final String prefix = (String) namespaces_
227: .get(namespaceURI);
228: if (prefix != null) {
229: qualifiedName = prefix + ':' + localName;
230: } else {
231: qualifiedName = localName;
232: }
233: } else {
234: qualifiedName = localName;
235: }
236: return qualifiedName;
237: }
238:
239: /**
240: * Create an attribute map as needed by HtmlElement. This is just used by the element factories.
241: * @param attributeCount the initial number of attributes to be added to the map.
242: * @return the attribute map.
243: */
244: static Map createAttributeMap(final int attributeCount) {
245: return ListOrderedMap.decorate(new HashMap(attributeCount)); // preserve insertion order
246: }
247:
248: /**
249: * Add an attribute to the attribute map. This is just used by the element factories.
250: * @param attributeMap the attribute map where the attribute will be added.
251: * @param namespaceURI the URI that identifies an XML namespace.
252: * @param qualifiedName The qualified name of the attribute
253: * @param value The value of the attribute
254: */
255: static XmlAttr addAttributeToMap(final XmlPage page,
256: final Map attributeMap, final String namespaceURI,
257: final String qualifiedName, final String value) {
258: final XmlAttr newAttr = new XmlAttr(page, namespaceURI,
259: qualifiedName, value);
260: attributeMap.put(qualifiedName, newAttr);
261: return newAttr;
262: }
263:
264: }
|