KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > Logicsheet


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.language.markup;
17
18 import org.apache.excalibur.xml.xslt.XSLTProcessor;
19 import org.apache.excalibur.xml.xslt.XSLTProcessorException;
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21 import org.apache.avalon.framework.service.ServiceException;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.components.source.SourceUtil;
25 import org.apache.excalibur.source.Source;
26 import org.apache.excalibur.source.SourceException;
27 import org.apache.excalibur.source.SourceResolver;
28 import org.xml.sax.SAXException JavaDoc;
29
30 import javax.xml.transform.sax.TransformerHandler JavaDoc;
31
32 import java.io.IOException JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * A code-generation logicsheet. This class is actually a wrapper for
39  * a "standard" XSLT stylesheet stored as <code>trax.Templates</code>
40  * object. Though this will change shortly: a new markup language
41  * will be used for logicsheet authoring; logicsheets written in this
42  * language will be transformed into an equivalent XSLT stylesheet
43  * anyway... This class should probably be based on an interface...
44  *
45  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
46  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
47  * @author <a HREF="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
48  * @version CVS $Id: Logicsheet.java 278591 2005-09-04 13:24:23Z pier $
49  */

50 public class Logicsheet extends AbstractLogEnabled
51 {
52     /**
53      * The Source Resolver object for this logicsheet.
54      */

55     private SourceResolver resolver;
56
57     /**
58      * The system id to resolve
59      */

60     private String JavaDoc systemId;
61
62     /**
63      * the template namespace's list
64      */

65     protected Map JavaDoc namespaceURIs = new HashMap JavaDoc();
66
67     /**
68      * The ServiceManager of this instance.
69      */

70     private ServiceManager manager;
71
72     /**
73      * An optional filter to preprocess the logicsheet source code.
74      */

75     private LogicsheetFilter filter;
76
77     public Logicsheet(Source source, ServiceManager manager,
78                       SourceResolver resolver, LogicsheetFilter filter)
79         throws SAXException JavaDoc, IOException JavaDoc, ProcessingException
80     {
81         this.resolver = resolver;
82         this.systemId = source.getURI();
83         this.manager = manager;
84         this.filter = filter;
85     }
86
87     public Logicsheet(String JavaDoc systemId, ServiceManager manager,
88                       SourceResolver resolver, LogicsheetFilter filter)
89         throws SAXException JavaDoc, IOException JavaDoc, SourceException, ProcessingException
90     {
91         this.resolver = resolver;
92         this.manager = manager;
93         this.filter = filter;
94         Source source = null;
95         try {
96             source = this.resolver.resolveURI( systemId );
97             this.systemId = source.getURI();
98         } finally {
99             this.resolver.release( source );
100         }
101     }
102
103     /**
104      * Return true if other logicsheet has the same system id.
105      */

106     public boolean equals(Object JavaDoc other)
107     {
108         if (other == this)
109             return true;
110         if (other == null)
111             return false;
112         if (!(other instanceof Logicsheet))
113             return false;
114         Logicsheet that = (Logicsheet)other;
115         return this.systemId.equals(that.systemId);
116     }
117
118     /**
119      * Return hash code value for logicsheet.
120      */

121     public int hashCode()
122     {
123         return this.systemId.hashCode();
124     }
125
126     /**
127      * Return system id which uniquely identifies logicsheet.
128      */

129     public String JavaDoc getSystemId()
130     {
131         return this.systemId;
132     }
133
134     /**
135      * This will return the list of namespaces in this logicsheet.
136      */

137     public Map JavaDoc getNamespaceURIs() throws ProcessingException
138     {
139         // Force the parsing of the Source or, if nothing changed,
140
// return the old content of namespaces.
141
getTransformerHandler();
142         return namespaceURIs;
143     }
144
145     /**
146      * Obtain the TransformerHandler object that will perform the
147      * transformation associated with this logicsheet.
148      *
149      * @return a <code>TransformerHandler</code> value
150      */

151     public TransformerHandler JavaDoc getTransformerHandler() throws ProcessingException
152     {
153         XSLTProcessor xsltProcessor = null;
154         Source source = null;
155         try {
156             xsltProcessor = (XSLTProcessor)this.manager.lookup(XSLTProcessor.ROLE);
157             source = this.resolver.resolveURI( this.systemId );
158
159             // If the Source object is not changed, the
160
// getTransformerHandler() of XSLTProcessor will simply return
161
// the old template object. If the Source is unchanged, the
162
// namespaces are not modified either.
163
filter.setNamespaceMap(namespaceURIs);
164             return xsltProcessor.getTransformerHandler(source, filter);
165
166         } catch (ServiceException e) {
167             throw new ProcessingException("Could not obtain XSLT processor", e);
168         } catch (MalformedURLException JavaDoc e) {
169             throw new ProcessingException("Could not resolve " + this.systemId, e);
170         } catch (SourceException e) {
171             throw SourceUtil.handle("Could not resolve " + this.systemId, e);
172         } catch (IOException JavaDoc e) {
173             throw new ProcessingException("Could not resolve " + this.systemId, e);
174         } catch (XSLTProcessorException e) {
175             throw new ProcessingException("Could not transform " + this.systemId, e);
176         } finally {
177             this.manager.release(xsltProcessor);
178             // Release used resources
179
this.resolver.release( source );
180         }
181     }
182 }
183
Popular Tags