KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > model > DefaultPluginParser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.model;
12
13
14 import java.io.*;
15 import javax.xml.parsers.*;
16
17 import org.eclipse.update.core.*;
18 import org.eclipse.update.internal.core.*;
19 import org.xml.sax.*;
20 import org.xml.sax.helpers.*;
21
22 /**
23  * Parse default feature.xml
24  */

25
26 public class DefaultPluginParser extends DefaultHandler {
27     private final static SAXParserFactory parserFactory =
28         SAXParserFactory.newInstance();
29     private SAXParser parser;
30     private String JavaDoc id = null;
31     private String JavaDoc version = null;
32     private PluginEntry pluginEntry;
33
34     private static final String JavaDoc PLUGIN = "plugin"; //$NON-NLS-1$
35
private static final String JavaDoc FRAGMENT = "fragment"; //$NON-NLS-1$
36

37     private class ParseCompleteException extends SAXException {
38
39         private static final long serialVersionUID = 1L;
40
41         public ParseCompleteException(String JavaDoc arg0) {
42             super(arg0);
43         }
44     }
45
46     /**
47      * Constructor for DefaultFeatureParser
48      */

49     public DefaultPluginParser() {
50         super();
51         try {
52             parserFactory.setNamespaceAware(true);
53             this.parser = parserFactory.newSAXParser();
54         } catch (ParserConfigurationException e) {
55             UpdateCore.log(e);
56         } catch (SAXException e) {
57             UpdateCore.log(e);
58         }
59     }
60
61     /**
62      * @since 2.0
63      */

64     public synchronized PluginEntry parse(InputStream in) throws SAXException, IOException {
65         try {
66             pluginEntry = new PluginEntry();
67             parser.parse(new InputSource(in), this);
68         } catch (ParseCompleteException e) {
69             // expected, we stopped the parsing when we have the information we need
70
/// no need to pursue the parsing
71
}
72
73         if (id == null || id.trim().length() == 0)
74             id = "_no_id_"; //$NON-NLS-1$
75
pluginEntry.setVersionedIdentifier(new VersionedIdentifier(id, version));
76         return pluginEntry;
77     }
78
79     /**
80      * @see DefaultHandler#startElement(String, String, String, Attributes)
81      */

82     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes attributes) throws SAXException {
83
84         String JavaDoc tag = localName.trim();
85
86         if (tag.equalsIgnoreCase(PLUGIN)) {
87             pluginEntry.isFragment(false);
88             processPlugin(attributes);
89             return;
90         }
91
92         if (tag.equalsIgnoreCase(FRAGMENT)) {
93             pluginEntry.isFragment(true);
94             processPlugin(attributes);
95             return;
96         }
97     }
98
99     /**
100      * process plugin entry info
101      */

102     private void processPlugin(Attributes attributes) throws ParseCompleteException {
103         id = attributes.getValue("id"); //$NON-NLS-1$
104
version = attributes.getValue("version"); //$NON-NLS-1$
105
throw new ParseCompleteException(""); //$NON-NLS-1$
106
}
107 }
108
Popular Tags