1 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 ; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.SAXException ; 35 36 import javax.xml.transform.stream.StreamResult ; 37 import java.io.CharArrayWriter ; 38 import java.io.IOException ; 39 import java.util.Date ; 40 41 50 public abstract class XScriptObject implements Source, Serviceable { 51 52 55 Date lastModifiedDate = new Date (); 56 57 61 XScriptManager xscriptManager; 62 63 protected ServiceManager serviceManager; 64 65 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 92 public XScriptObject transform(XScriptObject stylesheet, Parameters params) 93 throws IllegalArgumentException , ProcessingException { 94 try { 95 CharArrayWriter writer = new CharArrayWriter (); 96 StreamResult result = new StreamResult (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 ex) { 111 throw new ProcessingException(ex); 112 } 113 } 114 115 public void toEmbeddedSAX(ContentHandler handler) throws SAXException { 116 toSAX(new EmbeddedXMLPipe(handler)); 117 } 118 119 120 121 public void toSAX(ContentHandler handler) throws SAXException { 122 SAXParser parser = null; 123 try { 124 parser = (SAXParser) serviceManager.lookup(SAXParser.ROLE); 125 InputSource source = getInputSource(); 126 parser.parse(source, handler); 127 } catch (SAXException e) { 128 throw e; 129 } catch (Exception e) { 130 throw new SAXException (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 getInputSource() throws ProcessingException, IOException { 145 InputSource is = new InputSource (getInputStream()); 146 is.setSystemId(getURI()); 147 return is; 148 } 149 150 public void recycle() { 151 } 152 153 public String getScheme() { 154 return "xscript"; 155 } 156 157 public void refresh() { 158 } 159 160 public String 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 |