1 20 21 25 26 package org.ozoneDB.xml.dom; 27 28 import java.util.*; 29 import java.io.*; 30 import org.w3c.dom.*; 31 import org.ozoneDB.*; 32 import org.ozoneDB.xml.dom.*; 33 import org.ozoneDB.xml.util.XMLContainerHelper; 34 35 36 66 public class DocumentImpl extends NodeImpl implements DocumentProxy { 67 68 final static long serialVersionUID = 1; 69 70 private XMLContainerHelper container; 71 72 73 77 private DocumentTypeProxy _docType; 78 79 87 private Thread _lockThread; 88 89 90 98 private int _lockCount; 99 100 101 107 private Hashtable _elementTypes; 108 109 public void writeExternal( ObjectOutput out ) throws IOException { 110 super.writeExternal( out ); 111 out.writeObject ( container ); 112 } 113 114 115 public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException { 116 super.readExternal( in ); 117 container = (XMLContainerHelper)in.readObject(); 118 } 119 120 121 125 public void setContainer( XMLContainerHelper _container ) { 126 this.container = _container; 127 } 128 129 130 134 public XMLContainerHelper getContainer() { 135 return this.container; 136 } 137 138 139 public Node importNode( Node importedNode, boolean deep ) throws DOMException { 140 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 141 "Document.importNode() : ozone's persistent DOM doesn't support DOM level 2 yet." ); 142 } 143 144 145 public Element createElementNS( java.lang.String namespaceURI, java.lang.String qualifiedName ) 146 throws DOMException { 147 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 148 "Document.createElementNS() : ozone's persistent DOM doesn't support DOM level 2 yet." ); 149 } 150 151 152 public Attr createAttributeNS( java.lang.String namespaceURI, java.lang.String qualifiedName ) throws DOMException { 153 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 154 "Document.createAttributeNS() : ozone's persistent DOM doesn't support DOM level 2 yet." ); 155 } 156 157 158 public NodeList getElementsByTagNameNS( java.lang.String namespaceURI, java.lang.String localName ) { 159 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 160 "Document.getElementsByTagNameNS() : ozone's persistent DOM doesn't support DOM level 2 yet." ); 161 } 162 163 164 public Element getElementById( java.lang.String elementId ) { 165 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 166 "Document.getElementById() : ozone's persistent DOM doesn't support DOM level 2 yet." ); 167 } 168 169 170 public Document createDocument( java.lang.String namespaceURI, java.lang.String qualifiedName, 171 DocumentType doctype ) throws DOMException { 172 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 173 "Document.createDocument() : ozone's persistent DOM doesn't support DOM level 2 yet." ); 174 } 175 176 177 public DocumentType createDocumentType( java.lang.String qualifiedName, java.lang.String publicId, 178 java.lang.String systemId ) throws DOMException { 179 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 180 "Document.createDocumentType() : ozone's persistent DOM doesn't support DOM level 2 yet." ); 181 } 182 183 184 public short getNodeType() { 185 return DOCUMENT_NODE; 186 } 187 188 189 public final void setNodeValue( String value ) { 190 throw new DOMExceptionImpl( DOMException.NO_DATA_ALLOWED_ERR, "This node type does not support values." ); 191 } 192 193 194 public final DocumentType getDoctype() { 195 return _docType; 196 } 197 198 199 public final void setDoctype( DocumentType docType ) { 200 _docType = (DocumentTypeProxy)docType; 201 } 202 203 204 public final DOMImplementation getImplementation() { 205 return this; 206 } 207 208 209 public Element getDocumentElement() { 210 Node child; 211 212 child = getFirstChild(); 215 while (child != null) { 216 if (child instanceof Element) { 217 return (Element)child; 218 } 219 child = child.getNextSibling(); 220 } 221 return null; 222 } 223 224 225 public void setElementTypes( Hashtable elementTypes ) { 226 _elementTypes = elementTypes; 227 } 228 229 230 public Element createElement( String tagName ) throws DOMException { 231 ElementProxy elem = null; 232 try { 233 elem = (ElementProxy)database().createObject( ElementImpl.class.getName() ); 234 elem.init( this, tagName, null, true ); 235 } catch (Exception except) { 236 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 237 } 238 return elem; 239 } 240 241 242 public final DocumentFragment createDocumentFragment() { 243 DocumentFragmentProxy fragment = null; 244 try { 245 fragment = (DocumentFragmentProxy)database().createObject( DocumentFragmentImpl.class.getName() ); 246 fragment.init( this, null ); 247 } catch (Exception except) { 248 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 249 } 250 return fragment; 251 } 252 253 254 public final Text createTextNode( String data ) { 255 TextProxy text = null; 256 try { 257 text = (TextProxy)database().createObject( TextImpl.class.getName() ); 258 text.init( this, data ); 259 } catch (Exception except) { 260 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 261 } 262 return text; 263 } 264 265 266 public final Comment createComment( String data ) { 267 CommentProxy comment = null; 268 try { 269 comment = (CommentProxy)database().createObject( CommentImpl.class.getName() ); 270 comment.init( this, data ); 271 } catch (Exception except) { 272 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 273 } 274 return comment; 275 } 276 277 278 public final CDATASection createCDATASection( String data ) throws DOMException { 279 CDATASectionProxy section = null; 280 try { 281 section = (CDATASectionProxy)database().createObject( CDATASectionImpl.class.getName() ); 282 section.init( this, data ); 283 } catch (Exception except) { 284 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 285 } 286 return section; 287 } 288 289 290 public final ProcessingInstruction createProcessingInstruction( String target, String data ) throws DOMException { 291 ProcessingInstructionProxy pi = null; 292 try { 293 pi = (ProcessingInstructionProxy)database().createObject( ProcessingInstructionImpl.class.getName() ); 294 pi.init( this, target, data ); 295 } catch (Exception except) { 296 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 297 } 298 return pi; 299 } 300 301 302 public final Attr createAttribute( String name ) throws DOMException { 303 AttrProxy attr = null; 304 try { 305 attr = (AttrProxy)database().createObject( AttrImpl.class.getName() ); 306 attr.init( this, name, "" ); 307 } catch (Exception except) { 308 except.printStackTrace(); 309 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 310 } 311 return attr; 312 } 313 314 315 322 public final Attr createAttribute( String name, String defValue ) throws DOMException { 323 AttrProxy attr = null; 324 try { 325 attr = (AttrProxy)database().createObject( AttrImpl.class.getName() ); 326 attr.init( this, name, defValue == null ? "" : defValue ); 327 } catch (Exception except) { 328 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 329 } 330 return attr; 331 } 332 333 334 public final EntityReference createEntityReference( String name ) throws DOMException { 335 EntityReferenceProxy entity = null; 336 try { 337 entity = (EntityReferenceProxy)database().createObject( EntityReferenceImpl.class.getName() ); 338 } catch (Exception except) { 340 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 341 } 342 return entity; 343 } 344 345 358 368 369 370 public final NodeList getElementsByTagName( String tagName ) { 371 return new org.ozoneDB.xml.dom.ElementListImpl( this, tagName ); 372 } 373 374 375 383 public boolean hasFeature( String feature, String version ) { 384 if (version != null) { 386 if (version.indexOf( "1.0" ) < 0 && version.indexOf( "Level 1" ) < 0) { 387 return false; 388 } 389 } 390 if (feature != null) { 392 if (feature.equalsIgnoreCase( "XML" ) || feature.equalsIgnoreCase( "HTML" )) { 393 return true; 394 } 395 } 396 return false; 397 } 398 399 400 410 public void registerElement( String tagName, Class elementClass ) { 411 if (tagName == null || tagName.length() == 0) { 412 throw new NullPointerException ( "Argument 'tagName' is null or an empty string." ); 413 } 414 if (elementClass == null || elementClass.isAssignableFrom( Element.class )) { 415 throw new IllegalArgumentException ( "Argument 'elementClass' is null or does not extend Element." ); 416 } 417 if (_elementTypes == null) { 418 _elementTypes = new Hashtable(); 419 } 420 _elementTypes.put( tagName, elementClass ); 421 } 422 423 424 public void assignDoctype( DocumentTypeProxy docType ) { 425 if (docType == null) { 426 throw new NullPointerException ( "Argument 'docType' is null." ); 427 } 428 if (_docType != null) { 429 throw new IllegalStateException ( "Document type already assigned to this document." ); 430 } 431 _docType = docType; 432 } 433 434 457 458 459 473 public void lock() throws RuntimeException { 474 synchronized (this) { 475 if (_lockThread == Thread.currentThread()) { 480 ++_lockCount; 481 } else { 482 acquire( Long.MAX_VALUE ); 483 _lockThread = Thread.currentThread(); 484 ++_lockCount; 485 } 486 } 487 } 488 489 490 499 public void unlock() { 500 if (_lockThread == Thread.currentThread()) { 504 synchronized (this) { 505 --_lockCount; 506 if (_lockCount == 0) { 507 _lockThread = null; 508 notify(); 509 } 510 } 511 } 512 } 513 514 515 524 public void acquire( long lockTimeout ) throws RuntimeException { 525 long start; 526 527 synchronized (this) { 528 try { 529 if (_lockThread != Thread.currentThread() && _lockThread != null) { 532 start = System.currentTimeMillis(); 536 while (_lockThread != null && lockTimeout > (System.currentTimeMillis() - start)) { 537 if (!_lockThread.isAlive()) { 540 _lockThread = null; 541 } else { 542 wait( lockTimeout - System.currentTimeMillis() - start ); 543 } 544 } 545 if (_lockThread != null) { 547 throw new RuntimeException ( "Timeout waiting for lock to be released." ); 548 } 549 } 550 } catch (InterruptedException except) { 551 throw new RuntimeException ( "Timeout waiting for lock to be released." ); 553 } 554 } 555 } 556 557 558 public synchronized boolean equals( Object other ) { 559 DocumentProxy otherX; 560 561 if (!super.equals( other )) { 564 return false; 565 } 566 567 synchronized (other) { 570 otherX = (DocumentProxy)other; 571 return 572 getDoctype() == null && otherX.getDoctype() == null || getDoctype() != null && otherX.getDoctype() 573 != null && _docType.equals( otherX.getDoctype() ); 574 } 575 } 576 577 578 public Object clone() { 579 DocumentProxy clone = null; 580 try { 581 clone = (DocumentProxy)database().createObject( DocumentImpl.class.getName() ); 582 cloneInto( clone, true ); 583 } catch (Exception except) { 584 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 585 } 586 return clone; 587 } 588 589 590 public Node cloneNode( boolean deep ) { 591 DocumentProxy clone = null; 592 try { 593 clone = (DocumentProxy)database().createObject( DocumentImpl.class.getName() ); 594 cloneInto( clone, deep ); 595 } catch (Exception except) { 596 throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() ); 597 } 598 return clone; 599 } 600 601 public String toString() { 602 return "Document: "; 603 } 604 605 606 public synchronized void cloneInto( NodeProxy into, boolean deep ) { 607 608 super.cloneInto( into, deep ); 612 if (deep) { 613 if (_docType != null) { 614 ((DocumentProxy)into).setDoctype( (DocumentTypeProxy)_docType.clone() ); 615 } 616 if (_elementTypes != null) { 618 ((DocumentProxy)into).setElementTypes( (Hashtable)_elementTypes.clone() ); 619 } 620 } else { 621 ((DocumentProxy)into).setDoctype( _docType ); 622 ((DocumentProxy)into).setElementTypes( _elementTypes ); 623 } 624 } 626 627 628 protected final boolean supportsChildern() { 629 return true; 630 } 631 632 633 public DocumentImpl() { 634 super( null, "#document", null, false ); 635 _ownerDocument = this; 636 } 637 638 639 protected DocumentImpl( String rootElement ) { 640 super( null, rootElement != null ? rootElement : "", null, rootElement != null ); 641 _ownerDocument = this; 642 } 643 644 645 public void onDelete() { 646 clearDocument(); 647 } 648 649 650 private void deleteAllChildern( Node node ) { 651 Node dummyNode; 652 while (node != null) { 653 if (node.hasChildNodes()) { 654 deleteAllChildern( node.getFirstChild() ); 655 } 656 dummyNode = node.getNextSibling(); 657 System.out.println( "deleting " + node.getNodeName() ); 658 database().deleteObject( (OzoneRemote)node ); 659 node = dummyNode; 660 } 661 } 662 663 668 public void clearDocument() { 669 if (_docType != null) { 671 database().deleteObject( _docType ); 672 } 673 Node node = getFirstChild(); 674 while (node != null) { 675 deleteAllChildern( node ); 676 node = getNextSibling(); 677 } 678 } 679 680 } | Popular Tags |