1 /********************************************** 2 * Copyright (C) 2010 Lukas Laag 3 * This file is part of lib-gwt-svg. 4 * 5 * libgwtsvg is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * libgwtsvg is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/ 17 **********************************************/ 18 /* 19 * Copyright (c) 2004 World Wide Web Consortium, 20 * 21 * (Massachusetts Institute of Technology, European Research Consortium for 22 * Informatics and Mathematics, Keio University). All Rights Reserved. This 23 * work is distributed under the W3C(r) Software License [1] in the hope that 24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 26 * 27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231 28 */ 29 package org.vectomatic.dom.svg; 30 31 import org.vectomatic.dom.svg.utils.DOMHelper; 32 import org.w3c.dom.DOMException; 33 34 import com.google.gwt.core.client.JavaScriptException; 35 import com.google.gwt.dom.client.Element; 36 37 /** 38 * Wrapper class for DOM Element 39 * @author laaglu 40 */ 41 public class OMElement extends OMNode { 42 /** 43 * Constructor 44 * @param element The wrapped element 45 */ 46 47 protected OMElement(Element element) { 48 super(element); 49 } 50 51 /** 52 * Returns the wrapped Element 53 * @return the wrapped Element 54 */ 55 public Element getElement() { 56 return ot.cast(); 57 } 58 59 // Implementation of the dom::Element W3C IDL interface 60 /** 61 * Returns a <code>OMNodeList</code> of all descendant <code>OMElements</code> 62 * with a given tag name, in document order. 63 * @param name The name of the tag to match on. The special value "*" 64 * matches all tags. 65 * @return A list of matching <code>OMElement</code> nodes. 66 */ 67 public final <T extends OMElement> OMNodeList<T> getElementsByTagName(String name) { 68 return new OMNodeList<T>(((Element) ot).getElementsByTagName(name)); 69 } 70 71 /** 72 * Returns a <code>OMNodeList</code> of all the descendant 73 * <code>OMElements</code> with a given local name and namespace URI in 74 * document order. 75 * @param namespaceURI The namespace URI of the elements to match on. The 76 * special value "*" matches all namespaces. 77 * @param localName The local name of the elements to match on. The 78 * special value "*" matches all local names. 79 * @return A new <code>OMNodeList</code> object containing all the matched 80 * <code>OMElements</code>. 81 * @exception DOMException 82 * NOT_SUPPORTED_ERR: May be raised if the implementation does not 83 * support the feature <code>"XML"</code> and the language exposed 84 * through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]). 85 */ 86 public final <T extends OMElement> OMNodeList<T> getElementsByTagNameNS(String namespaceURI, String localName) throws JavaScriptException { 87 return new OMNodeList<T>(DOMHelper.getElementsByTagNameNS(((Element) ot), namespaceURI, localName)); 88 } 89 90 /** 91 * Returns the element id 92 * @return the element id 93 */ 94 public final String getId() { 95 return ((Element) ot).getId(); 96 } 97 98 /** 99 * The name of the element. If <code>Node.localName</code> is different 100 * from <code>null</code>, this attribute is a qualified name. For 101 * example, in: 102 * <pre> <elementExample id="demo"> ... 103 * </elementExample> , </pre> 104 * <code>tagName</code> has the value 105 * <code>"elementExample"</code>. Note that this is case-preserving in 106 * XML, as are all of the operations of the DOM. The HTML DOM returns 107 * the <code>tagName</code> of an HTML element in the canonical 108 * uppercase form, regardless of the case in the source HTML document. 109 * @return the name of the element 110 */ 111 public final String getTagName() { 112 return ((Element) ot).getTagName(); 113 } 114 115 /** 116 * Determines whether an element has an attribute with a given name. 117 * <p> 118 * Note that IE, prior to version 8, will return false-positives for names 119 * that collide with element properties (e.g., style, width, and so forth). 120 * </p> 121 * 122 * @param name 123 * the name of the attribute 124 * @return <code>true</code> if this element has the specified attribute 125 */ 126 public final boolean hasAttribute(String name) { 127 return ((Element) ot).hasAttribute(name); 128 } 129 130 /** 131 * Returns <code>true</code> when an attribute with a given local name and 132 * namespace URI is specified on the specified element or has a default value, 133 * <code>false</code> otherwise. 134 * <br>Per [<a href='http://www.w3.org/TR/1999/REC-xml-names-19990114/'>XML Namespaces</a>] 135 * , applications must use the value <code>null</code> as the 136 * <code>namespaceURI</code> parameter for methods if they wish to have 137 * no namespace. 138 * @param namespaceURI The namespace URI of the attribute to look for. 139 * @param localName The local name of the attribute to look for. 140 * @return <code>true</code> if an attribute with the given local name 141 * and namespace URI is specified or has a default value on this 142 * element, <code>false</code> otherwise. 143 * @exception DOMException 144 * NOT_SUPPORTED_ERR: May be raised if the implementation does not 145 * support the feature <code>"XML"</code> and the language exposed 146 * through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]). 147 */ 148 public final boolean hasAttributeNS(String namespaceURI, String localName) throws JavaScriptException { 149 return DOMHelper.hasAttributeNS((Element) ot, namespaceURI, localName); 150 } 151 /** 152 * Retrieves an attribute value by name. 153 * @param name The name of the attribute to retrieve. 154 * @return The <code>OMAttr</code> value as a string, or the empty string 155 * if that attribute does not have a specified or default value. 156 */ 157 public final String getAttribute(String name) { 158 return ((Element) ot).getAttribute(name); 159 } 160 /** 161 * Retrieves an attribute node by name on the specified element. 162 * <br>To retrieve an attribute node by qualified name and namespace URI, 163 * use the <code>getAttributeNodeNS</code> method. 164 * @param attrName The name (<code>nodeName</code>) of the attribute to 165 * retrieve. 166 * @return The {@link OMAttr} node with the specified name ( 167 * <code>nodeName</code>) or <code>null</code> if there is no such 168 * attribute. 169 */ 170 public final OMAttr getAttributeNode(String attrName) { 171 return OMNode.convert(DOMHelper.getAttributeNode((Element) ot, attrName)); 172 } 173 /** 174 * Retrieves an attribute value by local name and namespace URI. 175 * <br>Per [<a href='http://www.w3.org/TR/1999/REC-xml-names-19990114/'>XML Namespaces</a>] 176 * , applications must use the value <code>null</code> as the 177 * <code>namespaceURI</code> parameter for methods if they wish to have 178 * no namespace. 179 * @param namespaceURI The namespace URI of the attribute to retrieve. 180 * @param localName The local name of the attribute to retrieve. 181 * @return The <code>OMAttr</code> value as a string, or the empty string 182 * if that attribute does not have a specified or default value. 183 * @exception DOMException 184 * NOT_SUPPORTED_ERR: May be raised if the implementation does not 185 * support the feature <code>"XML"</code> and the language exposed 186 * through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]). 187 */ 188 public final String getAttributeNS(String namespaceURI, String localName) throws JavaScriptException { 189 return DOMHelper.getAttributeNS((Element) ot, namespaceURI, localName); 190 } 191 /** 192 * Returns a {@link OMNamedNodeMap} containing the attributes of the 193 * specified element. 194 * @return a {@link OMNamedNodeMap} containing the attributes of the 195 * specified element 196 */ 197 public final OMNamedNodeMap<OMAttr> getAttributes() { 198 return new OMNamedNodeMap<OMAttr>(DOMHelper.getAttributes((Element) ot)); 199 } 200 /** 201 * Adds a new attribute. If an attribute with that name is already present 202 * in the element, its value is changed to be that of the value 203 * parameter. This value is a simple string; it is not parsed as it is 204 * being set. So any markup (such as syntax to be recognized as an 205 * entity reference) is treated as literal text, and needs to be 206 * appropriately escaped by the implementation when it is written out. 207 * <br>To set an attribute with a qualified name and namespace URI, use 208 * the <code>setAttributeNS</code> method. 209 * @param name The name of the attribute to create or alter. 210 * @param value Value to set in string form. 211 * @exception DOMException 212 * INVALID_CHARACTER_ERR: Raised if the specified name is not an XML 213 * name according to the XML version in use specified in the 214 * <code>Document.xmlVersion</code> attribute. 215 * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. 216 */ 217 public final void setAttribute(String name, String value) throws JavaScriptException { 218 ((Element) ot).setAttribute(name, value); 219 } 220 /** 221 * Adds a new attribute to the specified element. If an attribute with the same local name and 222 * namespace URI is already present on the element, its prefix is 223 * changed to be the prefix part of the <code>qualifiedName</code>, and 224 * its value is changed to be the <code>value</code> parameter. This 225 * value is a simple string; it is not parsed as it is being set. So any 226 * markup (such as syntax to be recognized as an entity reference) is 227 * treated as literal text, and needs to be appropriately escaped by the 228 * implementation when it is written out. In order to assign an 229 * attribute value that contains entity references, the user must create 230 * an {@link OMAttr} node plus any {@link OMText} and 231 * <code>EntityReference</code> nodes, build the appropriate subtree, 232 * and use <code>setAttributeNodeNS</code> or 233 * <code>setAttributeNode</code> to assign it as the value of an 234 * attribute. 235 * <br>Per [<a href='http://www.w3.org/TR/1999/REC-xml-names-19990114/'>XML Namespaces</a>] 236 * , applications must use the value <code>null</code> as the 237 * <code>namespaceURI</code> parameter for methods if they wish to have 238 * no namespace. 239 * @param namespaceURI The namespace URI of the attribute to create or 240 * alter. 241 * @param localName The local name of the attribute to create or 242 * alter. 243 * @param value The value to set in string form. 244 * @exception DOMException 245 * INVALID_CHARACTER_ERR: Raised if the specified qualified name is not 246 * an XML name according to the XML version in use specified in the 247 * <code>Document.xmlVersion</code> attribute. 248 * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. 249 * <br>NAMESPACE_ERR: Raised if the <code>qualifiedName</code> is 250 * malformed per the Namespaces in XML specification, if the 251 * <code>qualifiedName</code> has a prefix and the 252 * <code>namespaceURI</code> is <code>null</code>, if the 253 * <code>qualifiedName</code> has a prefix that is "xml" and the 254 * <code>namespaceURI</code> is different from "<a href='http://www.w3.org/XML/1998/namespace'> 255 * http://www.w3.org/XML/1998/namespace</a>", if the <code>qualifiedName</code> or its prefix is "xmlns" and the 256 * <code>namespaceURI</code> is different from "<a href='http://www.w3.org/2000/xmlns/'>http://www.w3.org/2000/xmlns/</a>", or if the <code>namespaceURI</code> is "<a href='http://www.w3.org/2000/xmlns/'>http://www.w3.org/2000/xmlns/</a>" and neither the <code>qualifiedName</code> nor its prefix is "xmlns". 257 * <br>NOT_SUPPORTED_ERR: May be raised if the implementation does not 258 * support the feature <code>"XML"</code> and the language exposed 259 * through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]). 260 */ 261 public final void setAttributeNS(String namespaceURI, String localName, String value) throws JavaScriptException { 262 DOMHelper.setAttributeNS((Element) ot, namespaceURI, localName, value); 263 } 264 265 /** 266 * Adds a new attribute node to the specified element. If an attribute with that name ( 267 * <code>nodeName</code>) is already present in the element, it is 268 * replaced by the new one. Replacing an attribute node by itself has no 269 * effect. 270 * <br>To add a new attribute node with a qualified name and namespace 271 * URI, use the <code>setAttributeNodeNS</code> method. 272 * @param attr The {@link OMAttr} node to add to the attribute list. 273 * @return If the <code>attr</code> attribute replaces an existing 274 * attribute, the replaced {@link OMAttr} node is returned, 275 * otherwise <code>null</code> is returned. 276 * @exception DOMException 277 * WRONG_DOCUMENT_ERR: Raised if <code>attr</code> was created from a 278 * different document than the one that created the element. 279 * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. 280 * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>attr</code> is already an 281 * attribute of another <code>Element</code> object. The DOM user must 282 * explicitly clone {@link OMAttr} nodes to re-use them in other 283 * elements. 284 */ 285 public final OMAttr setAttributeNode(OMAttr attr) throws JavaScriptException { 286 return OMNode.convert(DOMHelper.setAttributeNode((Element) ot, attr.getAttr())); 287 } 288 /** 289 * Removes an attribute by name. If a default value for the removed 290 * attribute is defined in the DTD, a new attribute immediately appears 291 * with the default value as well as the corresponding namespace URI, 292 * local name, and prefix when applicable. The implementation may handle 293 * default values from other schemas similarly but applications should 294 * use <code>OMDocument.normalizeDocument()</code> to guarantee this 295 * information is up-to-date. 296 * <br>If no attribute with this name is found, this method has no effect. 297 * <br>To remove an attribute by local name and namespace URI, use the 298 * <code>removeAttributeNS</code> method. 299 * @param name The name of the attribute to remove. 300 * @exception DOMException 301 * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. 302 */ 303 public final void removeAttribute(String name) throws JavaScriptException { 304 ((Element) ot).removeAttribute(name); 305 } 306 307 }