001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SchemaEntityResolver.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.util.xml;
025:
026: import java.io.IOException;
027: import java.net.URL;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import org.xml.sax.EntityResolver;
032: import org.xml.sax.InputSource;
033: import org.xml.sax.SAXException;
034:
035: /**
036: * Entity resolver allowing to find schema within the classloader.
037: * @author Florent Benoit
038: */
039: public class SchemaEntityResolver implements EntityResolver {
040:
041: /**
042: * Map where the schemas URLs are stored.
043: */
044: private Map<String, String> schemasUrls = null;
045:
046: /**
047: * Constructor. Finds the XSD with classloader.
048: * @param schemas the name of the schemas to resolve.
049: */
050: public SchemaEntityResolver(final String[] schemas) {
051: schemasUrls = new HashMap<String, String>();
052: URL url = null;
053: for (int i = 0; i < schemas.length; i++) {
054: url = SchemaEntityResolver.class.getResource("/"
055: + schemas[i]);
056: if (url == null) {
057: throw new IllegalStateException(
058: "'"
059: + schemas[i]
060: + "' was not found in the current classloader !");
061: }
062: String urlString = url.toString();
063: String id = urlString
064: .substring(urlString.lastIndexOf('/') + 1);
065: schemasUrls.put(id, urlString);
066:
067: }
068: }
069:
070: /**
071: * The Parser will call this method before opening any external entity
072: * except the top-level document entity.
073: * @param publicId The public identifier of the external entity being
074: * referenced, or null if none was supplied.
075: * @param systemId The system identifier of the external entity being
076: * referenced.
077: * @return An InputSource object describing the new input source, or null to
078: * request that the parser open a regular URI connection to the
079: * system identifier.
080: * @throws SAXException Any SAX exception, possibly wrapping another
081: * exception.
082: * @throws IOException A Java-specific IO exception, possibly the result of
083: * creating a new InputStream or Reader for the InputSource.
084: */
085: public InputSource resolveEntity(final String publicId,
086: final String systemId) throws IOException, SAXException {
087:
088: String localPath = null;
089:
090: if (systemId != null) {
091: // Can be a schema
092: if (systemId.toLowerCase().endsWith(".xsd")) {
093: // Retrieve basename
094: String baseName = systemId.substring(systemId
095: .lastIndexOf('/') + 1);
096:
097: // Registred ?
098: localPath = schemasUrls.get(baseName);
099: }
100: }
101:
102: // schema not found
103: if (localPath == null) {
104: throw new SAXException("No XSD found for '" + systemId
105: + "'.");
106: }
107:
108: // Return the local path source
109: return (new InputSource(localPath));
110: }
111:
112: }
|