KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > xml > DefaultEntityResolver


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.xml;
18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.component.Component;
23 import org.apache.avalon.framework.logger.AbstractLogEnabled;
24 import org.apache.avalon.framework.parameters.ParameterException;
25 import org.apache.avalon.framework.parameters.Parameterizable;
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.Serviceable;
30 import org.apache.avalon.framework.thread.ThreadSafe;
31 import org.apache.excalibur.source.Source;
32 import org.apache.excalibur.source.SourceResolver;
33 import org.apache.xml.resolver.CatalogManager;
34 import org.apache.xml.resolver.tools.CatalogResolver;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38
39 /**
40  * A component that uses catalogs for resolving entities.
41  * This implementation uses the XML Entity and URI Resolvers from
42  * http://xml.apache.org/commons/
43  * published by Norman Walsh. More information on the catalogs can be
44  * found at
45  * http://xml.apache.org/cocoon/userdocs/concepts/catalog.html
46  *
47  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
48  * @version CVS $Id: DefaultEntityResolver.java,v 1.4 2004/02/28 11:47:36 cziegeler Exp $
49  */

50 public class DefaultEntityResolver extends AbstractLogEnabled
51   implements EntityResolver,
52              Serviceable,
53              Parameterizable,
54              ThreadSafe,
55              Disposable,
56              Component {
57
58     /** The catalog manager */
59     protected CatalogManager catalogManager = new CatalogManager();
60
61     /** The catalog resolver */
62     protected CatalogResolver catalogResolver = new CatalogResolver(catalogManager);
63
64     /** The component manager */
65     protected ServiceManager manager;
66
67     /** SourceResolver */
68     protected SourceResolver resolver;
69     
70     /**
71      * Set the configuration. Load the system catalog and apply any
72      * parameters that may have been specified in cocoon.xconf
73      * @param params The configuration information
74      * @exception ParameterException
75      */

76     public void parameterize(Parameters params)
77     throws ParameterException
78     {
79
80         // Over-ride debug level that is set by CatalogManager.properties
81
String JavaDoc verbosity = params.getParameter("verbosity", null);
82         if ( null != verbosity )
83         {
84             if (this.getLogger().isDebugEnabled())
85             {
86                 this.getLogger().debug("Setting Catalog resolver "
87                     + "verbosity level to " + verbosity);
88             }
89             int verbosityLevel = 0;
90             try
91             {
92                 verbosityLevel = Integer.parseInt(verbosity);
93                 catalogManager.setVerbosity(verbosityLevel);
94             }
95             catch (NumberFormatException JavaDoc ce1)
96             {
97                 this.getLogger().warn("Trouble setting Catalog verbosity",
98                                         ce1);
99             }
100         }
101
102         // Load the built-in catalog
103
String JavaDoc catalogFile = params.getParameter("catalog", this.defaultCatalog());
104         if ( null == catalogFile)
105         {
106             this.getLogger().warn("No default catalog defined.");
107         }
108         else
109         {
110             this.parseCatalog(catalogFile);
111         }
112
113         // Load a single additional local catalog
114
String JavaDoc localCatalogFile = params.getParameter("local-catalog", null);
115         if ( null != localCatalogFile )
116         {
117             this.parseCatalog( localCatalogFile );
118         }
119     }
120
121     /**
122      * Parse a catalog
123      */

124     protected void parseCatalog(String JavaDoc file)
125     {
126         if (this.getLogger().isDebugEnabled())
127         {
128             this.getLogger().debug("Additional Catalog is " + file);
129         }
130         
131         Source source = null;
132         try
133         {
134             source = this.resolver.resolveURI(file);
135             this.catalogResolver.getCatalog().parseCatalog(source.getURI());
136         }
137         catch (Exception JavaDoc e)
138         {
139             this.getLogger().warn("Could not get Catalog file. Trying again: " + file, e);
140                         
141             // try it again
142
if ( null != source )
143             {
144                 try
145                 {
146                     String JavaDoc mimeType = source.getMimeType();
147                     if ( null == mimeType ) mimeType =" text/plain";
148                     this.catalogResolver.getCatalog().parseCatalog(mimeType, source.getInputStream());
149                 }
150                 catch (Exception JavaDoc ex)
151                 {
152                     this.getLogger().warn("Could not get Catalog file: " + file, ex);
153                 }
154             }
155         }
156         finally
157         {
158             this.resolver.release( source );
159         }
160     }
161     
162     /**
163      * Default catalog path
164      */

165     protected String JavaDoc defaultCatalog()
166     {
167         return null;
168     }
169     
170     /**
171      * Set the global component manager.
172      * @param manager The global component manager
173      * @exception ComponentException
174      */

175     public void service(ServiceManager manager)
176     throws ServiceException
177     {
178         this.manager = manager;
179         this.resolver = (SourceResolver) this.manager.lookup( SourceResolver.ROLE );
180     }
181
182     /**
183      * Allow the application to resolve external entities.
184      *
185      * <p>The Parser will call this method before opening any external
186      * entity except the top-level document entity (including the
187      * external DTD subset, external entities referenced within the
188      * DTD, and external entities referenced within the document
189      * element): the application may request that the parser resolve
190      * the entity itself, that it use an alternative URI, or that it
191      * use an entirely different input source.</p>
192      *
193      * <p>Application writers can use this method to redirect external
194      * system identifiers to secure and/or local URIs, to look up
195      * public identifiers in a catalogue, or to read an entity from a
196      * database or other input source (including, for example, a dialog
197      * box).</p>
198      *
199      * <p>If the system identifier is a URL, the SAX parser must
200      * resolve it fully before reporting it to the application.</p>
201      *
202      * @param publicId The public identifier of the external entity
203      * being referenced, or null if none was supplied.
204      * @param systemId The system identifier of the external entity
205      * being referenced.
206      * @return An InputSource object describing the new input source,
207      * or null to request that the parser open a regular
208      * URI connection to the system identifier.
209      * @exception org.xml.sax.SAXException Any SAX exception, possibly
210      * wrapping another exception.
211      * @exception java.io.IOException A Java-specific IO exception,
212      * possibly the result of creating a new InputStream
213      * or Reader for the InputSource.
214      * @see org.xml.sax.InputSource
215      */

216     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
217             throws SAXException JavaDoc, IOException JavaDoc
218     {
219         InputSource JavaDoc altInputSource = catalogResolver.resolveEntity(publicId,
220                                         systemId);
221         if (altInputSource != null)
222         {
223             if (this.getLogger().isDebugEnabled())
224             {
225                 this.getLogger().debug("Resolved catalog entity: "
226                     + publicId + " " + altInputSource.getSystemId());
227             }
228         }
229
230         return altInputSource;
231     }
232
233     /**
234      * Dispose
235      */

236     public void dispose()
237     {
238         if ( null != this.resolver )
239         {
240             this.manager.release( this.resolver );
241         }
242     }
243 }
244
Popular Tags