| 1 16 package org.outerj.daisy.frontend; 17 18 import org.apache.cocoon.transformation.AbstractTransformer; 19 import org.apache.cocoon.environment.SourceResolver; 20 import org.apache.cocoon.ProcessingException; 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.Attributes ; 24 import org.xml.sax.Locator ; 25 26 import javax.xml.transform.sax.SAXTransformerFactory ; 27 import javax.xml.transform.sax.TransformerHandler ; 28 import javax.xml.transform.stream.StreamResult ; 29 import javax.xml.transform.TransformerConfigurationException ; 30 import javax.xml.transform.OutputKeys ; 31 import java.util.Map ; 32 import java.io.IOException ; 33 import java.io.CharArrayWriter ; 34 35 38 public class SerializeTransformer extends AbstractTransformer { 39 private int nestingLevel; 40 private int serializeNestingLevel; 41 private boolean inSerialize; 42 private TransformerHandler serializer; 43 private CharArrayWriter writer; 44 private static final String NAMESPACE = "http://outerx.org/daisywiki/1.0#serializer"; 45 private static final String TRIGGER_EL = "serialize"; 46 47 SAXTransformerFactory transformerFactory = (SAXTransformerFactory ) SAXTransformerFactory.newInstance(); 48 49 public void setup(SourceResolver sourceResolver, Map map, String s, Parameters parameters) throws ProcessingException, SAXException , IOException { 50 this.nestingLevel = 0; 51 } 52 53 public void recycle() { 54 super.recycle(); 55 this.writer = null; 56 this.serializer = null; 57 } 58 59 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 60 nestingLevel++; 61 if (namespaceURI.equals(NAMESPACE) && localName.equals(TRIGGER_EL)) { 62 inSerialize = true; 63 serializeNestingLevel = nestingLevel; 64 65 String method = atts.getValue("method"); 67 if (method == null) method = "html"; 68 String encoding = atts.getValue("encoding"); 69 if (encoding == null) encoding = "UTF-8"; 70 71 try { 72 this.serializer = transformerFactory.newTransformerHandler(); 73 this.serializer.getTransformer().setOutputProperty(OutputKeys.METHOD, method); 74 this.serializer.getTransformer().setOutputProperty(OutputKeys.ENCODING, encoding); 75 } catch (TransformerConfigurationException e) { 76 throw new SAXException (e); 77 } 78 this.writer = new CharArrayWriter (5000); 79 serializer.setResult(new StreamResult (writer)); 80 this.serializer.startDocument(); 81 } else if (inSerialize) { 82 serializer.startElement(namespaceURI, localName, qName, atts); 83 } else { 84 super.startElement(namespaceURI, localName, qName, atts); 85 } 86 } 87 88 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 89 if (!inSerialize) { 90 super.endElement(namespaceURI, localName, qName); 91 } else { 92 if (nestingLevel == serializeNestingLevel && inSerialize && namespaceURI.equals(NAMESPACE) && localName.equals(TRIGGER_EL)) { 93 inSerialize = false; 94 this.serializer.endDocument(); 95 char[] text = writer.toCharArray(); 96 super.characters(text, 0, text.length); 97 this.serializer = null; 98 this.writer = null; 99 } else { 100 serializer.endElement(namespaceURI, localName, qName); 101 } 102 } 103 nestingLevel--; 104 } 105 106 public void setDocumentLocator(Locator locator) { 107 if (!inSerialize) 108 super.setDocumentLocator(locator); 109 } 110 111 public void startDocument() throws SAXException { 112 if (!inSerialize) 113 super.startDocument(); 114 } 115 116 public void endDocument() throws SAXException { 117 if (!inSerialize) 118 super.endDocument(); 119 } 120 121 public void startPrefixMapping(String s, String s1) throws SAXException { 122 if (!inSerialize) 123 super.startPrefixMapping(s, s1); 124 else 125 serializer.startPrefixMapping(s, s1); 126 } 127 128 public void endPrefixMapping(String s) throws SAXException { 129 if (!inSerialize) 130 super.endPrefixMapping(s); 131 else 132 serializer.endPrefixMapping(s); 133 } 134 135 public void characters(char[] chars, int i, int i1) throws SAXException { 136 if (!inSerialize) 137 super.characters(chars, i, i1); 138 else 139 serializer.characters(chars, i, i1); 140 } 141 142 public void ignorableWhitespace(char[] chars, int i, int i1) throws SAXException { 143 if (!inSerialize) 144 super.ignorableWhitespace(chars, i, i1); 145 } 146 147 public void processingInstruction(String s, String s1) throws SAXException { 148 if (!inSerialize) 149 super.processingInstruction(s, s1); 150 else 151 serializer.processingInstruction(s, s1); 152 } 153 154 public void skippedEntity(String s) throws SAXException { 155 if (!inSerialize) 156 super.skippedEntity(s); 157 } 158 159 public void startDTD(String s, String s1, String s2) throws SAXException { 160 if (!inSerialize) 161 super.startDTD(s, s1, s2); 162 } 163 164 public void endDTD() throws SAXException { 165 if (!inSerialize) 166 super.endDTD(); 167 } 168 169 public void startEntity(String s) throws SAXException { 170 if (!inSerialize) 171 super.startEntity(s); 172 else 173 serializer.startEntity(s); 174 } 175 176 public void endEntity(String s) throws SAXException { 177 if (!inSerialize) 178 super.endEntity(s); 179 else 180 serializer.endEntity(s); 181 } 182 183 public void startCDATA() throws SAXException { 184 if (!inSerialize) 185 super.startCDATA(); 186 else 187 serializer.startCDATA(); 188 } 189 190 public void endCDATA() throws SAXException { 191 if (!inSerialize) 192 super.endCDATA(); 193 else 194 serializer.endCDATA(); 195 } 196 197 public void comment(char[] chars, int i, int i1) throws SAXException { 198 if (!inSerialize) 199 super.comment(chars, i, i1); 200 else 201 serializer.comment(chars, i, i1); 202 } 203 204 205 } 206 | Popular Tags |