KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > xni > XNIConfigurableFileGenerator


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

17 package org.apache.forrest.xni;
18
19 import org.apache.cocoon.generation.ServiceableGenerator;
20 import org.apache.cocoon.ProcessingException;
21 import org.apache.cocoon.ResourceNotFoundException;
22 import org.apache.cocoon.components.source.SourceUtil;
23 import org.apache.cocoon.environment.SourceResolver;
24 import org.apache.cocoon.caching.CacheableProcessingComponent;
25 import org.apache.avalon.excalibur.pool.Recyclable;
26 import org.apache.excalibur.xml.EntityResolver;
27 import org.apache.avalon.framework.parameters.Parameters;
28 import org.apache.avalon.framework.parameters.ParameterException;
29 import org.apache.xerces.xni.parser.XMLParserConfiguration;
30 import org.apache.xerces.xni.parser.XMLConfigurationException;
31 import org.apache.xerces.util.EntityResolverWrapper;
32 import org.apache.xerces.parsers.AbstractSAXParser;
33 import org.apache.excalibur.source.*;
34
35 import org.xml.sax.*;
36
37 import java.util.Map JavaDoc;
38 import java.io.IOException JavaDoc;
39
40
41 /** Class <code>org.apache.forrest.components.generator.XNIConfigurableFileGenerator</code>
42  * This class implements a Cocoon Generator that is configurable through
43  * the XNI mechanism that is built into xerces 2.x
44  * The generator is used with:
45  *
46     <map:generators default="">
47       <map:generator name="nekodtd" SRC="org.apache.forrest.components.generator.XNIConfigurableFileGenerator" label="content" />
48     </map:generators>
49  *
50  * and:
51  *
52    <map:match pattern="foobar">
53      <map:generate type="nekodtd" SRC="resources/schema/dtd/{1}.dtd">
54        <map:parameter name="config-class" value="org.cyberneko.dtd.DTDConfiguration" />
55      </map:generate>
56      <map:serialize type="xml"/>
57    </map:match>
58  *
59  * TODO: check how some XNIConfigurableXMLReader component (Excalibur style
60  * like the JaxpParser) can be built to do this. Then the complete
61  * parser can be recycled.
62  *
63  */

