001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.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 1any 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: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: JEntityResolver.java 7486 2005-10-07 22:30:10Z glapouch $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib.deployment.validation;
026:
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032:
033: import org.xml.sax.EntityResolver;
034: import org.xml.sax.InputSource;
035: import org.xml.sax.SAXException;
036:
037: import org.objectweb.jonas_lib.deployment.api.DTDs;
038: import org.objectweb.jonas_lib.deployment.api.Schemas;
039: import org.objectweb.jonas_lib.deployment.digester.JDigester;
040:
041: /**
042: * This class defines the entity resolver used to resolve DTDs/Schemas during
043: * the xml parsing
044: * @author Florent Benoit
045: */
046: public class JEntityResolver implements EntityResolver {
047:
048: /**
049: * Map which contains DTDs and Schemas available in local
050: */
051: private HashMap entries = new HashMap();
052:
053: /**
054: * Constructor without JDigester instance (used in WsGen only)
055: */
056: public JEntityResolver() {
057: super ();
058: }
059:
060: /**
061: * Add a local schema to the known entries
062: * @param path path to a local schema
063: */
064: private void addSchema(String path) {
065: String id = path.substring(path.lastIndexOf('/') + 1);
066: entries.put(id, path);
067: }
068:
069: /**
070: * The Parser will call this method before opening any external entity except
071: * the top-level document entity.
072: * @param publicId The public identifier of the external entity being referenced,
073: * or null if none was supplied.
074: * @param systemId The system identifier of the external entity being referenced.
075: * @return An InputSource object describing the new input source, or null to request that
076: * the parser open a regular URI connection to the system identifier.
077: * @throws SAXException Any SAX exception, possibly wrapping another exception.
078: * @throws IOException A Java-specific IO exception, possibly the result of creating
079: * a new InputStream or Reader for the InputSource.
080: */
081: public InputSource resolveEntity(String publicId, String systemId)
082: throws IOException, SAXException {
083:
084: // DTD : check if we got this entry
085: String localPath = null;
086: if (publicId != null) {
087: localPath = (String) entries.get(publicId);
088: } else if (systemId != null) {
089: // Can be a schema
090: if (systemId.toLowerCase().endsWith(".xsd")) {
091: // Retrieve basename
092: String baseName = systemId.substring(systemId
093: .lastIndexOf('/') + 1);
094:
095: // Registred ?
096: localPath = (String) entries.get(baseName);
097: }
098: }
099:
100: if (localPath == null) {
101: // This DTD/Schema was not added to this DTD/Schame local repository
102: return null;
103: }
104:
105: // Return the local path source
106: return (new InputSource(localPath));
107: }
108:
109: /**
110: * Add the mapping between a public Id and the local path of the DTD
111: * @param dtds Object containing the mapping PublicId --> Local URL
112: */
113: public void addDtds(DTDs dtds) {
114: if (dtds != null) {
115: Map dtdsMapping = dtds.getMapping();
116: if (dtdsMapping != null) {
117: for (Iterator it = dtdsMapping.entrySet().iterator(); it
118: .hasNext();) {
119: Map.Entry entry = (Map.Entry) it.next();
120: String publicId = (String) entry.getKey();
121: String localURL = (String) entry.getValue();
122: entries.put(publicId, localURL);
123: }
124: }
125: }
126: }
127:
128: /**
129: * Add the given Schemas to the schemas available for this resolver
130: * @param schemas Definition of the schemas to add to this resolver
131: */
132: public void addSchemas(Schemas schemas) {
133: // no schemas
134: if (schemas == null) {
135: return;
136: }
137:
138: List localSchemas = schemas.getlocalSchemas();
139:
140: // there are schemas
141: if (localSchemas != null) {
142: for (Iterator it = localSchemas.iterator(); it.hasNext();) {
143: String schema = (String) it.next();
144: addSchema(schema);
145: }
146: }
147: }
148:
149: }
|