KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > xml > client > impl > NodeImpl


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.xml.client.impl;
17
18 import com.google.gwt.core.client.JavaScriptException;
19 import com.google.gwt.core.client.JavaScriptObject;
20 import com.google.gwt.xml.client.DOMException;
21 import com.google.gwt.xml.client.Document;
22 import com.google.gwt.xml.client.NamedNodeMap;
23 import com.google.gwt.xml.client.Node;
24 import com.google.gwt.xml.client.NodeList;
25
26 /**
27  * This class wraps the native Node object.
28  */

29 class NodeImpl extends DOMItem implements Node {
30
31   /**
32    * This method creates a new node of the correct type.
33    *
34    * @param node - the supplied DOM JavaScript object
35    * @return a Node object that corresponds to the DOM object
36    */

37   static Node build(JavaScriptObject node) {
38     if (node == null) {
39       return null;
40     }
41     short nodeType = XMLParserImpl.getNodeType(node);
42     switch (nodeType) {
43       case Node.ATTRIBUTE_NODE:
44         return new AttrImpl(node);
45       case Node.CDATA_SECTION_NODE:
46         return new CDATASectionImpl(node);
47       case Node.COMMENT_NODE:
48         return new CommentImpl(node);
49       case Node.DOCUMENT_FRAGMENT_NODE:
50         return new DocumentFragmentImpl(node);
51       case Node.DOCUMENT_NODE:
52         return new DocumentImpl(node);
53       case Node.ELEMENT_NODE:
54         return new ElementImpl(node);
55       case Node.PROCESSING_INSTRUCTION_NODE:
56         return new ProcessingInstructionImpl(node);
57       case Node.TEXT_NODE:
58         return new TextImpl(node);
59       default:
60         return new NodeImpl(node);
61     }
62   }
63
64   /**
65    * creates a new NodeImpl from the supplied JavaScriptObject.
66    *
67    * @param jso - the DOM node JavaScriptObject
68    */

69   protected NodeImpl(JavaScriptObject jso) {
70     super(jso);
71   }
72
73   /**
74    * This function delegates to the native method <code>appendChild</code> in
75    * XMLParserImpl.
76    */

77   public Node appendChild(Node newChild) {
78     try {
79       final JavaScriptObject newChildJs = ((DOMItem) newChild).getJsObject();
80       final JavaScriptObject appendChildResults = XMLParserImpl.appendChild(
81         this.getJsObject(), newChildJs);
82       return NodeImpl.build(appendChildResults);
83     } catch (JavaScriptException e) {
84       throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this);
85     }
86   }
87
88   /**
89    * This function delegates to the native method <code>cloneNode</code> in
90    * XMLParserImpl.
91    */

92   public Node cloneNode(boolean deep) {
93     return NodeImpl.build(XMLParserImpl.cloneNode(this.getJsObject(), deep));
94   }
95
96   public NamedNodeMap getAttributes() {
97     return new NamedNodeMapImpl(XMLParserImpl.getAttributes(this.getJsObject()));
98   }
99
100   public NodeList getChildNodes() {
101     return new NodeListImpl(XMLParserImpl.getChildNodes(this.getJsObject()));
102   }
103
104   public Node getFirstChild() {
105     return getChildNodes().item(0);
106   }
107
108   public Node getLastChild() {
109     return getChildNodes().item(getChildNodes().getLength() - 1);
110   }
111
112   /**
113    * This function delegates to the native method <code>getNamespaceURI</code>
114    * in XMLParserImpl.
115    */

