1 16 17 package org.apache.cocoon.slop.generation; 18 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.io.LineNumberReader ; 22 import java.io.Serializable ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.caching.CacheableProcessingComponent; 28 import org.apache.cocoon.environment.SourceResolver; 29 import org.apache.cocoon.generation.ServiceableGenerator; 30 import org.apache.cocoon.slop.interfaces.SlopParser; 31 import org.apache.cocoon.slop.parsing.SimpleSlopParser; 32 import org.apache.excalibur.source.Source; 33 import org.apache.excalibur.source.SourceException; 34 import org.apache.excalibur.source.SourceValidity; 35 import org.xml.sax.SAXException ; 36 import org.xml.sax.helpers.LocatorImpl ; 37 38 45 46 public class SlopGenerator extends ServiceableGenerator 47 implements CacheableProcessingComponent { 48 49 private Source inputSource = null; 50 private String encoding = null; 51 private SlopParser parser = null; 52 private boolean preserveSpace = false; 53 private String validTagnameChars = null; 54 55 59 public void recycle() { 60 if (inputSource != null) { 61 super.resolver.release(inputSource); 62 } 63 inputSource = null; 64 encoding = null; 65 preserveSpace = false; 66 parser = null; 67 validTagnameChars = null; 68 69 super.recycle(); 70 } 71 72 85 public void setup(SourceResolver resolver, Map objectmodel, String src, Parameters parameters) 86 throws ProcessingException, SAXException , IOException { 87 super.setup(resolver, objectmodel, src, parameters); 88 try { 89 encoding = parameters.getParameter("encoding", null); 90 preserveSpace = parameters.getParameterAsBoolean("preserve-space",false); 91 validTagnameChars = parameters.getParameter("valid-tagname-chars",null); 92 inputSource = resolver.resolveURI(src); 93 94 final SimpleSlopParser ssp = new SimpleSlopParser(); 95 parser = ssp; 96 ssp.setPreserveWhitespace(preserveSpace); 97 ssp.setValidTagnameChars(validTagnameChars); 98 } catch (SourceException se) { 99 throw new ProcessingException("Error during resolving of '" + src + "'.", se); 100 } 101 } 102 103 109 public Serializable getKey() { 110 return inputSource.getURI(); 111 } 112 113 119 public SourceValidity getValidity() { 120 return this.inputSource.getValidity(); 121 } 122 123 130 public void generate() 131 throws IOException , SAXException , ProcessingException { 132 133 InputStreamReader in = null; 135 136 try { 137 if (this.inputSource.getInputStream() == null) { 138 throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found"); 139 } 140 141 if (encoding != null) { 142 in = new InputStreamReader (this.inputSource.getInputStream(), encoding); 143 } else { 144 in = new InputStreamReader (this.inputSource.getInputStream()); 145 } 146 } catch (SourceException se) { 147 throw new ProcessingException("Error during resolving of '" + this.source + "'.", se); 148 } 149 150 final LocatorImpl locator = new LocatorImpl (); 152 153 locator.setSystemId(this.inputSource.getURI()); 154 locator.setLineNumber(1); 155 locator.setColumnNumber(1); 156 157 contentHandler.setDocumentLocator(locator); 158 159 parser.startDocument(contentHandler); 161 162 LineNumberReader reader = new LineNumberReader (in); 163 String line, newline = null; 164 165 while (true) { 166 if (newline == null) { 167 line = reader.readLine(); 168 } else { 169 line = newline; 170 } 171 172 if (line == null) { 173 break; 174 } 175 176 newline = reader.readLine(); 177 178 locator.setLineNumber(reader.getLineNumber()); 179 locator.setColumnNumber(1); 180 parser.processLine(line); 181 182 if (newline == null) { 183 break; 184 } 185 } 186 187 parser.endDocument(); 189 } 190 } | Popular Tags |