001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999-2001 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: * <P>
075: * This particular class, along with CoreDocumentImpl, only supports the DOM
076: * Core. Optional modules are supported by the more complete DOMImplementation
077: * class along with DocumentImpl.
078: *
079: * @version
080: * @since PR-DOM-Level-1-19980818.
081: */
082: public class CoreDOMImplementationImpl implements DOMImplementation {
083:
084: //
085: // Data
086: //
087:
088: // static
089:
090: /** Dom implementation singleton. */
091: static CoreDOMImplementationImpl singleton = new CoreDOMImplementationImpl();
092:
093: //
094: // DOMImplementation methods
095: //
096:
097: /**
098: * Test if the DOM implementation supports a specific "feature" --
099: * currently meaning language and level thereof.
100: *
101: * @param feature The package name of the feature to test.
102: * In Level 1, supported values are "HTML" and "XML" (case-insensitive).
103: * At this writing, org.apache.xerces.dom supports only XML.
104: *
105: * @param version The version number of the feature being tested.
106: * This is interpreted as "Version of the DOM API supported for the
107: * specified Feature", and in Level 1 should be "1.0"
108: *
109: * @returns true iff this implementation is compatable with the
110: * specified feature and version.
111: */
112: public boolean hasFeature(String feature, String version) {
113:
114: // Currently, we support only XML Level 1 version 1.0
115: boolean anyVersion = version == null || version.length() == 0;
116: return (feature.equalsIgnoreCase("Core") && (anyVersion
117: || version.equals("1.0") || version.equals("2.0")))
118: || (feature.equalsIgnoreCase("XML") && (anyVersion
119: || version.equals("1.0") || version
120: .equals("2.0")));
121:
122: } // hasFeature(String,String):boolean
123:
124: //
125: // Public methods
126: //
127:
128: /** NON-DOM: Obtain and return the single shared object */
129: public static DOMImplementation getDOMImplementation() {
130: return singleton;
131: }
132:
133: /**
134: * Introduced in DOM Level 2. <p>
135: *
136: * Creates an empty DocumentType node.
137: *
138: * @param qualifiedName The qualified name of the document type to be created.
139: * @param publicID The document type public identifier.
140: * @param systemID The document type system identifier.
141: * @since WD-DOM-Level-2-19990923
142: */
143: public DocumentType createDocumentType(String qualifiedName,
144: String publicID, String systemID) {
145: if (!CoreDocumentImpl.isXMLName(qualifiedName)) {
146: throw new DOMException(DOMException.INVALID_CHARACTER_ERR,
147: "DOM002 Illegal character");
148: }
149: int index = qualifiedName.indexOf(':');
150: int lastIndex = qualifiedName.lastIndexOf(':');
151: // it is an error for NCName to have more than one ':'
152: if (index == 0 || index == qualifiedName.length() - 1
153: || lastIndex != index) {
154: throw new DOMException(DOMException.NAMESPACE_ERR,
155: "DOM003 Namespace error");
156: }
157: return new DocumentTypeImpl(null, qualifiedName, publicID,
158: systemID);
159: }
160:
161: /**
162: * Introduced in DOM Level 2. <p>
163: *
164: * Creates an XML Document object of the specified type with its document
165: * element.
166: *
167: * @param namespaceURI The namespace URI of the document
168: * element to create, or null.
169: * @param qualifiedName The qualified name of the document
170: * element to create.
171: * @param doctype The type of document to be created or null.<p>
172: *
173: * When doctype is not null, its
174: * Node.ownerDocument attribute is set to
175: * the document being created.
176: * @return Document A new Document object.
177: * @throws DOMException WRONG_DOCUMENT_ERR: Raised if doctype has
178: * already been used with a different document.
179: * @since WD-DOM-Level-2-19990923
180: */
181: public Document createDocument(String namespaceURI,
182: String qualifiedName, DocumentType doctype)
183: throws DOMException {
184: if (doctype != null && doctype.getOwnerDocument() != null) {
185: throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
186: "DOM005 Wrong document");
187: }
188: CoreDocumentImpl doc = new CoreDocumentImpl(doctype);
189: Element e = doc.createElementNS(namespaceURI, qualifiedName);
190: doc.appendChild(e);
191: return doc;
192: }
193:
194: } // class DOMImplementationImpl
|