Changeset 1743
- Timestamp:
- 2010-10-15 14:52:27 (3 years ago)
- Location:
- trunk/universe
- Files:
-
- 3 added
- 11 edited
- 1 moved
-
kauri-template/src/main/java/org/kauriproject/template/InsertBlock.java (modified) (5 diffs)
-
kauri-template/src/main/java/org/kauriproject/template/TemplateBlock.java (modified) (2 diffs)
-
kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java (modified) (7 diffs)
-
kauri-template/src/main/java/org/kauriproject/template/el/ELFacade.java (modified) (1 diff)
-
kauri-template/src/main/java/org/kauriproject/template/handling/HTMLHandlingStrategy.java (modified) (2 diffs)
-
kauri-template/src/main/java/org/kauriproject/template/handling/Handling.java (modified) (4 diffs)
-
kauri-template/src/main/java/org/kauriproject/template/handling/HandlingInput.java (moved) (moved from trunk/universe/kauri-template/src/main/java/org/kauriproject/template/handling/InsertBlockInput.java) (2 diffs)
-
kauri-template/src/main/java/org/kauriproject/template/handling/HandlingStrategy.java (modified) (1 diff)
-
kauri-template/src/main/java/org/kauriproject/template/handling/JSONHandlingStrategy.java (added)
-
kauri-template/src/main/java/org/kauriproject/template/handling/TextHandlingStrategy.java (modified) (2 diffs)
-
kauri-template/src/main/java/org/kauriproject/template/handling/XMLHandlingStrategy.java (modified) (2 diffs)
-
kauri-template/src/test/java/org/kauriproject/template/TemplateExecutionTest.java (modified) (1 diff)
-
kauri-template/src/test/resources/org/kauriproject/template/mode_handling.xml (added)
-
kauri-template/src/test/resources/org/kauriproject/template/mode_handling_result.xml (added)
-
kauri-xmlcore/src/main/java/org/kauriproject/xml/html/HtmlBodyContentsFilter.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/InsertBlock.java
r1741 r1743 23 23 import org.kauriproject.template.el.Expression; 24 24 import org.kauriproject.template.handling.Handling; 25 import org.kauriproject.template.handling.InsertBlockInput; 25 import org.kauriproject.template.handling.HandlingInput; 26 import org.kauriproject.template.handling.HandlingStrategy; 26 27 import org.kauriproject.template.source.Source; 27 28 import org.kauriproject.util.io.IOUtils; … … 59 60 class StartStep extends Step { 60 61 61 private Expression sourceExpr, valueExpr; 62 private String mode; 63 private String encoding; 62 private final Expression sourceExpr; 63 private final Expression valueExpr; 64 private final Expression modeExpr; 65 private final Expression encodingExpr; 64 66 65 67 public StartStep(Locator locator) { 66 68 super(locator); 67 Attributes attributes = getSaxElement().getAttributes();69 final Attributes attributes = getSaxElement().getAttributes(); 68 70 69 String srcAttr = attributes.getValue(SRC);70 String valueAttr = attributes.getValue(VALUE);71 final String srcAttr = attributes.getValue(SRC); 72 final String valueAttr = attributes.getValue(VALUE); 71 73 72 74 if(srcAttr != null) { 73 75 sourceExpr = elFacade.createExpression(srcAttr, String.class); 74 }else{ 75 // only read 'value' attribute if 'src' is not defined 76 valueExpr = elFacade.createExpression(valueAttr, String.class); 76 valueExpr = null; 77 } else { 78 sourceExpr = null; 79 valueExpr = elFacade.createExpression(valueAttr, String.class); 77 80 } 78 mode = attributes.getValue(MODE); 79 encoding = attributes.getValue(ENCODING); 81 82 final String modeAttr = attributes.getValue(MODE); 83 modeExpr = elFacade.createExpression(modeAttr, String.class); 84 85 final String encodingAttr = attributes.getValue(ENCODING); 86 encodingExpr = elFacade.createExpression(encodingAttr, String.class); 80 87 } 81 88 82 89 @Override 83 90 public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { 84 final String sourceLocation = (sourceExpr!=null ? (String) sourceExpr.evaluate(context.getTemplateContext()) : null); 91 final String sourceLocation = stringEval(context, sourceExpr, null); 92 final String encoding = stringEval(context, encodingExpr, null); 85 93 Source source = null; 86 StringvalueStr = null;87 Handling handling = null;88 InsertBlockInput input = null;94 CharSequence valueStr = null; 95 HandlingStrategy handling = null; 96 HandlingInput input = null; 89 97 90 98 try { … … 94 102 source = context.getSourceResolver().resolve(sourceLocation, context.getBaseUri()); 95 103 else{ 96 valueStr = ( String) valueExpr.evaluate(context.getTemplateContext());104 valueStr = (CharSequence) valueExpr.evaluate(context.getTemplateContext()); 97 105 } 98 106 99 107 // decide on handling mode based on @mode, media-type or default 100 if (mode != null) { 108 if (modeExpr != null) { 109 final String mode = (String)modeExpr.evaluate(context.getTemplateContext()); 101 110 handling = Handling.fromMode(mode); // this is silent about unknown modes 102 111 } … … 109 118 110 119 //actually insert 111 input = InsertBlockInput.newInput(source, valueStr, encoding);120 input = HandlingInput.newInput(source, encoding, valueStr); 112 121 handling.doInsert(input, result, parser); // this could NPE when parseType was still not found 113 122 114 123 } catch (FileNotFoundException ex) { 115 throw new TemplateException("Error inserting " + handling.name() + "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex);124 throw new TemplateException("Error inserting from " + (sourceLocation != null ? sourceLocation : valueStr) + " using " + handling + " : " + ex); 116 125 } catch (Exception ex) { 117 throw new TemplateException("Error parsing " + handling.name() + "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex);126 throw new TemplateException("Error parsing from " + (sourceLocation != null ? sourceLocation : valueStr) + " using " + handling + " : " + ex); 118 127 } finally { 119 128 if (source != null) { … … 133 142 super(locator); 134 143 } 135 136 144 } 137 145 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/TemplateBlock.java
r1350 r1743 16 16 package org.kauriproject.template; 17 17 18 import org.kauriproject.template.el.Expression; 18 19 import org.xml.sax.Locator; 19 20 … … 57 58 return saxElement; 58 59 } 59 60 61 public String stringEval(final ExecutionContext context, final Expression expr, final String dfault) { 62 return expr != null ? (String)expr.evaluate(context.getTemplateContext()): dfault; 63 } 60 64 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java
r1741 r1743 16 16 package org.kauriproject.template; 17 17 18 import java.io.*; 19 20 import org.codehaus.jackson.map.ObjectMapper; 18 import java.io.ByteArrayOutputStream; 19 import java.io.IOException; 20 21 import javax.xml.parsers.SAXParser; 22 21 23 import org.kauriproject.template.KauriSaxHandler.OutputFormat; 22 import org.kauriproject.template.el.CompositeExpression;23 24 import org.kauriproject.template.el.ELFacade; 24 25 import org.kauriproject.template.el.Expression; 26 import org.kauriproject.template.handling.Handling; 27 import org.kauriproject.template.handling.HandlingInput; 25 28 import org.kauriproject.template.source.Source; 26 import org.kauriproject.util.xml.LocalDocumentBuilderFactory;27 import org.kauriproject.util.xml.XmlMediaTypeHelper;28 29 import org.kauriproject.util.io.IOUtils; 29 30 import org.xml.sax.Attributes; 30 31 import org.xml.sax.Locator; 31 32 import org.xml.sax.SAXException; 32 import org.w3c.dom.Document;33 33 34 34 /** … … 45 45 private static final String MODE = "mode"; 46 46 47 // EL 47 // EL support 48 48 private ELFacade elFacade; 49 50 // XML support 51 protected SAXParser parser; 52 49 53 50 54 public VariableBlock(ELFacade elFacade, SaxElement saxElement) { … … 67 71 // variable stuff 68 72 private final String name; 69 private final Expression valueExpr ession;70 private final Expression srcExpr ession;71 private final Expression acceptExpr ession;72 private final CompositeExpression modeExpression;73 private final Expression valueExpr; 74 private final Expression srcExpr; 75 private final Expression acceptExpr; 76 private final Expression modeExpr; 73 77 private final boolean overwrite; 74 78 … … 82 86 name = attributes.getValue(NAME); 83 87 84 String expression = attributes.getValue(VALUE); 85 if (expression != null) { 86 valueExpression = elFacade.createExpression(expression, Object.class); 87 } else { 88 valueExpression = null; 89 } 90 91 String src = attributes.getValue(SRC); 92 if (src != null) { 93 srcExpression = elFacade.createExpression(src, String.class); 94 } else { 95 srcExpression = null; 96 } 97 98 String accept = attributes.getValue(ACCEPT); 99 if (accept != null) { 100 acceptExpression = elFacade.createExpression(accept, String.class); 101 } else { 102 acceptExpression = null; 103 } 104 105 String mode = attributes.getValue(MODE); 106 if (mode != null) { 107 modeExpression = elFacade.createExpression(accept, String.class); 108 } else { 109 modeExpression = null; 110 } 88 final String valAttr = attributes.getValue(VALUE); 89 valueExpr = elFacade.createExpression(valAttr, Object.class); 90 91 final String srcAttr = attributes.getValue(SRC); 92 srcExpr = elFacade.createExpression(srcAttr, String.class); 93 94 final String acceptAttr = attributes.getValue(ACCEPT); 95 acceptExpr = elFacade.createExpression(acceptAttr, String.class); 96 97 final String modeAttr = attributes.getValue(MODE); 98 modeExpr = elFacade.createExpression(modeAttr, String.class); 111 99 112 100 overwrite = !"false".equalsIgnoreCase(attributes.getValue(OVERWRITE)); … … 119 107 if (overwrite || !tmplContext.containsKey(name)) { 120 108 Object value = NO_VALUE; // use our own null to differentiate from null 121 if (valueExpr ession!= null) {122 value = valueExpr ession.evaluate(context.getTemplateContext());109 if (valueExpr != null) { 110 value = valueExpr.evaluate(context.getTemplateContext()); 123 111 } else { 124 112 // use element body as value … … 149 137 150 138 // value retrieved from @value or body can be overwritten by loaded data from @src 151 if (srcExpression != null) { 152 value = loadFromSrc(context, value); 153 } 154 139 value = overideFromSrc(context, value); 140 155 141 value = (value == NO_VALUE) ? null : value; // switch tracking NO_VALUE back to null before setting it in the context 156 142 tmplContext.put(name, value); … … 160 146 } 161 147 162 private Object loadFromSrc(ExecutionContext context, Object fallbackValue) { 163 String src = (String)srcExpression.evaluate(context.getTemplateContext()); 164 String accept = acceptExpression != null ? (String)acceptExpression.evaluate(context.getTemplateContext()) : null; 148 private Object overideFromSrc(final ExecutionContext context, final Object value) { 149 final String src = stringEval(context, srcExpr, null); 150 final String accept = stringEval(context, acceptExpr, null); 151 165 152 Source source = null; 153 HandlingInput input = null; 154 Handling handling = null; 166 155 try { 167 source = context.getSourceResolver().resolve(src, context.getBaseUri(), accept); 168 169 final String mode = null; 170 171 if (source.getMediaType().equals("application/json")) { 172 return loadJSON(source); 173 } else if (XmlMediaTypeHelper.isXmlMediaType(source.getMediaType())) { 174 return loadXml(source); 175 } else { 176 return loadString(source); 177 } 156 source = (src == null) ? null : context.getSourceResolver().resolve(src, context.getBaseUri(), accept); 157 158 // decide on handling mode based on @mode, media-type or default 159 if (modeExpr != null) { 160 final String mode = (String)modeExpr.evaluate(context.getTemplateContext()); 161 handling = Handling.fromMode(mode); // this is silent about unknown modes 162 } 163 164 if (source != null) { 165 input = HandlingInput.newInput(source); 166 if (handling == null) { 167 handling = Handling.fromMediaType(source.getMediaType()); 168 } 169 } else if (value instanceof CharSequence) { 170 input = HandlingInput.newInput(null, null, (CharSequence)value); 171 } else if (value == NO_VALUE) { 172 throw new TemplateException("No practical source or value found for variable " + name + ". Tried URI " + src); 173 } else 174 return value; 175 176 if (handling == null) { 177 handling = Handling.TXT; // default to txt mode to be sure to include something 178 } 179 return handling.parseToObject(input); 178 180 } catch (Throwable e) { 179 if ( fallbackValue != NO_VALUE)180 return fallbackValue;181 if (value == NO_VALUE) 182 throw new TemplateException("Some error occured while loading data for variable " + name + " from URI " + src, e); 181 183 // else 182 throw new TemplateException("Some error occured while loading data for variable " + name + " from URI " + src, e);184 return value; 183 185 } finally { 184 186 if (source != null) { 185 187 source.release(); 186 188 } 189 IOUtils.closeQuietly(input); 187 190 } 188 }189 190 private Object loadJSON(Source source) throws IOException {191 //String jsonText = loadString(source);192 ObjectMapper mapper = new ObjectMapper();193 InputStream is = source.getInputStream();194 Object obj = mapper.readValue(is, Object.class);195 is.close();196 return obj;197 }198 199 private Document loadXml(Source source) throws IOException, SAXException {200 InputStream is = null;201 try {202 is = source.getInputStream();203 return LocalDocumentBuilderFactory.getBuilder().parse(is);204 } finally {205 IOUtils.closeQuietly(is);206 }207 }208 209 private String loadString(Source source) throws IOException {210 StringWriter writer = new StringWriter();211 source.write(writer);212 return writer.toString();213 191 } 214 192 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/el/ELFacade.java
r1058 r1743 46 46 47 47 public CompositeExpression createExpression(String expression, Class<?> expectedType) { 48 if (expression == null ) 49 return null; 48 50 CompositeExpression cex = new CompositeExpression(); 49 51 parseExpression(expression, expectedType, cex); -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/handling/HTMLHandlingStrategy.java
r1741 r1743 5 5 import javax.xml.parsers.SAXParser; 6 6 7 import org.kauriproject.template.TemplateResult; 7 import org.kauriproject.util.xml.LocalDocumentBuilderFactory; 8 import org.kauriproject.xml.html.HtmlBodyContentsFilter; 8 9 import org.kauriproject.xml.html.NekoHtmlParser; 9 import org.kauriproject.xml.html.HtmlBodyContentsFilter; 10 import org.w3c.dom.Document; 11 import org.xml.sax.ContentHandler; 10 12 import org.xml.sax.SAXException; 11 13 12 class HTMLHandlingStrategy implements HandlingStrategy { 13 public void doInsert(InsertBlockInput input, final TemplateResult result, SAXParser parser) throws SAXException, IOException { 14 14 import com.sun.org.apache.xml.internal.utils.DOMBuilder; 15 16 public class HTMLHandlingStrategy implements HandlingStrategy { 17 18 public void doInsert(HandlingInput input, final ContentHandler result, SAXParser parser) throws SAXException, IOException { 15 19 //TODO check proper namespace-handling and correctly marking html to xhtml spaced elements 16 20 // will be important for post-processing … … 19 23 20 24 } 25 26 public Object parseToObject(HandlingInput input) throws SAXException, IOException { 27 28 final Document doc = LocalDocumentBuilderFactory.newDocument(); 29 final DOMBuilder result = new DOMBuilder(doc); 30 31 NekoHtmlParser neko = new NekoHtmlParser(); 32 neko.parse(input.getInputSource(), result); 33 34 return doc; 35 } 21 36 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/handling/Handling.java
r1741 r1743 5 5 import javax.xml.parsers.SAXParser; 6 6 7 import org.kauriproject.template.TemplateResult;8 7 import org.kauriproject.util.xml.XmlMediaTypeHelper; 8 import org.xml.sax.ContentHandler; 9 9 import org.xml.sax.SAXException; 10 10 11 11 public enum Handling implements HandlingStrategy{ 12 12 XML("xml", new XMLHandlingStrategy()), 13 JSON("json", new JSONHandlingStrategy()), 13 14 TXT("txt", new TextHandlingStrategy()), 14 15 HTML("html", new HTMLHandlingStrategy()); … … 22 23 } 23 24 24 public void doInsert( InsertBlockInput input, TemplateResultresult, SAXParser parser) throws IOException, SAXException{25 public void doInsert(HandlingInput input, ContentHandler result, SAXParser parser) throws IOException, SAXException{ 25 26 this.strategy.doInsert(input, result, parser); 27 } 28 29 public Object parseToObject(HandlingInput input) throws IOException, SAXException { 30 return this.strategy.parseToObject(input); 26 31 } 27 32 … … 37 42 if (XmlMediaTypeHelper.isXmlMediaType(mime)) { 38 43 return XML; 44 } else if (mime.equals("application/json")) { 45 return JSON; 39 46 } else if (mime.startsWith("text/html")) { 40 47 return HTML; … … 45 52 } 46 53 } 54 47 55 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/handling/HandlingInput.java
r1741 r1743 13 13 import org.xml.sax.InputSource; 14 14 15 public class InsertBlockInput implements Closeable {16 15 public class HandlingInput implements Closeable { 16 17 17 private final Reader reader; 18 19 InsertBlockInput(final InputStream is, final String encoding) throws UnsupportedEncodingException {20 this( encoding == null ? new InputStreamReader(is) :new InputStreamReader(is, encoding));18 19 HandlingInput(final InputStream is, final String encoding) throws UnsupportedEncodingException { 20 this(encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding)); 21 21 } 22 23 InsertBlockInput(final Reader reader) {22 23 HandlingInput(final Reader reader) { 24 24 this.reader = reader; 25 25 } 26 27 public static InsertBlockInput newInput(final Source source, final String valueStr, String encoding) throws UnsupportedEncodingException, IOException { 28 if (source == null ){ 26 27 public static HandlingInput newInput(final Source source, final String encoding, final CharSequence valueStr) 28 throws UnsupportedEncodingException, IOException { 29 if (source == null) { 29 30 // we read from the string, no encoding to be followed 30 final Reader input = new StringReader(valueStr); 31 return new InsertBlockInput(input); 32 } 33 //else 34 if (encoding == null) //if nothing specified, let source charset decide 31 final Reader input = new StringReader(valueStr.toString()); 32 return new HandlingInput(input); 33 } 34 // else 35 return newInput(source, encoding); 36 } 37 38 public static HandlingInput newInput(final Source source, String encoding) throws IOException, 39 UnsupportedEncodingException { 40 if (encoding == null) // if nothing specified, let source charset decide 35 41 encoding = source.getEncoding(); 36 42 37 43 final InputStream input = source.getInputStream(); 38 return new InsertBlockInput(input, encoding);44 return new HandlingInput(input, encoding); 39 45 } 40 46 47 public static HandlingInput newInput(final Source source) throws IOException, 48 UnsupportedEncodingException { 49 50 final InputStream input = source.getInputStream(); 51 return new HandlingInput(input, source.getEncoding()); 52 } 53 41 54 InputSource getInputSource() { 42 55 return new InputSource(reader); … … 46 59 return reader; 47 60 } 48 61 49 62 public void close() { 50 63 IOUtils.closeQuietly(reader); -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/handling/HandlingStrategy.java
r1741 r1743 5 5 import javax.xml.parsers.SAXParser; 6 6 7 import org. kauriproject.template.TemplateResult;7 import org.xml.sax.ContentHandler; 8 8 import org.xml.sax.SAXException; 9 9 10 10 public interface HandlingStrategy { 11 void doInsert(InsertBlockInput input, TemplateResult result, SAXParser parser) throws IOException, SAXException; 11 void doInsert(HandlingInput input, ContentHandler result, SAXParser parser) throws IOException, SAXException; 12 Object parseToObject(HandlingInput input) throws IOException, SAXException; 13 12 14 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/handling/TextHandlingStrategy.java
r1741 r1743 3 3 import java.io.IOException; 4 4 import java.io.Reader; 5 import java.io.StringWriter; 5 6 6 7 import javax.xml.parsers.SAXParser; 7 8 8 import org. kauriproject.template.TemplateResult;9 import org.xml.sax.ContentHandler; 9 10 import org.xml.sax.SAXException; 10 11 11 12 class TextHandlingStrategy implements HandlingStrategy { 12 public void doInsert( InsertBlockInput input, final TemplateResultresult, SAXParser parser) throws IOException, SAXException {13 public void doInsert(HandlingInput input, final ContentHandler result, SAXParser parser) throws IOException, SAXException { 13 14 14 15 //TODO check encoding used to read the text! -- possibly allow encoding to be specified or read from inputstream? … … 21 22 } 22 23 } 24 25 public Object parseToObject(HandlingInput input) throws SAXException, IOException { 26 StringWriter writer = new StringWriter(); 27 28 //TODO reconsider this: sources can write to streams directly maybe some input.writeTo() 29 30 Reader reader = input.getReader(); 31 char[] buf = new char[1024]; 32 int read = -1; 33 while ( (read = reader.read(buf)) > 0) { 34 writer.write(buf, 0, read); 35 } 36 return writer.toString(); 37 } 23 38 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/handling/XMLHandlingStrategy.java
r1741 r1743 5 5 import javax.xml.parsers.SAXParser; 6 6 7 import org.kauriproject.template.TemplateResult; 7 import org.kauriproject.util.io.IOUtils; 8 import org.kauriproject.util.xml.LocalDocumentBuilderFactory; 8 9 import org.kauriproject.xml.sax.EmbedXmlFilter; 9 10 import org.kauriproject.xml.sax.XmlConsumer; 11 import org.xml.sax.ContentHandler; 12 import org.xml.sax.InputSource; 10 13 import org.xml.sax.SAXException; 11 14 import org.xml.sax.XMLReader; 12 15 13 16 class XMLHandlingStrategy implements HandlingStrategy { 14 public void doInsert( InsertBlockInput input, final TemplateResultresult, final SAXParser parser) throws IOException, SAXException {17 public void doInsert(HandlingInput input, final ContentHandler result, final SAXParser parser) throws IOException, SAXException { 15 18 // default to XML: parse with saxparser 16 19 XmlConsumer consumer = new EmbedXmlFilter(result); … … 20 23 xmlReader.parse(input.getInputSource()); 21 24 } 25 26 public Object parseToObject(HandlingInput input) throws SAXException, IOException { 27 InputSource is = null; 28 try { 29 is = input.getInputSource(); 30 return LocalDocumentBuilderFactory.getBuilder().parse(is); 31 } finally { 32 IOUtils.closeQuietly(input); 33 } 34 } 22 35 } -
trunk/universe/kauri-template/src/test/java/org/kauriproject/template/TemplateExecutionTest.java
r1737 r1743 193 193 testFlow("/org/kauriproject/template/jsonvariables.xml", true); 194 194 } 195 196 public void testModeHandling() throws Exception { 197 testFlow("/org/kauriproject/template/mode_handling.xml", true); 198 } 199 195 200 } -
trunk/universe/kauri-xmlcore/src/main/java/org/kauriproject/xml/html/HtmlBodyContentsFilter.java
r1741 r1743 7 7 8 8 public class HtmlBodyContentsFilter implements ContentHandler { 9 private ContentHandler consumer;9 private final ContentHandler consumer; 10 10 private int depthInBody = 0; 11 11 12 public HtmlBodyContentsFilter( ContentHandler consumer) {12 public HtmlBodyContentsFilter(final ContentHandler consumer) { 13 13 this.consumer = consumer; 14 14 }
Note: See TracChangeset
for help on using the changeset viewer.