1 57 package com.sun.org.apache.xerces.internal.dom; 58 import com.sun.org.apache.xerces.internal.impl.RevalidationHandler; 59 import com.sun.org.apache.xerces.internal.parsers.DOMParserImpl; 60 import com.sun.org.apache.xerces.internal.util.XMLChar; 61 import com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl; 62 import org.w3c.dom.DOMException ; 63 import org.w3c.dom.DOMImplementation ; 64 import org.w3c.dom.Document ; 65 import org.w3c.dom.DocumentType ; 66 import org.w3c.dom.Element ; 67 import org.w3c.dom.ls.LSParser ; 68 import org.w3c.dom.ls.DOMImplementationLS ; 69 import org.w3c.dom.ls.LSInput ; 70 import org.w3c.dom.ls.LSOutput ; 71 import org.w3c.dom.ls.LSSerializer ; 72 87 public class CoreDOMImplementationImpl 88 implements DOMImplementation , DOMImplementationLS { 89 93 private static final int SIZE = 2; 95 private RevalidationHandler validators[] = new RevalidationHandler[SIZE]; 96 private int freeValidatorIndex = -1; 97 private int currentSize = SIZE; 98 99 private int docAndDoctypeCounter = 0; 103 104 106 static CoreDOMImplementationImpl singleton = 107 new CoreDOMImplementationImpl(); 108 112 public static DOMImplementation getDOMImplementation() { 113 return singleton; 114 } 115 133 public boolean hasFeature(String feature, String version) { 134 135 boolean anyVersion = version == null || version.length() == 0; 136 if (feature.startsWith("+")) { 137 feature = feature.substring(1); 138 } 139 if ((feature.equalsIgnoreCase("XPath") 142 || feature.equalsIgnoreCase("+XPath")) 143 && (anyVersion || version.equals("3.0"))) { 144 try { 145 Class xpathClass = 146 ObjectFactory.findProviderClass( 147 "com.sun.org.apache.xpath.internal.domapi.XPathEvaluatorImpl", 148 ObjectFactory.findClassLoader(), 149 true); 150 } catch (Exception e) { 151 return false; 152 } 153 return true; 154 } 155 return ( 156 feature.equalsIgnoreCase("Core") 157 && (anyVersion 158 || version.equals("1.0") 159 || version.equals("2.0") 160 || version.equals("3.0"))) 161 || (feature.equalsIgnoreCase("XML") 162 && (anyVersion 163 || version.equals("1.0") 164 || version.equals("2.0") 165 || version.equals("3.0"))) 166 || (feature.equalsIgnoreCase("LS") 167 && (anyVersion || version.equals("3.0"))); 168 } 170 171 181 public DocumentType createDocumentType( String qualifiedName, 182 String publicID, String systemID) { 183 checkQName(qualifiedName); 187 return new DocumentTypeImpl(null, qualifiedName, publicID, systemID); 188 } 189 190 final void checkQName(String qname){ 191 int index = qname.indexOf(':'); 192 int lastIndex = qname.lastIndexOf(':'); 193 int length = qname.length(); 194 195 if (index == 0 || index == length - 1 || lastIndex != index) { 198 String msg = 199 DOMMessageFormatter.formatMessage( 200 DOMMessageFormatter.DOM_DOMAIN, 201 "NAMESPACE_ERR", 202 null); 203 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 204 } 205 int start = 0; 206 if (index > 0) { 208 if (!XMLChar.isNCNameStart(qname.charAt(start))) { 210 String msg = 211 DOMMessageFormatter.formatMessage( 212 DOMMessageFormatter.DOM_DOMAIN, 213 "INVALID_CHARACTER_ERR", 214 null); 215 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 216 } 217 for (int i = 1; i < index; i++) { 218 if (!XMLChar.isNCName(qname.charAt(i))) { 219 String msg = 220 DOMMessageFormatter.formatMessage( 221 DOMMessageFormatter.DOM_DOMAIN, 222 "INVALID_CHARACTER_ERR", 223 null); 224 throw new DOMException ( 225 DOMException.INVALID_CHARACTER_ERR, 226 msg); 227 } 228 } 229 start = index + 1; 230 } 231 232 if (!XMLChar.isNCNameStart(qname.charAt(start))) { 234 String msg = 236 DOMMessageFormatter.formatMessage( 237 DOMMessageFormatter.DOM_DOMAIN, 238 "INVALID_CHARACTER_ERR", 239 null); 240 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 241 } 242 for (int i = start + 1; i < length; i++) { 243 if (!XMLChar.isNCName(qname.charAt(i))) { 244 String msg = 245 DOMMessageFormatter.formatMessage( 246 DOMMessageFormatter.DOM_DOMAIN, 247 "INVALID_CHARACTER_ERR", 248 null); 249 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 250 } 251 } 252 } 253 254 255 275 public Document createDocument( 276 String namespaceURI, 277 String qualifiedName, 278 DocumentType doctype) 279 throws DOMException { 280 if (doctype != null && doctype.getOwnerDocument() != null) { 281 String msg = 282 DOMMessageFormatter.formatMessage( 283 DOMMessageFormatter.DOM_DOMAIN, 284 "WRONG_DOCUMENT_ERR", 285 null); 286 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 287 } 288 CoreDocumentImpl doc = new CoreDocumentImpl(doctype); 289 Element e = doc.createElementNS(namespaceURI, qualifiedName); 290 doc.appendChild(e); 291 return doc; 292 } 293 294 297 public Object getFeature(String feature, String version) { 298 if (singleton.hasFeature(feature, version)){ 299 return singleton; 300 } 301 return null; 302 303 } 304 305 307 349 public LSParser createLSParser(short mode, String schemaType) 350 throws DOMException { 351 if (mode != DOMImplementationLS.MODE_SYNCHRONOUS || (schemaType !=null && 352 !"http://www.w3.org/2001/XMLSchema".equals(schemaType) && 353 !"http://www.w3.org/TR/REC-xml".equals(schemaType))) { 354 String msg = 355 DOMMessageFormatter.formatMessage( 356 DOMMessageFormatter.DOM_DOMAIN, 357 "NOT_SUPPORTED_ERR", 358 null); 359 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 360 } 361 if (schemaType != null 362 && schemaType.equals("http://www.w3.org/TR/REC-xml")) { 363 return new DOMParserImpl( 364 "com.sun.org.apache.xerces.internal.parsers.XML11Configuration", 365 schemaType); 366 } 367 else { 368 return new DOMParserImpl( 370 "com.sun.org.apache.xerces.internal.parsers.XML11Configuration", 371 schemaType); 372 } 373 } 374 375 388 public LSSerializer createLSSerializer() { 389 return new DOMSerializerImpl(); 390 } 391 396 public LSInput createLSInput() { 397 return new DOMInputImpl(); 398 } 399 400 synchronized Object getDTDValidator() { 401 return ObjectFactory.newInstance( "com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator", ObjectFactory.findClassLoader(), true); 402 } 403 404 408 synchronized RevalidationHandler getValidator(String schemaType) { 409 if (freeValidatorIndex < 0) { 411 return (RevalidationHandler) (ObjectFactory .newInstance( "com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator", ObjectFactory.findClassLoader(), true)); 415 416 } 417 RevalidationHandler val = validators[freeValidatorIndex]; 419 validators[freeValidatorIndex--] = null; 420 return val; 421 } 422 423 424 synchronized void releaseValidator(String schemaType, 425 RevalidationHandler validator) { 426 ++freeValidatorIndex; 428 if (validators.length == freeValidatorIndex ){ 429 currentSize+=SIZE; 431 RevalidationHandler newarray[] = new RevalidationHandler[currentSize]; 432 System.arraycopy(validators, 0, newarray, 0, validators.length); 433 validators = newarray; 434 } 435 validators[freeValidatorIndex]=validator; 436 } 437 438 439 protected synchronized int assignDocumentNumber() { 440 return ++docAndDoctypeCounter; 441 } 442 443 protected synchronized int assignDocTypeNumber() { 444 return ++docAndDoctypeCounter; 445 } 446 447 456 public LSOutput createLSOutput() { 457 return new DOMOutputImpl(); 458 } 459 460 } | Popular Tags |