KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > DigestParser


1 /*******************************************************************************
2  * Copyright (c) 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.update.internal.core;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.ArrayList JavaDoc;
16
17 import javax.xml.parsers.ParserConfigurationException JavaDoc;
18 import javax.xml.parsers.SAXParser JavaDoc;
19 import javax.xml.parsers.SAXParserFactory JavaDoc;
20
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.update.core.model.FeatureModelFactory;
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.SAXParseException JavaDoc;
27 import org.xml.sax.helpers.DefaultHandler JavaDoc;
28
29 public class DigestParser extends DefaultHandler JavaDoc {
30
31     private InternalFeatureParser featureParser;
32     
33     private ArrayList JavaDoc featureModels;
34
35     private SAXParser JavaDoc parser;
36
37     private FeatureModelFactory factory;
38
39     private String JavaDoc location;
40     
41     private final static SAXParserFactory JavaDoc parserFactory =
42         SAXParserFactory.newInstance();
43     
44     public DigestParser() {
45         super();
46         featureParser = new InternalFeatureParser();
47         try {
48             parserFactory.setNamespaceAware(true);
49             this.parser = parserFactory.newSAXParser();
50         } catch (ParserConfigurationException JavaDoc e) {
51             UpdateCore.log(e);
52         } catch (SAXException JavaDoc e) {
53             UpdateCore.log(e);
54         }
55     }
56
57     public void init(FeatureModelFactory factory) {
58         init(factory, null);
59     }
60     
61     /**
62      * @param factory
63      * @param location
64      * @since 3.1
65      */

66     public void init(FeatureModelFactory factory, String JavaDoc location) {
67         
68         this.factory = factory;
69         this.location = location;
70         factory = new LiteFeatureFactory();
71         featureModels = new ArrayList JavaDoc();
72         featureParser.internalInit(factory, location);
73     }
74
75     /**
76      * Parses the specified input steam and constructs a feature model.
77      * The input stream is not closed as part of this operation.
78      *
79      * @param in input stream
80      * @return feature model
81      * @exception SAXException
82      * @exception IOException
83      * @since 2.0
84      */

85     public LiteFeature[] parse(InputStream JavaDoc in) throws SAXException JavaDoc, IOException JavaDoc {
86         
87         parser.parse(new InputSource JavaDoc(in), this);
88         return (LiteFeature[])featureModels.toArray( new LiteFeature[featureModels.size()]);
89     }
90
91
92     /**
93      * Returns all status objects accumulated by the parser.
94      *
95      * @return multi-status containing accumulated status, or <code>null</code>.
96      * @since 2.0
97      */

98     public MultiStatus getStatus() {
99         return featureParser.getStatus();
100     }
101
102     /**
103      * Handle start of element tags
104      * @see DefaultHandler#startElement(String, String, String, Attributes)
105      * @since 2.0
106      */

107     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
108         if(localName.equals("digest")) //$NON-NLS-1$
109
return;
110         if(localName.equals("feature")) //$NON-NLS-1$
111
featureParser.internalInit(factory, location);
112         
113         featureParser.startElement(uri, localName, qName, attributes);
114     }
115
116     /**
117      * Handle end of element tags
118      * @see DefaultHandler#endElement(String, String, String)
119      * @since 2.0
120      */

121     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) {
122         if(localName.equals("digest")) //$NON-NLS-1$
123
return;
124         featureParser.endElement(uri, localName, qName);
125         if(localName.equals("feature")) { //$NON-NLS-1$
126
try {
127                 featureModels.add(featureParser.getFeatureModel());
128             } catch (SAXException JavaDoc e) {
129                 e.printStackTrace();
130             }
131         }
132     }
133     
134
135     /**
136      * Handle character text
137      * @see DefaultHandler#characters(char[], int, int)
138      * @since 2.0
139      */

140     public void characters(char[] ch, int start, int length) {
141         featureParser.characters(ch, start, length);
142     }
143
144     /**
145      * Handle errors
146      * @see DefaultHandler#error(SAXParseException)
147      * @since 2.0
148      */

149     public void error(SAXParseException JavaDoc ex) {
150         featureParser.error(ex);
151     }
152
153     /**
154      * Handle fatal errors
155      * @see DefaultHandler#fatalError(SAXParseException)
156      * @exception SAXException
157      * @since 2.0
158      */

159     public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
160         featureParser.fatalError(ex);
161     }
162
163     /**
164      * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
165      */

166     public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException JavaDoc {
167         featureParser.ignorableWhitespace(arg0, arg1, arg2);
168     }
169 }
170
Popular Tags