64 public class XNIConfigurableFileGenerator
65 extends ServiceableGenerator implements CacheableProcessingComponent, Recyclable
66 {
67
68   /** Default constructor
69    *
70    */

71   public XNIConfigurableFileGenerator()
72   {
73   }
74
75   public static final String JavaDoc CONFIGCLASS_PARAMETER = "config-class";
76   public static final String JavaDoc FULL_ENTITY_RESOLVER_PROPERTY_URI =
77       org.apache.xerces.impl.Constants.XERCES_PROPERTY_PREFIX +
78       org.apache.xerces.impl.Constants.ENTITY_RESOLVER_PROPERTY;
79   /** The source */
80   private Source inputSource;
81
82   /** The XNIConfiguredParser */
83   XMLParserConfiguration parserConfig;
84
85   /**
86    * Recycle this component.
87    * All instance variables are set to <code>null</code>.
88    */

89   public void recycle() {
90     if (this.inputSource != null) {
91       this.resolver.release(inputSource);
92       this.inputSource = null;
93     }
94     super.recycle();
95  }
96
97   /**
98    * Copy paste en serious cut from cocoon HTML Generator
99    */

100   public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
101   throws ProcessingException, SAXException, IOException JavaDoc {
102     super.setup(resolver, objectModel, src, par);
103     String JavaDoc parserName = null;
104
105     try {
106         this.inputSource = resolver.resolveURI(super.source);
107         parserName = par.getParameter(CONFIGCLASS_PARAMETER);
108         parserConfig = (XMLParserConfiguration)Class.forName(parserName).newInstance();
109     } catch(ParameterException e) {
110       getLogger().error("Missing parameter " + CONFIGCLASS_PARAMETER, e);
111       throw new ProcessingException("XNIConfigurable.setup()",e);
112     } catch(InstantiationException JavaDoc e) {
113       getLogger().error("Can not make instance of " + parserName, e);
114       throw new ProcessingException("XNIConfigurable.setup()",e);
115     } catch(IllegalAccessException JavaDoc e) {
116       getLogger().error("Can not access constructor of " + parserName, e);
117       throw new ProcessingException("XNIConfigurable.setup()",e);
118     } catch(ClassNotFoundException JavaDoc e) {
119       getLogger().error("Can not find " + parserName, e);
120       throw new ProcessingException("XNIConfigurable.setup()",e);
121     } catch (SourceException e) {
122       getLogger().error("Can not resolve " + super.source);
123       throw SourceUtil.handle("Unable to resolve " + super.source, e);
124     }
125   }
126
127   /**
128    * Generate the unique key.
129    * This key must be unique inside the space of this component.
130    * This method must be invoked before the generateValidity() method.
131    *
132    * @return The generated key or <code>0</code> if the component
133    * is currently not cacheable.
134    */

135   public java.io.Serializable JavaDoc getKey() {
136     return this.inputSource.getURI();
137   }
138
139   // For backwards-compat with old versions of Cocoon
140
public java.io.Serializable JavaDoc generateKey() {
141     return getKey();
142   }
143
144   /**
145    * Generate the validity object.
146    * Before this method can be invoked the generateKey() method
147    * must be invoked.
148    *
149    * @return The generated validity object or <code>null</code> if the
150    * component is currently not cacheable.
151    */

152   public SourceValidity getValidity() {
153     if (this.inputSource.getLastModified() != 0) {
154       this.inputSource.getValidity();
155     }
156     return null;
157   }
158
159   // For backwards-compat with old versions of Cocoon
160
public SourceValidity generateValidity() {
161     return getValidity();
162   }
163
164   /**
165    * Generate XML data.
166    */

167   public void generate()
168   throws IOException JavaDoc, SAXException, ProcessingException {
169     EntityResolver catalogResolver = null;
170     final String JavaDoc[] extendRecognizedProperties = {FULL_ENTITY_RESOLVER_PROPERTY_URI};
171     try {
172       getLogger().debug("XNIConfigurable generator start generate()");
173
174       //TODO?: Make XNIConfigurableParser an avalon component in it's own right
175
// Let the resolver and namespace stuff be configured and composed on that level.
176
// some ideas on this: (any others?)
177
// - build a XNIConfigurableParser Component Interface with its ROLE
178
// - add a XNIConfigurableParserSelector that can select() based on full qualified class name
179
// - and release() after usage
180
// the select method could do 6 next lines:
181
catalogResolver = (EntityResolver)this.manager.lookup(EntityResolver.ROLE);
182       parserConfig.addRecognizedProperties(extendRecognizedProperties);
183       parserConfig.setProperty(FULL_ENTITY_RESOLVER_PROPERTY_URI, new EntityResolverWrapper(catalogResolver));
184       final XMLReader parser = new AbstractSAXParser(parserConfig){};
185       parser.setFeature("http://xml.org/sax/features/namespaces", true);
186       parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
187       parser.setContentHandler(this.contentHandler);
188       parser.parse(new InputSource(this.inputSource.getInputStream()));
189
190     } catch (IOException JavaDoc e){
191       getLogger().warn("XNIConfigurable.generate()", e);
192       throw new ResourceNotFoundException("Could not get resource to process:\n["
193               + "src = " + this.inputSource.getURI() + "]\n", e);
194     } catch (SAXException e){
195       getLogger().error("XNIConfigurable.generate()", e);
196       throw e;
197     } catch (XMLConfigurationException e) {
198       getLogger().error( "Misconfig " + e.getType(), e);
199       throw new ProcessingException("XNIConfigurable.generate()",e);
200     } catch (Exception JavaDoc e){
201       getLogger().error("Some strange thing just happened!!", e);
202       throw new ProcessingException("XNIConfigurable.generate()",e);
203     } finally {
204       this.manager.release(catalogResolver);
205     }
206   }
207 }
208
Popular Tags