1 16 package org.apache.cocoon.transformation; 17 18 import java.io.BufferedInputStream ; 19 import java.io.ByteArrayInputStream ; 20 import java.io.IOException ; 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.StringTokenizer ; 27 28 import org.apache.avalon.framework.configuration.Configurable; 29 import org.apache.avalon.framework.configuration.Configuration; 30 import org.apache.avalon.framework.configuration.ConfigurationException; 31 import org.apache.avalon.framework.parameters.Parameters; 32 import org.apache.cocoon.ProcessingException; 33 import org.apache.cocoon.environment.SourceResolver; 34 import org.apache.cocoon.transformation.AbstractSAXTransformer; 35 import org.apache.cocoon.xml.XMLUtils; 36 import org.apache.cocoon.xml.IncludeXMLConsumer; 37 import org.apache.excalibur.source.Source; 38 import org.w3c.tidy.Tidy; 39 import org.xml.sax.Attributes ; 40 import org.xml.sax.SAXException ; 41 42 56 public class HTMLTransformer 57 extends AbstractSAXTransformer 58 implements Configurable { 59 60 63 private Properties properties; 64 65 68 private Map tags; 69 70 76 public void endElement(String uri, String name, String raw) 77 throws SAXException { 78 if (this.tags.containsKey(name)) { 79 String toBeNormalized = this.endTextRecording(); 80 try { 81 this.normalize(toBeNormalized); 82 } catch (ProcessingException e) { 83 e.printStackTrace(); 84 } 85 } 86 super.endElement(uri, name, raw); 87 } 88 89 95 public void startElement( 96 String uri, 97 String name, 98 String raw, 99 Attributes attr) 100 throws SAXException { 101 super.startElement(uri, name, raw, attr); 102 if (this.tags.containsKey(name)) { 103 this.startTextRecording(); 104 } 105 } 106 107 111 public void configure(Configuration config) throws ConfigurationException { 112 super.configure(config); 113 114 String configUrl = config.getChild("jtidy-config").getValue(null); 115 if (configUrl != null) { 116 org.apache.excalibur.source.SourceResolver resolver = null; 117 Source configSource = null; 118 try { 119 resolver = (org.apache.excalibur.source.SourceResolver) 120 this.manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE); 121 configSource = resolver.resolveURI(configUrl); 122 if (getLogger().isDebugEnabled()) { 123 getLogger().debug( 124 "Loading configuration from " + configSource.getURI()); 125 } 126 this.properties = new Properties (); 127 this.properties.load(configSource.getInputStream()); 128 129 } catch (Exception e) { 130 getLogger().warn("Cannot load configuration from " + configUrl); 131 throw new ConfigurationException( 132 "Cannot load configuration from " + configUrl, 133 e); 134 } finally { 135 if (null != resolver) { 136 this.manager.release(resolver); 137 resolver.release(configSource); 138 } 139 } 140 } 141 } 142 143 149 private void normalize(String text) throws ProcessingException { 150 try { 151 Tidy tidy = new Tidy(); 153 tidy.setXmlOut(true); 154 155 if (this.properties == null) { 156 tidy.setXHTML(true); 157 } else { 158 tidy.setConfigurationFromProps(this.properties); 159 } 160 161 tidy.setShowWarnings(getLogger().isWarnEnabled()); 163 tidy.setQuiet(!getLogger().isInfoEnabled()); 165 StringWriter stringWriter = new StringWriter (); 167 PrintWriter errorWriter = new PrintWriter (stringWriter); 168 tidy.setErrout(errorWriter); 169 170 ByteArrayInputStream bais = 172 new ByteArrayInputStream (text.getBytes()); 173 org.w3c.dom.Document doc = 174 tidy.parseDOM(new BufferedInputStream (bais), null); 175 176 XMLUtils.stripDuplicateAttributes(doc, null); 179 180 errorWriter.flush(); 181 errorWriter.close(); 182 if (getLogger().isWarnEnabled()) { 183 getLogger().warn(stringWriter.toString()); 184 } 185 186 IncludeXMLConsumer.includeNode(doc, this.contentHandler, this.lexicalHandler); 187 } catch (Exception e) { 188 throw new ProcessingException( 189 "Exception in HTMLTransformer.normalize()", 190 e); 191 } 192 } 193 194 197 198 public void setup( 199 SourceResolver resolver, 200 Map objectModel, 201 String src, 202 Parameters par) 203 throws ProcessingException, SAXException , IOException { 204 super.setup(resolver, objectModel, src, par); 205 String tagsParam = par.getParameter("tags", ""); 206 if (getLogger().isDebugEnabled()) { 207 getLogger().debug("tags: " + tagsParam); 208 } 209 this.tags = new HashMap (); 210 StringTokenizer tokenizer = new StringTokenizer (tagsParam, ","); 211 while (tokenizer.hasMoreElements()) { 212 String tok = tokenizer.nextToken().trim(); 213 this.tags.put(tok, tok); 214 } 215 } 216 } 217 | Popular Tags |