KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > xml > CatalogLocation


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.util.xml;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URI JavaDoc;
30 import java.net.URISyntaxException JavaDoc;
31 import java.net.URL JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.util.xml.catalog.CatalogManager;
35 import org.jboss.util.xml.catalog.Resolver;
36 import org.xml.sax.InputSource JavaDoc;
37
38 /**
39  *
40  * A ThreadSpecificCatalogs class maintains all catalogfiles <code>catolog.xml</code> found in the
41  * <code>Thread.currentThread().getContextClassLoader()</code>.
42  *
43  * @author <a HREF="wiesed@gmail.com">Daniel Wiese</a>
44  * @version $Revision: 2082 $
45  */

46 public class CatalogLocation
47 {
48
49    private static Logger log = Logger.getLogger(CatalogLocation.class);
50
51    /**
52     * The catalog is assembled by taking into account all accessible resources whose name is
53     * META-INF/jax-ws-catalog.xml. Each resource MUST be a valid entity catalog according to the XML Catalogs
54     */

55    private static final String JavaDoc[] catalogFilesNames =
56    {"META-INF/jax-ws-catalog.xml", "WEB-INF/jax-ws-catalog.xml", "jax-ws-catalog.xml"};
57
58    private final Resolver catologResolver;
59    
60    private final URL JavaDoc location;
61
62    private boolean isLastEntityResolved = false;
63
64    static
65    {
66       // If the source document contains "oasis-xml-catalog" processing instructions,
67
// should they be used?
68
System.setProperty("xml.catalog.allowPI", "true");
69       //Which identifier is preferred, "public" or "system"?
70
System.setProperty("xml.catalog.prefer", "public");
71       //If non-zero, the Catalog classes will print informative and debugging messages.
72
//The higher the number, the more messages.
73
System.setProperty("xml.catalog.verbosity", "0");
74    }
75
76    /**
77     * Create a new CatalogLocation.
78     * @param url - the location of the catalog xml file
79     * @throws IOException if the catalog files cannot be loaded
80     */

81    public CatalogLocation(URL JavaDoc url) throws IOException JavaDoc
82    {
83       catologResolver = new Resolver();
84       catologResolver.setCatalogManager(CatalogManager.getStaticManager());
85       catologResolver.setupReaders();
86       catologResolver.parseCatalog(url);
87       this.location=url;
88    }
89
90    /**
91     * Tries to resolve the entity using the thread specific catolog resolvers
92     *
93     * @param publicId - Public ID of DTD, or null if it is a schema
94     * @param systemId - the system ID of DTD or Schema
95     * @return InputSource of entity
96     * @throws MalformedURLException - if the url is wrong
97     * @throws IOException - error reading the local file
98     */

99    public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws MalformedURLException JavaDoc, IOException JavaDoc
100    {
101       String JavaDoc resolvedURI = catologResolver.resolveSystem(systemId);
102
103       if (resolvedURI == null)
104       {
105          resolvedURI = catologResolver.resolvePublic(publicId, systemId);
106       }
107
108       if (resolvedURI != null)
109       {
110          final InputSource JavaDoc is = new InputSource JavaDoc();
111          is.setPublicId(publicId);
112          is.setSystemId(systemId);
113          is.setByteStream(this.loadResource(resolvedURI));
114          this.isLastEntityResolved = true;
115          return is;
116       }
117       else
118       {
119          //resource could�t be resloved
120
this.isLastEntityResolved = false;
121          return null;
122       }
123    }
124
125    /**
126     * Seach the path for oasis catalog files. The classpath of
127     * <code>Thread.currentThread().getContextClassLoader()</code>
128     * is used for the lookup.
129     * @return the url where the <code>jax-ws-catalog.xml</code> is located
130     * @throws IOException if the catalog files cannot be loaded
131     *
132     */

133    public static URL JavaDoc lookupCatalogFiles() throws IOException JavaDoc
134    {
135       URL JavaDoc url = null;
136       //JAXWS-2.-0 spec, Line 27:the current context class loader MUST be used to
137
//retrieve all the resources with the specified name
138
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
139
140       for (int i = 0; i < catalogFilesNames.length; i++)
141       {
142          url = loader.getResource(catalogFilesNames[i]);
143          //use the first hit
144
if (url != null)
145          {
146             break;
147          }
148       }
149
150       return url;
151    }
152
153    /**
154     * Returns the boolean value to inform id DTD was found in the XML file or not
155     *
156     *
157     * @return boolean - true if DTD was found in XML
158     */

159    public boolean isEntityResolved()
160    {
161       return this.isLastEntityResolved;
162    }
163
164    /**
165     *
166     * Loads the resolved resource.
167     *
168     * @param resolvedURI - the full qualified URI of the resoved local ressource
169     * @return - the inputstram represnting this resource
170     * @throws IOException - if the resource cannot be opened
171     */

172    private InputStream JavaDoc loadResource(String JavaDoc resolvedURI) throws IOException JavaDoc
173    {
174       try
175       {
176          final URI JavaDoc toLoad = new URI JavaDoc(resolvedURI);
177          InputStream JavaDoc inputStream = null;
178          if (toLoad != null)
179          {
180             try
181             {
182                inputStream = new FileInputStream JavaDoc(new File JavaDoc(toLoad));
183             }
184             catch (IOException JavaDoc e)
185             {
186                log.error("Failed to open url stream", e);
187                throw e;
188             }
189          }
190          return inputStream;
191       }
192       catch (URISyntaxException JavaDoc e)
193       {
194          log.error("The URI (" + resolvedURI + ") is malfomed");
195          throw new IOException JavaDoc("The URI (" + resolvedURI + ") is malfomed");
196       }
197    }
198
199    /**
200     * To catalog locations are qual if the location is equal.
201     * @param other - the catlog location to compare
202     * @return true if equal
203     */

204    public boolean equals(Object JavaDoc other)
205    {
206      boolean back=false;
207       if (other!=null && other instanceof CatalogLocation){
208          final CatalogLocation otherC=(CatalogLocation)other;
209          back=this.location.equals(otherC.location);
210       }
211       
212       return back;
213    }
214
215    /**
216     * Two catalog locations have the same hash code if the location is equal.
217     * @return - the hash code
218     */

219    public int hashCode()
220    {
221       return this.location.hashCode();
222    }
223    
224    
225 }
226
Popular Tags