Skip to main content
summaryrefslogtreecommitdiffstats
blob: 660f13c7951f88d45088958b16b80056c74e2ef9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
package org.eclipse.jdt.internal.core.jdom;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import java.util.Enumeration;

import org.eclipse.jdt.core.jdom.DOMException;
import org.eclipse.jdt.core.jdom.DOMFactory;
import org.eclipse.jdt.core.jdom.IDOMCompilationUnit;
import org.eclipse.jdt.core.jdom.IDOMFactory;
import org.eclipse.jdt.core.jdom.IDOMMethod;
import org.eclipse.jdt.core.jdom.IDOMNode;
import org.eclipse.jdt.internal.core.Util;
import org.eclipse.jdt.internal.core.util.CharArrayBuffer;

/**
 * DOMNode provides an implementation for <code>IDOMNode</code>.
 *
 * <p>A node represents a document fragment. When a node is created, its
 * contents are located in a contiguous range of a shared document. A shared
 * document is a char array, and is shared in the sense that the contents of other
 * document fragments may also be contained in the array.
 * 
 * <p>A node maintains indicies of relevant portions of its contents
 * in the shared document. Thus the original document and indicies create a
 * form from which to generate the contents of the document fragment. As attributes
 * of a node are changed, the node attempts to maintain the original formatting
 * by only replacing relevant portions of the shared document with the value
 * of new attributes (i.e. filling in the form with replacement values).
 *
 * <p>When a node is first created, it is considered unfragmented. When any
 * attribute of the node is altered, the node is then considered fragmented
 * from that point on. A node is also considered fragmented if any of its
 * descendants are fragmented. When a node is unfragmented, the contents of the
 * node can be efficiently generated from the original shared document. When
 * a node is fragmented, the contents of the node must be created using the
 * original document and indicies as a form, filling in replacement values
 * as required.
 *
 * <p>Generally, a node's contents consists of complete lines in a shared document.
 * The contents of the node are normalized on creation to include any whitespace
 * preceding the node on the line where the node begins, and to include and trailing
 * whitespace up to the line where the next node begins. Any trailing // comments
 * that begin on the line where the current node ends, are considered part of that
 * node. 
 *
 * @see IDOMNode
 */
public abstract class DOMNode implements IDOMNode {

	/**
	 * The first child of this node - <code>null</code>
	 * when this node has no children. (Children of a node
	 * are implemented as a doubly linked list).
	 */
	protected DOMNode fFirstChild= null;

	/**
	 * The last child of this node - <code>null</code>
	 * when this node has no children. Used for efficient
	 * access to the last child when adding new children
	 * at the end of the linked list of children.
	 */
	protected DOMNode fLastChild= null;

	/**
	 * The sibling node following this node - <code>null</code>
	 * for the last node in the sibling list.
	 */
	protected DOMNode fNextNode= null;

	/**
	 * The parent of this node. A <code>null</code>
	 * parent indicates that this node is a root
	 * node of a document fragment.
	 */
	protected DOMNode fParent= null;

	/**
	 * The sibling node preceding this node - <code>null</code>
	 * for the first node in the sibling list.
	 */
	protected DOMNode fPreviousNode= null;

	/**
	 * True when this node has attributes that have
	 * been altered from their original state in the
	 * shared document, or when the attributes of a
	 * descendant have been altered. False when the
	 * contents of this node and all descendants are
	 * consistent with the content of the shared 
	 * document.
	 */
	protected boolean fIsFragmented= false;

	/**
	 * The name of this node. For efficiency, the
	 * name of a node is duplicated in this variable
	 * on creation, rather than always having to fetch
	 * the name from the shared document.
	 */
	protected String  fName= null;

	/**
	 * The original inclusive indicies of this node's name in
	 * the shared document. Values of -1 indiciate the name
	 * does not exist in the document.
	 */
	protected int[]	  fNameRange;

	/**
	 * The shared document that the contents for this node
	 * are contained in. Attribute indicies are positions
	 * in this character array.
	 */
	protected char[]  fDocument= null;

