001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.xml.client.impl;
017:
018: import com.google.gwt.core.client.JavaScriptException;
019: import com.google.gwt.core.client.JavaScriptObject;
020: import com.google.gwt.xml.client.Attr;
021: import com.google.gwt.xml.client.DOMException;
022: import com.google.gwt.xml.client.Element;
023: import com.google.gwt.xml.client.NodeList;
024:
025: /**
026: * This method implements the Element interface.
027: */
028: class ElementImpl extends NodeImpl implements Element {
029:
030: protected ElementImpl(JavaScriptObject o) {
031: super (o);
032: }
033:
034: /**
035: * This function delegates to the native method <code>getAttribute</code> in
036: * XMLParserImpl.
037: */
038: public String getAttribute(String tagName) {
039: return XMLParserImpl.getAttribute(this .getJsObject(), tagName);
040: }
041:
042: /**
043: * This function delegates to the native method <code>getAttributeNode</code>
044: * in XMLParserImpl.
045: */
046: public Attr getAttributeNode(String tagName) {
047: return (Attr) NodeImpl.build(XMLParserImpl.getAttributeNode(
048: this .getJsObject(), tagName));
049: }
050:
051: /**
052: * This function delegates to the native method
053: * <code>getElementsByTagName</code> in XMLParserImpl.
054: */
055: public NodeList getElementsByTagName(String tagName) {
056: return new NodeListImpl(XMLParserImpl.getElementsByTagName(this
057: .getJsObject(), tagName));
058: }
059:
060: /**
061: * This function delegates to the native method <code>getTagName</code> in
062: * XMLParserImpl.
063: */
064: public String getTagName() {
065: return XMLParserImpl.getTagName(this .getJsObject());
066: }
067:
068: /**
069: * This function delegates to the native method <code>hasAttribute</code> in
070: * XMLParserImpl.
071: */
072: public boolean hasAttribute(String tagName) {
073: return getAttribute(tagName) != null;
074: }
075:
076: /**
077: * This function delegates to the native method <code>removeAttribute</code>
078: * in XMLParserImpl.
079: */
080: public void removeAttribute(String name) throws DOMNodeException {
081: try {
082: XMLParserImpl.removeAttribute(this .getJsObject(), name);
083: } catch (JavaScriptException e) {
084: throw new DOMNodeException(
085: DOMException.INVALID_MODIFICATION_ERR, e, this );
086: }
087: }
088:
089: /**
090: * This function delegates to the native method <code>setAttribute</code> in
091: * XMLParserImpl.
092: */
093: public void setAttribute(String name, String value)
094: throws DOMNodeException {
095: try {
096: XMLParserImpl.setAttribute(this .getJsObject(), name, value);
097: } catch (JavaScriptException e) {
098: throw new DOMNodeException(
099: DOMException.INVALID_MODIFICATION_ERR, e, this );
100: }
101: }
102:
103: /**
104: * This method returns the string representation of this
105: * <code>ElementImpl</code>.
106: * @return the string representation of this
107: * <code>ElementImpl</code>.
108: * @see java.lang.Object#toString()
109: */
110: @Override
111: public String toString() {
112: final StringBuffer b = new StringBuffer("<");
113: b.append(getTagName());
114: if (hasAttributes()) {
115: b.append(getAttributes().toString());
116: }
117: if (hasChildNodes()) {
118: b.append(">");
119: b.append(getChildNodes().toString());
120: b.append("</");
121: b.append(getTagName());
122: b.append(">");
123: } else {
124: b.append("/>");
125: }
126: return b.toString();
127: }
128: }
|