001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.editor.ext.html.javadoc;
043:
044: import java.util.Hashtable;
045: import org.xml.sax.Attributes;
046: import org.xml.sax.SAXException;
047: import org.xml.sax.helpers.DefaultHandler;
048:
049: /**
050: *
051: * @author Petr Pisl
052: */
053:
054: class SAXHelpHandler extends DefaultHandler {
055: private TagHelpItem tag;
056: private TagHelpItem attribute;
057:
058: private static final int HELP_CODE = "help".hashCode(); //NOI18N
059: private static final int TAG_CODE = "tag".hashCode();// NOI18N
060: private static final int ATTRIBUTE_CODE = "attribute".hashCode();// NOI18N
061: private static final int LOCATION_CODE = "location".hashCode();// NOI18N
062: private static final int START_TEXT_CODE = "start-text".hashCode();// NOI18N
063: private static final int END_TEXT_CODE = "end-text".hashCode();// NOI18N
064: private static final int ADD_TEXT_CODE = "add-text".hashCode();// NOI18N
065: private static final int BEFORE_CODE = "before".hashCode();// NOI18N
066: private static final int AFTER_CODE = "after".hashCode();// NOI18N
067:
068: private static final String NAME_STRING = "name";// NOI18N
069: private static final String LOCATION_STRING = "location"; //NOI18N
070: private static final String FILE_STRING = "file"; //NOI18N
071: private static final String IDENTICAL_STRING = "identical"; //NOI18N
072: private static final String TEXT_STRING = "text"; //NOI18N
073: private static final String OFFSET_STRING = "offset"; //NOI18N
074: private static final String BEFORE_STRING = "before"; //NOI18N
075: private static final String AFTER_STRING = "after"; //NOI18N
076:
077: private static final int TAG_STATE = 1;
078: private static final int ATTRIBUTE_STATE = 2;
079:
080: private static final int BEFORE_STATE = 10;
081: private static final int AFTER_STATE = 11;
082:
083: private int state;
084: private int textState;
085:
086: private Hashtable map;
087: private String file;
088: private String location;
089: private String identical;
090:
091: private String mezery;
092:
093: public SAXHelpHandler() {
094: super ();
095: map = new Hashtable();
096: file = null;
097: tag = null;
098: }
099:
100: public void startElement(String uri, String localname,
101: String qname, Attributes attrs) throws SAXException {
102: int controlCode = qname.hashCode();
103: String value;
104: if (controlCode == TAG_CODE) {
105: value = attrs.getValue(NAME_STRING);
106: tag = new TagHelpItem(value);
107: map.put(tag.getName().toUpperCase(), tag);
108: state = TAG_STATE;
109: //System.out.println("tag");
110: value = attrs.getValue(IDENTICAL_STRING);
111: if (value != null) {
112: //System.out.println("value: " + value);
113: tag.setIdentical(value);
114: }
115: } else if (controlCode == ATTRIBUTE_CODE) {
116: value = attrs.getValue(NAME_STRING);
117: attribute = new TagHelpItem(value);
118: state = ATTRIBUTE_STATE;
119: map.put((tag.getName() + "#" + attribute.getName())
120: .toUpperCase(), attribute); // NOI18N
121: value = attrs.getValue(IDENTICAL_STRING);
122: if (value != null) {
123: //System.out.println("value: " + value);
124: attribute.setIdentical(value);
125: }
126: } else if (controlCode == LOCATION_CODE) {
127: value = attrs.getValue(FILE_STRING);
128: switch (state) {
129: case TAG_STATE:
130: tag.setFile(value);
131: break;
132: case ATTRIBUTE_STATE:
133: attribute.setFile(value);
134: break;
135: }
136: } else if (controlCode == START_TEXT_CODE) {
137: value = attrs.getValue(OFFSET_STRING);
138: int offset = 0;
139: if (value != null) {
140: try {
141: offset = (new Integer(value)).intValue();
142: } catch (NumberFormatException e) {
143: }
144: }
145: value = attrs.getValue(TEXT_STRING);
146: switch (state) {
147: case TAG_STATE:
148: tag.setStartText(value);
149: tag.setStartTextOffset(offset);
150: break;
151: case ATTRIBUTE_STATE:
152: attribute.setStartText(value);
153: attribute.setStartTextOffset(offset);
154: break;
155: }
156: } else if (controlCode == END_TEXT_CODE) {
157: value = attrs.getValue(OFFSET_STRING);
158: int offset = 0;
159: if (value != null) {
160: try {
161: offset = (new Integer(value)).intValue();
162: } catch (NumberFormatException e) {
163: }
164: }
165: value = attrs.getValue(TEXT_STRING);
166: switch (state) {
167: case TAG_STATE:
168: tag.setEndText(value);
169: tag.setEndTextOffset(offset);
170: break;
171: case ATTRIBUTE_STATE:
172: attribute.setEndText(value);
173: attribute.setEndTextOffset(offset);
174: break;
175: }
176: } else if (controlCode == ADD_TEXT_CODE) {
177: String before = attrs.getValue(BEFORE_STRING);
178: String after = attrs.getValue(AFTER_STRING);
179: switch (state) {
180: case TAG_STATE:
181: tag.setTextBefore(before);
182: tag.setTextAfter(after);
183: break;
184: case ATTRIBUTE_STATE:
185: attribute.setTextBefore(before);
186: attribute.setTextAfter(after);
187: break;
188: }
189: } else if (controlCode == START_TEXT_CODE) {
190:
191: } else if (controlCode == BEFORE_CODE) {
192: textState = BEFORE_STATE;
193: } else if (controlCode == AFTER_CODE) {
194: textState = AFTER_STATE;
195: } else if (controlCode == HELP_CODE) {
196: file = attrs.getValue(FILE_STRING);
197:
198: }
199:
200: }
201:
202: public void characters(char[] ch, int start, int length)
203: throws SAXException {
204: String text = (new String(ch, start, length)).trim();
205: if (text != null && text.length() > 0) {
206: TagHelpItem key = null;
207: switch (state) {
208: case TAG_STATE:
209: key = tag;
210: break;
211: case ATTRIBUTE_STATE:
212: key = attribute;
213: break;
214: }
215: if (key != null) {
216: switch (textState) {
217: case BEFORE_STATE:
218: if (key.getTextBefore() != null)
219: key.setTextBefore(key.getTextBefore() + text);
220: else
221: key.setTextBefore(text);
222: break;
223: case AFTER_STATE:
224: if (key.getTextAfter() != null)
225: key.setTextAfter(key.getTextAfter() + text);
226: else
227: key.setTextAfter(text);
228: }
229: }
230: }
231: }
232:
233: public String getHelpFile() {
234: return file;
235: }
236:
237: public Hashtable getMap() {
238: return map;
239: }
240: }
|