allow the scope to be passed in, so we can support narwhal more easily
[rhinodom.git] / com / metaweb / util / javascript / DOM / JSDocument.java
blob7d06aa7ff603da86bb0043c64e9be34f0178b0f9
1 package com.metaweb.util.javascript.DOM;
3 import javax.xml.parsers.DocumentBuilderFactory;
5 import org.mozilla.javascript.Context;
6 import org.mozilla.javascript.Function;
7 import org.mozilla.javascript.JavaScriptException;
8 import org.mozilla.javascript.Scriptable;
9 import org.mozilla.javascript.ScriptableObject;
10 import org.w3c.dom.DOMException;
11 import org.w3c.dom.Document;
12 import org.w3c.dom.NodeList;
14 public class JSDocument extends JSNode {
16 private static final long serialVersionUID = 4518158334015139097L;
18 public JSDocument() { }
20 public JSDocument(Document doc, Scriptable scope) {
21 _wrapped = doc;
22 _scope = scope;
25 public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) {
26 Document doc = null;
27 if (args.length == 1) {
28 try {
29 doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
30 } catch (Exception e) {
31 /// XXX throw a JS exception if this fails...
33 } else {
34 doc = ((JSDocument)args[1]).getWrapped();
36 return new JSDocument(doc, (Scriptable)args[0]);
39 public String getClassName() {
40 return "Document";
43 public Scriptable jsGet_doctype() {
44 return JSNode.makeByNodeType(((Document)_wrapped).getDoctype(), _scope);
47 public Scriptable jsGet_implementation() {
48 return (new JSDOMImplementation(((Document)_wrapped).getImplementation(), _scope)).makeJSInstance();
52 public Scriptable jsGet_documentElement() {
53 return JSNode.makeByNodeType(((Document)_wrapped).getDocumentElement(), _scope);
56 public Scriptable jsFunction_createElement(String tagName) {
57 try {
58 return JSNode.makeByNodeType(((Document)_wrapped).createElement(tagName), _scope);
59 } catch (DOMException e) {
60 JSDOMException jse = new JSDOMException(e.code, _scope);
61 throw new JavaScriptException(jse.makeJSInstance(), "", 0);
65 public Scriptable jsFunction_createDocumentFragment() {
66 return JSNode.makeByNodeType(((Document)_wrapped).createDocumentFragment(), _scope);
69 public Scriptable jsFunction_createTextNode(String data) {
70 return JSNode.makeByNodeType(((Document)_wrapped).createTextNode(data), _scope);
73 public Scriptable jsFunction_createComment(String data) {
74 return JSNode.makeByNodeType(((Document)_wrapped).createComment(data), _scope);
77 public Scriptable jsFunction_createCDATASection(String data) {
78 try {
79 return JSNode.makeByNodeType(((Document)_wrapped).createCDATASection(data), _scope);
80 } catch (DOMException e) {
81 JSDOMException jse = new JSDOMException(e.code, _scope);
82 throw new JavaScriptException(jse.makeJSInstance(), "", 0);
86 public Scriptable jsFunction_createProcessingInstruction(String target, String data) {
87 try {
88 return JSNode.makeByNodeType(((Document)_wrapped).createProcessingInstruction(target, data), _scope);
89 } catch (DOMException e) {
90 JSDOMException jse = new JSDOMException(e.code, _scope);
91 throw new JavaScriptException(jse.makeJSInstance(), "", 0);
95 public Scriptable jsFunction_createAttribute(String name) {
96 try {
97 return JSNode.makeByNodeType(((Document)_wrapped).createAttribute(name), _scope);
98 } catch (DOMException e) {
99 JSDOMException jse = new JSDOMException(e.code, _scope);
100 throw new JavaScriptException(jse.makeJSInstance(), "", 0);
104 public Scriptable jsFunction_createEntityReference(String name) {
105 try {
106 return JSNode.makeByNodeType(((Document)_wrapped).createEntityReference(name), _scope);
107 } catch (DOMException e) {
108 JSDOMException jse = new JSDOMException(e.code, _scope);
109 throw new JavaScriptException(jse.makeJSInstance(), "", 0);
113 public Scriptable jsFunction_getElementsByTagName(String tagName) {
114 NodeList nodes = ((Document)_wrapped).getElementsByTagName(tagName);
115 if (nodes != null) {
116 return (new JSNodeList(nodes, _scope)).makeJSInstance();
117 } else {
118 return null;
122 public Scriptable jsFunction_importNode(JSNode importedNode, Boolean deep) {
123 try {
124 return JSNode.makeByNodeType(((Document)_wrapped).importNode(importedNode.getWrapped(), deep), _scope);
125 } catch (DOMException e) {
126 JSDOMException jse = new JSDOMException(e.code, _scope);
127 throw new JavaScriptException(jse.makeJSInstance(), "", 0);
131 public Scriptable jsFunction_createElementNS(String namespaceURI, String qualifiedName) {
132 try {
133 return JSNode.makeByNodeType(((Document)_wrapped).createElementNS(namespaceURI, qualifiedName), _scope);
134 } catch (DOMException e) {
135 JSDOMException jse = new JSDOMException(e.code, _scope);
136 throw new JavaScriptException(jse.makeJSInstance(), "", 0);
140 public Scriptable jsFunction_createAttributeNS(String namespaceURI, String qualifiedName) {
141 try {
142 return JSNode.makeByNodeType(((Document)_wrapped).createAttributeNS(namespaceURI, qualifiedName), _scope);
143 } catch (DOMException e) {
144 JSDOMException jse = new JSDOMException(e.code, _scope);
145 throw new JavaScriptException(jse.makeJSInstance(), "", 0);
149 public Scriptable jsFunction_getElementsByTagNameNS(String namespaceURI, String localName) {
150 NodeList nodes = ((Document)_wrapped).getElementsByTagNameNS(namespaceURI, localName);
151 if (nodes != null) {
152 return (new JSNodeList(nodes, _scope)).makeJSInstance();
153 } else {
154 return null;
158 public Scriptable jsFunction_getElementById(String elementId) {
159 return JSNode.makeByNodeType(((Document)_wrapped).getElementById(elementId), _scope);
162 public Document getWrapped() {
163 return ((Document)_wrapped);