KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > registry > xml > ManifestParser


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2007 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.registry.xml;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.net.URL JavaDoc;
27
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.java.plugin.util.IoUtil;
34 import org.xml.sax.EntityResolver JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /**
39  * @version $Id: ManifestParser.java,v 1.8 2007/01/08 09:07:07 ddimon Exp $
40  */

41 final class ManifestParser {
42     static Log log = LogFactory.getLog(ManifestParser.class);
43
44     static final String JavaDoc PLUGIN_DTD_1_0 = loadPluginDtd("1_0"); //$NON-NLS-1$
45

46     private static String JavaDoc loadPluginDtd(final String JavaDoc version) {
47         try {
48             Reader JavaDoc in = new InputStreamReader JavaDoc(
49                     PluginRegistryImpl.class.getResourceAsStream("plugin_" //$NON-NLS-1$
50
+ version + ".dtd"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
51
try {
52                 StringBuffer JavaDoc sBuf = new StringBuffer JavaDoc();
53                 char[] cBuf = new char[64];
54                 int read;
55                 while ((read = in.read(cBuf)) != -1) {
56                     sBuf.append(cBuf, 0, read);
57                 }
58                 return sBuf.toString();
59             } finally {
60                 in.close();
61             }
62         } catch (IOException JavaDoc ioe) {
63             log.error("can't read plug-in DTD file of version " + version, ioe); //$NON-NLS-1$
64
}
65         return null;
66     }
67
68     private static EntityResolver JavaDoc getDtdEntityResolver() {
69         return new EntityResolver JavaDoc() {
70             public InputSource JavaDoc resolveEntity(final String JavaDoc publicId,
71                     final String JavaDoc systemId) {
72                 if (publicId == null) {
73                     log.debug("can't resolve entity, public ID is NULL, systemId=" + systemId); //$NON-NLS-1$
74
return null;
75                 }
76                 if (PLUGIN_DTD_1_0 == null) {
77                     return null;
78                 }
79                 if (publicId.equals("-//JPF//Java Plug-in Manifest 1.0") //$NON-NLS-1$
80
|| publicId.equals("-//JPF//Java Plug-in Manifest 0.7") //$NON-NLS-1$
81
|| publicId.equals("-//JPF//Java Plug-in Manifest 0.6") //$NON-NLS-1$
82
|| publicId.equals("-//JPF//Java Plug-in Manifest 0.5") //$NON-NLS-1$
83
|| publicId.equals("-//JPF//Java Plug-in Manifest 0.4") //$NON-NLS-1$
84
|| publicId.equals("-//JPF//Java Plug-in Manifest 0.3") //$NON-NLS-1$
85
|| publicId.equals("-//JPF//Java Plug-in Manifest 0.2")) { //$NON-NLS-1$
86
if (log.isDebugEnabled()) {
87                         log.debug("entity resolved to plug-in manifest DTD, publicId=" //$NON-NLS-1$
88
+ publicId + ", systemId=" + systemId); //$NON-NLS-1$
89
}
90                     return new InputSource JavaDoc(new StringReader JavaDoc(PLUGIN_DTD_1_0));
91                 }
92                 if (log.isDebugEnabled()) {
93                     log.debug("entity not resolved, publicId=" //$NON-NLS-1$
94
+ publicId + ", systemId=" + systemId); //$NON-NLS-1$
95
}
96                 return null;
97             }
98         };
99     }
100
101     private final SAXParserFactory JavaDoc parserFactory;
102     private final EntityResolver JavaDoc entityResolver;
103
104     ManifestParser(final boolean isValidating) {
105         parserFactory = SAXParserFactory.newInstance();
106         parserFactory.setValidating(isValidating);
107         entityResolver = isValidating ? getDtdEntityResolver() : null;
108         log.info("got SAX parser factory - " + parserFactory); //$NON-NLS-1$
109
}
110
111     ModelPluginManifest parseManifest(final URL JavaDoc url)
112             throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
113         ManifestHandler handler =
114             new ManifestHandler(entityResolver);
115         //InputStream strm = url.openStream();
116
InputStream JavaDoc strm = IoUtil.getResourceInputStream(url);
117         try {
118             parserFactory.newSAXParser().parse(strm, handler);
119         } finally {
120             strm.close();
121         }
122         return handler.getResult();
123     }
124
125     ModelManifestInfo parseManifestInfo(final URL JavaDoc url)
126             throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
127         ManifestInfoHandler handler =
128             new ManifestInfoHandler(entityResolver);
129         //InputStream strm = url.openStream();
130
InputStream JavaDoc strm = IoUtil.getResourceInputStream(url);
131         try {
132             parserFactory.newSAXParser().parse(strm, handler);
133         } finally {
134             strm.close();
135         }
136         return handler.getResult();
137     }
138 }
139
Popular Tags