	/**
	 * The original entire inclusive range of this node's contents
	 * within its document. Values of -1 indicate the contents
	 * of this node do not exist in the document.
	 */
	protected int[] fSourceRange;

	/**
	 * The current state of bit masks defined by this node.
	 * Initially all bit flags are turned off. All bit masks
	 * are defined by this class to avoid overlap, although
	 * bit masks are node type specific.
	 *
	 * @see #setMask
	 * @see #getMask
	 */
	protected int fStateMask= 0;

	/**
	 * A bit mask indicating this field has an initializer
	 * expression
	 */
	protected static final int MASK_FIELD_HAS_INITIALIZER= 0x00000001;
	
	/**
	 * A bit mask indicating this field is a secondary variable
	 * declarator for a previous field declaration.
	 */
	protected static final int MASK_FIELD_IS_VARIABLE_DECLARATOR= 0x00000002;

	/**
	 * A bit mask indicating this field's type has been
	 * altered from its original contents in the document.
	 */
	protected static final int MASK_FIELD_TYPE_ALTERED= 0x00000004;

	/**
	 * A bit mask indicating this node's name has been
	 * altered from its original contents in the document.
	 */
	protected static final int MASK_NAME_ALTERED= 0x00000008;

	/**
	 * A bit mask indicating this node currently has a
	 * body.
	 */
	protected static final int MASK_HAS_BODY= 0x00000010;
	
	/**
	 * A bit mask indicating this node currently has a
	 * preceding comment.
	 */
	protected static final int MASK_HAS_COMMENT= 0x00000020;

	/**
	 * A bit mask indicating this method is a constructor.
	 */
	protected static final int MASK_IS_CONSTRUCTOR= 0x00000040;

	/**
	 * A bit mask indicating this type is a class.
	 */
	protected static final int MASK_TYPE_IS_CLASS= 0x00000080;

	/**
	 * A bit mask indicating this type has a superclass
	 * (requires or has an 'extends' clause).
	 */
	protected static final int MASK_TYPE_HAS_SUPERCLASS= 0x00000100;

	/**
	 * A bit mask indicating this type implements
	 * or extends some interfaces
	 */
	protected static final int MASK_TYPE_HAS_INTERFACES= 0x00000200;

	/**
	 * A bit mask indicating this return type of this method has
	 * been altered from the original contents.
	 */
	protected static final int MASK_RETURN_TYPE_ALTERED= 0x00000400;

