KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > html > parser > 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
21 package org.netbeans.editor.ext.html.parser;
22
23
24 import org.netbeans.editor.ext.html.*;
25 import java.util.*;
26 import javax.swing.text.*;
27 import org.netbeans.editor.ext.*;
28 import org.openide.ErrorManager;
29
30 /**This class is used during the analysis of the HTML code.
31  *
32  * It is an element of the dynamically created chain of other SyntaxElements.
33  * The access to it is done through the HTMLSyntaxSupport, which also takes
34  * care of dynamically extending it when needed.
35  *
36  * @author Petr Nejedly
37  * @author Marek.Fukala@Sun.com
38  * @version 1.0
39  */

40 public class SyntaxElement {
41     
42     public static final int TYPE_COMMENT = 0;
43     public static final int TYPE_DECLARATION = 1;
44     public static final int TYPE_ERROR = 2;
45     public static final int TYPE_TEXT = 3;
46     public static final int TYPE_TAG = 4;
47     public static final int TYPE_ENDTAG = 5;
48     public static final int TYPE_SCRIPT = 6;
49     
50     public static final String JavaDoc[] TYPE_NAMES =
51             new String JavaDoc[]{"comment","declaration","error","text","tag","endtag","script"};
52     
53     private SyntaxElement previous;
54     private SyntaxElement next;
55     private SyntaxParser parser;
56     
57     int offset;
58     int length;
59     int type;
60     
61     /** Creates new SyntaxElement */
62     public SyntaxElement( SyntaxParser parser, int from, int to, int type ) {
63         this.offset = from;
64         this.length = to-from;
65         this.type = type;
66         this.parser = parser;
67     }
68     
69     public int getElementOffset() {
70         return offset;
71     }
72     
73     public int getElementLength() {
74         return length;
75     }
76     
77     public int getType() {
78         return type;
79     }
80     
81     
82     public String JavaDoc getText() {
83         try {
84             return parser.getDocument().getText(getElementOffset(), getElementLength());
85         }catch(BadLocationException ble) {
86             ErrorManager.getDefault().notify(ErrorManager.WARNING, ble);
87         }
88         return null;
89     }
90     
91     public SyntaxElement getPrevious() throws BadLocationException {
92         if( previous == null ) {
93             previous = parser.getPreviousElement( offset );
94             if( previous != null ) previous.next = this;
95         }
96         return previous;
97     }
98
99     public SyntaxElement getNext() throws BadLocationException {
100         if( next == null ) {
101             next = parser.getNextElement( offset+length );
102             if( next != null ) next.previous = this;
103         }
104         return next;
105     }
106
107     
108     public String JavaDoc toString() {
109         String JavaDoc textContent = getType() == TYPE_TEXT ? getText() : "";
110         return "Element(" +TYPE_NAMES[type]+")[" + offset + "," + (offset+length-1) + "] \"" + textContent + ""; // NOI18N
111
}
112     
113     
114     /**
115      * Declaration models SGML declaration with emphasis on <!DOCTYPE
116      * declaration, as other declarations are not allowed inside HTML.
117      * It represents unknown/broken declaration or either public or system
118      * DOCTYPE declaration.
119      */

120     public static class Declaration extends SyntaxElement {
121         private String JavaDoc root;
122         private String JavaDoc publicID;
123         private String JavaDoc file;
124         
125         
126         /**
127          * Creates a model of SGML declaration with some properties of
128          * DOCTYPE declaration.
129          * @param doctypeRootElement the name of the root element for a DOCTYPE.
130          * Can be null to express that the declaration is not DOCTYPE
131          * declaration or is broken.
132          * @param doctypePI public identifier for this DOCTYPE, if available.
133          * null for system doctype or other/broken declaration.
134          * @param doctypeFile system identifier for this DOCTYPE, if available.
135          * null otherwise.
136          */

137         public Declaration( SyntaxParser parser, int from, int to,
138                 String JavaDoc doctypeRootElement,
139                 String JavaDoc doctypePI, String JavaDoc doctypeFile
140                 ) {
141             super( parser, from, to, TYPE_DECLARATION );
142             root = doctypeRootElement;
143             publicID = doctypePI;
144             file = doctypeFile;
145         }
146         
147         /**
148          * @return the name of the root element for a DOCTYPE declaration
149          * or null if the declatarion is not DOCTYPE or is broken.
150          */

151         public String JavaDoc getRootElement() {
152             return root;
153         }
154         
155         /**
156          * @return a public identifier of the PUBLIC DOCTYPE declaration
157          * or null for SYSTEM DOCTYPE and broken or other declaration.
158          */

159         public String JavaDoc getPublicIdentifier() {
160             return publicID;
161         }
162         
163         /**
164          * @return a system identifier of both PUBLIC and SYSTEM DOCTYPE
165          * declaration or null for PUBLIC declaration with system identifier
166          * not specified and broken or other declaration.
167          */

168         public String JavaDoc getDoctypeFile() {
169             return file;
170         }
171         
172     }
173     
174     public static class Named extends SyntaxElement {
175         String JavaDoc name;
176         
177         public Named( SyntaxParser parser, int from, int to, int type, String JavaDoc name ) {
178             super( parser, from, to, type );
179             this.name = name;
180         }
181         
182         public String JavaDoc getName() {
183             return name;
184         }
185         public String JavaDoc toString() {
186             return super.toString() + " - \"" + name + '"'; // NOI18N
187
}
188     }
189     
190     
191     public static class Tag extends org.netbeans.editor.ext.html.parser.SyntaxElement.Named {
192         private List<TagAttribute> attribs;
193         private boolean empty = false;
194         
195         public Tag( SyntaxParser parser, int from, int to, String JavaDoc name, List<TagAttribute> attribs) {
196             this(parser, from, to, name, attribs, false);
197         }
198         
199         public Tag( SyntaxParser parser, int from, int to, String JavaDoc name, List attribs, boolean isEmpty ) {
200             super( parser, from, to, TYPE_TAG, name );
201             this.attribs = attribs;
202             this.empty = isEmpty;
203         }
204         
205         public boolean isEmpty() {
206             return empty;
207         }
208         
209         public List<TagAttribute> getAttributes() {
210             return attribs;
211         }
212         
213         public String JavaDoc toString() {
214             StringBuffer JavaDoc ret = new StringBuffer JavaDoc( super.toString() );
215             ret.append( " - {" ); // NOI18N
216

217             for( Iterator i = attribs.iterator(); i.hasNext(); ) {
218                 ret.append( i.next() );
219                 ret.append( ", " ); // NOI18N
220
}
221             
222             ret.append( "}" ); //NOI18N
223
if(isEmpty()) ret.append(" (EMPTY TAG)");
224             
225             return ret.toString();
226         }
227     }
228     
229     public static class TagAttribute {
230         
231         private String JavaDoc name, value;
232         private int nameOffset, valueOffset;
233         
234         TagAttribute(String JavaDoc name, String JavaDoc value, int nameOffset, int valueOffset) {
235             this.name = name;
236             this.value = value;
237             this.nameOffset = nameOffset;
238             this.valueOffset = valueOffset;
239         }
240         
241         public String JavaDoc getName() {
242             return name;
243         }
244         
245         void setName(String JavaDoc name) {
246             this.name = name;
247         }
248         
249         public String JavaDoc getValue() {
250             return value;
251         }
252         
253         void setValue(String JavaDoc value) {
254             this.value = value;
255         }
256         
257         public int getNameOffset() {
258             return nameOffset;
259         }
260         
261         void setNameOffset(int ofs) {
262             this.nameOffset = ofs;
263         }
264         
265         public int getValueOffset() {
266             return valueOffset;
267         }
268         
269         void setValueOffset(int ofs) {
270             this.valueOffset = ofs;
271         }
272         
273         public String JavaDoc toString() {
274             return "TagAttribute[name=" + getName() + "; value=" + getValue() + "; nameOffset=" + getNameOffset() + "; valueOffset=" + getValueOffset() +"]";
275         }
276        
277         //backward compatibility
278
public boolean equals(Object JavaDoc o) {
279             if (!(o instanceof TagAttribute)) {
280                 return false;
281             } else {
282                 return getName().equals(((TagAttribute)o).getName());
283             }
284         }
285     }
286 }
287
Popular Tags