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.CDATASection;
021: import com.google.gwt.xml.client.Comment;
022: import com.google.gwt.xml.client.DOMException;
023: import com.google.gwt.xml.client.Document;
024: import com.google.gwt.xml.client.DocumentFragment;
025: import com.google.gwt.xml.client.Element;
026: import com.google.gwt.xml.client.Node;
027: import com.google.gwt.xml.client.NodeList;
028: import com.google.gwt.xml.client.ProcessingInstruction;
029: import com.google.gwt.xml.client.Text;
030:
031: /**
032: * This class wraps the native Document object.
033: */
034: class DocumentImpl extends NodeImpl implements Document {
035:
036: protected DocumentImpl(JavaScriptObject o) {
037: super (o);
038: }
039:
040: /**
041: * This function delegates to the native method
042: * <code>createCDATASection</code> in XMLParserImpl.
043: */
044: public CDATASection createCDATASection(String data) {
045: try {
046: return (CDATASection) NodeImpl.build(XMLParserImpl
047: .createCDATASection(this .getJsObject(), data));
048: } catch (JavaScriptException e) {
049: throw new DOMNodeException(
050: DOMException.INVALID_CHARACTER_ERR, e, this );
051: }
052: }
053:
054: /**
055: * This function delegates to the native method <code>createComment</code>
056: * in XMLParserImpl.
057: */
058: public Comment createComment(String data) {
059: try {
060: return (Comment) NodeImpl.build(XMLParserImpl
061: .createComment(this .getJsObject(), data));
062: } catch (JavaScriptException e) {
063: throw new DOMNodeException(
064: DOMException.INVALID_CHARACTER_ERR, e, this );
065: }
066: }
067:
068: /**
069: * This function delegates to the native method
070: * <code>createDocumentFragment</code> in XMLParserImpl.
071: */
072: public DocumentFragment createDocumentFragment() {
073: try {
074: return (DocumentFragment) NodeImpl.build(XMLParserImpl
075: .createDocumentFragment(this .getJsObject()));
076: } catch (JavaScriptException e) {
077: throw new DOMNodeException(
078: DOMException.INVALID_CHARACTER_ERR, e, this );
079: }
080: }
081:
082: /**
083: * This function delegates to the native method <code>createElement</code>
084: * in XMLParserImpl.
085: */
086: public Element createElement(String tagName) {
087: try {
088: return (Element) NodeImpl.build(XMLParserImpl
089: .createElement(this .getJsObject(), tagName));
090: } catch (JavaScriptException e) {
091: throw new DOMNodeException(
092: DOMException.INVALID_CHARACTER_ERR, e, this );
093: }
094: }
095:
096: /**
097: * This function delegates to the native method
098: * <code>createProcessingInstruction</code> in XMLParserImpl.
099: */
100: public ProcessingInstruction createProcessingInstruction(
101: String target, String data) {
102: try {
103: return (ProcessingInstruction) NodeImpl.build(XMLParserImpl
104: .createProcessingInstruction(this .getJsObject(),
105: target, data));
106: } catch (JavaScriptException e) {
107: throw new DOMNodeException(
108: DOMException.INVALID_CHARACTER_ERR, e, this );
109: }
110: }
111:
112: /**
113: * This function delegates to the native method <code>createTextNode</code>
114: * in XMLParserImpl.
115: */
116: public Text createTextNode(String data) {
117: try {
118: return (Text) NodeImpl.build(XMLParserImpl.createTextNode(
119: this .getJsObject(), data));
120: } catch (JavaScriptException e) {
121: throw new DOMNodeException(
122: DOMException.INVALID_CHARACTER_ERR, e, this );
123: }
124: }
125:
126: /**
127: * This function delegates to the native method
128: * <code>getDocumentElement</code> in XMLParserImpl.
129: */
130: public Element getDocumentElement() {
131: return (Element) NodeImpl.build(XMLParserImpl
132: .getDocumentElement(this .getJsObject()));
133: }
134:
135: /**
136: * This function delegates to the native method <code>getElementById</code>
137: * in XMLParserImpl.
138: */
139: public Element getElementById(String elementId) {
140: return (Element) NodeImpl.build(XMLParserImpl.getElementById(
141: this .getJsObject(), elementId));
142: }
143:
144: /**
145: * This function delegates to the native method
146: * <code>getElementsByTagName</code> in XMLParserImpl.
147: */
148: public NodeList getElementsByTagName(String tagName) {
149: return new NodeListImpl(XMLParserImpl.getElementsByTagName(this
150: .getJsObject(), tagName));
151: }
152:
153: /**
154: * This function delegates to the native method <code>importNode</code> in
155: * XMLParserImpl.
156: */
157: public Node importNode(Node importedNode, boolean deep) {
158: try {
159: return NodeImpl.build(XMLParserImpl.importNode(this
160: .getJsObject(), ((DOMItem) importedNode)
161: .getJsObject(), deep));
162: } catch (JavaScriptException e) {
163: throw new DOMNodeException(DOMException.INVALID_STATE_ERR,
164: e, this );
165: }
166: }
167:
168: /**
169: * This method returns the string representation of this
170: * <code>DocumentImpl</code>.
171: *
172: * @return the string representation of this <code>DocumentImpl</code>.
173: * @see java.lang.Object#toString()
174: */
175: @Override
176: public String toString() {
177: StringBuffer b = new StringBuffer();
178: NodeList children = getChildNodes();
179: for (int i = 0; i < children.getLength(); i++) {
180: b.append(children.item(i).toString());
181: }
182: return b.toString();
183: }
184: }
|