001: /*
002: * Copyright 2006 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.DOMException;
021: import com.google.gwt.xml.client.Document;
022: import com.google.gwt.xml.client.NamedNodeMap;
023: import com.google.gwt.xml.client.Node;
024: import com.google.gwt.xml.client.NodeList;
025:
026: /**
027: * This class wraps the native Node object.
028: */
029: class NodeImpl extends DOMItem implements Node {
030:
031: /**
032: * This method creates a new node of the correct type.
033: *
034: * @param node - the supplied DOM JavaScript object
035: * @return a Node object that corresponds to the DOM object
036: */
037: static Node build(JavaScriptObject node) {
038: if (node == null) {
039: return null;
040: }
041: short nodeType = XMLParserImpl.getNodeType(node);
042: switch (nodeType) {
043: case Node.ATTRIBUTE_NODE:
044: return new AttrImpl(node);
045: case Node.CDATA_SECTION_NODE:
046: return new CDATASectionImpl(node);
047: case Node.COMMENT_NODE:
048: return new CommentImpl(node);
049: case Node.DOCUMENT_FRAGMENT_NODE:
050: return new DocumentFragmentImpl(node);
051: case Node.DOCUMENT_NODE:
052: return new DocumentImpl(node);
053: case Node.ELEMENT_NODE:
054: return new ElementImpl(node);
055: case Node.PROCESSING_INSTRUCTION_NODE:
056: return new ProcessingInstructionImpl(node);
057: case Node.TEXT_NODE:
058: return new TextImpl(node);
059: default:
060: return new NodeImpl(node);
061: }
062: }
063:
064: /**
065: * creates a new NodeImpl from the supplied JavaScriptObject.
066: *
067: * @param jso - the DOM node JavaScriptObject
068: */
069: protected NodeImpl(JavaScriptObject jso) {
070: super (jso);
071: }
072:
073: /**
074: * This function delegates to the native method <code>appendChild</code> in
075: * XMLParserImpl.
076: */
077: public Node appendChild(Node newChild) {
078: try {
079: final JavaScriptObject newChildJs = ((DOMItem) newChild)
080: .getJsObject();
081: final JavaScriptObject appendChildResults = XMLParserImpl
082: .appendChild(this .getJsObject(), newChildJs);
083: return NodeImpl.build(appendChildResults);
084: } catch (JavaScriptException e) {
085: throw new DOMNodeException(
086: DOMException.INVALID_MODIFICATION_ERR, e, this );
087: }
088: }
089:
090: /**
091: * This function delegates to the native method <code>cloneNode</code> in
092: * XMLParserImpl.
093: */
094: public Node cloneNode(boolean deep) {
095: return NodeImpl.build(XMLParserImpl.cloneNode(this
096: .getJsObject(), deep));
097: }
098:
099: public NamedNodeMap getAttributes() {
100: return new NamedNodeMapImpl(XMLParserImpl.getAttributes(this
101: .getJsObject()));
102: }
103:
104: public NodeList getChildNodes() {
105: return new NodeListImpl(XMLParserImpl.getChildNodes(this
106: .getJsObject()));
107: }
108:
109: public Node getFirstChild() {
110: return getChildNodes().item(0);
111: }
112:
113: public Node getLastChild() {
114: return getChildNodes().item(getChildNodes().getLength() - 1);
115: }
116:
117: /**
118: * This function delegates to the native method <code>getNamespaceURI</code>
119: * in XMLParserImpl.
120: */
121: public String getNamespaceURI() {
122: return XMLParserImpl.getNamespaceURI(this .getJsObject());
123: }
124:
125: public Node getNextSibling() {
126: return NodeImpl.build(XMLParserImpl.getNextSibling(this
127: .getJsObject()));
128: }
129:
130: public String getNodeName() {
131: return XMLParserImpl.getNodeName(this .getJsObject());
132: }
133:
134: public short getNodeType() {
135: return XMLParserImpl.getNodeType(this .getJsObject());
136: }
137:
138: public String getNodeValue() {
139: return XMLParserImpl.getNodeValue(this .getJsObject());
140: }
141:
142: public Document getOwnerDocument() {
143: return (Document) NodeImpl.build(XMLParserImpl
144: .getOwnerDocument(this .getJsObject()));
145: }
146:
147: public Node getParentNode() {
148: return NodeImpl.build(XMLParserImpl.getParentNode(this
149: .getJsObject()));
150: }
151:
152: /**
153: * This function delegates to the native method <code>getPrefix</code> in
154: * XMLParserImpl.
155: */
156: public String getPrefix() {
157: return XMLParserImpl.getPrefix(this .getJsObject());
158: }
159:
160: public Node getPreviousSibling() {
161: return NodeImpl.build(XMLParserImpl.getPreviousSibling(this
162: .getJsObject()));
163: }
164:
165: /**
166: * This function delegates to the native method <code>hasAttributes</code>
167: * in XMLParserImpl.
168: */
169: public boolean hasAttributes() {
170: return XMLParserImpl.hasAttributes(this .getJsObject());
171: }
172:
173: /**
174: * This function delegates to the native method <code>hasChildNodes</code>
175: * in XMLParserImpl.
176: */
177: public boolean hasChildNodes() {
178: return XMLParserImpl.hasChildNodes(this .getJsObject());
179: }
180:
181: /**
182: * This function delegates to the native method <code>insertBefore</code> in
183: * XMLParserImpl.
184: */
185: public Node insertBefore(Node newChild, Node refChild) {
186: try {
187: final JavaScriptObject newChildJs = ((DOMItem) newChild)
188: .getJsObject();
189: final JavaScriptObject refChildJs;
190: if (refChild != null) {
191: refChildJs = ((DOMItem) refChild).getJsObject();
192: } else {
193: refChildJs = null;
194: }
195: final JavaScriptObject insertBeforeResults = XMLParserImpl
196: .insertBefore(this .getJsObject(), newChildJs,
197: refChildJs);
198: return NodeImpl.build(insertBeforeResults);
199: } catch (JavaScriptException e) {
200: throw new DOMNodeException(
201: DOMException.INVALID_MODIFICATION_ERR, e, this );
202: }
203: }
204:
205: /**
206: * This function delegates to the native method <code>normalize</code> in
207: * XMLParserImpl.
208: */
209: public void normalize() {
210: XMLParserImpl.normalize(this .getJsObject());
211: }
212:
213: /**
214: * This function delegates to the native method <code>removeChild</code> in
215: * XMLParserImpl.
216: */
217: public Node removeChild(Node oldChild) {
218: try {
219: final JavaScriptObject oldChildJs = ((DOMItem) oldChild)
220: .getJsObject();
221: final JavaScriptObject removeChildResults = XMLParserImpl
222: .removeChild(this .getJsObject(), oldChildJs);
223: return NodeImpl.build(removeChildResults);
224: } catch (JavaScriptException e) {
225: throw new DOMNodeException(
226: DOMException.INVALID_MODIFICATION_ERR, e, this );
227: }
228: }
229:
230: /**
231: * This function delegates to the native method <code>replaceChild</code> in
232: * XMLParserImpl.
233: */
234: public Node replaceChild(Node newChild, Node oldChild) {
235: try {
236: final JavaScriptObject newChildJs = ((DOMItem) newChild)
237: .getJsObject();
238: final JavaScriptObject oldChildJs = ((DOMItem) oldChild)
239: .getJsObject();
240: final JavaScriptObject replaceChildResults = XMLParserImpl
241: .replaceChild(this .getJsObject(), newChildJs,
242: oldChildJs);
243: return NodeImpl.build(replaceChildResults);
244: } catch (JavaScriptException e) {
245: throw new DOMNodeException(
246: DOMException.INVALID_MODIFICATION_ERR, e, this );
247: }
248: }
249:
250: /**
251: * This function delegates to the native method <code>setNodeValue</code> in
252: * XMLParserImpl.
253: */
254: public void setNodeValue(String nodeValue) {
255: try {
256: XMLParserImpl.setNodeValue(this .getJsObject(), nodeValue);
257: } catch (JavaScriptException e) {
258: throw new DOMNodeException(
259: DOMException.INVALID_MODIFICATION_ERR, e, this);
260: }
261: }
262: }
|