001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.xscript;
018:
019: import org.apache.avalon.framework.parameters.Parameters;
020: import org.apache.avalon.framework.service.ServiceException;
021: import org.apache.avalon.framework.service.ServiceManager;
022: import org.apache.avalon.framework.service.Serviceable;
023:
024: import org.apache.excalibur.xml.xslt.XSLTProcessor;
025: import org.apache.excalibur.xml.xslt.XSLTProcessorException;
026:
027: import org.apache.cocoon.ProcessingException;
028: import org.apache.cocoon.xml.EmbeddedXMLPipe;
029: import org.apache.excalibur.xml.sax.SAXParser;
030: import org.apache.excalibur.source.Source;
031: import org.apache.excalibur.source.SourceValidity;
032:
033: import org.xml.sax.ContentHandler;
034: import org.xml.sax.InputSource;
035: import org.xml.sax.SAXException;
036:
037: import javax.xml.transform.stream.StreamResult;
038: import java.io.CharArrayWriter;
039: import java.io.IOException;
040: import java.util.Date;
041:
042: /**
043: * <code>XScriptObject</code> is the root class implemented by all the
044: * object types in XScript. Every XScriptObject is essentially a
045: * Source object.
046: *
047: * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
048: * @version CVS $Id: XScriptObject.java 433543 2006-08-22 06:22:54Z crossley $
049: * @since August 4, 2001
050: */
051: public abstract class XScriptObject implements Source, Serviceable {
052:
053: /**
054: * The creation date of this <code>XScriptObject</code>.
055: */
056: Date lastModifiedDate = new Date();
057:
058: /**
059: * The <code>XScriptManager</code> object that's managing this
060: * <code>XScriptObject</code> value.
061: */
062: XScriptManager xscriptManager;
063:
064: protected ServiceManager serviceManager;
065:
066: /**
067: * Creates a new <code>XScriptObject</code> instance.
068: *
069: * @param manager a <code>XScriptManager</code> value
070: */
071: public XScriptObject(XScriptManager manager) {
072: this .xscriptManager = manager;
073: ((XScriptManagerImpl) this .xscriptManager).register(this );
074: }
075:
076: public void service(ServiceManager manager) throws ServiceException {
077: this .serviceManager = manager;
078: }
079:
080: /**
081: * Apply the XSLT stylesheet defined by the <code>stylesheet</code>
082: * variable to this instance. Return the result of the
083: * transformation as an <code>XScriptObject</code>.
084: *
085: * @param stylesheet a <code>XScriptObject</code> value
086: * @param params a <code>Parameters</code> value containing optional
087: * arguments to be passed to the XSLT processor.
088: * @return <code>XScriptObject</code> object containing the result
089: * of the XSLT processing.
090: * @exception IllegalArgumentException if an error occurs
091: * @exception ProcessingException if an error occurs
092: */
093: public XScriptObject transform(XScriptObject stylesheet,
094: Parameters params) throws IllegalArgumentException,
095: ProcessingException {
096: try {
097: CharArrayWriter writer = new CharArrayWriter();
098: StreamResult result = new StreamResult(writer);
099:
100: XSLTProcessor transformer = (XSLTProcessor) serviceManager
101: .lookup(XSLTProcessor.ROLE);
102:
103: try {
104: transformer.transform(this , stylesheet, params, result);
105: } finally {
106: serviceManager.release(transformer);
107: }
108:
109: return new XScriptObjectResult(xscriptManager, writer
110: .toString());
111: } catch (XSLTProcessorException ex) {
112: throw new ProcessingException(ex);
113: } catch (Exception ex) {
114: throw new ProcessingException(ex);
115: }
116: }
117:
118: public void toEmbeddedSAX(ContentHandler handler)
119: throws SAXException {
120: toSAX(new EmbeddedXMLPipe(handler));
121: }
122:
123: /* The Source interface methods. */
124:
125: public void toSAX(ContentHandler handler) throws SAXException {
126: SAXParser parser = null;
127: try {
128: parser = (SAXParser) serviceManager.lookup(SAXParser.ROLE);
129: InputSource source = getInputSource();
130: parser.parse(source, handler);
131: } catch (SAXException e) {
132: throw e;
133: } catch (Exception e) {
134: throw new SAXException(e);
135: } finally {
136: if (parser != null) {
137: serviceManager.release(parser);
138: }
139: }
140: }
141:
142: public long getLastModified() {
143: return lastModifiedDate.getTime();
144: }
145:
146: public abstract long getContentLength();
147:
148: public InputSource getInputSource() throws ProcessingException,
149: IOException {
150: InputSource is = new InputSource(getInputStream());
151: is.setSystemId(getURI());
152: return is;
153: }
154:
155: public void recycle() {
156: }
157:
158: public String getScheme() {
159: return "xscript";
160: }
161:
162: public void refresh() {
163: }
164:
165: public String getMimeType() {
166: return "text/xml";
167: }
168:
169: public SourceValidity getValidity() {
170: return null;
171: }
172:
173: public boolean exists() {
174: return true;
175: }
176: }
|