KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > syntax > SyntaxElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.text.syntax;
21
22 import java.util.*;
23
24 import org.w3c.dom.*;
25
26 import javax.swing.text.*;
27
28 import org.netbeans.editor.ext.*;
29 import org.netbeans.editor.*;
30 import org.openide.ErrorManager;
31
32 /**
33  *
34  * Instances are produced by {@link XMLSyntaxSupport}.
35  * <p>
36  * <b>Warning:</b> class is public only for private purposes!
37  *
38  * @author Petr Nejedly - original HTML design
39  * @author Sandeep Randhawa - XML port
40  * @author Petr Kuzel - DOM Nodes
41  *
42  * @version 1.0
43  */

44 public abstract class SyntaxElement {
45
46 // to do do not handle prolog as text!
47
// support PIs
48

49     protected XMLSyntaxSupport support; // it produced us
50
protected TokenItem first; // cached first token chain item
51

52     private SyntaxElement previous; // cached previous element
53
private SyntaxElement next; // cached next element
54

55     // let it be visible by static inner classes extending us
56
protected int offset; // original position in document //??? use item instead
57
protected int length; // original lenght in document
58

59
60     /** Creates new SyntaxElement */
61     public SyntaxElement(XMLSyntaxSupport support, TokenItem first, int to) {
62         
63         this.support = support;
64         this.first = first;
65         this.offset = first.getOffset();
66         this.length = to-offset;
67     }
68
69     public int getElementOffset() {
70
71         return offset;
72     }
73
74     public int getElementLength() {
75         return length;
76     }
77
78     /**
79      * Get previous SyntaxElement. Cache results.
80      * @return previous SyntaxElement or <code>null</code> at document begining
81      * or illegal location.
82      */

83     public SyntaxElement getPrevious() {
84         try {
85             if( previous == null ) {
86                 if (first.getOffset() == 0) return null;
87                 previous = support.getElementChain( getElementOffset() - 1 );
88                 if( previous != null ) {
89                     previous.next = this;
90                     if (previous.first.getOffset() == first.getOffset()) {
91                         Exception JavaDoc ex = new IllegalStateException JavaDoc("Previous cannot be the same as current element at offset " + first.getOffset() + " " + first.getImage());
92                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
93                         return null;
94                     }
95                 }
96             }
97             return previous;
98         } catch (BadLocationException ex) {
99             return null;
100         }
101     }
102
103     /**
104      * Get next SyntaxElement. Cache results.
105      * @return next SyntaxElement or <code>null</code> at document end
106      * or illegal location.
107      */

108     public SyntaxElement getNext() {
109         try {
110             if( next == null ) {
111                 next = support.getElementChain( offset+length + 1 );
112                 if( next != null ) {
113                     next.previous = this;
114                     if (next.first.getOffset() == first.getOffset()) {
115
116                         // TODO see #43297 for causes and try to relax them
117

118 // Exception ex = new IllegalStateException("Next cannot be the same as current element at offset " + first.getOffset() + " " + first.getImage());
119
// ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
120
return null;
121                     }
122                 }
123             }
124             return next;
125         } catch (BadLocationException ex) {
126             return null;
127         }
128     }
129
130     /**
131      * Print element content for debug purposes.
132      */

133     public String JavaDoc toString() {
134         String JavaDoc content = "?";
135         try {
136             content = support.getDocument().getText(offset, length);
137         }catch(BadLocationException e) {}
138         return "SyntaxElement [offset=" + offset + "; length=" + length + " ;type = " + this.getClass().getName() + "; content:" + content +"]";
139     }
140     
141     /**
142      *
143      */

144     public int hashCode() {
145         return super.hashCode() ^ offset ^ length;
146     }
147
148     /**
149      * DOM Node equals. It's not compatible with Object's equals specs!
150      */

151     public boolean equals(Object JavaDoc obj) {
152         if (obj instanceof SyntaxElement) {
153             if (((SyntaxElement)obj).offset == offset) return true;
154         }
155         return false;
156     }
157     
158         
159     // Particular non-DOM syntax elements ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160

161     /**
162      * It may stop some DOM traversing. //!!!
163      */

164     public static class Error extends SyntaxElement {
165         
166         public Error( XMLSyntaxSupport support, TokenItem from, int to ) {
167             super( support, from, to );
168         }
169
170         public String JavaDoc toString() {
171             return "Error" + super.toString(); // NOI18N
172
}
173     }
174
175 }
176
Popular Tags