1 19 20 package org.netbeans.editor.ext.html.javadoc; 21 22 import java.util.Hashtable ; 23 import org.xml.sax.Attributes ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.helpers.DefaultHandler ; 26 27 28 32 33 class SAXHelpHandler extends DefaultHandler { 34 private TagHelpItem tag; 35 private TagHelpItem attribute; 36 37 private static final int HELP_CODE = "help".hashCode(); private static final int TAG_CODE = "tag".hashCode(); private static final int ATTRIBUTE_CODE = "attribute".hashCode(); private static final int LOCATION_CODE = "location".hashCode(); private static final int START_TEXT_CODE = "start-text".hashCode(); private static final int END_TEXT_CODE = "end-text".hashCode(); private static final int ADD_TEXT_CODE = "add-text".hashCode(); private static final int BEFORE_CODE = "before".hashCode(); private static final int AFTER_CODE = "after".hashCode(); 47 private static final String NAME_STRING = "name"; private static final String LOCATION_STRING = "location"; private static final String FILE_STRING = "file"; private static final String IDENTICAL_STRING = "identical"; private static final String TEXT_STRING = "text"; private static final String OFFSET_STRING = "offset"; private static final String BEFORE_STRING = "before"; private static final String AFTER_STRING = "after"; 56 57 private static final int TAG_STATE = 1; 58 private static final int ATTRIBUTE_STATE = 2; 59 60 private static final int BEFORE_STATE = 10; 61 private static final int AFTER_STATE = 11; 62 63 private int state; 64 private int textState; 65 66 private Hashtable map; 67 private String file; 68 private String location; 69 private String identical; 70 71 private String mezery; 72 73 public SAXHelpHandler(){ 74 super(); 75 map = new Hashtable (); 76 file = null; 77 tag = null; 78 } 79 80 public void startElement(String uri, String localname, String qname, Attributes attrs) throws SAXException { 81 int controlCode = qname.hashCode(); 82 String value; 83 if (controlCode == TAG_CODE){ 84 value = attrs.getValue(NAME_STRING); 85 tag = new TagHelpItem(value); 86 map.put (tag.getName().toUpperCase(), tag); 87 state = TAG_STATE; 88 value = attrs.getValue(IDENTICAL_STRING); 90 if (value != null){ 91 tag.setIdentical(value); 93 } 94 } 95 else if (controlCode == ATTRIBUTE_CODE){ 96 value = attrs.getValue(NAME_STRING); 97 attribute = new TagHelpItem(value); 98 state = ATTRIBUTE_STATE; 99 map.put((tag.getName() + "#" + attribute.getName()).toUpperCase(), attribute); value = attrs.getValue(IDENTICAL_STRING); 101 if (value != null){ 102 attribute.setIdentical(value); 104 } 105 } 106 else if (controlCode == LOCATION_CODE){ 107 value = attrs.getValue(FILE_STRING); 108 switch (state){ 109 case TAG_STATE: tag.setFile(value); break; 110 case ATTRIBUTE_STATE: attribute.setFile(value); break; 111 } 112 } 113 else if (controlCode == START_TEXT_CODE){ 114 value = attrs.getValue(OFFSET_STRING); 115 int offset = 0; 116 if (value != null){ 117 try{ 118 offset = (new Integer (value)).intValue(); 119 } 120 catch (NumberFormatException e){ 121 } 122 } 123 value = attrs.getValue(TEXT_STRING); 124 switch (state){ 125 case TAG_STATE: 126 tag.setStartText(value); 127 tag.setStartTextOffset(offset); 128 break; 129 case ATTRIBUTE_STATE: 130 attribute.setStartText(value); 131 attribute.setStartTextOffset(offset); 132 break; 133 } 134 } 135 else if (controlCode == END_TEXT_CODE){ 136 value = attrs.getValue(OFFSET_STRING); 137 int offset = 0; 138 if (value != null){ 139 try{ 140 offset = (new Integer (value)).intValue(); 141 } 142 catch (NumberFormatException e){ 143 } 144 } 145 value = attrs.getValue(TEXT_STRING); 146 switch (state){ 147 case TAG_STATE: 148 tag.setEndText(value); 149 tag.setEndTextOffset(offset); 150 break; 151 case ATTRIBUTE_STATE: 152 attribute.setEndText(value); 153 attribute.setEndTextOffset(offset); 154 break; 155 } 156 } 157 else if (controlCode == ADD_TEXT_CODE){ 158 String before = attrs.getValue(BEFORE_STRING); 159 String after = attrs.getValue(AFTER_STRING); 160 switch (state){ 161 case TAG_STATE: 162 tag.setTextBefore(before); 163 tag.setTextAfter(after); 164 break; 165 case ATTRIBUTE_STATE: 166 attribute.setTextBefore(before); 167 attribute.setTextAfter(after); 168 break; 169 } 170 } 171 else if(controlCode == START_TEXT_CODE){ 172 173 } 174 else if(controlCode == BEFORE_CODE){ 175 textState = BEFORE_STATE; 176 } 177 else if(controlCode == AFTER_CODE){ 178 textState = AFTER_STATE; 179 } 180 else if ( controlCode == HELP_CODE){ 181 file = attrs.getValue(FILE_STRING); 182 183 } 184 185 } 186 187 public void characters(char[] ch, int start, int length) throws SAXException { 188 String text = (new String (ch, start, length)).trim(); 189 if (text != null && text.length() > 0){ 190 TagHelpItem key = null; 191 switch (state){ 192 case TAG_STATE: 193 key = tag; 194 break; 195 case ATTRIBUTE_STATE: 196 key = attribute; 197 break; 198 } 199 if (key != null){ 200 switch (textState){ 201 case BEFORE_STATE: 202 if (key.getTextBefore() != null) 203 key.setTextBefore(key.getTextBefore() + text); 204 else 205 key.setTextBefore(text); 206 break; 207 case AFTER_STATE: 208 if (key.getTextAfter() != null) 209 key.setTextAfter(key.getTextAfter() + text); 210 else 211 key.setTextAfter(text); 212 } 213 } 214 } 215 } 216 217 public String getHelpFile(){ 218 return file; 219 } 220 221 public Hashtable getMap(){ 222 return map; 223 } 224 } | Popular Tags |