1 5 6 package org.ozoneDB.xml.dom; 7 8 import java.io.*; 9 import org.ozoneDB.*; 10 import org.ozoneDB.xml.dom.iterator.*; 11 import org.w3c.dom.*; 12 13 14 public abstract class NodeImpl extends OzoneObject implements NodeProxy, Externalizable { 15 16 final static long serialVersionUID = 1; 17 18 24 25 public boolean supports( java.lang.String feature, java.lang.String version ) { 26 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 27 "Node.supports(): ozone's persistent DOM doesn't support DOM level 2 yet." ); 28 } 29 30 31 public void normalize() { 32 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 33 "Node.normalize(): ozone's persistent DOM doesn't support DOM level 2 yet." ); 34 } 35 36 37 public java.lang.String getNamespaceURI() { 38 return null; 42 } 43 44 45 public java.lang.String getPrefix() { 46 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 47 "Node.getPrefix(): ozone's persistent DOM doesn't support DOM level 2 yet." ); 48 } 49 50 51 public void setPrefix( java.lang.String prefix ) throws DOMException { 52 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 53 "Node.setPrefix(): ozone's persistent DOM doesn't support DOM level 2 yet." ); 54 } 55 56 57 public java.lang.String getLocalName() { 58 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 59 "Node.getLocalName(): ozone's persistent DOM doesn't support DOM level 2 yet." ); 60 } 61 62 63 68 public abstract short getNodeType(); 69 70 71 79 public final String getNodeName() { 80 return _nodeName; 81 } 82 83 84 86 public final void setNodeName( String nodeName ) { 87 _nodeName = nodeName; 88 } 89 90 91 100 public final String getNodeValue() { 101 return _nodeValue; 102 } 103 104 105 133 public void setNodeValue( String value ) { 134 if (isReadOnly()) { 135 throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR ); 136 } 137 _nodeValue = value == null ? "" : value; 138 } 139 140 141 149 public Node getParentNode() { 150 return _parent; 151 } 152 153 154 156 public void setParentNode( Node newParent ) { 157 _parent = (NodeProxy)newParent; 158 } 159 160 161 175 protected void notifyIterators( Node removedChild ) { 176 189 } 190 191 192 205 public NodeList getChildNodes() { 206 if (!supportsChildern()) { 208 throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, 209 "No childern supported by this node type." ); 210 } 211 return new org.ozoneDB.xml.dom.NodeListImpl( this ); 212 } 213 214 215 220 public final Node getFirstChild() { 221 return _firstChild; 222 } 223 224 225 230 public final Node getLastChild() { 231 return _lastChild; 232 } 233 234 235 241 public Node getPreviousSibling() { 242 return _prevNode; 243 } 244 245 246 248 public void setPreviousSibling( Node prevNode ) { 249 _prevNode = (NodeProxy)prevNode; 250 } 251 252 253 259 public Node getNextSibling() { 260 return _nextNode; 261 } 262 263 264 266 public void setNextSibling( Node nextNode ) { 267 _nextNode = (NodeProxy)nextNode; 268 } 269 270 271 279 public NamedNodeMap getAttributes() { 280 return null; 281 } 282 283 284 public boolean hasAttributes() { 285 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 286 "ozone's persistent DOM doesn't support DOM level 2 yet." ); 287 } 288 289 290 291 292 public final Document getOwnerDocument() { 293 if (_ownerDocument != this) { 294 return _ownerDocument; 295 } else { 296 return null; 297 } 298 } 299 300 301 307 public final boolean hasChildNodes() { 308 return _firstChild != null; 309 } 310 311 312 332 public synchronized final Node appendChild( Node newChild ) { 333 NodeProxy newChildX; 335 336 if (isReadOnly()) { 339 throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR ); 340 } 341 if (!supportsChildern()) { 342 throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, 343 "No childern supported by this node type." ); 344 } 345 346 newChildX = (NodeProxy)castNewChild( newChild ); 348 349 synchronized (newChild) { 352 if (newChildX.getParentNode() != null) { 356 if (((NodeProxy)newChildX.getParentNode()).isReadOnly()) { 357 throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR ); 358 } 359 newChildX.getParentNode().removeChild( newChildX ); 360 } 361 362 if (newChildX instanceof DocumentFragment) { 365 NodeProxy nextChild; 366 367 newChildX = (NodeProxy)newChildX.getFirstChild(); 368 while (newChildX != null) { 369 nextChild = (NodeProxy)newChildX.getNextSibling(); 370 appendChild( newChildX ); 371 newChildX = nextChild; 372 } 373 return newChild; 374 } 375 376 newChildX.setParentNode( this ); 382 if (_ownerDocument != null) { 383 newChildX.setOwnerDocument( _ownerDocument ); 384 } 385 386 if (_lastChild == null) { 389 _lastChild = newChildX; 390 _firstChild = newChildX; 391 newChildX.setPreviousSibling( null ); 392 newChildX.setNextSibling( null ); 393 } else { 394 _lastChild.setNextSibling( newChildX ); 397 newChildX.setPreviousSibling( _lastChild ); 398 newChildX.setNextSibling( null ); 399 _lastChild = newChildX; 400 } 401 ++_childsCount; 403 } 404 return newChild; 405 } 406 407 408 423 public synchronized final Node removeChild( Node oldChild ) throws DOMException { 424 NodeProxy oldChildX; 425 426 if (isReadOnly()) { 429 throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR ); 430 } 431 if (!supportsChildern()) { 432 throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, 433 "No childern supported by this node type." ); 434 } 435 436 oldChildX = (NodeProxy)castOldChild( oldChild ); 438 439 synchronized (oldChild) { 442 notifyIterators( oldChild ); 449 450 oldChildX.setParentNode( null ); 453 454 if (_firstChild != null && _firstChild.equals( oldChildX )) { 455 _firstChild = (NodeProxy)oldChildX.getNextSibling(); 456 } 457 458 if (_lastChild != null && _lastChild.equals( oldChildX )) { 459 _lastChild = (NodeProxy)oldChildX.getPreviousSibling(); 460 } 461 462 if (oldChildX.getPreviousSibling() != null) { 463 ((NodeProxy)oldChildX.getPreviousSibling()).setNextSibling( oldChildX.getNextSibling() ); 464 } 465 466 if (oldChildX.getNextSibling() != null) { 467 ((NodeProxy)oldChildX.getNextSibling()).setPreviousSibling( oldChildX.getPreviousSibling() ); 468 } 469 470 oldChildX.setPreviousSibling( null ); 471 oldChildX.setNextSibling( null ); 472 473 --_childsCount; 475 476 } 477 return oldChild; 478 } 479 480 481 506 public synchronized final Node replaceChild( Node newChild, Node oldChild ) throws DOMException { 507 NodeProxy newChildX; 509 NodeProxy oldChildX; 510 511 if (isReadOnly()) { 514 throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR ); 515 } 516 if (!supportsChildern()) { 517 throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, 518 "No childern supported by this node type." ); 519 } 520 521 if (newChild != null) { 524 newChildX = (NodeProxy)castNewChild( newChild ); 525 } 526 oldChildX = (NodeProxy)castOldChild( oldChild ); 527 528 synchronized (oldChild) { 531 if (newChild != null) { 532 synchronized (newChild) { 534 insertBefore( newChild, oldChild ); 537 removeChild( oldChild ); 538 } 539 } else { 540 removeChild( oldChild ); 543 } 544 } 545 return oldChild; 546 } 547 548 549 576 public synchronized final Node insertBefore( Node newChild, Node refChild ) throws DOMException { 577 NodeProxy newChildX; 579 NodeProxy refChildX; 580 581 if (isReadOnly()) { 584 throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR ); 585 } 586 if (!supportsChildern()) { 587 throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, 588 "No childern supported by this node type." ); 589 } 590 591 if (refChild == null) { 593 return appendChild( newChild ); 594 } 595 596 newChildX = (NodeProxy)castNewChild( newChild ); 599 refChildX = (NodeProxy)castOldChild( refChild ); 600 601 synchronized (newChild) { 604 synchronized (refChild) { 606 if (newChildX.getParentNode() != null) { 610 if (((NodeProxy)newChildX.getParentNode()).isReadOnly()) { 611 throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR ); 612 } 613 newChildX.getParentNode().removeChild( newChildX ); 614 } 615 616 if (newChildX instanceof DocumentFragment) { 619 NodeProxy nextChild; 620 621 newChildX = (NodeProxy)newChildX.getFirstChild(); 622 while (newChildX != null) { 623 nextChild = (NodeProxy)newChildX.getNextSibling(); 624 insertBefore( newChildX, refChild ); 625 newChildX = nextChild; 626 } 627 return newChild; 628 } 629 630 newChildX.setParentNode( this ); 635 newChildX.setOwnerDocument( _ownerDocument ); 636 637 if (_firstChild.equals( refChildX )) { 640 _firstChild = newChildX; 641 } 642 if (refChildX.getPreviousSibling() != null) { 645 newChildX.setPreviousSibling( refChildX.getPreviousSibling() ); 646 ((NodeProxy)refChildX.getPreviousSibling()).setNextSibling( newChildX ); 647 } 648 refChildX.setPreviousSibling( newChildX ); 650 newChildX.setNextSibling( refChildX ); 651 ++_childsCount; 653 } 654 } 655 return newChild; 656 } 657 658 659 684 protected Node castNewChild( Node newChild ) throws DOMException { 685 if (newChild == null) { 686 throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, "Child reference is null." ); 687 } 688 689 if (!(newChild instanceof Node)) { 693 throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, 694 "Child is not a compatible type for this node." ); 695 } 696 if (!(newChild instanceof Element || newChild instanceof CharacterData || newChild instanceof DocumentFragment 697 || newChild instanceof EntityReference || newChild instanceof ProcessingInstruction)) { 698 throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, 699 "Child is not a compatible type for this node." ); 700 } 701 return newChild; 702 } 703 704 705 714 protected final Node castOldChild( Node oldChild ) throws DOMException { 715 if (oldChild == null || !(oldChild instanceof NodeProxy) 716 || !((OzoneProxy)oldChild.getParentNode()).remoteID().equals( container().id() )) { 717 throw new DOMExceptionImpl( DOMException.NOT_FOUND_ERR, "Not a direct child of this node." ); 718 } 719 return oldChild; 720 } 721 722 723 747 public synchronized void cloneInto( NodeProxy into, boolean deep ) { 748 NodeProxy child; 749 750 757 into.setNodeName( _nodeName ); 759 String nodeValue = getNodeValue(); 760 if (nodeValue != null) { 761 into.setNodeValue( nodeValue ); 762 } 763 into.setOwnerDocument( _ownerDocument ); 764 if (deep) { 765 child = (NodeProxy)getFirstChild(); 766 while (child != null) { 767 into.appendChild( child.cloneNode( true ) ); 768 child = (NodeProxy)child.getNextSibling(); 769 } 770 } 771 } 772 773 774 public synchronized void setOwnerDocument( Document owner ) { 775 Node node; 776 777 if (owner == null) { 778 _ownerDocument = null; 779 } else { 780 if (!(owner instanceof DocumentProxy)) { 781 throw new IllegalArgumentException ( "Argument 'owner' not of compatible DOM class." ); 782 } 783 _ownerDocument = (DocumentProxy)owner; 784 } 785 node = getFirstChild(); 786 while (node != null) { 787 ((NodeProxy)node).setOwnerDocument( owner ); 788 node = node.getNextSibling(); 789 } 790 } 791 792 793 798 public synchronized final void setReadOnly() { 799 NodeProxy child; 800 801 _readOnly = true; 802 child = (NodeProxy)getFirstChild(); 805 while (child != null) { 806 child.setReadOnly(); 807 child = (NodeProxy)child.getNextSibling(); 808 } 809 } 810 811 812 819 public final boolean isReadOnly() { 820 return _readOnly; 821 } 822 823 824 831 boolean supportsChildern() { 832 return false; 833 } 834 835 836 844 public synchronized final Node getChild( int index ) { 845 NodeProxy node; 846 847 if (index < 0 || index > _childsCount) { 848 return null; 849 } 850 node = (NodeProxy)getFirstChild(); 851 while (node != null && index > 0) { 852 node = (NodeProxy)node.getNextSibling(); 853 --index; 854 } 855 return node; 856 } 857 858 859 866 public final int getChildCount() { 867 return _childsCount; 868 } 869 870 871 888 protected NodeImpl( DocumentImpl owner, String name, String value, boolean checkName ) throws DOMException{ 889 init( owner, name, value, checkName ); 890 } 891 892 893 protected NodeImpl() { 894 } 895 896 897 public final void init( DocumentProxy owner, String name, String value, boolean checkName ) throws DOMException { 898 char ch; 899 int i; 900 901 if (name == null) { 902 throw new NullPointerException ( "Argument 'name' is null." ); 903 } 904 905 _nodeName = name; 906 _ownerDocument = owner; 907 if (checkName && name.length() > 0) { 912 ch = name.charAt( 0 ); 913 if (!Character.isLetter( ch ) && ch != '_' && ch != ':') { 914 throw new DOMExceptionImpl( DOMException.INVALID_CHARACTER_ERR ); 915 } 916 for (i = 1; i < name.length(); ++i) { 917 ch = name.charAt( 1 ); 918 if (!Character.isLetterOrDigit( ch ) && ch != '_' && ch != ':' && ch != '-' && ch != '.') { 919 throw new DOMExceptionImpl( DOMException.INVALID_CHARACTER_ERR ); 920 } 921 } 922 } 923 if (value != null) { 924 setNodeValue( value ); 925 } 926 } 927 928 932 public final static short ELEMENT_DECL_NODE = 13; 933 934 935 939 public final static short ATTLIST_DECL_NODE = 14; 940 941 942 947 public final static short PARAM_ENTITY_NODE = 15; 948 949 950 955 NodeProxy _nextNode; 956 957 962 NodeProxy _prevNode; 963 964 968 NodeProxy _parent; 969 970 976 DocumentProxy _ownerDocument; 977 978 982 private String _nodeName; 983 984 988 private String _nodeValue; 989 990 994 private NodeProxy _firstChild; 995 996 1000 private NodeProxy _lastChild; 1001 1002 1006 private int _childsCount; 1007 1008 1011 private boolean _readOnly; 1012 1013 1017 private NodeIteratorListener[] _iterators; 1018 1019 1020 1022 public void writeExternal( ObjectOutput out ) throws IOException { 1023 out.writeObject( _nextNode ); 1024 out.writeObject( _prevNode ); 1025 out.writeObject( _parent ); 1026 out.writeObject( _ownerDocument ); 1027 1028 if (_nodeName != null) { 1030 out.writeByte( 1 ); 1031 out.writeUTF( _nodeName ); 1032 } else { 1033 out.writeByte( -1 ); 1034 } 1035 1036 if (_nodeValue != null) { 1038 out.writeByte( 1 ); 1039 out.writeUTF( _nodeValue ); 1040 } else { 1041 out.writeByte( -1 ); 1042 } 1043 1044 out.writeObject( _firstChild ); 1045 out.writeObject( _lastChild ); 1046 out.writeInt( _childsCount ); 1047 out.writeBoolean( _readOnly ); 1048 1049 if (_iterators != null) { 1050 int len = _iterators.length; 1051 out.writeInt( len ); 1052 for (int i = 0; i < len; i++) { 1053 out.writeObject( _iterators[i] ); 1054 } 1055 } else { 1056 out.writeInt( -1 ); 1057 } 1058 } 1059 1060 1061 1063 public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException { 1064 _nextNode = (NodeProxy)in.readObject(); 1065 _prevNode = (NodeProxy)in.readObject(); 1066 _parent = (NodeProxy)in.readObject(); 1067 _ownerDocument = (DocumentProxy)in.readObject(); 1068 1069 int n = in.readByte(); 1070 if (n > 0) { 1071 _nodeName = in.readUTF(); 1072 } else { 1073 _nodeName = null; 1074 } 1075 1076 n = in.readByte(); 1078 if (n > 0) { 1079 _nodeValue = in.readUTF(); 1080 } else { 1081 _nodeValue = null; 1082 } 1083 1084 _firstChild = (NodeProxy)in.readObject(); 1085 _lastChild = (NodeProxy)in.readObject(); 1086 _childsCount = in.readInt(); 1087 _readOnly = in.readBoolean(); 1088 1089 int len = in.readInt(); 1090 if (len > -1) { 1091 _iterators = new NodeIteratorListener[len]; 1092 for (int i = 0; i < len; i++) { 1093 _iterators[i] = (NodeIteratorListener)in.readObject(); 1094 } 1095 } else { 1096 _iterators = null; 1097 } 1098 } 1099 1100 public boolean isSupported(String s, String s1) { 1101 throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR, 1102 "Node.getLocalName(): ozone's persistent DOM doesn't support DOM level 2 yet." ); 1103 } 1104} 1105 | Popular Tags |