KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > xml2mi > lib > DTDResolver


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
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 of the License, or (at your option) 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 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23 package org.objectweb.jorm.xml2mi.lib;
24
25 import org.apache.xerces.xni.parser.XMLInputSource;
26 import org.apache.xerces.xni.parser.XMLEntityResolver;
27 import org.apache.xerces.xni.XMLResourceIdentifier;
28 import org.apache.xerces.xni.XNIException;
29 import org.apache.tools.ant.types.DTDLocation;
30 import org.objectweb.util.monolog.api.Logger;
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.LoggerFactory;
33 import org.objectweb.jorm.util.api.Loggable;
34
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.FileInputStream JavaDoc;
38 import java.io.FileNotFoundException JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.zip.ZipEntry JavaDoc;
42 import java.util.jar.JarFile JavaDoc;
43 import java.net.URL JavaDoc;
44
45 /**
46  * @author S.Chassande-Barrioz
47  */

48 public class DTDResolver implements XMLEntityResolver, Loggable {
49
50     private ArrayList JavaDoc dtdLocations = null;
51     private Logger logger = null;
52
53     public DTDResolver(ArrayList JavaDoc dtdLocations) {
54         this.dtdLocations = dtdLocations;
55     }
56
57     public DTDResolver(ArrayList JavaDoc dtdLocations, Logger logger) {
58         this.dtdLocations = dtdLocations;
59         this.logger = logger;
60     }
61
62     // IMPLEMENTATION OF METHODS FROM THE XMLEntityResolver INTERFACE
63

64     public XMLInputSource resolveEntity(XMLResourceIdentifier identifier) throws XNIException, IOException JavaDoc {
65         String JavaDoc publicid = identifier.getPublicId();
66         String JavaDoc location = null;
67         if (publicid == null) {
68             location = identifier.getBaseSystemId();
69         } else {
70             Iterator JavaDoc it = dtdLocations.iterator();
71             logger.log(BasicLevel.DEBUG, "Looking for the publicId: " + publicid);
72             while (it.hasNext()) {
73                 DTDLocation dtdloc = (DTDLocation) it.next();
74                 String JavaDoc current = dtdloc.getPublicId();
75                 logger.log(BasicLevel.DEBUG, "Current public id: " + current);
76                 if (current.equals(publicid)) {
77                     location = dtdloc.getLocation();
78                     break;
79                 }
80             }
81         }
82         if (location == null) {
83             logger.log(BasicLevel.WARN, "No dtd location found for the publicId:" + publicid);
84             if (identifier.getBaseSystemId() == null)
85                 throw new IOException JavaDoc("Could not resolve publicid: [" + publicid + "]");
86             logger.log(BasicLevel.WARN, "Resolved " + publicid + " to with default location " + identifier.getBaseSystemId());
87             return new XMLInputSource(identifier);
88         }
89
90         InputStream JavaDoc is = null;
91
92         // try in the file system
93
try {
94             is = new FileInputStream JavaDoc(location);
95             logger.log(BasicLevel.DEBUG, "Resolved " + publicid + " to local file " + location);
96         } catch (FileNotFoundException JavaDoc e) {
97             logger.log(BasicLevel.DEBUG, location + " is not a local file ");
98         }
99
100         if (is == null) {
101             URL JavaDoc urldtd = null;
102             try {
103                 urldtd = new URL JavaDoc(location);
104                 is = urldtd.openStream();
105                 logger.log(BasicLevel.DEBUG, "Resolved " + publicid + " to url " + urldtd);
106             } catch (IOException JavaDoc e) {
107                 logger.log(BasicLevel.DEBUG, location + " is not an URL ");
108             }
109         }
110
111         // Try in the classpath
112
if (is == null) {
113             is = getClass().getClassLoader().getResourceAsStream(location);
114             if (is != null) {
115                 logger.log(BasicLevel.DEBUG, "Resolved " + publicid + " to resource " + location);
116             } else {
117                 logger.log(BasicLevel.DEBUG, location + " is not availlable in the classpath");
118             }
119         }
120
121         //the location can be a jar file
122
if (is == null) {
123             int idx = location.indexOf(".jar!");
124             if (idx != -1) {
125                 try {
126                     JarFile JavaDoc jarfile = new JarFile JavaDoc(location.substring(0, idx));
127                     // the path is the jar file, can continue
128
if (jarfile != null) {
129                         ZipEntry JavaDoc zipentry = jarfile.getEntry(
130                                 location.substring(idx + 5, location.length()));
131                         // if the entry (file) exists in the jar file
132
// extracts it and return an input stream from it
133
if (zipentry != null) {
134                             is = jarfile.getInputStream(zipentry);
135                         }
136                     }
137                 } catch (Exception JavaDoc e) {
138                     logger.log(BasicLevel.DEBUG, location + " is not an jar file");
139                 }
140             }
141         }
142
143         if (is != null)
144             return new XMLInputSource(publicid,
145                                       identifier.getLiteralSystemId(),
146                                       identifier.getBaseSystemId(),
147                                       is,
148                                       null);
149         else
150             throw new IOException JavaDoc("Could not resolve publicid: ["
151                                   + publicid + "] location [" + location + "]");
152     }
153
154     // IMPLEMENTATION OF METHODS FROM THE Loggable INTERFACE //
155
public Logger getLogger() {
156         return logger;
157     }
158
159     public LoggerFactory getLoggerFactory() {
160         return null;
161     }
162
163     public void setLogger(Logger logger) {
164         this.logger = logger;
165     }
166
167     public void setLoggerFactory(LoggerFactory loggerfactory) {
168     }
169 }
170
Popular Tags