1 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 44 public abstract class SyntaxElement { 45 46 49 protected XMLSyntaxSupport support; protected TokenItem first; 52 private SyntaxElement previous; private SyntaxElement next; 55 protected int offset; protected int length; 59 60 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 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 ex = new IllegalStateException ("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 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 118 return null; 121 } 122 } 123 } 124 return next; 125 } catch (BadLocationException ex) { 126 return null; 127 } 128 } 129 130 133 public String toString() { 134 String 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 144 public int hashCode() { 145 return super.hashCode() ^ offset ^ length; 146 } 147 148 151 public boolean equals(Object obj) { 152 if (obj instanceof SyntaxElement) { 153 if (((SyntaxElement)obj).offset == offset) return true; 154 } 155 return false; 156 } 157 158 159 161 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 toString() { 171 return "Error" + super.toString(); } 173 } 174 175 } 176 | Popular Tags |