CoreDOMImplementationImplpublic class CoreDOMImplementationImpl extends Object implements DOMImplementationLS, DOMImplementationThe DOMImplementation class is description of a particular
implementation of the Document Object Model. As such its data is
static, shared by all instances of this implementation.
The DOM API requires that it be a real object rather than static
methods. However, there's nothing that says it can't be a singleton,
so that's how I've implemented it.
This particular class, along with CoreDocumentImpl, supports the DOM
Core and Load/Save (Experimental). Optional modules are supported by
the more complete DOMImplementation class along with DocumentImpl. |
Fields Summary |
---|
private static final int | SIZE | private SoftReference[] | schemaValidators | private SoftReference[] | xml10DTDValidators | private SoftReference[] | xml11DTDValidators | private int | freeSchemaValidatorIndex | private int | freeXML10DTDValidatorIndex | private int | freeXML11DTDValidatorIndex | private int | schemaValidatorsCurrentSize | private int | xml10DTDValidatorsCurrentSize | private int | xml11DTDValidatorsCurrentSize | private SoftReference[] | xml10DTDLoaders | private SoftReference[] | xml11DTDLoaders | private int | freeXML10DTDLoaderIndex | private int | freeXML11DTDLoaderIndex | private int | xml10DTDLoaderCurrentSize | private int | xml11DTDLoaderCurrentSize | private int | docAndDoctypeCounter | static CoreDOMImplementationImpl | singletonDom implementation singleton. |
Methods Summary |
---|
protected synchronized int | assignDocTypeNumber()NON-DOM: increment document/doctype counter
return ++docAndDoctypeCounter;
| protected synchronized int | assignDocumentNumber()NON-DOM: increment document/doctype counter
return ++docAndDoctypeCounter;
| final void | checkQName(java.lang.String qname)
int index = qname.indexOf(':");
int lastIndex = qname.lastIndexOf(':");
int length = qname.length();
// it is an error for NCName to have more than one ':'
// check if it is valid QName [Namespace in XML production 6]
if (index == 0 || index == length - 1 || lastIndex != index) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"NAMESPACE_ERR",
null);
throw new DOMException(DOMException.NAMESPACE_ERR, msg);
}
int start = 0;
// Namespace in XML production [6]
if (index > 0) {
// check that prefix is NCName
if (!XMLChar.isNCNameStart(qname.charAt(start))) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"INVALID_CHARACTER_ERR",
null);
throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg);
}
for (int i = 1; i < index; i++) {
if (!XMLChar.isNCName(qname.charAt(i))) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"INVALID_CHARACTER_ERR",
null);
throw new DOMException(
DOMException.INVALID_CHARACTER_ERR,
msg);
}
}
start = index + 1;
}
// check local part
if (!XMLChar.isNCNameStart(qname.charAt(start))) {
// REVISIT: add qname parameter to the message
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"INVALID_CHARACTER_ERR",
null);
throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg);
}
for (int i = start + 1; i < length; i++) {
if (!XMLChar.isNCName(qname.charAt(i))) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"INVALID_CHARACTER_ERR",
null);
throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg);
}
}
| public org.w3c.dom.Document | createDocument(java.lang.String namespaceURI, java.lang.String qualifiedName, org.w3c.dom.DocumentType doctype)Introduced in DOM Level 2.
Creates an XML Document object of the specified type with its document
element.
if (doctype != null && doctype.getOwnerDocument() != null) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"WRONG_DOCUMENT_ERR",
null);
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
}
CoreDocumentImpl doc = new CoreDocumentImpl(doctype);
// If namespaceURI and qualifiedName are null return a Document with no document element.
if (qualifiedName != null || namespaceURI != null) {
Element e = doc.createElementNS(namespaceURI, qualifiedName);
doc.appendChild(e);
}
return doc;
| public org.w3c.dom.DocumentType | createDocumentType(java.lang.String qualifiedName, java.lang.String publicID, java.lang.String systemID)Introduced in DOM Level 2.
Creates an empty DocumentType node.
// REVISIT: this might allow creation of invalid name for DOCTYPE
// xmlns prefix.
// also there is no way for a user to turn off error checking.
checkQName(qualifiedName);
return new DocumentTypeImpl(null, qualifiedName, publicID, systemID);
| public org.w3c.dom.ls.LSInput | createLSInput()DOM Level 3 LS CR - Experimental.
Create a new empty input source.
return new DOMInputImpl();
| public org.w3c.dom.ls.LSOutput | createLSOutput()DOM Level 3 LS CR - Experimental.
Create a new empty output destination object where
LSOutput.characterStream ,
LSOutput.byteStream , LSOutput.systemId ,
LSOutput.encoding are null.
return new DOMOutputImpl();
| public org.w3c.dom.ls.LSParser | createLSParser(short mode, java.lang.String schemaType)DOM Level 3 LS CR - Experimental.
Create a new LSParser . The newly constructed parser may
then be configured by means of its DOMConfiguration
object, and used to parse documents by means of its parse
method.
if (mode != DOMImplementationLS.MODE_SYNCHRONOUS || (schemaType !=null &&
!"http://www.w3.org/2001/XMLSchema".equals(schemaType) &&
!"http://www.w3.org/TR/REC-xml".equals(schemaType))) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"NOT_SUPPORTED_ERR",
null);
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
if (schemaType != null
&& schemaType.equals("http://www.w3.org/TR/REC-xml")) {
return new DOMParserImpl(
"org.apache.xerces.parsers.DTDConfiguration",
schemaType);
}
else {
// create default parser configuration validating against XMLSchemas
return new DOMParserImpl(
"org.apache.xerces.parsers.XIncludeAwareParserConfiguration",
schemaType);
}
| public org.w3c.dom.ls.LSSerializer | createLSSerializer()DOM Level 3 LS CR - Experimental.
Create a new LSSerializer object.
try {
Class serializerClass = ObjectFactory.findProviderClass(
"org.apache.xml.serializer.dom3.LSSerializerImpl",
ObjectFactory.findClassLoader(), true);
return (LSSerializer) serializerClass.newInstance();
}
catch (Exception e) {}
// Fall back to Xerces' deprecated serializer if
// the Xalan based serializer is unavailable.
return new DOMSerializerImpl();
| public static org.w3c.dom.DOMImplementation | getDOMImplementation()NON-DOM: Obtain and return the single shared object
//
// Public methods
//
return singleton;
| final synchronized org.apache.xerces.impl.dtd.XMLDTDLoader | getDTDLoader(java.lang.String xmlVersion)NON-DOM: retrieve DTD loader
// return an instance of XML11DTDProcessor
if ("1.1".equals(xmlVersion)) {
while (freeXML11DTDLoaderIndex >= 0) {
// return first available DTD loader
SoftReference ref = xml11DTDLoaders[freeXML11DTDLoaderIndex];
XMLDTDLoaderHolder holder = (XMLDTDLoaderHolder) ref.get();
if (holder != null && holder.loader != null) {
XMLDTDLoader val = holder.loader;
holder.loader = null;
--freeXML11DTDLoaderIndex;
return val;
}
xml11DTDLoaders[freeXML11DTDLoaderIndex--] = null;
}
return (XMLDTDLoader) (ObjectFactory
.newInstance(
"org.apache.xerces.impl.dtd.XML11DTDProcessor",
ObjectFactory.findClassLoader(),
true));
}
// return an instance of XMLDTDLoader
else {
while (freeXML10DTDLoaderIndex >= 0) {
// return first available DTD loader
SoftReference ref = xml10DTDLoaders[freeXML10DTDLoaderIndex];
XMLDTDLoaderHolder holder = (XMLDTDLoaderHolder) ref.get();
if (holder != null && holder.loader != null) {
XMLDTDLoader val = holder.loader;
holder.loader = null;
--freeXML10DTDLoaderIndex;
return val;
}
xml10DTDLoaders[freeXML10DTDLoaderIndex--] = null;
}
return new XMLDTDLoader();
}
| public java.lang.Object | getFeature(java.lang.String feature, java.lang.String version)DOM Level 3 WD - Experimental.
if (singleton.hasFeature(feature, version)) {
if ((feature.equalsIgnoreCase("+XPath"))) {
try {
Class xpathClass = ObjectFactory.findProviderClass(
"org.apache.xpath.domapi.XPathEvaluatorImpl",
ObjectFactory.findClassLoader(), true);
// Check if the DOM XPath implementation implements
// the interface org.w3c.dom.XPathEvaluator
Class interfaces[] = xpathClass.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (interfaces[i].getName().equals(
"org.w3c.dom.xpath.XPathEvaluator")) {
return xpathClass.newInstance();
}
}
} catch (Exception e) {
return null;
}
} else {
return singleton;
}
}
return null;
| synchronized org.apache.xerces.impl.RevalidationHandler | getValidator(java.lang.String schemaType, java.lang.String xmlVersion)NON-DOM: retrieve validator.
if (schemaType == XMLGrammarDescription.XML_SCHEMA) {
// create new validator - we should not attempt
// to restrict the number of validation handlers being
// requested
while (freeSchemaValidatorIndex >= 0) {
// return first available validator
SoftReference ref = schemaValidators[freeSchemaValidatorIndex];
RevalidationHandlerHolder holder = (RevalidationHandlerHolder) ref.get();
if (holder != null && holder.handler != null) {
RevalidationHandler val = holder.handler;
holder.handler = null;
--freeSchemaValidatorIndex;
return val;
}
schemaValidators[freeSchemaValidatorIndex--] = null;
}
return (RevalidationHandler) (ObjectFactory
.newInstance(
"org.apache.xerces.impl.xs.XMLSchemaValidator",
ObjectFactory.findClassLoader(),
true));
}
else if(schemaType == XMLGrammarDescription.XML_DTD) {
// return an instance of XML11DTDValidator
if ("1.1".equals(xmlVersion)) {
while (freeXML11DTDValidatorIndex >= 0) {
// return first available validator
SoftReference ref = xml11DTDValidators[freeXML11DTDValidatorIndex];
RevalidationHandlerHolder holder = (RevalidationHandlerHolder) ref.get();
if (holder != null && holder.handler != null) {
RevalidationHandler val = holder.handler;
holder.handler = null;
--freeXML11DTDValidatorIndex;
return val;
}
xml11DTDValidators[freeXML11DTDValidatorIndex--] = null;
}
return (RevalidationHandler) (ObjectFactory
.newInstance(
"org.apache.xerces.impl.dtd.XML11DTDValidator",
ObjectFactory.findClassLoader(),
true));
}
// return an instance of XMLDTDValidator
else {
while (freeXML10DTDValidatorIndex >= 0) {
// return first available validator
SoftReference ref = xml10DTDValidators[freeXML10DTDValidatorIndex];
RevalidationHandlerHolder holder = (RevalidationHandlerHolder) ref.get();
if (holder != null && holder.handler != null) {
RevalidationHandler val = holder.handler;
holder.handler = null;
--freeXML10DTDValidatorIndex;
return val;
}
xml10DTDValidators[freeXML10DTDValidatorIndex--] = null;
}
return (RevalidationHandler) (ObjectFactory
.newInstance(
"org.apache.xerces.impl.dtd.XMLDTDValidator",
ObjectFactory.findClassLoader(),
true));
}
}
return null;
| public boolean | hasFeature(java.lang.String feature, java.lang.String version)Test if the DOM implementation supports a specific "feature" --
currently meaning language and level thereof.
boolean anyVersion = version == null || version.length() == 0;
// check if Xalan implementation is around and if yes report true for supporting
// XPath API
// if a plus sign "+" is prepended to any feature name, implementations
// are considered in which the specified feature may not be directly
// castable DOMImplementation.getFeature(feature, version). Without a
// plus, only features whose interfaces are directly castable are considered.
if ((feature.equalsIgnoreCase("+XPath"))
&& (anyVersion || version.equals("3.0"))) {
try {
Class xpathClass = ObjectFactory.findProviderClass(
"org.apache.xpath.domapi.XPathEvaluatorImpl",
ObjectFactory.findClassLoader(), true);
// Check if the DOM XPath implementation implements
// the interface org.w3c.dom.XPathEvaluator
Class interfaces[] = xpathClass.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (interfaces[i].getName().equals(
"org.w3c.dom.xpath.XPathEvaluator")) {
return true;
}
}
} catch (Exception e) {
return false;
}
return true;
}
if (feature.startsWith("+")) {
feature = feature.substring(1);
}
return (
feature.equalsIgnoreCase("Core")
&& (anyVersion
|| version.equals("1.0")
|| version.equals("2.0")
|| version.equals("3.0")))
|| (feature.equalsIgnoreCase("XML")
&& (anyVersion
|| version.equals("1.0")
|| version.equals("2.0")
|| version.equals("3.0")))
|| (feature.equalsIgnoreCase("XMLVersion")
&& (anyVersion
|| version.equals("1.0")
|| version.equals("1.1")))
|| (feature.equalsIgnoreCase("LS")
&& (anyVersion || version.equals("3.0")));
| final synchronized void | releaseDTDLoader(java.lang.String xmlVersion, org.apache.xerces.impl.dtd.XMLDTDLoader loader)NON-DOM: release DTD loader
// release an instance of XMLDTDLoader
if ("1.1".equals(xmlVersion)) {
++freeXML11DTDLoaderIndex;
if (xml11DTDLoaders.length == freeXML11DTDLoaderIndex) {
// resize size of the DTD loaders
xml11DTDLoaderCurrentSize += SIZE;
SoftReference [] newarray = new SoftReference[xml11DTDLoaderCurrentSize];
System.arraycopy(xml11DTDLoaders, 0, newarray, 0, xml11DTDLoaders.length);
xml11DTDLoaders = newarray;
}
SoftReference ref = xml11DTDLoaders[freeXML11DTDLoaderIndex];
if (ref != null) {
XMLDTDLoaderHolder holder = (XMLDTDLoaderHolder) ref.get();
if (holder != null) {
holder.loader = loader;
return;
}
}
xml11DTDLoaders[freeXML11DTDLoaderIndex] = new SoftReference(new XMLDTDLoaderHolder(loader));
}
// release an instance of XMLDTDLoader
else {
++freeXML10DTDLoaderIndex;
if (xml10DTDLoaders.length == freeXML10DTDLoaderIndex) {
// resize size of the DTD loaders
xml10DTDLoaderCurrentSize += SIZE;
SoftReference [] newarray = new SoftReference[xml10DTDLoaderCurrentSize];
System.arraycopy(xml10DTDLoaders, 0, newarray, 0, xml10DTDLoaders.length);
xml10DTDLoaders = newarray;
}
SoftReference ref = xml10DTDLoaders[freeXML10DTDLoaderIndex];
if (ref != null) {
XMLDTDLoaderHolder holder = (XMLDTDLoaderHolder) ref.get();
if (holder != null) {
holder.loader = loader;
return;
}
}
xml10DTDLoaders[freeXML10DTDLoaderIndex] = new SoftReference(new XMLDTDLoaderHolder(loader));
}
| synchronized void | releaseValidator(java.lang.String schemaType, java.lang.String xmlVersion, org.apache.xerces.impl.RevalidationHandler validator)NON-DOM: release validator
if (schemaType == XMLGrammarDescription.XML_SCHEMA) {
++freeSchemaValidatorIndex;
if (schemaValidators.length == freeSchemaValidatorIndex) {
// resize size of the validators
schemaValidatorsCurrentSize += SIZE;
SoftReference newarray[] = new SoftReference[schemaValidatorsCurrentSize];
System.arraycopy(schemaValidators, 0, newarray, 0, schemaValidators.length);
schemaValidators = newarray;
}
SoftReference ref = schemaValidators[freeSchemaValidatorIndex];
if (ref != null) {
RevalidationHandlerHolder holder = (RevalidationHandlerHolder) ref.get();
if (holder != null) {
holder.handler = validator;
return;
}
}
schemaValidators[freeSchemaValidatorIndex] = new SoftReference(new RevalidationHandlerHolder(validator));
}
else if (schemaType == XMLGrammarDescription.XML_DTD) {
// release an instance of XML11DTDValidator
if ("1.1".equals(xmlVersion)) {
++freeXML11DTDValidatorIndex;
if (xml11DTDValidators.length == freeXML11DTDValidatorIndex) {
// resize size of the validators
xml11DTDValidatorsCurrentSize += SIZE;
SoftReference [] newarray = new SoftReference[xml11DTDValidatorsCurrentSize];
System.arraycopy(xml11DTDValidators, 0, newarray, 0, xml11DTDValidators.length);
xml11DTDValidators = newarray;
}
SoftReference ref = xml11DTDValidators[freeXML11DTDValidatorIndex];
if (ref != null) {
RevalidationHandlerHolder holder = (RevalidationHandlerHolder) ref.get();
if (holder != null) {
holder.handler = validator;
return;
}
}
xml11DTDValidators[freeXML11DTDValidatorIndex] = new SoftReference(new RevalidationHandlerHolder(validator));
}
// release an instance of XMLDTDValidator
else {
++freeXML10DTDValidatorIndex;
if (xml10DTDValidators.length == freeXML10DTDValidatorIndex) {
// resize size of the validators
xml10DTDValidatorsCurrentSize += SIZE;
SoftReference [] newarray = new SoftReference[xml10DTDValidatorsCurrentSize];
System.arraycopy(xml10DTDValidators, 0, newarray, 0, xml10DTDValidators.length);
xml10DTDValidators = newarray;
}
SoftReference ref = xml10DTDValidators[freeXML10DTDValidatorIndex];
if (ref != null) {
RevalidationHandlerHolder holder = (RevalidationHandlerHolder) ref.get();
if (holder != null) {
holder.handler = validator;
return;
}
}
xml10DTDValidators[freeXML10DTDValidatorIndex] = new SoftReference(new RevalidationHandlerHolder(validator));
}
}
|
|