001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Sam
028: */
029:
030: package com.caucho.quercus.lib.dom;
031:
032: import com.caucho.quercus.UnimplementedException;
033: import com.caucho.quercus.annotation.NotNull;
034: import com.caucho.quercus.annotation.Optional;
035: import com.caucho.quercus.annotation.ReturnNullAsFalse;
036: import com.caucho.quercus.env.Env;
037: import com.caucho.util.L10N;
038: import com.caucho.vfs.ReadStream;
039:
040: import org.w3c.dom.*;
041: import org.xml.sax.SAXException;
042:
043: import java.io.IOException;
044: import java.util.IdentityHashMap;
045:
046: public class DOMImplementation {
047: private static L10N L = new L10N(DOMImplementation.class);
048:
049: private final IdentityHashMap<Object, Object> _wrapperMap = new IdentityHashMap<Object, Object>();
050: private final DOMFactory _factory;
051:
052: final org.w3c.dom.DOMImplementation _delegate;
053:
054: static DOMImplementation get(Env env) {
055: DOMImplementation impl = env.getSpecialValue("caucho.dom");
056:
057: if (impl == null) {
058: impl = new DOMImplementation();
059: env.setSpecialValue("caucho.dom", impl);
060: }
061:
062: return impl;
063: }
064:
065: public DOMImplementation() {
066: _factory = new QDOMFactory();
067: _delegate = _factory.getImplementation();
068: }
069:
070: static public boolean hasFeature(Env env, String feature,
071: String version) {
072: return get(env)._delegate.hasFeature(feature, version);
073: }
074:
075: static public DOMDocument createDocument(Env env, @Optional
076: String namespaceURI, @Optional
077: String name, @Optional
078: DOMDocumentType docType) {
079: DOMDocument doc;
080:
081: if (docType != null)
082: doc = get(env).createDocument(docType);
083: else
084: doc = get(env).createDocument();
085:
086: if (name != null && name.length() > 0) {
087: DOMElement elt;
088:
089: if (namespaceURI != null && namespaceURI.length() > 0)
090: elt = doc.createElementNS(namespaceURI, name);
091: else
092: elt = doc.createElement(name);
093:
094: doc.appendChild(elt);
095: }
096:
097: return doc;
098: }
099:
100: @ReturnNullAsFalse
101: static public DOMDocumentType createDocumentType(Env env, @NotNull
102: String qualifiedName, @Optional
103: String publicId, @Optional
104: String systemId) {
105: if (qualifiedName == null)
106: return null;
107:
108: if ((publicId != null && publicId.length() > 0)
109: && (publicId != null && publicId.length() > 0))
110: return get(env).createDocumentType(qualifiedName, publicId,
111: systemId);
112: else
113: return get(env).createDocumentType(qualifiedName);
114: }
115:
116: DOMAttr createWrapper(Attr node) {
117: DOMAttr wrapper = new DOMAttr(this , node);
118: _wrapperMap.put(node, wrapper);
119:
120: return wrapper;
121: }
122:
123: DOMCDATASection createWrapper(CDATASection node) {
124: DOMCDATASection wrapper = new DOMCDATASection(this , node);
125: _wrapperMap.put(node, wrapper);
126:
127: return wrapper;
128: }
129:
130: DOMComment createWrapper(Comment node) {
131: DOMComment wrapper = new DOMComment(this , node);
132: _wrapperMap.put(node, wrapper);
133:
134: return wrapper;
135: }
136:
137: DOMDocument createWrapper(Document node) {
138: DOMDocument wrapper = new DOMDocument(this , node);
139: _wrapperMap.put(node, wrapper);
140:
141: return wrapper;
142: }
143:
144: DOMDocumentFragment createWrapper(DocumentFragment node) {
145: DOMDocumentFragment wrapper = new DOMDocumentFragment(this ,
146: node);
147: _wrapperMap.put(node, wrapper);
148:
149: return wrapper;
150: }
151:
152: DOMDocumentType createWrapper(DocumentType node) {
153: DOMDocumentType wrapper = new DOMDocumentType(this , node);
154: _wrapperMap.put(node, wrapper);
155:
156: return wrapper;
157: }
158:
159: DOMConfiguration createWrapper(org.w3c.dom.DOMConfiguration node) {
160: DOMConfiguration wrapper = new DOMConfiguration(this , node);
161: _wrapperMap.put(node, wrapper);
162:
163: return wrapper;
164: }
165:
166: DOMException createWrapper(org.w3c.dom.DOMException node) {
167: DOMException wrapper = new DOMException(this , node);
168: _wrapperMap.put(node, wrapper);
169:
170: return wrapper;
171: }
172:
173: DOMElement createWrapper(Element node) {
174: DOMElement wrapper = new DOMElement(this , node);
175: _wrapperMap.put(node, wrapper);
176:
177: return wrapper;
178: }
179:
180: DOMEntityReference createWrapper(EntityReference node) {
181: DOMEntityReference wrapper = new DOMEntityReference(this , node);
182: _wrapperMap.put(node, wrapper);
183:
184: return wrapper;
185: }
186:
187: DOMNamedNodeMap createWrapper(NamedNodeMap node) {
188: DOMNamedNodeMap wrapper = new DOMNamedNodeMap(this , node);
189: _wrapperMap.put(node, wrapper);
190:
191: return wrapper;
192: }
193:
194: DOMNodeList createWrapper(NodeList node) {
195: DOMNodeList wrapper = new DOMNodeList(this , node);
196: _wrapperMap.put(node, wrapper);
197:
198: return wrapper;
199: }
200:
201: DOMNotation createWrapper(Notation node) {
202: DOMNotation wrapper = new DOMNotation(this , node);
203: _wrapperMap.put(node, wrapper);
204:
205: return wrapper;
206: }
207:
208: DOMProcessingInstruction createWrapper(ProcessingInstruction node) {
209: DOMProcessingInstruction wrapper = new DOMProcessingInstruction(
210: this , node);
211: _wrapperMap.put(node, wrapper);
212:
213: return wrapper;
214: }
215:
216: DOMStringList createWrapper(org.w3c.dom.DOMStringList node) {
217: DOMStringList wrapper = new DOMStringList(this , node);
218: _wrapperMap.put(node, wrapper);
219:
220: return wrapper;
221: }
222:
223: DOMText createWrapper(Text node) {
224: DOMText wrapper = new DOMText(this , node);
225: _wrapperMap.put(node, wrapper);
226:
227: return wrapper;
228: }
229:
230: DOMTypeInfo createWrapper(TypeInfo node) {
231: DOMTypeInfo wrapper = new DOMTypeInfo(this , node);
232: _wrapperMap.put(node, wrapper);
233:
234: return wrapper;
235: }
236:
237: Object getWrapper(Object obj) {
238: if (obj == null)
239: return null;
240:
241: Object wrapper;
242:
243: if (obj instanceof NodeList)
244: wrapper = createWrapper((NodeList) obj);
245: else {
246: wrapper = _wrapperMap.get(obj);
247:
248: if (wrapper == null) {
249: if (obj instanceof Attr)
250: wrapper = createWrapper((Attr) obj);
251: else if (obj instanceof CDATASection)
252: wrapper = createWrapper((CDATASection) obj);
253: else if (obj instanceof Comment)
254: wrapper = createWrapper((Comment) obj);
255: else if (obj instanceof Document)
256: wrapper = createWrapper((Document) obj);
257: else if (obj instanceof DocumentFragment)
258: wrapper = createWrapper((DocumentFragment) obj);
259: else if (obj instanceof DocumentType)
260: wrapper = createWrapper((DocumentType) obj);
261: else if (obj instanceof org.w3c.dom.DOMConfiguration)
262: wrapper = createWrapper((org.w3c.dom.DOMConfiguration) obj);
263: else if (obj instanceof org.w3c.dom.DOMException)
264: wrapper = createWrapper((org.w3c.dom.DOMException) obj);
265: else if (obj instanceof Element)
266: wrapper = createWrapper((Element) obj);
267: else if (obj instanceof EntityReference)
268: wrapper = createWrapper((EntityReference) obj);
269: else if (obj instanceof NamedNodeMap)
270: wrapper = createWrapper((NamedNodeMap) obj);
271: else if (obj instanceof Notation)
272: wrapper = createWrapper((Notation) obj);
273: else if (obj instanceof ProcessingInstruction)
274: wrapper = createWrapper((ProcessingInstruction) obj);
275: else if (obj instanceof org.w3c.dom.DOMStringList)
276: wrapper = createWrapper((org.w3c.dom.DOMStringList) obj);
277: else if (obj instanceof Text)
278: wrapper = createWrapper((Text) obj);
279: else if (obj instanceof TypeInfo)
280: wrapper = createWrapper((TypeInfo) obj);
281: else
282: throw new UnimplementedException(L.l(
283: "cannot wrap element of type {0}", obj
284: .getClass().getName()));
285:
286: _wrapperMap.put(obj, wrapper);
287: }
288: }
289:
290: return wrapper;
291: }
292:
293: public String toString() {
294: return getClass().getSimpleName();
295: }
296:
297: DOMAttr createAttr(String name) {
298: return createWrapper(_factory.createAttr(name));
299: }
300:
301: public DOMComment createComment() {
302: return createWrapper(_factory.createComment());
303: }
304:
305: public DOMDocument createDocument() {
306: return createWrapper(_factory.createDocument());
307: }
308:
309: public DOMDocument createDocument(DOMDocumentType docType) {
310: return createWrapper(_factory.createDocument(docType._delegate));
311: }
312:
313: public DOMDocumentType createDocumentType(String qualifiedName) {
314: return createWrapper(_factory.createDocumentType(qualifiedName));
315: }
316:
317: public DOMDocumentType createDocumentType(String qualifiedName,
318: String publicId, String systemId) {
319: return createWrapper(_factory.createDocumentType(qualifiedName,
320: publicId, systemId));
321: }
322:
323: public DOMElement createElement(String name) {
324: return createWrapper(_factory.createElement(name));
325: }
326:
327: public DOMElement createElement(String name, String namespace) {
328: return createWrapper(_factory.createElement(name, namespace));
329: }
330:
331: public DOMEntityReference createEntityReference(String name) {
332: return createWrapper(_factory.createEntityReference(name));
333: }
334:
335: public DOMProcessingInstruction createProcessingInstruction(
336: String name) {
337: return createWrapper(_factory.createProcessingInstruction(name));
338: }
339:
340: public DOMText createText() {
341: return createWrapper(_factory.createText());
342: }
343:
344: public void parseHTMLDocument(Document document, ReadStream is,
345: String path) throws IOException, SAXException {
346: _factory.parseXMLDocument(document, is, path);
347: }
348:
349: public void parseXMLDocument(Document document, ReadStream is,
350: String path) throws IOException, SAXException {
351: _factory.parseXMLDocument(document, is, path);
352: }
353: }
|