001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.dom;
059:
060: import org.w3c.dom.DOMException;
061: import org.w3c.dom.DOMImplementation;
062: import org.w3c.dom.Document;
063: import org.w3c.dom.DocumentType;
064: import org.w3c.dom.Element;
065:
066: /**
067: * The DOMImplementation class is description of a particular
068: * implementation of the Document Object Model. As such its data is
069: * static, shared by all instances of this implementation.
070: * <P>
071: * The DOM API requires that it be a real object rather than static
072: * methods. However, there's nothing that says it can't be a singleton,
073: * so that's how I've implemented it.
074: *
075: * @version
076: * @since PR-DOM-Level-1-19980818.
077: */
078: public class DOMImplementationImpl implements DOMImplementation {
079:
080: //
081: // Data
082: //
083:
084: // static
085:
086: /** Dom implementation singleton. */
087: static DOMImplementationImpl singleton = new DOMImplementationImpl();
088:
089: //
090: // DOMImplementation methods
091: //
092:
093: /**
094: * Test if the DOM implementation supports a specific "feature" --
095: * currently meaning language and level thereof.
096: *
097: * @param feature The package name of the feature to test.
098: * In Level 1, supported values are "HTML" and "XML" (case-insensitive).
099: * At this writing, org.apache.xerces.dom supports only XML.
100: *
101: * @param version The version number of the feature being tested.
102: * This is interpreted as "Version of the DOM API supported for the
103: * specified Feature", and in Level 1 should be "1.0"
104: *
105: * @returns true iff this implementation is compatable with the
106: * specified feature and version.
107: */
108: public boolean hasFeature(String feature, String version) {
109:
110: // Currently, we support only XML Level 1 version 1.0
111: boolean anyVersion = version == null || version.length() == 0;
112: return (feature.equalsIgnoreCase("Core") && (anyVersion
113: || version.equals("1.0") || version.equals("2.0")))
114: || (feature.equalsIgnoreCase("XML") && (anyVersion
115: || version.equals("1.0") || version
116: .equals("2.0")))
117: || (feature.equalsIgnoreCase("Events") && (anyVersion || version
118: .equals("2.0")))
119: || (feature.equalsIgnoreCase("MutationEvents") && (anyVersion || version
120: .equals("2.0")))
121: || (feature.equalsIgnoreCase("Traversal") && (anyVersion || version
122: .equals("2.0")));
123:
124: } // hasFeature(String,String):boolean
125:
126: //
127: // Public methods
128: //
129:
130: /** NON-DOM: Obtain and return the single shared object */
131: public static DOMImplementation getDOMImplementation() {
132: return singleton;
133: }
134:
135: /**
136: * Introduced in DOM Level 2. <p>
137: *
138: * Creates an empty DocumentType node.
139: *
140: * @param qualifiedName The qualified name of the document type to be created.
141: * @param publicID The document type public identifier.
142: * @param systemID The document type system identifier.
143: * @since WD-DOM-Level-2-19990923
144: */
145: public DocumentType createDocumentType(String qualifiedName,
146: String publicID, String systemID) {
147: if (!CoreDocumentImpl.isXMLName(qualifiedName)) {
148: throw new DOMException(DOMException.INVALID_CHARACTER_ERR,
149: "DOM002 Illegal character");
150: }
151: int index = qualifiedName.indexOf(':');
152: int lastIndex = qualifiedName.lastIndexOf(':');
153: // it is an error for NCName to have more than one ':'
154: if (index == 0 || index == qualifiedName.length() - 1
155: || lastIndex != index) {
156: throw new DOMException(DOMException.NAMESPACE_ERR,
157: "DOM003 Namespace error");
158: }
159: return new DocumentTypeImpl(null, qualifiedName, publicID,
160: systemID);
161: }
162:
163: /**
164: * Introduced in DOM Level 2. <p>
165: *
166: * Creates an XML Document object of the specified type with its document
167: * element.
168: *
169: * @param namespaceURI The namespace URI of the document
170: * element to create, or null.
171: * @param qualifiedName The qualified name of the document
172: * element to create.
173: * @param doctype The type of document to be created or null.<p>
174: *
175: * When doctype is not null, its
176: * Node.ownerDocument attribute is set to
177: * the document being created.
178: * @return Document A new Document object.
179: * @throws DOMException WRONG_DOCUMENT_ERR: Raised if doctype has
180: * already been used with a different document.
181: * @since WD-DOM-Level-2-19990923
182: */
183: public Document createDocument(String namespaceURI,
184: String qualifiedName, DocumentType doctype)
185: throws DOMException {
186: if (doctype != null && doctype.getOwnerDocument() != null) {
187: throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
188: "DOM005 Wrong document");
189: }
190: DocumentImpl doc = new DocumentImpl(doctype);
191: Element e = doc.createElementNS(namespaceURI, qualifiedName);
192: doc.appendChild(e);
193: return doc;
194: }
195:
196: } // class DOMImplementationImpl
|