KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > resolver > ResolverImpl


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

16 package org.apache.cocoon.components.resolver;
17
18 import org.apache.excalibur.xml.EntityResolver;
19 import org.apache.avalon.framework.activity.Disposable;
20 import org.apache.avalon.framework.component.ComponentException;
21 import org.apache.avalon.framework.component.ComponentManager;
22 import org.apache.avalon.framework.component.Composable;
23 import org.apache.avalon.framework.context.Context;
24 import org.apache.avalon.framework.context.ContextException;
25 import org.apache.avalon.framework.context.Contextualizable;
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.parameters.ParameterException;
28 import org.apache.avalon.framework.parameters.Parameterizable;
29 import org.apache.avalon.framework.parameters.Parameters;
30 import org.apache.avalon.framework.thread.ThreadSafe;
31 import org.apache.cocoon.Constants;
32 import org.apache.xml.resolver.CatalogManager;
33 import org.apache.xml.resolver.tools.CatalogResolver;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 import java.io.IOException JavaDoc;
38
39
40 /**
41  * A component that uses catalogs for resolving entities.
42  * This implementation uses the XML Entity and URI Resolvers from
43  * http://xml.apache.org/commons/
44  * published by Norman Walsh. More information on the catalogs can be
45  * found at
46  * http://cocoon.apache.org/userdocs/concepts/catalog.html
47  *
48  * The catalog is by default loaded from "WEB-INF/entities/catalog".
49  * This can be configured by the "catalog" parameter in the cocoon.xconf:
50  * <entity-resolver>
51  * <parameter name="catalog" value="mycatalog"/>
52  * </entity-resolver>
53  *
54  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
55  * @author <a HREF="mailto:crossley@apache.org">David Crossley</a>
56  * @version CVS $Id: ResolverImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
57  * @since 2.0rc1
58  */

59 public class ResolverImpl extends AbstractLogEnabled
60   implements EntityResolver,
61              Resolver,
62              Contextualizable,
63              Composable,
64              Parameterizable,
65              ThreadSafe,
66              Disposable {
67
68     /** The catalog manager */
69     protected CatalogManager catalogManager = new CatalogManager();
70
71     /** The catalog resolver */
72     protected CatalogResolver catalogResolver = new CatalogResolver(catalogManager);
73
74     /** The component manager */
75     protected ComponentManager manager = null;
76
77     /** The context */
78     protected org.apache.cocoon.environment.Context context;
79
80     /** Contextualize this class */
81     public void contextualize(Context context)
82             throws ContextException {
83         this.context = (org.apache.cocoon.environment.Context)
84                         context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
85     }
86
87
88     /**
89      * Set the configuration. Load the system catalog and apply any
90      * parameters that may have been specified in cocoon.xconf
91      * @param params The configuration information
92      * @exception ParameterException
93      */

94     public void parameterize(Parameters params) throws ParameterException {
95
96         /* Over-ride debug level that is set by CatalogManager.properties */
97         String JavaDoc verbosity = params.getParameter("verbosity", "");
98         if (verbosity != "") {
99             if (this.getLogger().isDebugEnabled()) {
100             this.getLogger().debug("Setting Catalog resolver "
101                 + "verbosity level to " + verbosity);
102             }
103             int verbosityLevel = 0;
104             try {
105                 verbosityLevel = Integer.parseInt(verbosity);
106                 catalogManager.setVerbosity(verbosityLevel);
107             } catch (NumberFormatException JavaDoc ce1) {
108                 this.getLogger().warn("Trouble setting Catalog verbosity",
109                                         ce1);
110             }
111         }
112
113         /* Load the built-in catalog */
114         String JavaDoc catalogFile = params.getParameter("catalog",
115                                 "/WEB-INF/entities/catalog");
116         try {
117             String JavaDoc catalogURL = null;
118             catalogURL = this.context.getRealPath(catalogFile);
119             if (catalogURL == null) {
120                 catalogURL =
121                     this.context.getResource(catalogFile).toExternalForm();
122             }
123             if (this.getLogger().isDebugEnabled()) {
124                 this.getLogger().debug("System OASIS Catalog URL is "
125                     + catalogURL);
126             }
127             catalogResolver.getCatalog().parseCatalog(catalogURL);
128         } catch (Exception JavaDoc e) {
129             this.getLogger().warn("Could not get Catalog URL", e);
130         }
131
132         /* Load a single additional local catalog */
133         String JavaDoc localCatalogFile = params.getParameter("local-catalog", null);
134         if (localCatalogFile != null) {
135             try {
136                 if (this.getLogger().isDebugEnabled()) {
137                     this.getLogger().debug("Additional Catalog is "
138                         + localCatalogFile);
139                 }
140                 catalogResolver.getCatalog().parseCatalog(localCatalogFile);
141             } catch (Exception JavaDoc e) {
142                 this.getLogger().warn("Could not get local Catalog file", e);
143             }
144         }
145     }
146
147     /**
148      * Set the global component manager.
149      * @param manager The global component manager
150      * @exception ComponentException
151      */

152     public void compose(ComponentManager manager) throws ComponentException {
153         if ((this.manager == null) && (manager != null)) {
154             this.manager = manager;
155         }
156     }
157
158     /**
159      * Allow the application to resolve external entities.
160      *
161      * <p>The Parser will call this method before opening any external
162      * entity except the top-level document entity (including the
163      * external DTD subset, external entities referenced within the
164      * DTD, and external entities referenced within the document
165      * element): the application may request that the parser resolve
166      * the entity itself, that it use an alternative URI, or that it
167      * use an entirely different input source.</p>
168      *
169      * <p>Application writers can use this method to redirect external
170      * system identifiers to secure and/or local URIs, to look up
171      * public identifiers in a catalogue, or to read an entity from a
172      * database or other input source (including, for example, a dialog
173      * box).</p>
174      *
175      * <p>If the system identifier is a URL, the SAX parser must
176      * resolve it fully before reporting it to the application.</p>
177      *
178      * @param publicId The public identifier of the external entity
179      * being referenced, or null if none was supplied.
180      * @param systemId The system identifier of the external entity
181      * being referenced.
182      * @return An InputSource object describing the new input source,
183      * or null to request that the parser open a regular
184      * URI connection to the system identifier.
185      * @exception org.xml.sax.SAXException Any SAX exception, possibly
186      * wrapping another exception.
187      * @exception java.io.IOException A Java-specific IO exception,
188      * possibly the result of creating a new InputStream
189      * or Reader for the InputSource.
190      * @see org.xml.sax.InputSource
191      */

192     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
193             throws SAXException JavaDoc, IOException JavaDoc {
194 /*
195         if (this.getLogger().isDebugEnabled()) {
196             this.getLogger().debug("CER resolution: publicId="
197                 + publicId + " systemId=" + systemId);
198         }
199 */

200         InputSource JavaDoc altInputSource = catalogResolver.resolveEntity(publicId,
201                                         systemId);
202         if (altInputSource != null) {
203             if (this.getLogger().isDebugEnabled()) {
204                 this.getLogger().debug("Resolved catalog entity: "
205                     + publicId + " " + altInputSource.getSystemId());
206             }
207         }
208 /*
209         else {
210             if (this.getLogger().isDebugEnabled()) {
211                 this.getLogger().debug("CER: altInputSource is null");
212             }
213         }
214 */

215         return altInputSource;
216     }
217
218     /**
219      * Dispose
220      */

221     public void dispose() {
222     }
223 }
224
Popular Tags