	/**
	 * A bit mask indicating this node has detailed source indexes
	 */
	protected static final int MASK_DETAILED_SOURCE_INDEXES = 0x00000800;

/**
 * Creates a new empty document fragment.
 */
DOMNode() {
	fName= null;
	fDocument= null;
	fSourceRange= new int[]{-1, -1};
	fNameRange= new int[]{-1, -1};
	fragment();
}
/**
 * Creates a new document fragment on the given range of the document.
 *
 * @param document - the document containing this node's original contents
 * @param sourceRange - a two element array of integers describing the
 *		entire inclusive source range of this node within its document.
 * 		Contents start on and include the character at the first position.
 *		Contents end on and include the character at the last position.
 *		An array of -1's indicates this node's contents do not exist
 *		in the document.
 * @param name - the identifier portion of the name of this node, or
 *		<code>null</code> if this node does not have a name
 * @param nameRange - a two element array of integers describing the
 *		entire inclusive source range of this node's name within its document,
 *		including any array qualifiers that might immediately follow the name
 *		or -1's if this node does not have a name.
 */
DOMNode(char[] document, int[] sourceRange, String name, int[] nameRange) {
	super();
	fDocument= document;
	fSourceRange= sourceRange;
	fName= name;
	fNameRange= nameRange;

}
/**
 * Adds the given un-parented node (document fragment) as the last child of
 * this node.
 *
 * <p>When a child is added, this node must be considered fragmented such that
 * the contents of this node are properly generated.
 * 
 * @see IDOMNode#addChild(IDOMNode)
 */
public void addChild(IDOMNode child) throws IllegalArgumentException, DOMException {
	basicAddChild(child);
	
	// if the node is a constructor, it must also be fragmented to update the constructor's name
	if (child.getNodeType() == IDOMNode.METHOD && ((IDOMMethod)child).isConstructor()) {
		((DOMNode)child).fragment();
	} else {
		fragment();
	}
}
/**
 * Appends the current contents of this document fragment
 * to the given <code>CharArrayBuffer</code>.
 *
 * <p>If this node is fragmented, contents must be generated by
 * using the original document and indicies as a form for the current
 * attribute values of this node. If this node not fragmented, the
 * contents can be obtained from the document.
 * 
 */
protected void appendContents(CharArrayBuffer buffer) {
	if (isFragmented()) {
		appendFragmentedContents(buffer);
	} else {
		buffer.append(fDocument, fSourceRange[0], fSourceRange[1] + 1 - fSourceRange[0]);
	}
}
/**
 * Appends the contents of all children of this node to the
 * given <code>CharArrayBuffer</code>.
 *
 * <p>This algorithm used minimizes String generation by merging
 * adjacent unfragmented children into one substring operation.
 *
 */
protected void appendContentsOfChildren(CharArrayBuffer buffer) {
	DOMNode child= fFirstChild;
	DOMNode sibling;
	
	int start= 0, end= 0;
	if (child != null) {
		start= child.getStartPosition();
		end= child.getEndPosition();
	}
	while (child != null) {
		sibling= child.fNextNode;
		if (sibling != null) {
			if (sibling.isContentMergableWith(child)) {
				end= sibling.getEndPosition();
			} else {
				if (child.isFragmented()) {
					child.appendContents(buffer);
				} else {
					buffer.append(child.getDocument(), start, end + 1 - start);
				}
				start= sibling.getStartPosition();
				end= sibling.getEndPosition();
			}
		} else {
			if (child.isFragmented()) {
				child.appendContents(buffer);
			} else {
				buffer.append(child.getDocument(), start, end + 1 - start);
			}
		}
		child= sibling;
	}
}
/**
 * Appends the contents of this node to the given <code>CharArrayBufer</code>, using
 * the original document and indicies as a form for the current attribute
 * values of this node.
 */
protected abstract void appendFragmentedContents(CharArrayBuffer buffer);
/**
 * Adds the given un-parented node (document fragment) as the last child of
 * this node without setting this node's 'fragmented' flag. This
 * method is only used by the <code>DOMBuilder</code> when creating a new DOM such
 * that a new DOM is unfragmented.
 */
void basicAddChild(IDOMNode child) throws IllegalArgumentException, DOMException {
	// verify child may be added
	if (!canHaveChildren()) {
		throw new DOMException(Util.bind("dom.unableAddChild"/*nonNLS*/));
	}
	if (child == null) {
		throw new IllegalArgumentException(Util.bind("dom.addNullChild"/*nonNLS*/));
	}
	if (!isAllowableChild(child)) {
		throw new DOMException(Util.bind("dom.addIncompatibleChild"/*nonNLS*/));
	}
	if (child.getParent() != null) {
		throw new DOMException(Util.bind("dom.addChildWithParent"/*nonNLS*/));
	}
	/* NOTE: To test if the child is an ancestor of this node, we
	 * need only test if the root of this node is the child (the child
	 * is already a root since we have just guarenteed it has no parent).
	 */
	if (child == getRoot()) {
		throw new DOMException(Util.bind("dom.addAncestorAsChild"/*nonNLS*/));
	}

	DOMNode node= (DOMNode)child;
	
	// if the child is not already part of this document, localize its contents
	// before adding it to the tree
	if (node.getDocument() != getDocument()) {
		node.localizeContents();
	}
	
	// add the child last
	if (fFirstChild == null) {
		// this is the first and only child
		fFirstChild= node;
	} else {
		fLastChild.fNextNode= node;
		node.fPreviousNode= fLastChild;
	}
	fLastChild= node;
	node.fParent= this;
}
/**
 * Generates detailed source indexes for this node if possible.
 *
 * @exception DOMException if unable to generate detailed source indexes
 * 	for this node
 */
protected void becomeDetailed() throws DOMException {
	if (!isDetailed()) {
		DOMNode detailed= getDetailedNode();
		if (detailed == null) {
			throw new DOMException(Util.bind("dom.cannotDetail"/*nonNLS*/));
		}
		if (detailed != this) {
			shareContents(detailed);
		}
	}
}
/**
 * Returns true if this node is allowed to have children, otherwise false.
 *
 * <p>Default implementation of <code>IDOMNode</code> interface method returns false; this
 * method must be overridden by subclasses that implement nodes that allow
 * children.
 *
 * @see IDOMNode#canHaveChildren()
 */
public boolean canHaveChildren() {
	return false;
}
/**
 * @see IDOMNode#clone()
 */
public Object clone() {

	// create a new buffer with all my contents and children contents
	int length= 0;
	char[] buffer= null;
	int offset= fSourceRange[0];
	
	if (offset >= 0) {
		length= fSourceRange[1] - offset + 1;
		buffer= new char[length];
		System.arraycopy(fDocument, offset, buffer, 0, length);
	}	
	DOMNode clone= newDOMNode();
	clone.shareContents(this);
	clone.fDocument = buffer;

	if (offset > 0) {
		clone.offset(0 - offset);
	}

	// clone my children
	if (canHaveChildren()) {
		Enumeration children= getChildren();
		while (children.hasMoreElements()) {
			DOMNode child= (DOMNode)children.nextElement();
			if (child.fDocument == fDocument) {
				DOMNode childClone= child.cloneSharingDocument(buffer, offset);
				clone.basicAddChild(childClone);
			} else {
				DOMNode childClone= (DOMNode)child.clone();
				clone.addChild(childClone);
			}
			
		}
	}
	
	return clone;
}
private DOMNode cloneSharingDocument(char[] document, int rootOffset) {

	int length = 0;
	int offset = fSourceRange[0];
		
	DOMNode clone = newDOMNode();
	clone.shareContents(this);
	clone.fDocument = document;
	if (rootOffset > 0) {
		clone.offset(0 - rootOffset);
	}
	
	if (canHaveChildren()) {
		Enumeration children = getChildren();
		while (children.hasMoreElements()) {
			DOMNode child = (DOMNode) children.nextElement();
			if (child.fDocument == fDocument) {
				DOMNode childClone= child.cloneSharingDocument(document, rootOffset);
				clone.basicAddChild(childClone);
			} else {
				DOMNode childClone= (DOMNode)child.clone();
				clone.addChild(childClone);
			}
		}
	}
	return clone;
}
/**
 * Sets this node's fragmented flag and all ancestor fragmented flags
 * to <code>true<code>. This happens when an attribute of this node or a descendant
 * node has been altered. When a node is fragmented, its contents must
 * be generated from its attributes and original "form" rather than
 * from the original contents in the document.
 */
protected void fragment() {
	if (!isFragmented()) {
		fIsFragmented= true;
		if (fParent != null) {
			fParent.fragment();
		}
	}
}
/**
 * @see IDOMNode#getCharacters()
 */
public char[] getCharacters() {
	CharArrayBuffer buffer= new CharArrayBuffer();
	appendContents(buffer);
	return buffer.getContents();
}
/**
 * @see IDOMNode#getChild(String)
 */
public IDOMNode getChild(String name) {
	DOMNode child= fFirstChild;
	while (child != null) {
		String n= child.getName();
		if (n != null && n.equals(name)) {
			return child;
		}
		child= child.fNextNode;
	}
	return null;
}
/**
 * @see IDOMNode#getChildren()
 */
public Enumeration getChildren() {
	return new SiblingEnumeration(fFirstChild);
}
/**
 * Returns the current contents of this document fragment,
 * or <code>null</code> if this node has no contents.
 *
 * <p>If this node is fragmented, contents must be generated by
 * using the original document and indicies as a form for the current
 * attribute values of this node. If this node not fragmented, the
 * contents can be obtained from the document.
 * 
 * @see IDOMNode#getContents()
 */
public String getContents() {
	CharArrayBuffer buffer= new CharArrayBuffer();
	appendContents(buffer);
	return buffer.toString();
}
/**
 * Returns a new document fragment representing this node with
 * detailed source indexes. Subclasses that provide a detailed
 * implementation must override this method.
 */
protected DOMNode getDetailedNode() {
	return this;
}
/**
 * Returns the document containing this node's original contents.
 * The document may be shared by other nodes.
 */
protected char[] getDocument() {
	return fDocument;
}
/**
 * Returns the original position of the last character of this
 * node's contents in its document.
 */
public int getEndPosition() {
	return fSourceRange[1];
}
/**
 * Returns a factory with which to create new document fragments.
 */
protected IDOMFactory getFactory() {
	return new DOMFactory();
}
/**
 * @see IDOMNode#getFirstChild()
 */
public IDOMNode getFirstChild() {
	return fFirstChild;
}
/**
 * Returns the position at which the first child of this node should be inserted.
 */
public int getInsertionPosition() {
	return getEndPosition();
}
/**
 * Returns <code>true</code> if the given mask of this node's state flag
 * is turned on, otherwise <code>false</code>.
 */
protected boolean getMask(int mask) {
	return (fStateMask & mask) > 0;
}
/**
 * @see IDOMNode#getName()
 */
public String getName() {
	return fName;
}
/**
 * Returns the source code to be used for this node's name.
 */
protected char[] getNameContents() {
	if (isNameAltered()) {
		return fName.toCharArray();
	} else {
		if (fName == null || fNameRange[0] < 0) {
			return null;
		} else {
			int length = fNameRange[1] + 1 - fNameRange[0];
			char[] result = new char[length];
			System.arraycopy(fDocument, fNameRange[0], result, 0, length);
			return result;
		}
	}
}
/**
 * @see IDOMNode#getNextNode()
 */
public IDOMNode getNextNode() {
	return fNextNode;
}
/**
 * @see IDOMNode#getParent()
 */
public IDOMNode getParent() {
	return fParent;
}
/**
 * Answers a source position which corresponds to the end of the parent
 * element's declaration.
 */
protected int getParentEndDeclaration() {
	IDOMNode parent = getParent();
	if (parent == null) {
		return 0;
	} else {
		if (parent instanceof IDOMCompilationUnit) {
			return 0;
		} else {
			return ((DOMType)parent).getOpenBodyEnd();
		}
	}
}
/**
 * @see IDOMNode#getPreviousNode()
 */
public IDOMNode getPreviousNode() {
	return fPreviousNode;
}
/**
 * Returns the root node of this document fragment.
 */
protected IDOMNode getRoot() {
	if (fParent == null) {
		return this;
	} else {
		return fParent.getRoot();
	}
}
/**
 * Returns the original position of the first character of this
 * node's contents in its document.
 */
public int getStartPosition() {
	return fSourceRange[0];
}
/**
 * @see IDOMNode#insertSibling(IDOMNode)
 */
public void insertSibling(IDOMNode sibling) throws IllegalArgumentException, DOMException {
	// verify sibling may be added
	if (sibling == null) {
		throw new IllegalArgumentException(Util.bind("dom.addNullSibling"/*nonNLS*/));
	}
	if (fParent == null) {
		throw new DOMException(Util.bind("dom.addSiblingBeforeRoot"/*nonNLS*/));
	}
	if (!fParent.isAllowableChild(sibling)) {
		throw new DOMException(Util.bind("dom.addIncompatibleSibling"/*nonNLS*/));
	}
	if (sibling.getParent() != null) {
		throw new DOMException(Util.bind("dom.addSiblingWithParent"/*nonNLS*/));
	}
	/* NOTE: To test if the sibling is an ancestor of this node, we
	 * need only test if the root of this node is the child (the sibling
	 * is already a root since we have just guaranteed it has no parent).
	 */
	if (sibling == getRoot()) {
		throw new DOMException(Util.bind("dom.addAncestorAsSibling"/*nonNLS*/));
	}

	DOMNode node= (DOMNode)sibling;
	
	// if the sibling is not already part of this document, localize its contents
	// before inserting it into the tree
	if (node.getDocument() != getDocument()) {
		node.localizeContents();
	}

	// insert the node
	if (fPreviousNode == null) {
		fParent.fFirstChild= node;
	} else {
		fPreviousNode.fNextNode= node;	
	}
	node.fParent= fParent;
	node.fPreviousNode= fPreviousNode;
	node.fNextNode= this;
	fPreviousNode= node;

	// if the node is a constructor, it must also be fragmented to update the constructor's name
	if (node.getNodeType() == IDOMNode.METHOD && ((IDOMMethod)node).isConstructor()) {
		node.fragment();
	} else {
		fParent.fragment();
	}
}
/**
 * @see IDOMNode
 */
public boolean isAllowableChild(IDOMNode node) {
	return false;
}
/**
 * Returns <code>true</code> if the contents of this node are from the same document as
 * the given node, the contents of this node immediately follow the contents
 * of the given node, and neither this node or the given node are fragmented -
 * otherwise <code>false</code>.
 */
protected boolean isContentMergableWith(DOMNode node) {
	return !node.isFragmented() && !isFragmented() && node.getDocument() == getDocument() &&
		node.getEndPosition() + 1 == getStartPosition();
}
/**
 * Returns <code>true</code> if this node has detailed source index information,
 * or <code>false</code> if this node has limited source index information. To
 * perform some manipulations, detailed indexes are required.
 */
protected boolean isDetailed() {
	return getMask(MASK_DETAILED_SOURCE_INDEXES);
}
/**
 * Returns <code>true</code> if this node's or a descendant node's contents 
 * have been altered since this node was created. This indicates
 * that the contents of this node are no longer consistent with
 * the contents of this node's document.
 */
protected boolean isFragmented() {
	return fIsFragmented;
}
/**
 * Returns <code>true</code> if this noed's name has been altered
 * from the original document contents.
 */
protected boolean isNameAltered() {
	return getMask(MASK_NAME_ALTERED);
}
/**
 * @see IDOMNode#isSignatureEqual(IDOMNode).
 *
 * <p>By default, the signatures of two nodes are equal if their
 * type and names are equal. Node types that have other requirements
 * for equality must override this method.
 */
public boolean isSignatureEqual(IDOMNode node) {
	return getNodeType() == node.getNodeType() && getName().equals(node.getName());
}
/**
 * Localizes the contents of this node and all descendant nodes,
 * such that this node is no longer dependent on its original
 * document in order to generate its contents. This node and all
 * descendant nodes become unfragmented and share a new
 * document.
 */
protected void localizeContents() {

	DOMNode clone= (DOMNode)clone();
	shareContents(clone);

}
/**
 * Returns a new empty <code>DOMNode</code> for this instance.
 */
protected abstract DOMNode newDOMNode();
/**
 * Normalizes this <code>DOMNode</code>'s source positions to include whitespace preceeding
 * the node on the line on which the node starts, and all whitespace after the node up to
 * the next node's start
 */
void normalize(ILineStartFinder finder) {
	if (getPreviousNode() == null)
		normalizeStartPosition(getParentEndDeclaration(), finder);

	// Set the children's position
	if (canHaveChildren()) {
		Enumeration children = getChildren();
		while(children.hasMoreElements())
			((DOMNode)children.nextElement()).normalize(finder);
	}

	normalizeEndPosition(finder, (DOMNode)getNextNode());
}
/**
 * Normalizes this <code>DOMNode</code>'s end position.
 */
void normalizeEndPosition(ILineStartFinder finder, DOMNode next) {
	if (next == null) {
		// this node's end position includes all of the characters up
		// to the end of the enclosing node
		DOMNode parent = (DOMNode) getParent();
		if (parent == null || parent instanceof DOMCompilationUnit) {
			setSourceRangeEnd(fDocument.length - 1);
		} else {
			// parent is a type
			setSourceRangeEnd(((DOMType)parent).getCloseBodyPosition() - 1);
		}
	} else {
		// this node's end position is just before the start of the next node
		next.normalizeStartPosition(getEndPosition(), finder);
		setSourceRangeEnd(next.getStartPosition() - 1);
	}
}
/**
 * Normalizes this <code>DOMNode</code>'s start position.
 */
void normalizeStartPosition(int previousEnd, ILineStartFinder finder) {
	int nodeStart = getStartPosition();
	int lineStart = finder.getLineStart(nodeStart);
	if (nodeStart > lineStart && (lineStart > previousEnd || (previousEnd == 0 && lineStart == 0)))
		setStartPosition(lineStart);			
}
/**
 * Offsets all the source indexes in this node by the given amount.
 */
protected void offset(int offset) {
	offsetRange(fNameRange, offset);
	offsetRange(fSourceRange, offset);
}
/**
 * Offsets the source range by the given amount
 */
protected void offsetRange(int[] range, int offset) {
	for (int i= 0; i < range.length; i++) {
		range[i]+=offset;
		if (range[i] < 0) {
			range[i]= -1;
		}
	}
}
/**
 * Returns a copy of the given range.
 */
protected int[] rangeCopy(int[] range) {
	int[] copy= new int[range.length];
	for (int i= 0; i < range.length; i++) {
		copy[i]= range[i];
	}
	return copy;
}
/**
 * Separates this node from its parent and siblings, maintaining any ties that
 * this node has to the underlying document fragment.
 *
 * <p>When a child is removed, its parent is fragmented such that it properly
 * generates its contents.
 *
 * @see IDOMNode#remove()
 */
public void remove() {

	if (fParent != null) {
		fParent.fragment();
	}
	
	// link siblings
	if (fNextNode != null) {
		fNextNode.fPreviousNode= fPreviousNode;
	}
	if (fPreviousNode != null) {
		fPreviousNode.fNextNode= fNextNode;
	}
	// fix parent's pointers
	if (fParent != null) {
		if (fParent.fFirstChild == this) {
			fParent.fFirstChild= fNextNode;
		}
		if (fParent.fLastChild == this) {
			fParent.fLastChild= fPreviousNode;
		}
	}
	// remove myself
	fParent= null;
	fNextNode= null;
	fPreviousNode= null;
}
/**
 * Sets the specified mask of this node's state mask on or off
 * based on the boolean value - true -> on, false -> off.
 */
protected void setMask(int mask, boolean on) {
	if (on) {
		fStateMask |= mask;
	} else {
		fStateMask &= ~mask;
	}
}
/**
 * @see IDOMNode#setName
 */
public void setName(String name) {
	fName= name;
	setNameAltered(true);
	fragment();
}
/**
 * Sets the state of this node as having
 * its name attribute altered from the original
 * document contents.
 */
protected void setNameAltered(boolean altered) {
	setMask(MASK_NAME_ALTERED, altered);
}
/**
 * Sets the original position of the last character of this node's contents
 * in its document. This method is only used during DOM creation while
 * normalizing the source range of each node.
 */
protected void setSourceRangeEnd(int end) {
	fSourceRange[1]= end;
}
/**
 * Sets the original position of the first character of this node's contents
 * in its document. This method is only used during DOM creation while
 * normalizing the source range of each node.
 */
protected void setStartPosition(int start) {
	fSourceRange[0]= start;
}
/**
 * Sets the contents of this node and descendant nodes to be the
 * (identical) contents of the given node and its descendants. This
 * does not effect this node's parent and sibling configuration,
 * only the contents of this node. This is used only to localize
 * the contents of this node.
 */
protected void shareContents(DOMNode node) {
	fDocument= node.fDocument;
	fIsFragmented= node.fIsFragmented;
	fName= node.fName;
	fNameRange= rangeCopy(node.fNameRange);
	fSourceRange= rangeCopy(node.fSourceRange);
	fStateMask= node.fStateMask;

	
	if (canHaveChildren()) {
		Enumeration myChildren= getChildren();
		Enumeration otherChildren= node.getChildren();
		DOMNode myChild, otherChild;
		while (myChildren.hasMoreElements()) {
			myChild= (DOMNode)myChildren.nextElement();
			otherChild= (DOMNode)otherChildren.nextElement();
			myChild.shareContents(otherChild);
		}
	}
}
/**
 * Returns a <code>String</code> representing this node - for Debug purposes only.
 */
public abstract String toString();
}

Back to the top