KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > deployment > digester > JEntityResolver


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JEntityResolver.java,v 1.1 2004/05/25 14:26:34 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.deployment.digester;
28
29
30 import java.io.IOException JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.xml.sax.EntityResolver JavaDoc;
37 import org.xml.sax.InputSource JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39
40 import org.objectweb.jonas_lib.deployment.api.DTDs;
41 import org.objectweb.jonas_lib.deployment.api.Schemas;
42
43 /**
44  * This class defines the entity resolver used to resolve DTDs/Schemas during
45  * the xml parsing
46  * @author Florent Benoit
47  */

48 public class JEntityResolver implements EntityResolver JavaDoc {
49
50     /**
51      * Map which contains DTDs and Schemas available in local
52      */

53     private HashMap JavaDoc entries = new HashMap JavaDoc();
54
55     /**
56      * JDigester Object associated
57      */

58     private JDigester jDigester = null;
59
60
61     /**
62      * Constructor
63      *
64      * @param jd linked JDigester object
65      */

66     public JEntityResolver(JDigester jd) {
67         super();
68         jDigester = jd;
69     }
70
71     /**
72      * Constructor without JDigester instance (used in WsGen only)
73      */

74     public JEntityResolver() {
75         super();
76     }
77
78     /**
79      * Add a local schema to the known entries
80      * @param path path to a local schema
81      */

82     private void addSchema(String JavaDoc path) {
83         String JavaDoc id = path.substring(path.lastIndexOf('/') + 1);
84         entries.put(id, path);
85     }
86
87
88
89     /**
90      * The Parser will call this method before opening any external entity except
91      * the top-level document entity.
92      * @param publicId The public identifier of the external entity being referenced,
93      * or null if none was supplied.
94      * @param systemId The system identifier of the external entity being referenced.
95      * @return An InputSource object describing the new input source, or null to request that
96      * the parser open a regular URI connection to the system identifier.
97      * @throws SAXException Any SAX exception, possibly wrapping another exception.
98      * @throws IOException A Java-specific IO exception, possibly the result of creating
99      * a new InputStream or Reader for the InputSource.
100      */

101     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
102         throws IOException JavaDoc, SAXException JavaDoc {
103
104         // If we have a JDigester instance
105
if (jDigester != null) {
106             jDigester.setPublicId(publicId);
107         }
108
109         // DTD : check if we got this entry
110
String JavaDoc localPath = null;
111         if (publicId != null) {
112             localPath = (String JavaDoc) entries.get(publicId);
113         } else if (systemId != null) {
114             // Can be a schema
115
if (systemId.toLowerCase().endsWith(".xsd")) {
116                 // Retrieve basename
117
String JavaDoc baseName = systemId.substring(systemId.lastIndexOf('/') + 1);
118
119                 // Registred ?
120
localPath = (String JavaDoc) entries.get(baseName);
121             }
122         }
123
124         if (localPath == null) {
125             // This DTD/Schema was not added to this DTD/Schame local repository
126
return null;
127         }
128
129         // Return the local path source
130
return (new InputSource JavaDoc(localPath));
131     }
132
133
134     /**
135      * Add the mapping between a public Id and the local path of the DTD
136      * @param dtds Object containing the mapping PublicId --> Local URL
137      */

138     public void addDtds(DTDs dtds) {
139         if (dtds != null) {
140             Map JavaDoc dtdsMapping = dtds.getMapping();
141             if (dtdsMapping != null) {
142                 for (Iterator JavaDoc it = dtdsMapping.entrySet().iterator(); it.hasNext();) {
143                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
144                     String JavaDoc publicId = (String JavaDoc) entry.getKey();
145                     String JavaDoc localURL = (String JavaDoc) entry.getValue();
146                     entries.put(publicId, localURL);
147                 }
148             }
149         }
150     }
151
152
153     /**
154      * Add the given Schemas to the schemas available for this resolver
155      * @param schemas Definition of the schemas to add to this resolver
156      */

157     public void addSchemas(Schemas schemas) {
158         // no schemas
159
if (schemas == null) {
160             return;
161         }
162
163         List JavaDoc localSchemas = schemas.getlocalSchemas();
164
165         // there are schemas
166
if (localSchemas != null) {
167             for (Iterator JavaDoc it = localSchemas.iterator(); it.hasNext();) {
168                String JavaDoc schema = (String JavaDoc) it.next();
169                addSchema(schema);
170             }
171         }
172     }
173
174
175
176 }
177
178
Popular Tags