Changeset 1694
- Timestamp:
- 2010-08-23 20:39:24 (3 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 9 edited
-
modules/kauri-template/kauri-template-service-impl/src/main/java/org/kauriproject/template/service/impl/RestletSource.java (modified) (1 diff)
-
universe/kauri-template/src/main/java/org/kauriproject/template/InsertBlock.java (modified) (9 diffs)
-
universe/kauri-template/src/main/java/org/kauriproject/template/source/ClasspathSource.java (modified) (2 diffs)
-
universe/kauri-template/src/main/java/org/kauriproject/template/source/ClasspathSourceResolver.java (modified) (1 diff)
-
universe/kauri-template/src/main/java/org/kauriproject/template/source/Source.java (modified) (1 diff)
-
universe/kauri-template/src/test/resources/org/kauriproject/template/insert_text.xml (modified) (1 diff)
-
universe/kauri-template/src/test/resources/org/kauriproject/template/insert_text_result.xml (modified) (1 diff)
-
universe/kauri-template/src/test/resources/org/kauriproject/template/plain.html (modified) (1 diff)
-
universe/kauri-template/src/test/resources/org/kauriproject/template/plain.xml (modified) (1 diff)
-
universe/kauri-template/src/test/resources/org/kauriproject/template/plain_Cp1252.txt (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/kauri-template/kauri-template-service-impl/src/main/java/org/kauriproject/template/service/impl/RestletSource.java
r1329 r1694 84 84 return representation.getMediaType().toString(); 85 85 } 86 87 public String getEncoding() { 88 return representation.getCharacterSet().getName(); 89 } 86 90 87 91 public Validity getValidity() { -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/InsertBlock.java
r1688 r1694 16 16 package org.kauriproject.template; 17 17 18 import java.io.BufferedReader; 19 import java.io.ByteArrayInputStream; 18 import java.io.Closeable; 20 19 import java.io.FileNotFoundException; 21 20 import java.io.IOException; 22 21 import java.io.InputStream; 23 22 import java.io.InputStreamReader; 23 import java.io.Reader; 24 import java.io.StringReader; 25 import java.io.UnsupportedEncodingException; 26 import java.util.regex.Matcher; 27 import java.util.regex.Pattern; 24 28 25 29 import javax.xml.parsers.SAXParser; 26 30 27 import org.apache.commons.logging.Log;28 import org.apache.commons.logging.LogFactory;29 31 import org.kauriproject.template.el.ELFacade; 30 32 import org.kauriproject.template.el.Expression; … … 48 50 public static final String VALUE = "value"; 49 51 public static final String MODE = "mode"; 52 public static final String ENCODING = "encoding"; 50 53 51 54 // EL … … 72 75 private Expression sourceExpr, valueExpr; 73 76 private String mode; 77 private String encoding; 74 78 75 79 public StartStep(Locator locator) { … … 87 91 } 88 92 mode = attributes.getValue(MODE); 93 encoding = attributes.getValue(ENCODING); 89 94 } 90 95 … … 94 99 Source source = null; 95 100 String valueStr = null; 96 String usedMode = "undefined"; 101 ParseType parseType = null; 102 InsertBlockInput input = null; 97 103 98 104 try { 99 // resolve and parse 100 ParseType parseType = null; 101 102 if(sourceLocation != null) 105 106 // find content to insert 107 if(sourceLocation != null) 103 108 source = context.getSourceResolver().resolve(sourceLocation, context.getBaseUri()); 104 109 else{ … … 106 111 } 107 112 113 // decide on mode 108 114 if (mode != null) { 109 115 parseType = ParseType.fromMode(mode); // this is silent about unknown modes 110 116 } 117 if (parseType == null && source!=null) { 118 parseType = ParseType.fromMediaType(source.getMediaType()); 119 } 111 120 if (parseType == null) { 112 parseType = ParseType. fromMediaType(source.getMediaType()); // this could NPE when source == null121 parseType = ParseType.TXT; // default to txt mode to be sure to include something 113 122 } 114 115 usedMode = parseType.name(); 116 parseType.doInsert(source, valueStr, result, parser); // this could NPE when parseType was still not found 123 124 //actually insert 125 input = InsertBlockInput.newInput(source, valueStr, encoding); 126 parseType.doInsert(input, result, parser); // this could NPE when parseType was still not found 117 127 118 128 } catch (FileNotFoundException ex) { 119 throw new TemplateException("Error inserting " + usedMode+ "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex);129 throw new TemplateException("Error inserting " + parseType.name() + "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex); 120 130 } catch (Exception ex) { 121 throw new TemplateException("Error parsing " + usedMode+ "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex);131 throw new TemplateException("Error parsing " + parseType.name() + "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex); 122 132 } finally { 123 133 if (source != null) { 124 134 source.release(); 125 135 } 136 IOUtils.closeQuietly(input); 126 137 } 127 138 … … 153 164 } 154 165 155 void doInsert( Source src, String value, TemplateResult result, SAXParser parser) throws IOException, SAXException{156 this.strategy.doInsert( src, value, result, parser);166 void doInsert(InsertBlockInput input, TemplateResult result, SAXParser parser) throws IOException, SAXException{ 167 this.strategy.doInsert(input, result, parser); 157 168 } 158 169 … … 178 189 } 179 190 191 class InsertBlockInput implements Closeable { 192 193 private final Reader reader; 194 195 InsertBlockInput(final InputStream is, final String encoding) throws UnsupportedEncodingException { 196 this( encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding)); 197 } 198 199 InsertBlockInput(final Reader reader) { 200 this.reader = reader; 201 } 202 203 static InsertBlockInput newInput(final Source source, final String valueStr, String encoding) throws UnsupportedEncodingException, IOException { 204 if (source == null ){ 205 // we read from the string, no encoding to be followed 206 final Reader input = new StringReader(valueStr); 207 return new InsertBlockInput(input); 208 } 209 //else 210 if (encoding == null) //if nothing specified, let source charset decide 211 encoding = source.getEncoding(); 212 213 final InputStream input = source.getInputStream(); 214 return new InsertBlockInput(input, encoding); 215 } 216 217 InputSource getInputSource() { 218 return new InputSource(reader); 219 } 220 221 public Reader getReader() { 222 return reader; 223 } 224 225 public void close() { 226 IOUtils.closeQuietly(reader); 227 } 228 } 229 180 230 interface InsertStrategy { 181 void doInsert( Source src, String value, TemplateResult result, SAXParser parser) throws IOException, SAXException;231 void doInsert(InsertBlockInput input, TemplateResult result, SAXParser parser) throws IOException, SAXException; 182 232 } 183 233 184 234 class XMLInsertStrategy implements InsertStrategy { 185 public void doInsert(final Source source, final String valueStr, final TemplateResult result, final SAXParser parser) throws IOException, SAXException { 186 InputStream input = null; 187 try { 188 input = (source != null ? source.getInputStream() : new ByteArrayInputStream(valueStr.getBytes())); 189 190 // default to XML: parse with saxparser 191 XmlConsumer consumer = new EmbedXmlFilter(result); 192 XMLReader xmlReader = parser.getXMLReader(); 193 xmlReader.setContentHandler(consumer); 194 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", consumer); 195 xmlReader.parse(new InputSource(input)); 196 } finally { 197 IOUtils.closeQuietly(input); 198 } 235 public void doInsert(InsertBlockInput input, final TemplateResult result, final SAXParser parser) throws IOException, SAXException { 236 // default to XML: parse with saxparser 237 XmlConsumer consumer = new EmbedXmlFilter(result); 238 XMLReader xmlReader = parser.getXMLReader(); 239 xmlReader.setContentHandler(consumer); 240 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", consumer); 241 xmlReader.parse(input.getInputSource()); 199 242 } 200 243 } 201 244 202 245 class TextInsertStrategy implements InsertStrategy { 203 public void doInsert(final Source source, final String valueStr, final TemplateResult result, SAXParser parser) throws IOException, SAXException { 204 InputStream input = null; 205 try { 206 input = (source != null ? source.getInputStream() : new ByteArrayInputStream(valueStr.getBytes())); 246 public void doInsert(InsertBlockInput input, final TemplateResult result, SAXParser parser) throws IOException, SAXException { 207 247 208 //TODO check encoding used to read the text! -- possibly allow encoding to be specified or read from inputstream? 209 BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 210 String line = reader.readLine(); 211 while (line != null) { 212 result.characters(line.toCharArray(), 0, line.length()); 213 line = reader.readLine(); 214 } 215 } finally { 216 IOUtils.closeQuietly(input); 248 //TODO check encoding used to read the text! -- possibly allow encoding to be specified or read from inputstream? 249 250 Reader reader = input.getReader(); 251 char[] buf = new char[1024]; 252 int read = -1; 253 while ( (read = reader.read(buf)) > 0) { 254 result.characters(buf, 0, read); 217 255 } 218 256 } … … 220 258 221 259 class HTMLInsertStrategy implements InsertStrategy { 222 public void doInsert(final Source source, final String valueStr, final TemplateResult result, SAXParser parser) throws SAXException, IOException { 223 InputStream input = null; 224 try { 225 input = (source != null ? source.getInputStream() : new ByteArrayInputStream(valueStr.getBytes())); 226 227 //TODO check proper namespace-handling and correctly marking html to xhtml spaced elements 228 // will be important for post-processing 229 NekoHtmlParser neko = new NekoHtmlParser(); 230 neko.parse(new InputSource(input), new HtmlBodyContentsFilter(result)); 260 public void doInsert(InsertBlockInput input, final TemplateResult result, SAXParser parser) throws SAXException, IOException { 261 262 //TODO check proper namespace-handling and correctly marking html to xhtml spaced elements 263 // will be important for post-processing 264 NekoHtmlParser neko = new NekoHtmlParser(); 265 neko.parse(input.getInputSource(), new HtmlBodyContentsFilter(result)); 231 266 232 } finally {233 IOUtils.closeQuietly(input);234 }235 267 } 236 268 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/source/ClasspathSource.java
r1329 r1694 16 16 package org.kauriproject.template.source; 17 17 18 import java.io.IOException; 19 import java.io.InputStream; 20 import java.io.InputStreamReader; 21 import java.io.OutputStream; 22 import java.io.Reader; 23 import java.io.Writer; 24 import java.net.URI; 25 import java.net.URISyntaxException; 26 import java.net.URL; 27 import java.net.URLConnection; 28 29 import org.apache.commons.lang.builder.EqualsBuilder; 18 30 import org.apache.commons.lang.builder.HashCodeBuilder; 19 import org.apache.commons.lang.builder.EqualsBuilder;20 31 import org.apache.commons.lang.builder.ToStringBuilder; 21 32 import org.kauriproject.util.io.IOUtils; 22 23 import java.net.URL;24 import java.net.URI;25 import java.net.URISyntaxException;26 import java.net.URLConnection;27 import java.io.*;28 33 29 34 public class ClasspathSource implements Source { … … 104 109 return mediaType; 105 110 } 111 112 public String getEncoding() { 113 return System.getProperty("file.encoding"); // just the default encoding. Note for HTML and XML a better effort could be done baed on the content. 114 } 106 115 107 116 public void release() { -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/source/ClasspathSourceResolver.java
r900 r1694 42 42 if (baseLocation.endsWith("/")) 43 43 return baseLocation + sourceLocation; 44 else { 45 int end = baseLocation.lastIndexOf('/'); 46 if (end < 0) { 47 return sourceLocation; 48 } 49 return baseLocation.substring(0, ++end) + sourceLocation; 44 //else 45 int end = baseLocation.lastIndexOf('/'); 46 if (end < 0) { 47 return sourceLocation; 50 48 } 49 return baseLocation.substring(0, ++end) + sourceLocation; 51 50 } 52 51 -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/source/Source.java
r1329 r1694 46 46 47 47 String getMediaType(); 48 49 String getEncoding(); 48 50 49 51 Validity getValidity(); -
trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/insert_text.xml
r1688 r1694 7 7 <h3>test insert text from url</h3> 8 8 <t:insert src="plain.txt"/><!-- silently taking text mode from media-type? --> 9 <br /> 10 <t:insert src="plain_Cp1252.txt" encoding="Cp1252"/> 9 11 </body> 10 12 </html> -
trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/insert_text_result.xml
r1690 r1694 11 11 </h3> 12 12 There is no song, just a delusion of silence. We dance and the music dies. 13 <br> 14 </br> 15 This file must be encoded in Cp1252. To test it contains the chars €(euro) é(e-acute) ©(copyright) 13 16 </body> 14 17 </html> -
trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/plain.html
r1688 r1694 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 2 2 <html> 3 3 <head> -
trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/plain.xml
r523 r1694 1 <?xml version="1.0" encoding="UTF-8"?>1 <?xml version="1.0" encoding="UTF-8"?> 2 2 <root> 3 3 <h1>Hello Kauri</h1>
Note: See TracChangeset
for help on using the changeset viewer.