1 16 package org.apache.cocoon.portal.transformation; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.Serializable ; 22 import java.util.Map ; 23 24 import org.apache.avalon.framework.parameters.Parameters; 25 import org.apache.avalon.framework.service.ServiceException; 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.caching.CacheableProcessingComponent; 28 import org.apache.cocoon.components.sax.XMLDeserializer; 29 import org.apache.cocoon.components.sax.XMLSerializer; 30 import org.apache.cocoon.environment.SourceResolver; 31 import org.apache.cocoon.transformation.AbstractSAXTransformer; 32 import org.apache.cocoon.xml.IncludeXMLConsumer; 33 import org.apache.cocoon.xml.XMLConsumer; 34 import org.apache.excalibur.source.SourceValidity; 35 import org.apache.excalibur.source.impl.validity.NOPValidity; 36 import org.apache.excalibur.xmlizer.XMLizer; 37 import org.xml.sax.Attributes ; 38 import org.xml.sax.SAXException ; 39 40 48 public final class RSSTransformer 49 extends AbstractSAXTransformer 50 implements CacheableProcessingComponent { 51 52 53 protected XMLizer xmlizer; 54 55 56 protected XMLDeserializer deserializer; 57 58 59 protected XMLConsumer filter; 60 61 64 public void startElement(String uri, String name, String raw, Attributes attributes) 65 throws SAXException { 66 super.startElement(uri,name,raw,attributes); 67 if ("description".equals(name)) { 68 this.startTextRecording(); 69 } 70 } 71 72 75 public void endElement(String uri,String name,String raw) 76 throws SAXException { 77 if ("description".equals(name)) { 78 final String text = this.endTextRecording(); 79 final String html = "<html><body>"+text+"</body></html>"; 80 81 Object parsed = null; 82 XMLSerializer serializer = null; 83 try { 84 serializer = (XMLSerializer)this.manager.lookup(XMLSerializer.ROLE); 85 InputStream inputStream = new ByteArrayInputStream (html.getBytes()); 86 this.xmlizer.toSAX(inputStream, 87 "text/html", 88 null, 89 serializer); 90 parsed = serializer.getSAXFragment(); 92 } catch (Exception ignore) { 93 } finally { 95 this.manager.release( serializer ); 96 } 97 if ( parsed != null ) { 98 this.deserializer.setConsumer( this.filter ); 99 this.deserializer.deserialize( parsed ); 100 } else { 101 this.sendTextEvent(text); 102 } 103 } 104 super.endElement(uri,name,raw); 105 } 106 107 110 public void recycle() { 111 this.manager.release( this.xmlizer ); 112 this.manager.release( this.deserializer ); 113 this.xmlizer = null; 114 this.deserializer = null; 115 this.filter = null; 116 super.recycle(); 117 } 118 119 122 public void setup(SourceResolver resolver, 123 Map objectModel, 124 String src, 125 Parameters par) 126 throws ProcessingException, SAXException , IOException { 127 super.setup(resolver, objectModel, src, par); 128 try { 129 this.xmlizer = (XMLizer)this.manager.lookup(XMLizer.ROLE); 130 this.deserializer = (XMLDeserializer)this.manager.lookup(XMLDeserializer.ROLE); 131 } catch (ServiceException ce) { 132 throw new ProcessingException("Unable to lookup component.", ce); 133 } 134 } 135 136 static class HTMLFilter extends IncludeXMLConsumer { 137 138 int bodyCount = 0; 139 140 public HTMLFilter(XMLConsumer consumer) { 141 super(consumer); 142 } 143 144 public void startElement(String uri, String local, String qName, Attributes attr) throws SAXException { 145 if (bodyCount > 0 ) { 146 super.startElement(uri, local, qName, attr); 147 } 148 if ("body".equalsIgnoreCase(local)) { 149 bodyCount++; 150 } 151 } 152 153 public void endElement(String uri, String local, String qName) throws SAXException { 154 if ("body".equalsIgnoreCase(local)) { 155 bodyCount--; 156 } 157 if (bodyCount > 0 ) { 158 super.endElement(uri, local, qName ); 159 } 160 } 161 162 } 163 164 167 public void setupTransforming() 168 throws IOException , ProcessingException, SAXException { 169 super.setupTransforming(); 170 this.filter = new HTMLFilter( this.xmlConsumer ); 171 } 172 173 176 public Serializable getKey() { 177 return "1"; 178 } 179 180 183 public SourceValidity getValidity() { 184 return NOPValidity.SHARED_INSTANCE; 185 } 186 187 } 188 | Popular Tags |