1 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 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 [] TYPE_NAMES = 51 new String []{"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 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 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 toString() { 109 String textContent = getType() == TYPE_TEXT ? getText() : ""; 110 return "Element(" +TYPE_NAMES[type]+")[" + offset + "," + (offset+length-1) + "] \"" + textContent + ""; } 112 113 114 120 public static class Declaration extends SyntaxElement { 121 private String root; 122 private String publicID; 123 private String file; 124 125 126 137 public Declaration( SyntaxParser parser, int from, int to, 138 String doctypeRootElement, 139 String doctypePI, String doctypeFile 140 ) { 141 super( parser, from, to, TYPE_DECLARATION ); 142 root = doctypeRootElement; 143 publicID = doctypePI; 144 file = doctypeFile; 145 } 146 147 151 public String getRootElement() { 152 return root; 153 } 154 155 159 public String getPublicIdentifier() { 160 return publicID; 161 } 162 163 168 public String getDoctypeFile() { 169 return file; 170 } 171 172 } 173 174 public static class Named extends SyntaxElement { 175 String name; 176 177 public Named( SyntaxParser parser, int from, int to, int type, String name ) { 178 super( parser, from, to, type ); 179 this.name = name; 180 } 181 182 public String getName() { 183 return name; 184 } 185 public String toString() { 186 return super.toString() + " - \"" + name + '"'; } 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 name, List<TagAttribute> attribs) { 196 this(parser, from, to, name, attribs, false); 197 } 198 199 public Tag( SyntaxParser parser, int from, int to, String 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 toString() { 214 StringBuffer ret = new StringBuffer ( super.toString() ); 215 ret.append( " - {" ); 217 for( Iterator i = attribs.iterator(); i.hasNext(); ) { 218 ret.append( i.next() ); 219 ret.append( ", " ); } 221 222 ret.append( "}" ); if(isEmpty()) ret.append(" (EMPTY TAG)"); 224 225 return ret.toString(); 226 } 227 } 228 229 public static class TagAttribute { 230 231 private String name, value; 232 private int nameOffset, valueOffset; 233 234 TagAttribute(String name, String 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 getName() { 242 return name; 243 } 244 245 void setName(String name) { 246 this.name = name; 247 } 248 249 public String getValue() { 250 return value; 251 } 252 253 void setValue(String 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 toString() { 274 return "TagAttribute[name=" + getName() + "; value=" + getValue() + "; nameOffset=" + getNameOffset() + "; valueOffset=" + getValueOffset() +"]"; 275 } 276 277 public boolean equals(Object 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 |