KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > util > xml > SchemaEntityResolver


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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 any 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  * --------------------------------------------------------------------------
22  * $Id: SchemaEntityResolver.java 120 2006-03-05 19:51:32Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.util.xml;
27
28
29 import java.io.IOException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.xml.sax.EntityResolver JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /**
39  * Entity resolver allowing to find schema within the classloader.
40  * @author Florent Benoit
41  */

42 public class SchemaEntityResolver implements EntityResolver JavaDoc {
43
44     /**
45      * Map where the schemas URLs are stored.
46      */

47     private Map JavaDoc<String JavaDoc, String JavaDoc> schemasUrls = null;
48
49     /**
50      * Constructor. Finds the XSD with classloader.
51      * @param schemas the name of the schemas to resolve.
52      */

53     public SchemaEntityResolver(final String JavaDoc[] schemas) {
54         schemasUrls = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
55         URL JavaDoc url = null;
56         for (int i = 0; i < schemas.length; i++) {
57             url = SchemaEntityResolver.class.getResource("/" + schemas[i]);
58             if (url == null) {
59                 throw new IllegalStateException JavaDoc("'" + schemas[i] + "' was not found in the current classloader !");
60             }
61             String JavaDoc urlString = url.toString();
62             String JavaDoc id = urlString.substring(urlString.lastIndexOf('/') + 1);
63             schemasUrls.put(id, urlString);
64
65         }
66     }
67
68     /**
69      * The Parser will call this method before opening any external entity
70      * except the top-level document entity.
71      * @param publicId The public identifier of the external entity being
72      * referenced, or null if none was supplied.
73      * @param systemId The system identifier of the external entity being
74      * referenced.
75      * @return An InputSource object describing the new input source, or null to
76      * request that the parser open a regular URI connection to the
77      * system identifier.
78      * @throws SAXException Any SAX exception, possibly wrapping another
79      * exception.
80      * @throws IOException A Java-specific IO exception, possibly the result of
81      * creating a new InputStream or Reader for the InputSource.
82      */

83     public InputSource JavaDoc resolveEntity(final String JavaDoc publicId, final String JavaDoc systemId) throws IOException JavaDoc, SAXException JavaDoc {
84
85         String JavaDoc localPath = null;
86
87         if (systemId != null) {
88             // Can be a schema
89
if (systemId.toLowerCase().endsWith(".xsd")) {
90                 // Retrieve basename
91
String JavaDoc baseName = systemId.substring(systemId.lastIndexOf('/') + 1);
92
93                 // Registred ?
94
localPath = schemasUrls.get(baseName);
95             }
96         }
97
98         // schema not found
99
if (localPath == null) {
100             throw new SAXException JavaDoc("No XSD found for '" + systemId + "'.");
101         }
102
103         // Return the local path source
104
return (new InputSource JavaDoc(localPath));
105     }
106
107 }
108
Popular Tags