KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > xscript > XScriptObject


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.xscript;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.avalon.framework.service.ServiceException;
20 import org.apache.avalon.framework.service.ServiceManager;
21 import org.apache.avalon.framework.service.Serviceable;
22
23 import org.apache.excalibur.xml.xslt.XSLTProcessor;
24 import org.apache.excalibur.xml.xslt.XSLTProcessorException;
25
26 import org.apache.cocoon.ProcessingException;
27 import org.apache.cocoon.xml.EmbeddedXMLPipe;
28 import org.apache.excalibur.xml.sax.SAXParser;
29 import org.apache.excalibur.source.Source;
30 import org.apache.excalibur.source.SourceValidity;
31
32 import org.xml.sax.ContentHandler JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36 import javax.xml.transform.stream.StreamResult JavaDoc;
37 import java.io.CharArrayWriter JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.Date JavaDoc;
40
41 /**
42  * <code>XScriptObject</code> is the root class implemented by all the
43  * object types in XScript. Every XScriptObject is essentially a
44  * Source object.
45  *
46  * @author <a HREF="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
47  * @version CVS $Id: XScriptObject.java 53976 2004-10-07 14:21:36Z vgritsenko $
48  * @since August 4, 2001
49  */

50 public abstract class XScriptObject implements Source, Serviceable {
51
52     /**
53      * The creation date of this <code>XScriptObject</code>.
54      */

55     Date JavaDoc lastModifiedDate = new Date JavaDoc();
56
57     /**
58      * The <code>XScriptManager</code> object that's managing this
59      * <code>XScriptObject</code> value.
60      */

61     XScriptManager xscriptManager;
62
63     protected ServiceManager serviceManager;
64
65     /**
66      * Creates a new <code>XScriptObject</code> instance.
67      *
68      * @param manager a <code>XScriptManager</code> value
69      */

70     public XScriptObject(XScriptManager manager) {
71         this.xscriptManager = manager;
72         ((XScriptManagerImpl) this.xscriptManager).register(this);
73     }
74
75     public void service(ServiceManager manager) throws ServiceException {
76         this.serviceManager = manager;
77     }
78
79     /**
80      * Apply the XSLT stylesheet defined by the <code>stylesheet</code>
81      * variable to this instance. Return the result of the
82      * transformation as an <code>XScriptObject</code>.
83      *
84      * @param stylesheet a <code>XScriptObject</code> value
85      * @param params a <code>Parameters</code> value containing optional
86      * arguments to be passed to the XSLT processor.
87      * @return <code>XScriptObject</code> object containing the result
88      * of the XSLT processing.
89      * @exception IllegalArgumentException if an error occurs
90      * @exception ProcessingException if an error occurs
91      */

92     public XScriptObject transform(XScriptObject stylesheet, Parameters params)
93             throws IllegalArgumentException JavaDoc, ProcessingException {
94         try {
95             CharArrayWriter JavaDoc writer = new CharArrayWriter JavaDoc();
96             StreamResult JavaDoc result = new StreamResult JavaDoc(writer);
97
98             XSLTProcessor transformer
99                     = (XSLTProcessor) serviceManager.lookup(XSLTProcessor.ROLE);
100
101             try {
102                 transformer.transform(this, stylesheet, params, result);
103             } finally {
104                 serviceManager.release(transformer);
105             }
106
107             return new XScriptObjectResult(xscriptManager, writer.toString());
108         } catch (XSLTProcessorException ex) {
109             throw new ProcessingException(ex);
110         } catch (Exception JavaDoc ex) {
111             throw new ProcessingException(ex);
112         }
113     }
114
115     public void toEmbeddedSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
116         toSAX(new EmbeddedXMLPipe(handler));
117     }
118
119     /* The Source interface methods. */
120
121     public void toSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
122         SAXParser parser = null;
123         try {
124             parser = (SAXParser) serviceManager.lookup(SAXParser.ROLE);
125             InputSource JavaDoc source = getInputSource();
126             parser.parse(source, handler);
127         } catch (SAXException JavaDoc e) {
128             throw e;
129         } catch (Exception JavaDoc e) {
130             throw new SAXException JavaDoc(e);
131         } finally {
132             if (parser != null) {
133                 serviceManager.release(parser);
134             }
135         }
136     }
137
138     public long getLastModified() {
139         return lastModifiedDate.getTime();
140     }
141
142     public abstract long getContentLength();
143
144     public InputSource JavaDoc getInputSource() throws ProcessingException, IOException JavaDoc {
145         InputSource JavaDoc is = new InputSource JavaDoc(getInputStream());
146         is.setSystemId(getURI());
147         return is;
148     }
149
150     public void recycle() {
151     }
152
153     public String JavaDoc getScheme() {
154         return "xscript";
155     }
156
157     public void refresh() {
158     }
159
160     public String JavaDoc getMimeType() {
161        return "text/xml";
162     }
163
164     public SourceValidity getValidity() {
165         return null;
166     }
167
168     public boolean exists() {
169         return true;
170     }
171 }
172
Popular Tags