KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > xml > 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 import java.io.IOException;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Map;
12
13 import org.xml.sax.EntityResolver;
14 import org.xml.sax.InputSource;
15 import org.xml.sax.SAXException;
16 published by the Free Software Foundation; either
17  * version 2.1 of the License, or 1any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27  * USA
28  *
29  * Initial developer: Florent BENOIT
30  * --------------------------------------------------------------------------
31  * $Id: JEntityResolver.java,v 1.4 2003/11/28 14:26:08 sauthieg Exp $
32  * --------------------------------------------------------------------------
33  */

34
35 package com.bull.eclipse.jonas.utils.xml;
36
37
38 import java.io.IOException JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import org.xml.sax.EntityResolver JavaDoc;
45 import org.xml.sax.InputSource JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47
48 import com.bull.eclipse.jonas.utils.xml.desc.DTDs;
49 import com.bull.eclipse.jonas.utils.xml.desc.Schemas;
50
51 /**
52  * This class defines the entity resolver used to resolve DTDs/Schemas during
53  * the xml parsing
54  * @author Florent Benoit
55  */

56 public class JEntityResolver implements EntityResolver JavaDoc {
57
58     /**
59      * Map which contains DTDs and Schemas available in local
60      */

61     private HashMap JavaDoc entries = new HashMap JavaDoc();
62
63     /**
64      * JDigester Object associated
65      */

66     private JDigester jDigester = null;
67
68
69     /**
70      * Constructor
71      */

72     public JEntityResolver(JDigester jd) {
73         super();
74         jDigester = jd;
75     }
76
77     /**
78      * Constructor without JDigester instance (used in WsGen only)
79      */

80     public JEntityResolver() {
81         super();
82     }
83
84     /**
85      * Add a local schema to the known entries
86      * @param path path to a local schema
87      */

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

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

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

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