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.GWT;
019: import com.google.gwt.core.client.JavaScriptException;
020: import com.google.gwt.core.client.JavaScriptObject;
021: import com.google.gwt.xml.client.Document;
022:
023: /**
024: * Native implementation associated with
025: * {@link com.google.gwt.xml.client.XMLParser}.
026: */
027: public abstract class XMLParserImpl {
028:
029: private static XMLParserImpl impl = GWT.create(XMLParserImpl.class);
030:
031: public static XMLParserImpl getInstance() {
032: return impl;
033: }
034:
035: static native JavaScriptObject appendChild(
036: JavaScriptObject jsObject, JavaScriptObject newChildJs) /*-{
037: var out = jsObject.appendChild(newChildJs);
038: return (out == null) ? null : out;
039: }-*/;
040:
041: static native void appendData(JavaScriptObject jsObject, String arg) /*-{
042: jsObject.appendData(arg);
043: }-*/;
044:
045: static native JavaScriptObject cloneNode(JavaScriptObject jsObject,
046: boolean deep) /*-{
047: var out = jsObject.cloneNode(deep);
048: return (out == null) ? null : out;
049: }-*/;
050:
051: static native JavaScriptObject createCDATASection(
052: JavaScriptObject jsObject, String data) /*-{
053: var out = jsObject.createCDATASection(data);
054: return (out == null) ? null : out;
055: }-*/;
056:
057: static native JavaScriptObject createComment(
058: JavaScriptObject jsObject, String data) /*-{
059: var out = jsObject.createComment(data);
060: return (out == null) ? null : out;
061: }-*/;
062:
063: static native JavaScriptObject createDocumentFragment(
064: JavaScriptObject jsObject) /*-{
065: var out = jsObject.createDocumentFragment();
066: return (out == null) ? null : out;
067: }-*/;
068:
069: static native JavaScriptObject createElement(
070: JavaScriptObject jsObject, String tagName) /*-{
071: var out = jsObject.createElement(tagName);
072: return (out == null) ? null : out;
073: }-*/;
074:
075: static native JavaScriptObject createProcessingInstruction(
076: JavaScriptObject jsObject, String target, String data) /*-{
077: var out = jsObject.createProcessingInstruction(target, data);
078: return (out == null) ? null : out;
079: }-*/;
080:
081: static native JavaScriptObject createTextNode(
082: JavaScriptObject jsObject, String data) /*-{
083: var out = jsObject.createTextNode(data);
084: return (out == null) ? null : out;
085: }-*/;
086:
087: static native void deleteData(JavaScriptObject jsObject,
088: int offset, int count) /*-{
089: jsObject.deleteData(offset, count);
090: }-*/;
091:
092: static native String getAttribute(JavaScriptObject o, String name) /*-{
093: return o.getAttribute(name);
094: }-*/;
095:
096: static native JavaScriptObject getAttributeNode(JavaScriptObject o,
097: String name) /*-{
098: var out = o.getAttributeNode(name);
099: return (out == null) ? null : out;
100: }-*/;
101:
102: static native JavaScriptObject getAttributes(JavaScriptObject t) /*-{
103: return t.attributes;
104: }-*/;
105:
106: static native JavaScriptObject getChildNodes(JavaScriptObject t) /*-{
107: var out = t.childNodes;
108: return (out == null) ? null : out;
109: }-*/;
110:
111: static native String getData(JavaScriptObject o) /*-{
112: return o.data;
113: }-*/;
114:
115: static native JavaScriptObject getDocumentElement(JavaScriptObject o) /*-{
116: return o.documentElement;
117: }-*/;
118:
119: static JavaScriptObject getElementById(JavaScriptObject document,
120: String id) {
121: return impl.getElementByIdImpl(document, id);
122: }
123:
124: static JavaScriptObject getElementsByTagName(JavaScriptObject o,
125: String tagName) {
126: return impl.getElementsByTagNameImpl(o, tagName);
127: }
128:
129: static native int getLength(JavaScriptObject o) /*-{
130: return o.length;
131: }-*/;
132:
133: static native String getName(JavaScriptObject o) /*-{
134: return o.name;
135: }-*/;
136:
137: static native JavaScriptObject getNamedItem(JavaScriptObject t,
138: String name) /*-{
139: var out = t.getNamedItem(name);
140: return (out == null) ? null : out;
141: }-*/;
142:
143: static native String getNamespaceURI(JavaScriptObject jsObject) /*-{
144: var out = jsObject.namespaceURI;
145: return (out == null) ? null : out;
146: }-*/;
147:
148: static native JavaScriptObject getNextSibling(JavaScriptObject o) /*-{
149: var out = o.nextSibling;
150: return (out == null) ? null : out;
151: }-*/;
152:
153: static native String getNodeName(JavaScriptObject o) /*-{
154: var out = o.nodeName;
155: return (out == null) ? null : out;
156: }-*/;
157:
158: static native short getNodeType(JavaScriptObject jsObject) /*-{
159: var out = jsObject.nodeType;
160: return (out == null) ? -1 : out;
161: }-*/;
162:
163: static native String getNodeValue(JavaScriptObject o) /*-{
164: return o.nodeValue;
165: }-*/;
166:
167: static native JavaScriptObject getOwnerDocument(JavaScriptObject o) /*-{
168: return o.ownerDocument;
169: }-*/;
170:
171: static native JavaScriptObject getParentNode(JavaScriptObject o) /*-{
172: var out = o.parentNode;
173: return (out == null) ? null : out;
174: }-*/;
175:
176: static String getPrefix(JavaScriptObject jsObject) {
177: return impl.getPrefixImpl(jsObject);
178: }
179:
180: static native JavaScriptObject getPreviousSibling(JavaScriptObject o) /*-{
181: return o.previousSibling;
182: }-*/;
183:
184: static native boolean getSpecified(JavaScriptObject o) /*-{
185: return o.specified;
186: }-*/;
187:
188: static native String getTagName(JavaScriptObject o) /*-{
189: return o.tagName;
190: }-*/;
191:
192: static native String getTarget(JavaScriptObject o) /*-{
193: return o.target;
194: }-*/;
195:
196: static native String getValue(JavaScriptObject o) /*-{
197: return o.value;
198: }-*/;
199:
200: static native boolean hasAttributes(JavaScriptObject jsObject) /*-{
201: return jsObject.attributes.length != 0;
202: }-*/;
203:
204: static native boolean hasChildNodes(JavaScriptObject jsObject) /*-{
205: return jsObject.hasChildNodes();
206: }-*/;
207:
208: static JavaScriptObject importNode(JavaScriptObject jsObject,
209: JavaScriptObject importedNode, boolean deep) {
210: return impl.importNodeImpl(jsObject, importedNode, deep);
211: }
212:
213: static native JavaScriptObject insertBefore(
214: JavaScriptObject jsObject, JavaScriptObject newChildJs,
215: JavaScriptObject refChildJs) /*-{
216: var out = jsObject.insertBefore(newChildJs, refChildJs);
217: return (out == null) ? null : out;
218: }-*/;
219:
220: static native void insertData(JavaScriptObject jsObject,
221: int offset, String arg) /*-{
222: jsObject.insertData(offset, arg);
223: }-*/;
224:
225: static native JavaScriptObject item(JavaScriptObject t, int index) /*-{
226: if (index >= t.length) {
227: return null;
228: }
229: var out = t.item(index);
230: return (out == null) ? null : out;
231: }-*/;
232:
233: static native void normalize(JavaScriptObject jsObject) /*-{
234: jsObject.normalize();
235: }-*/;
236:
237: static native void removeAttribute(JavaScriptObject jsObject,
238: String name) /*-{
239: jsObject.removeAttribute(name);
240: }-*/;
241:
242: static native JavaScriptObject removeChild(
243: JavaScriptObject jsObject, JavaScriptObject oldChildJs) /*-{
244: var out = jsObject.removeChild(oldChildJs);
245: return (out == null) ? null : out;
246: }-*/;
247:
248: static native JavaScriptObject removeNamedItem(
249: JavaScriptObject jsObject, String name) /*-{
250: var out = jsObject.removeNamedItem(name);
251: return (out == null) ? null : out;
252: }-*/;
253:
254: static native JavaScriptObject replaceChild(
255: JavaScriptObject jsObject, JavaScriptObject newChildJs,
256: JavaScriptObject oldChildJs) /*-{
257: var out = jsObject.replaceChild(newChildJs, oldChildJs);
258: return (out == null) ? null : out;
259: }-*/;
260:
261: static native void replaceData(JavaScriptObject jsObject,
262: int offset, int count, String arg) /*-{
263: jsObject.replaceData(offset, count, arg);
264: }-*/;
265:
266: static native void setAttribute(JavaScriptObject jsObject,
267: String name, String value) /*-{
268: jsObject.setAttribute(name, value);
269: }-*/;
270:
271: static native void setData(JavaScriptObject jsObject, String data) /*-{
272: jsObject.data = data;
273: }-*/;
274:
275: static native JavaScriptObject setNamedItem(
276: JavaScriptObject jsObject, JavaScriptObject arg) /*-{
277: var out = jsObject.setNamedItem(arg);
278: return (out == null) ? null : out;
279: }-*/;
280:
281: static native void setNodeValue(JavaScriptObject jsObject,
282: String nodeValue) /*-{
283: jsObject.nodeValue = nodeValue;
284: }-*/;
285:
286: static native JavaScriptObject splitText(JavaScriptObject jsObject,
287: int offset) /*-{
288: var out = jsObject.splitText(offset);
289: return (out == null) ? null : out;
290: }-*/;
291:
292: static native String substringData(JavaScriptObject o, int offset,
293: int count) /*-{
294: return o.substringData(offset, count);
295: }-*/;
296:
297: /**
298: * Not globally instantable.
299: */
300: XMLParserImpl() {
301: }
302:
303: public final Document createDocument() {
304: return (Document) NodeImpl.build(createDocumentImpl());
305: }
306:
307: public final Document parse(String contents) {
308: try {
309: return (Document) NodeImpl.build(parseImpl(contents));
310: } catch (JavaScriptException e) {
311: throw new DOMParseException(contents, e);
312: }
313: }
314:
315: public boolean supportsCDATASection() {
316: return true;
317: }
318:
319: protected abstract JavaScriptObject createDocumentImpl();
320:
321: protected abstract JavaScriptObject getElementByIdImpl(
322: JavaScriptObject document, String id);
323:
324: protected abstract JavaScriptObject getElementsByTagNameImpl(
325: JavaScriptObject o, String tagName);
326:
327: protected abstract String getPrefixImpl(JavaScriptObject jsObject);
328:
329: protected abstract JavaScriptObject importNodeImpl(
330: JavaScriptObject jsObject, JavaScriptObject importedNode,
331: boolean deep);
332:
333: protected abstract JavaScriptObject parseImpl(String contents);
334:
335: }
|