1 16 17 package org.apache.cocoon.generation; 18 19 import org.apache.avalon.framework.parameters.ParameterException; 20 import org.apache.avalon.framework.parameters.Parameterizable; 21 import org.apache.avalon.framework.parameters.Parameters; 22 23 import org.apache.cocoon.ProcessingException; 24 import org.apache.cocoon.caching.CacheableProcessingComponent; 25 import org.apache.cocoon.environment.SourceResolver; 26 import org.apache.commons.lang.SystemUtils; 27 28 import org.apache.excalibur.source.Source; 29 import org.apache.excalibur.source.SourceException; 30 import org.apache.excalibur.source.SourceValidity; 31 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.helpers.AttributesImpl ; 34 import org.xml.sax.helpers.LocatorImpl ; 35 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.io.InputStreamReader ; 39 import java.io.LineNumberReader ; 40 import java.io.Serializable ; 41 42 import java.util.Map ; 43 44 56 public class TextGenerator extends ServiceableGenerator implements Parameterizable, 57 CacheableProcessingComponent 58 { 59 60 public static final String URI = "http://chaperon.sourceforge.net/schema/text/1.0"; 61 private static final char[] initNonXmlChars = 62 { 63 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 64 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' 66 }; 67 68 69 private Source inputSource; 70 private String encoding; 71 private char[] nonXmlChars; 72 private boolean localizable = false; 73 74 77 public void recycle() 78 { 79 if (inputSource!=null) 80 super.resolver.release(inputSource); 81 82 inputSource = null; 83 encoding = null; 84 nonXmlChars = null; 85 86 super.recycle(); 87 } 88 89 96 public void parameterize(Parameters parameters) throws ParameterException 97 { 98 this.localizable = parameters.getParameterAsBoolean("localizable", false); 99 } 100 101 114 public void setup(SourceResolver resolver, Map objectmodel, String src, Parameters parameters) 115 throws ProcessingException, SAXException , IOException { 116 super.setup(resolver, objectmodel, src, parameters); 117 try { 118 this.encoding = parameters.getParameter("encoding", null); 119 this.inputSource = resolver.resolveURI(src); 120 121 String nXmlCh = parameters.getParameter("nonXmlChars", String.valueOf(initNonXmlChars)); 122 if (nXmlCh.length() != initNonXmlChars.length) 123 throw new ProcessingException("Error during resolving of '"+src+"'.", 124 new SourceException("length of attribute string 'nonXmlChars' is "+ 125 nXmlCh.length()+" where it should be "+ 126 initNonXmlChars.length+"!")); 127 128 this.nonXmlChars = nXmlCh.toCharArray(); 129 } catch (SourceException se) { 130 throw new ProcessingException("Error during resolving of '"+src+"'.", se); 131 } 132 } 133 134 139 public Serializable getKey() 140 { 141 return inputSource.getURI() 142 + ";localizable=" + localizable 143 + ";encoding=" + encoding; 144 } 145 146 152 public SourceValidity getValidity() 153 { 154 return this.inputSource.getValidity(); 155 } 156 157 164 public void generate() throws IOException , SAXException , ProcessingException { 165 InputStreamReader in = null; 166 167 try { 168 final InputStream sis = this.inputSource.getInputStream(); 169 if (sis == null) { 170 throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found"); 171 } 172 173 if (encoding != null) { 174 in = new InputStreamReader (sis, encoding); 175 } else { 176 in = new InputStreamReader (sis); 177 } 178 } catch (SourceException se) { 179 throw new ProcessingException("Error during resolving of '" + this.source + "'.", se); 180 } 181 182 LocatorImpl locator = new LocatorImpl (); 183 184 locator.setSystemId(this.inputSource.getURI()); 185 locator.setLineNumber(1); 186 locator.setColumnNumber(1); 187 188 contentHandler.setDocumentLocator(locator); 189 contentHandler.startDocument(); 190 contentHandler.startPrefixMapping("", URI); 191 192 AttributesImpl atts = new AttributesImpl (); 193 if (localizable) { 194 atts.addAttribute("", "source", "source", "CDATA", locator.getSystemId()); 195 atts.addAttribute("", "line", "line", "CDATA", String.valueOf(locator.getLineNumber())); 196 atts.addAttribute("", "column", "column", "CDATA", String.valueOf(locator.getColumnNumber())); 197 } 198 199 contentHandler.startElement(URI, "text", "text", atts); 200 201 LineNumberReader reader = new LineNumberReader (in); 202 String line; 203 String newline = null; 204 205 while (true) { 206 if (newline==null) { 207 line = convertNonXmlChars(reader.readLine()); 208 } else { 209 line = newline; 210 } 211 if (line==null) { 212 break; 213 } 214 newline = convertNonXmlChars(reader.readLine()); 215 if (newline != null) { 216 line += SystemUtils.LINE_SEPARATOR; 217 } 218 locator.setLineNumber(reader.getLineNumber()); 219 locator.setColumnNumber(1); 220 contentHandler.characters(line.toCharArray(), 0, line.length()); 221 if (newline==null) { 222 break; 223 } 224 } 225 reader.close(); 226 contentHandler.endElement(URI, "text", "text"); 227 contentHandler.endPrefixMapping(""); 228 contentHandler.endDocument(); 229 } 230 231 private String convertNonXmlChars(String s) { 232 if (s != null) { 233 int nv; 234 char[] sc = s.toCharArray(); 235 236 for (int i = 0; i<sc.length; i++) { 237 nv = sc[i]; 238 239 if ((nv>=0) && (nv<nonXmlChars.length)) { 240 if ((nv!=9) && (nv!=10) && (nv!=13)) 242 sc[i] = nonXmlChars[nv]; 243 } 244 } 245 return String.valueOf(sc); 246 } else { 247 return null; 248 } 249 } 250 } 251 | Popular Tags |