1 16 package org.apache.xerces.dom; 17 18 import org.apache.xerces.impl.RevalidationHandler; 19 import org.apache.xerces.parsers.DOMParserImpl; 20 import org.apache.xerces.util.XMLChar; 21 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 22 import org.apache.xml.serialize.DOMSerializerImpl; 23 import org.w3c.dom.DOMException ; 24 import org.w3c.dom.DOMImplementation ; 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.DocumentType ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.ls.LSParser ; 29 import org.w3c.dom.ls.DOMImplementationLS ; 30 import org.w3c.dom.ls.LSInput ; 31 import org.w3c.dom.ls.LSOutput ; 32 import org.w3c.dom.ls.LSSerializer ; 33 51 public class CoreDOMImplementationImpl 52 implements DOMImplementation , DOMImplementationLS { 53 57 private static final int SIZE = 2; 59 private RevalidationHandler validators[] = new RevalidationHandler[SIZE]; 60 61 private RevalidationHandler dtdValidators[] = new RevalidationHandler[SIZE]; 62 private int freeValidatorIndex = -1; 63 private int freeDTDValidatorIndex = -1; 64 private int currentSize = SIZE; 65 66 private int docAndDoctypeCounter = 0; 70 71 73 static CoreDOMImplementationImpl singleton = 74 new CoreDOMImplementationImpl(); 75 79 public static DOMImplementation getDOMImplementation() { 80 return singleton; 81 } 82 100 public boolean hasFeature(String feature, String version) { 101 102 boolean anyVersion = version == null || version.length() == 0; 103 104 if ((feature.equalsIgnoreCase("+XPath")) 111 && (anyVersion || version.equals("3.0"))) { 112 try { 113 Class xpathClass = ObjectFactory.findProviderClass( 114 "org.apache.xpath.domapi.XPathEvaluatorImpl", 115 ObjectFactory.findClassLoader(), true); 116 117 Class interfaces[] = xpathClass.getInterfaces(); 120 for (int i = 0; i < interfaces.length; i++) { 121 if (interfaces[i].getName().equals( 122 "org.w3c.dom.xpath.XPathEvaluator")) { 123 return true; 124 } 125 } 126 } catch (Exception e) { 127 return false; 128 } 129 return true; 130 } 131 if (feature.startsWith("+")) { 132 feature = feature.substring(1); 133 } 134 return ( 135 feature.equalsIgnoreCase("Core") 136 && (anyVersion 137 || version.equals("1.0") 138 || version.equals("2.0") 139 || version.equals("3.0"))) 140 || (feature.equalsIgnoreCase("XML") 141 && (anyVersion 142 || version.equals("1.0") 143 || version.equals("2.0") 144 || version.equals("3.0"))) 145 || (feature.equalsIgnoreCase("LS") 146 && (anyVersion || version.equals("3.0"))); 147 } 149 150 160 public DocumentType createDocumentType( String qualifiedName, 161 String publicID, String systemID) { 162 checkQName(qualifiedName); 166 return new DocumentTypeImpl(null, qualifiedName, publicID, systemID); 167 } 168 169 final void checkQName(String qname){ 170 int index = qname.indexOf(':'); 171 int lastIndex = qname.lastIndexOf(':'); 172 int length = qname.length(); 173 174 if (index == 0 || index == length - 1 || lastIndex != index) { 177 String msg = 178 DOMMessageFormatter.formatMessage( 179 DOMMessageFormatter.DOM_DOMAIN, 180 "NAMESPACE_ERR", 181 null); 182 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 183 } 184 int start = 0; 185 if (index > 0) { 187 if (!XMLChar.isNCNameStart(qname.charAt(start))) { 189 String msg = 190 DOMMessageFormatter.formatMessage( 191 DOMMessageFormatter.DOM_DOMAIN, 192 "INVALID_CHARACTER_ERR", 193 null); 194 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 195 } 196 for (int i = 1; i < index; i++) { 197 if (!XMLChar.isNCName(qname.charAt(i))) { 198 String msg = 199 DOMMessageFormatter.formatMessage( 200 DOMMessageFormatter.DOM_DOMAIN, 201 "INVALID_CHARACTER_ERR", 202 null); 203 throw new DOMException ( 204 DOMException.INVALID_CHARACTER_ERR, 205 msg); 206 } 207 } 208 start = index + 1; 209 } 210 211 if (!XMLChar.isNCNameStart(qname.charAt(start))) { 213 String msg = 215 DOMMessageFormatter.formatMessage( 216 DOMMessageFormatter.DOM_DOMAIN, 217 "INVALID_CHARACTER_ERR", 218 null); 219 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 220 } 221 for (int i = start + 1; i < length; i++) { 222 if (!XMLChar.isNCName(qname.charAt(i))) { 223 String msg = 224 DOMMessageFormatter.formatMessage( 225 DOMMessageFormatter.DOM_DOMAIN, 226 "INVALID_CHARACTER_ERR", 227 null); 228 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 229 } 230 } 231 } 232 233 234 254 public Document createDocument( 255 String namespaceURI, 256 String qualifiedName, 257 DocumentType doctype) 258 throws DOMException { 259 if (doctype != null && doctype.getOwnerDocument() != null) { 260 String msg = 261 DOMMessageFormatter.formatMessage( 262 DOMMessageFormatter.DOM_DOMAIN, 263 "WRONG_DOCUMENT_ERR", 264 null); 265 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 266 } 267 CoreDocumentImpl doc = new CoreDocumentImpl(doctype); 268 Element e = doc.createElementNS(namespaceURI, qualifiedName); 269 doc.appendChild(e); 270 return doc; 271 } 272 273 276 public Object getFeature(String feature, String version) { 277 if (singleton.hasFeature(feature, version)) { 278 if ((feature.equalsIgnoreCase("+XPath"))) { 279 try { 280 Class xpathClass = ObjectFactory.findProviderClass( 281 "org.apache.xpath.domapi.XPathEvaluatorImpl", 282 ObjectFactory.findClassLoader(), true); 283 284 Class interfaces[] = xpathClass.getInterfaces(); 287 for (int i = 0; i < interfaces.length; i++) { 288 if (interfaces[i].getName().equals( 289 "org.w3c.dom.xpath.XPathEvaluator")) { 290 return xpathClass.newInstance(); 291 } 292 } 293 } catch (Exception e) { 294 return null; 295 } 296 } else { 297 return singleton; 298 } 299 } 300 return null; 301 } 302 303 305 347 public LSParser createLSParser(short mode, String schemaType) 348 throws DOMException { 349 if (mode != DOMImplementationLS.MODE_SYNCHRONOUS || (schemaType !=null && 350 !"http://www.w3.org/2001/XMLSchema".equals(schemaType) && 351 !"http://www.w3.org/TR/REC-xml".equals(schemaType))) { 352 String msg = 353 DOMMessageFormatter.formatMessage( 354 DOMMessageFormatter.DOM_DOMAIN, 355 "NOT_SUPPORTED_ERR", 356 null); 357 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 358 } 359 if (schemaType != null 360 && schemaType.equals("http://www.w3.org/TR/REC-xml")) { 361 return new DOMParserImpl( 362 "org.apache.xerces.parsers.DTDConfiguration", 363 schemaType); 364 } 365 else { 366 return new DOMParserImpl( 368 "org.apache.xerces.parsers.XIncludeAwareParserConfiguration", 369 schemaType); 370 } 371 } 372 373 386 public LSSerializer createLSSerializer() { 387 return new DOMSerializerImpl(); 388 } 389 394 public LSInput createLSInput() { 395 return new DOMInputImpl(); 396 } 397 398 402 synchronized RevalidationHandler getValidator(String schemaType) { 403 if (schemaType == XMLGrammarDescription.XML_SCHEMA) { 405 if(freeValidatorIndex < 0) { 409 return (RevalidationHandler) (ObjectFactory 410 .newInstance( 411 "org.apache.xerces.impl.xs.XMLSchemaValidator", 412 ObjectFactory.findClassLoader(), 413 true)); 414 } 415 RevalidationHandler val = validators[freeValidatorIndex]; 417 validators[freeValidatorIndex--] = null; 418 return val; 419 } 420 else if(schemaType == XMLGrammarDescription.XML_DTD) { 421 if(freeDTDValidatorIndex < 0) { 422 return (RevalidationHandler) (ObjectFactory 423 .newInstance( 424 "org.apache.xerces.impl.dtd.XMLDTDValidator", 425 ObjectFactory.findClassLoader(), 426 true)); 427 } 428 RevalidationHandler val = dtdValidators[freeDTDValidatorIndex]; 430 dtdValidators[freeDTDValidatorIndex--] = null; 431 return val; 432 } 433 return null; 434 } 435 436 437 synchronized void releaseValidator(String schemaType, 438 RevalidationHandler validator) { 439 if(schemaType == XMLGrammarDescription.XML_SCHEMA) { 441 ++freeValidatorIndex; 442 if (validators.length == freeValidatorIndex ){ 443 currentSize+=SIZE; 445 RevalidationHandler newarray[] = new RevalidationHandler[currentSize]; 446 System.arraycopy(validators, 0, newarray, 0, validators.length); 447 validators = newarray; 448 } 449 validators[freeValidatorIndex]=validator; 450 } 451 else if(schemaType == XMLGrammarDescription.XML_DTD) { 452 ++freeDTDValidatorIndex; 453 if (dtdValidators.length == freeDTDValidatorIndex ){ 454 currentSize+=SIZE; 456 RevalidationHandler newarray[] = new RevalidationHandler[currentSize]; 457 System.arraycopy(dtdValidators, 0, newarray, 0, dtdValidators.length); 458 dtdValidators = newarray; 459 } 460 dtdValidators[freeDTDValidatorIndex]=validator; 461 } 462 } 463 464 465 protected synchronized int assignDocumentNumber() { 466 return ++docAndDoctypeCounter; 467 } 468 469 protected synchronized int assignDocTypeNumber() { 470 return ++docAndDoctypeCounter; 471 } 472 473 482 public LSOutput createLSOutput() { 483 return new DOMOutputImpl(); 484 } 485 486 } | Popular Tags |