Changeset 1685
- Timestamp:
- 2010-08-18 07:48:38 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/InsertBlock.java
r1565 r1685 19 19 import java.io.ByteArrayInputStream; 20 20 import java.io.FileNotFoundException; 21 import java.io.IOException; 21 22 import java.io.InputStream; 22 23 import java.io.InputStreamReader; … … 27 28 import org.kauriproject.template.el.Expression; 28 29 import org.kauriproject.template.source.Source; 30 import org.kauriproject.util.io.IOUtils; 29 31 import org.kauriproject.util.xml.XmlMediaTypeHelper; 30 32 import org.kauriproject.xml.sax.EmbedXmlFilter; … … 88 90 Source source = null; 89 91 String valueStr = null; 92 String usedMode = "undefined"; 90 93 91 94 try { … … 98 101 valueStr = (String) valueExpr.evaluate(context.getTemplateContext()); 99 102 } 100 InputStream inputStr = (source != null ? source.getInputStream() : new ByteArrayInputStream(valueStr.getBytes())); 101 102 if (mode != null) { 103 parseType = ParseType.fromMode(mode); 103 104 if (mode != null) { 105 parseType = ParseType.fromMode(mode); // this is silent about unknown modes 104 106 } 105 if (parseType == null) { 106 parseType = ParseType.from String(source.getMediaType());107 if (parseType == null) { 108 parseType = ParseType.fromMediaType(source.getMediaType()); // this could NPE when source == null 107 109 } 108 110 109 if (ParseType.TXT == parseType) { 110 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStr)); 111 String line = reader.readLine(); 112 while (line != null) { 113 result.characters(line.toCharArray(), 0, line.length()); 114 line = reader.readLine(); 115 } 116 } else { 117 // default to XML: parse with saxparser 118 XmlConsumer consumer = new EmbedXmlFilter(result); 119 XMLReader xmlReader = parser.getXMLReader(); 120 xmlReader.setContentHandler(consumer); 121 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", consumer); 122 xmlReader.parse(new InputSource(inputStr)); 123 } 124 inputStr.close(); 111 usedMode = parseType.name(); 112 parseType.doInsert(source, valueStr, result, parser); // this could NPE when parseType was still not found 113 125 114 } catch (FileNotFoundException ex) { 126 throw new TemplateException("Error inserting XMLfrom " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex);115 throw new TemplateException("Error inserting " + usedMode + "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex); 127 116 } catch (Exception ex) { 128 throw new TemplateException("Error parsing XMLfrom " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex);117 throw new TemplateException("Error parsing " + usedMode + "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex); 129 118 } finally { 130 119 if (source != null) { … … 145 134 146 135 } 147 148 enum ParseType { 149 XML, TXT; 150 151 public static ParseType fromMode(String mode) { 152 if (mode.equals("xml")) { 153 return XML; 154 } else if (mode.equals("txt")) { 155 return TXT; 156 } else { 157 return null; 136 } 137 138 enum ParseType { 139 XML("xml", new XMLInsertStrategy()), 140 TXT("txt", new TextInsertStrategy()), 141 HTML("html", new HTMLInsertStrategy()); 142 143 private final String mode; 144 private final InsertStrategy strategy; 145 146 private ParseType(final String mode, final InsertStrategy strategy){ 147 this.mode = mode; 148 this.strategy = strategy; 149 } 150 151 void doInsert(Source src, String value, TemplateResult result, SAXParser parser) throws IOException, SAXException{ 152 this.strategy.doInsert(src, value, result, parser); 153 } 154 155 static ParseType fromMode(final String lookupMode) { 156 for (ParseType candidate : ParseType.values()) { 157 if (candidate.mode.equals(lookupMode)) 158 return candidate; 159 } 160 return null; // none found 161 } 162 163 static ParseType fromMediaType(String mime) { 164 if (XmlMediaTypeHelper.isXmlMediaType(mime)) { 165 return XML; 166 } else if (mime.startsWith("text/html")) { 167 return HTML; 168 } else if (mime.startsWith("text")) { 169 return TXT; 170 } else { 171 return null; 172 } 173 } 174 } 175 176 interface InsertStrategy { 177 void doInsert(Source src, String value, TemplateResult result, SAXParser parser) throws IOException, SAXException; 178 } 179 180 class XMLInsertStrategy implements InsertStrategy { 181 public void doInsert(final Source source, final String valueStr, final TemplateResult result, final SAXParser parser) throws IOException, SAXException { 182 InputStream inputStr = null; 183 try { 184 inputStr = (source != null ? source.getInputStream() : new ByteArrayInputStream(valueStr.getBytes())); 185 186 // default to XML: parse with saxparser 187 XmlConsumer consumer = new EmbedXmlFilter(result); 188 XMLReader xmlReader = parser.getXMLReader(); 189 xmlReader.setContentHandler(consumer); 190 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", consumer); 191 xmlReader.parse(new InputSource(inputStr)); 192 } finally { 193 IOUtils.closeQuietly(inputStr); 194 } 195 } 196 } 197 198 class TextInsertStrategy implements InsertStrategy { 199 public void doInsert(final Source source, final String valueStr, final TemplateResult result, SAXParser parser) throws IOException, SAXException { 200 InputStream inputStr = null; 201 try { 202 inputStr = (source != null ? source.getInputStream() : new ByteArrayInputStream(valueStr.getBytes())); 203 204 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStr)); 205 String line = reader.readLine(); 206 while (line != null) { 207 result.characters(line.toCharArray(), 0, line.length()); 208 line = reader.readLine(); 158 209 } 159 } 160 161 public static ParseType fromString(String name) { 162 if (XmlMediaTypeHelper.isXmlMediaType(name)) { 163 return XML; 164 } else if (name.startsWith("text")) { 165 return TXT; 166 } else { 167 return null; 168 } 169 } 170 } 171 172 } 210 } finally { 211 IOUtils.closeQuietly(inputStr); 212 } 213 } 214 } 215 216 class HTMLInsertStrategy implements InsertStrategy { 217 public void doInsert(final Source source, final String valueStr, final TemplateResult result, SAXParser parser) throws SAXException, IOException { 218 InputStream inputStr = null; 219 try { 220 inputStr = (source != null ? source.getInputStream() : new ByteArrayInputStream(valueStr.getBytes())); 221 222 // fake for now 223 final char[] msg = "TODO: HTML insert mode not yet supported.".toCharArray(); 224 result.characters(msg, 0, msg.length); 225 226 // TODO real HTML cleaning and insertion 227 228 } finally { 229 IOUtils.closeQuietly(inputStr); 230 } 231 } 232 }
Note: See TracChangeset
for help on using the changeset viewer.