001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.registry.xml;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023: import java.io.Reader;
024: import java.io.StringReader;
025: import java.net.URL;
026:
027: import javax.xml.parsers.ParserConfigurationException;
028: import javax.xml.parsers.SAXParserFactory;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.java.plugin.util.IoUtil;
033: import org.xml.sax.EntityResolver;
034: import org.xml.sax.InputSource;
035: import org.xml.sax.SAXException;
036:
037: /**
038: * @version $Id: ManifestParser.java,v 1.4 2007/03/03 17:16:26 ddimon Exp $
039: */
040: final class ManifestParser {
041: static Log log = LogFactory.getLog(ManifestParser.class);
042:
043: static final String PLUGIN_DTD_1_0 = loadPluginDtd("1_0"); //$NON-NLS-1$
044:
045: private static String loadPluginDtd(final String version) {
046: try {
047: Reader in = new InputStreamReader(PluginRegistryImpl.class
048: .getResourceAsStream("plugin_" //$NON-NLS-1$
049: + version + ".dtd"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
050: try {
051: StringBuilder sBuf = new StringBuilder();
052: char[] cBuf = new char[64];
053: int read;
054: while ((read = in.read(cBuf)) != -1) {
055: sBuf.append(cBuf, 0, read);
056: }
057: return sBuf.toString();
058: } finally {
059: in.close();
060: }
061: } catch (IOException ioe) {
062: log
063: .error(
064: "can't read plug-in DTD file of version " + version, ioe); //$NON-NLS-1$
065: }
066: return null;
067: }
068:
069: private static EntityResolver getDtdEntityResolver() {
070: return new EntityResolver() {
071: public InputSource resolveEntity(final String publicId,
072: final String systemId) {
073: if (publicId == null) {
074: log
075: .debug("can't resolve entity, public ID is NULL, systemId=" + systemId); //$NON-NLS-1$
076: return null;
077: }
078: if (PLUGIN_DTD_1_0 == null) {
079: return null;
080: }
081: if (publicId
082: .equals("-//JPF//Java Plug-in Manifest 1.0") //$NON-NLS-1$
083: || publicId
084: .equals("-//JPF//Java Plug-in Manifest 0.7") //$NON-NLS-1$
085: || publicId
086: .equals("-//JPF//Java Plug-in Manifest 0.6") //$NON-NLS-1$
087: || publicId
088: .equals("-//JPF//Java Plug-in Manifest 0.5") //$NON-NLS-1$
089: || publicId
090: .equals("-//JPF//Java Plug-in Manifest 0.4") //$NON-NLS-1$
091: || publicId
092: .equals("-//JPF//Java Plug-in Manifest 0.3") //$NON-NLS-1$
093: || publicId
094: .equals("-//JPF//Java Plug-in Manifest 0.2")) { //$NON-NLS-1$
095: if (log.isDebugEnabled()) {
096: log
097: .debug("entity resolved to plug-in manifest DTD, publicId=" //$NON-NLS-1$
098: + publicId
099: + ", systemId=" + systemId); //$NON-NLS-1$
100: }
101: return new InputSource(new StringReader(
102: PLUGIN_DTD_1_0));
103: }
104: if (log.isDebugEnabled()) {
105: log.debug("entity not resolved, publicId=" //$NON-NLS-1$
106: + publicId + ", systemId=" + systemId); //$NON-NLS-1$
107: }
108: return null;
109: }
110: };
111: }
112:
113: private final SAXParserFactory parserFactory;
114: private final EntityResolver entityResolver;
115:
116: ManifestParser(final boolean isValidating) {
117: parserFactory = SAXParserFactory.newInstance();
118: parserFactory.setValidating(isValidating);
119: entityResolver = isValidating ? getDtdEntityResolver() : null;
120: log.info("got SAX parser factory - " + parserFactory); //$NON-NLS-1$
121: }
122:
123: ModelPluginManifest parseManifest(final URL url)
124: throws ParserConfigurationException, SAXException,
125: IOException {
126: ManifestHandler handler = new ManifestHandler(entityResolver);
127: //InputStream strm = url.openStream();
128: InputStream strm = IoUtil.getResourceInputStream(url);
129: try {
130: parserFactory.newSAXParser().parse(strm, handler);
131: } finally {
132: strm.close();
133: }
134: ModelPluginManifest result = handler.getResult();
135: result.setLocation(url);
136: return result;
137: }
138:
139: ModelManifestInfo parseManifestInfo(final URL url)
140: throws ParserConfigurationException, SAXException,
141: IOException {
142: ManifestInfoHandler handler = new ManifestInfoHandler(
143: entityResolver);
144: //InputStream strm = url.openStream();
145: InputStream strm = IoUtil.getResourceInputStream(url);
146: try {
147: parserFactory.newSAXParser().parse(strm, handler);
148: } finally {
149: strm.close();
150: }
151: return handler.getResult();
152: }
153: }
|