KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > html > javadoc > SAXHelpHandler


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 package org.netbeans.editor.ext.html.javadoc;
21
22 import java.util.Hashtable JavaDoc;
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.helpers.DefaultHandler JavaDoc;
26
27
28 /**
29  *
30  * @author Petr Pisl
31  */

32
33 class SAXHelpHandler extends DefaultHandler JavaDoc {
34         private TagHelpItem tag;
35         private TagHelpItem attribute;
36
37         private static final int HELP_CODE = "help".hashCode(); //NOI18N
38
private static final int TAG_CODE = "tag".hashCode();// NOI18N
39
private static final int ATTRIBUTE_CODE = "attribute".hashCode();// NOI18N
40
private static final int LOCATION_CODE = "location".hashCode();// NOI18N
41
private static final int START_TEXT_CODE = "start-text".hashCode();// NOI18N
42
private static final int END_TEXT_CODE = "end-text".hashCode();// NOI18N
43
private static final int ADD_TEXT_CODE = "add-text".hashCode();// NOI18N
44
private static final int BEFORE_CODE = "before".hashCode();// NOI18N
45
private static final int AFTER_CODE = "after".hashCode();// NOI18N
46

47         private static final String JavaDoc NAME_STRING = "name";// NOI18N
48
private static final String JavaDoc LOCATION_STRING = "location"; //NOI18N
49
private static final String JavaDoc FILE_STRING = "file"; //NOI18N
50
private static final String JavaDoc IDENTICAL_STRING = "identical"; //NOI18N
51
private static final String JavaDoc TEXT_STRING = "text"; //NOI18N
52
private static final String JavaDoc OFFSET_STRING = "offset"; //NOI18N
53
private static final String JavaDoc BEFORE_STRING = "before"; //NOI18N
54
private static final String JavaDoc AFTER_STRING = "after"; //NOI18N
55

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 JavaDoc map;
67         private String JavaDoc file;
68         private String JavaDoc location;
69         private String JavaDoc identical;
70         
71         private String JavaDoc mezery;
72         
73         public SAXHelpHandler(){
74             super();
75             map = new Hashtable JavaDoc();
76             file = null;
77             tag = null;
78         }
79         
80         public void startElement(String JavaDoc uri, String JavaDoc localname, String JavaDoc qname, Attributes JavaDoc attrs) throws SAXException JavaDoc {
81             int controlCode = qname.hashCode();
82             String JavaDoc 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                 //System.out.println("tag");
89
value = attrs.getValue(IDENTICAL_STRING);
90                 if (value != null){
91                     //System.out.println("value: " + value);
92
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); // NOI18N
100
value = attrs.getValue(IDENTICAL_STRING);
101                 if (value != null){
102                     //System.out.println("value: " + value);
103
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 JavaDoc(value)).intValue();
119                     }
120                     catch (NumberFormatException JavaDoc 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 JavaDoc(value)).intValue();
141                     }
142                     catch (NumberFormatException JavaDoc 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 JavaDoc before = attrs.getValue(BEFORE_STRING);
159                 String JavaDoc 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 JavaDoc{
188             String JavaDoc text = (new String JavaDoc(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 JavaDoc getHelpFile(){
218             return file;
219         }
220         
221         public Hashtable JavaDoc getMap(){
222             return map;
223         }
224     }
Popular Tags