001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor.ext.html;
015:
016: import java.util.Collection;
017: import java.util.ConcurrentModificationException;
018: import java.util.Iterator;
019:
020: import javax.swing.text.BadLocationException;
021:
022: /**
023: * SyntaxElement is class internal to HTML Completion engine, it is used during
024: * the analysis of the HTML code.
025: *
026: * It is an element of the dynamically created chain of other SyntaxElements.
027: * The access to it is done through the HTMLSyntaxSupport, which also takes care
028: * of dynamically extending it when needed.
029: *
030: * @author Petr Nejedly
031: * @version 1.0
032: */
033: class SyntaxElement {
034:
035: public static final int TYPE_COMMENT = 0;
036: public static final int TYPE_DECLARATION = 1;
037: public static final int TYPE_ERROR = 2;
038: public static final int TYPE_TEXT = 3;
039: public static final int TYPE_TAG = 4;
040: public static final int TYPE_ENDTAG = 5;
041:
042: private HTMLSyntaxSupport support;
043: private SyntaxElement previous;
044: private SyntaxElement next;
045:
046: int offset;
047: int length;
048: int type;
049:
050: /** Creates new SyntaxElement */
051: public SyntaxElement(HTMLSyntaxSupport support, int from, int to,
052: int type) {
053: this .support = support;
054: this .offset = from;
055: this .length = to - from;
056: this .type = type;
057: }
058:
059: public int getElementOffset() {
060: return offset;
061: }
062:
063: public int getElementLength() {
064: return length;
065: }
066:
067: public int getType() {
068: return type;
069: }
070:
071: public String getText() {
072: try {
073: return support.getDocument().getText(offset, length);
074: } catch (BadLocationException exc) {
075: // this could happen only when in inconsistent state
076: throw new ConcurrentModificationException(
077: "SyntaxElement in inconsistent state"); // NOI18N
078: }
079: }
080:
081: public SyntaxElement getPrevious() throws BadLocationException {
082: if (previous == null) {
083: previous = support.getPreviousElement(offset);
084: if (previous != null)
085: previous.next = this ;
086: }
087: return previous;
088: }
089:
090: public SyntaxElement getNext() throws BadLocationException {
091: if (next == null) {
092: next = support.getNextElement(offset + length);
093: if (next != null)
094: next.previous = this ;
095: }
096: return next;
097: }
098:
099: public String toString() {
100: return "Element(" + type + ")[" + offset + ","
101: + (offset + length - 1) + "]"; // NOI18N
102: }
103:
104: /**
105: * Declaration models SGML declaration with emphasis on <!DOCTYPE
106: * declaration, as other declarations are not allowed inside HTML. It
107: * represents unknown/broken declaration or either public or system DOCTYPE
108: * declaration.
109: */
110: static class Declaration extends SyntaxElement {
111: private String root;
112: private String publicID;
113: private String file;
114:
115: /**
116: * Creates a model of SGML declaration with some properties of DOCTYPE
117: * declaration.
118: *
119: * @param doctypeRootElement
120: * the name of the root element for a DOCTYPE. Can be null to
121: * express that the declaration is not DOCTYPE declaration or
122: * is broken.
123: * @param doctypePI
124: * public identifier for this DOCTYPE, if available. null for
125: * system doctype or other/broken declaration.
126: * @param doctypeFile
127: * system identifier for this DOCTYPE, if available. null
128: * otherwise.
129: */
130: public Declaration(HTMLSyntaxSupport support, int from, int to,
131: String doctypeRootElement, String doctypePI,
132: String doctypeFile) {
133: super (support, from, to, TYPE_DECLARATION);
134: root = doctypeRootElement;
135: publicID = doctypePI;
136: file = doctypeFile;
137: }
138:
139: /**
140: * @return the name of the root element for a DOCTYPE declaration or
141: * null if the declatarion is not DOCTYPE or is broken.
142: */
143: public String getRootElement() {
144: return root;
145: }
146:
147: /**
148: * @return a public identifier of the PUBLIC DOCTYPE declaration or null
149: * for SYSTEM DOCTYPE and broken or other declaration.
150: */
151: public String getPublicIdentifier() {
152: return publicID;
153: }
154:
155: /**
156: * @return a system identifier of both PUBLIC and SYSTEM DOCTYPE
157: * declaration or null for PUBLIC declaration with system
158: * identifier not specified and broken or other declaration.
159: */
160: public String getDoctypeFile() {
161: return file;
162: }
163:
164: }
165:
166: static class Named extends SyntaxElement {
167: String name;
168:
169: public Named(HTMLSyntaxSupport support, int from, int to,
170: int type, String name) {
171: super (support, from, to, type);
172: this .name = name;
173: }
174:
175: public String getName() {
176: return name;
177: }
178:
179: public String toString() {
180: return super .toString() + " - \"" + name + '"'; // NOI18N
181: }
182: }
183:
184: static class Tag extends SyntaxElement.Named {
185: Collection attribs;
186:
187: public Tag(HTMLSyntaxSupport support, int from, int to,
188: String name, Collection attribs) {
189: super (support, from, to, TYPE_TAG, name);
190: this .attribs = attribs;
191: }
192:
193: public Collection getAttributes() {
194: return attribs;
195: }
196:
197: public String toString() {
198: StringBuffer ret = new StringBuffer(super .toString());
199: ret.append(" - {"); // NOI18N
200:
201: for (Iterator i = attribs.iterator(); i.hasNext();) {
202: ret.append(i.next());
203: ret.append(", "); // NOI18N
204: }
205:
206: ret.append("}"); // NOI18N
207: return ret.toString();
208: }
209: }
210:
211: }
|