116   public String JavaDoc getNamespaceURI() {
117     return XMLParserImpl.getNamespaceURI(this.getJsObject());
118   }
119
120   public Node getNextSibling() {
121     return NodeImpl.build(XMLParserImpl.getNextSibling(this.getJsObject()));
122   }
123
124   public String JavaDoc getNodeName() {
125     return XMLParserImpl.getNodeName(this.getJsObject());
126   }
127
128   public short getNodeType() {
129     return XMLParserImpl.getNodeType(this.getJsObject());
130   }
131
132   public String JavaDoc getNodeValue() {
133     return XMLParserImpl.getNodeValue(this.getJsObject());
134   }
135
136   public Document getOwnerDocument() {
137     return (Document) NodeImpl.build(XMLParserImpl.getOwnerDocument(this
138       .getJsObject()));
139   }
140
141   public Node getParentNode() {
142     return NodeImpl.build(XMLParserImpl.getParentNode(this.getJsObject()));
143   }
144
145   /**
146    * This function delegates to the native method <code>getPrefix</code> in
147    * XMLParserImpl.
148    */

149   public String JavaDoc getPrefix() {
150     return XMLParserImpl.getPrefix(this.getJsObject());
151   }
152
153   public Node getPreviousSibling() {
154     return NodeImpl.build(XMLParserImpl.getPreviousSibling(this.getJsObject()));
155   }
156
157   /**
158    * This function delegates to the native method <code>hasAttributes</code>
159    * in XMLParserImpl.
160    */

161   public boolean hasAttributes() {
162     return XMLParserImpl.hasAttributes(this.getJsObject());
163   }
164
165   /**
166    * This function delegates to the native method <code>hasChildNodes</code>
167    * in XMLParserImpl.
168    */

169   public boolean hasChildNodes() {
170     return XMLParserImpl.hasChildNodes(this.getJsObject());
171   }
172
173   /**
174    * This function delegates to the native method <code>insertBefore</code> in
175    * XMLParserImpl.
176    */

177   public Node insertBefore(Node newChild, Node refChild) {
178     try {
179       final JavaScriptObject newChildJs = ((DOMItem) newChild).getJsObject();
180       final JavaScriptObject refChildJs;
181       if (refChild != null) {
182         refChildJs = ((DOMItem) refChild).getJsObject();
183       } else {
184         refChildJs = null;
185       }
186       final JavaScriptObject insertBeforeResults = XMLParserImpl.insertBefore(
187         this.getJsObject(), newChildJs, refChildJs);
188       return NodeImpl.build(insertBeforeResults);
189     } catch (JavaScriptException e) {
190       throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this);
191     }
192   }
193
194   /**
195    * This function delegates to the native method <code>normalize</code> in
196    * XMLParserImpl.
197    */

198   public void normalize() {
199     XMLParserImpl.normalize(this.getJsObject());
200   }
201
202   /**
203    * This function delegates to the native method <code>removeChild</code> in
204    * XMLParserImpl.
205    */

206   public Node removeChild(Node oldChild) {
207     try {
208       final JavaScriptObject oldChildJs = ((DOMItem) oldChild).getJsObject();
209       final JavaScriptObject removeChildResults = XMLParserImpl.removeChild(
210         this.getJsObject(), oldChildJs);
211       return NodeImpl.build(removeChildResults);
212     } catch (JavaScriptException e) {
213       throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this);
214     }
215   }
216
217   /**
218    * This function delegates to the native method <code>replaceChild</code> in
219    * XMLParserImpl.
220    */

221   public Node replaceChild(Node newChild, Node oldChild) {
222     try {
223       final JavaScriptObject newChildJs = ((DOMItem) newChild).getJsObject();
224       final JavaScriptObject oldChildJs = ((DOMItem) oldChild).getJsObject();
225       final JavaScriptObject replaceChildResults = XMLParserImpl.replaceChild(
226         this.getJsObject(), newChildJs, oldChildJs);
227       return NodeImpl.build(replaceChildResults);
228     } catch (JavaScriptException e) {
229       throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this);
230     }
231   }
232
233   /**
234    * This function delegates to the native method <code>setNodeValue</code> in
235    * XMLParserImpl.
236    */

237   public void setNodeValue(String JavaDoc nodeValue) {
238     try {
239       XMLParserImpl.setNodeValue(this.getJsObject(), nodeValue);
240     } catch (JavaScriptException e) {
241       throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this);
242     }
243   }
244 }
245
Popular Tags