1 16 package org.apache.cocoon.components.language.markup; 17 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 20 import org.apache.cocoon.ProcessingException; 21 import org.apache.cocoon.components.source.SourceUtil; 22 import org.apache.cocoon.xml.AbstractXMLPipe; 23 import org.apache.cocoon.util.TraxErrorHandler; 24 import org.apache.excalibur.source.Source; 25 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.SAXException ; 28 29 import javax.xml.transform.OutputKeys ; 30 import javax.xml.transform.TransformerConfigurationException ; 31 import javax.xml.transform.TransformerFactory ; 32 import javax.xml.transform.sax.SAXResult ; 33 import javax.xml.transform.sax.SAXTransformerFactory ; 34 import javax.xml.transform.sax.TransformerHandler ; 35 import javax.xml.transform.stream.StreamResult ; 36 import java.io.StringWriter ; 37 import java.util.Properties ; 38 39 47 public class LogicsheetCodeGenerator extends AbstractLogEnabled implements MarkupCodeGenerator { 48 49 private ContentHandler serializerContentHandler; 50 51 private AbstractXMLPipe end; 52 53 private TransformerHandler currentParent; 54 55 private StringWriter writer; 56 57 58 private SAXTransformerFactory tfactory = null; 59 60 63 public void initialize() { 64 Properties format = new Properties (); 65 try { 66 TransformerHandler handler = getTransformerFactory().newTransformerHandler(); 70 71 format.put(OutputKeys.METHOD,"text"); 73 format.put(OutputKeys.INDENT,"yes"); 75 handler.getTransformer().setOutputProperties(format); 76 77 this.writer = new StringWriter (); 78 handler.setResult(new StreamResult (writer)); 79 this.serializerContentHandler = handler; 80 } catch (TransformerConfigurationException tce) { 81 getLogger().error("LogicsheetCodeGenerator: unable to get TransformerHandler", tce); 82 } 83 } 84 85 88 private SAXTransformerFactory getTransformerFactory() 89 { 90 if(tfactory == null) { 91 tfactory = (SAXTransformerFactory ) TransformerFactory.newInstance(); 92 tfactory.setErrorListener(new TraxErrorHandler(getLogger())); 93 } 94 return tfactory; 95 } 96 97 102 public void addLogicsheet(Logicsheet logicsheet) throws ProcessingException { 103 if (this.currentParent == null) { 104 this.currentParent = logicsheet.getTransformerHandler(); 106 107 this.end.setContentHandler(this.currentParent); 109 110 this.currentParent.setResult(new SAXResult (this.serializerContentHandler)); 112 } else { 113 TransformerHandler newParent = logicsheet.getTransformerHandler(); 115 116 this.currentParent.setResult(new SAXResult (newParent)); 118 119 this.currentParent = newParent; 121 this.currentParent.setResult(new SAXResult (this.serializerContentHandler)); 122 } 123 } 124 125 133 public String generateCode(Source source, AbstractXMLPipe filter) 134 throws Exception { 135 try { 136 this.end = filter; 138 SourceUtil.toSAX(source, filter); 140 return this.writer.toString(); 141 } catch (SAXException e) { 142 if(e.getException() != null) { 143 getLogger().debug("Got SAXException; Rethrowing cause exception", e); 144 throw e.getException(); 145 } 146 getLogger().debug("Got SAXException", e); 147 throw e; 148 } 149 } 150 } 151 | Popular Tags |