KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > context > ContextsFileParser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.context;
12
13 import java.io.*;
14 import com.ibm.icu.text.MessageFormat;
15
16 import javax.xml.parsers.*;
17
18 import org.eclipse.help.internal.*;
19 import org.eclipse.help.internal.util.*;
20 import org.xml.sax.*;
21 import org.xml.sax.helpers.*;
22
23 /**
24  * Parser for xml file
25  */

26 public class ContextsFileParser extends DefaultHandler {
27     protected FastStack stack = new FastStack();
28
29     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
30
31     boolean seenDescription = false;
32
33     ContextsFile contextsFile;
34
35     private ContextsBuilder builder;
36
37     private final static SAXParserFactory factory = SAXParserFactory
38             .newInstance();
39
40     public ContextsFileParser(ContextsBuilder builder) {
41         super();
42         this.builder = builder;
43     }
44
45     /**
46      * Receive notification of character data.
47      */

48     public void characters(char ch[], int start, int length)
49             throws SAXException {
50         if (seenDescription)
51             buffer.append(ch, start, length);
52         if (HelpPlugin.DEBUG_CONTEXT) {
53             System.out
54                     .println("ContextsFileParser.characters(): got char from parser= " //$NON-NLS-1$
55
+ new StringBuffer JavaDoc().append(ch, start, length)
56                                     .toString());
57         }
58     }
59
60     /**
61      * Receive notification of the end of an element.
62      */

63     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
64             throws SAXException {
65         // make sure that no error has already occurred before adding to stack.
66
if (qName.equals(ContextsNode.DESC_ELEM)) {
67             seenDescription = false;
68             ((Context) stack.peek()).setStyledText(buffer.toString());
69             buffer.setLength(0);
70         } else if (qName.equals(ContextsNode.DESC_TXT_BOLD)) {
71             // pop the starting bold tag
72
stack.pop();
73             if (!(stack.peek()).equals(ContextsNode.BOLD_TAG))
74                 buffer.append(ContextsNode.BOLD_CLOSE_TAG);
75         } else if (!qName.equals("filter")) { //$NON-NLS-1$
76
ContextsNode node = (ContextsNode) stack.pop();
77             node.build(builder);
78         }
79     }
80
81     /**
82      * @see ErrorHandler#error(SAXParseException)
83      */

84     public void error(SAXParseException ex) {
85         HelpPlugin.logError("Error parsing " + getErrorDetails(ex), null); //$NON-NLS-1$
86
}
87
88     /**
89      * @see ErrorHandler#fatalError(SAXParseException)
90      */

91     public void fatalError(SAXParseException ex) throws SAXException {
92         HelpPlugin.logError("Failed to parse " + getErrorDetails(ex), ex); //$NON-NLS-1$
93
}
94
95     public String JavaDoc getErrorDetails(SAXParseException ex) {
96         String JavaDoc param0 = ex.getSystemId();
97         Integer JavaDoc param1 = new Integer JavaDoc(ex.getLineNumber());
98         Integer JavaDoc param2 = new Integer JavaDoc(ex.getColumnNumber());
99         String JavaDoc param3 = ex.getMessage();
100         String JavaDoc message = MessageFormat
101                 .format(
102                         "URL: {0} at line: {1,number,integer}, column: {2,number,integer}.\r\n{3}", //$NON-NLS-1$
103
new Object JavaDoc[] { param0, param1, param2, param3 });
104         return message;
105     }
106
107     /**
108      * Receive notification of the beginning of an element.
109      */

110     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
111             String JavaDoc qName, Attributes atts) throws SAXException {
112         // We don't create a description element
113
if (qName.equals(ContextsNode.DESC_ELEM))
114             seenDescription = true;
115         else if (qName.equals(ContextsNode.DESC_TXT_BOLD)) {
116             // peek into stack to findout if a bold tag element already
117
// exists. If we find one, then we do not add the bold tag to
118
// the current StringBuffer of description.
119
// ie: there are many bold start tags in the stack, but we appended
120
// the tag only once to the description string.
121
// eg: (b) some text (b) more test (/b) more text (/b) will result
122
// in all of the sentence being bold.
123
if (!(stack.peek()).equals(ContextsNode.BOLD_TAG))
124                 buffer.append(ContextsNode.BOLD_TAG);
125             stack.push(ContextsNode.BOLD_TAG);
126         } else {
127             ContextsNode e = null;
128             // NOTE: we don't create an element for the description
129
if (qName.equals(ContextsNode.CONTEXTS_ELEM)) {
130                 e = new Contexts(atts);
131             } else if (qName.equals(ContextsNode.CONTEXT_ELEM)) {
132                 e = new Context(atts);
133             } else if (qName.equals(ContextsNode.RELATED_ELEM)) {
134                 e = new RelatedTopic(atts);
135             } else if (qName.equals("filter")) { //$NON-NLS-1$
136
if (!stack.empty()) {
137                     Object JavaDoc parent = stack.peek();
138                     if (parent instanceof FilterableUAElement && atts != null) {
139                         FilterableUAElement filterableNode = (FilterableUAElement)parent;
140                         String JavaDoc name = atts.getValue("name"); //$NON-NLS-1$
141
String JavaDoc value = atts.getValue("value"); //$NON-NLS-1$
142
if (name != null && value != null) {
143                             filterableNode.addFilter(name, value);
144                         }
145                     }
146                 }
147                 return;
148             } else
149                 return;
150             if (!stack.empty())
151                 ((ContextsNode) stack.peek()).addChild(e);
152             stack.push(e);
153         }
154     }
155
156     public void warning(SAXParseException ex) {
157         HelpPlugin.logWarning("Warning parsing " + getErrorDetails(ex)); //$NON-NLS-1$
158
}
159
160     public void parse(ContextsFile contextsFile) {
161         this.contextsFile = contextsFile;
162         InputStream is = contextsFile.getInputStream();
163         if (is == null)
164             return;
165         InputSource inputSource = new InputSource(is);
166         String JavaDoc file = "/" + contextsFile.getDefiningPluginID() + "/" //$NON-NLS-1$ //$NON-NLS-2$
167
+ contextsFile.getHref();
168         inputSource.setSystemId(file);
169         try {
170             SAXParser parser = factory.newSAXParser();
171             parser.parse(inputSource, this);
172         } catch (ParserConfigurationException pce) {
173             HelpPlugin.logError(
174                     "SAXParser implementation could not be loaded.", pce); //$NON-NLS-1$
175
} catch (SAXException se) {
176             HelpPlugin.logError("", se); //$NON-NLS-1$
177
} catch (IOException ioe) {
178             HelpPlugin.logError("Error loading file " + file + ".", ioe); //$NON-NLS-1$ //$NON-NLS-2$
179
} finally {
180             if (is != null) {
181                 try {
182                     is.close();
183                 } catch (IOException e) {
184                 }
185             }
186         }
187     }
188
189     /**
190      * @see EntityResolver This method implementation prevents loading external
191      * entities instead of calling
192      * org.apache.xerces.parsers.SaxParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);
193      */

194     public InputSource resolveEntity(String JavaDoc publicId, String JavaDoc systemId) {
195         InputSource source = new InputSource(new ByteArrayInputStream(
196                 new byte[0]));
197         source.setPublicId(publicId);
198         source.setSystemId(systemId);
199         return source;
200     }
201 }
202
Popular Tags