Changeset 1685


Ignore:
Timestamp:
2010-08-18 07:48:38 (3 years ago)
Author:
mpo
Message:

preparing refactoring for adding feature requested in #335
marked some possible places where error detection and messaging could be enhanced

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/InsertBlock.java

    r1565 r1685  
    1919import java.io.ByteArrayInputStream; 
    2020import java.io.FileNotFoundException; 
     21import java.io.IOException; 
    2122import java.io.InputStream; 
    2223import java.io.InputStreamReader; 
     
    2728import org.kauriproject.template.el.Expression; 
    2829import org.kauriproject.template.source.Source; 
     30import org.kauriproject.util.io.IOUtils; 
    2931import org.kauriproject.util.xml.XmlMediaTypeHelper; 
    3032import org.kauriproject.xml.sax.EmbedXmlFilter; 
     
    8890            Source source = null; 
    8991            String valueStr = null; 
     92            String usedMode = "undefined"; 
    9093 
    9194            try { 
     
    98101                    valueStr = (String) valueExpr.evaluate(context.getTemplateContext()); 
    99102                }    
    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 
    104106                } 
    105                 if (parseType == null) { 
    106                     parseType = ParseType.fromString(source.getMediaType()); 
     107                if (parseType == null) {  
     108                    parseType = ParseType.fromMediaType(source.getMediaType()); // this could NPE when source == null 
    107109                } 
    108110 
    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 
    125114            } catch (FileNotFoundException ex) { 
    126                 throw new TemplateException("Error inserting XML from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex); 
     115                throw new TemplateException("Error inserting " + usedMode + "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex); 
    127116            } catch (Exception ex) { 
    128                 throw new TemplateException("Error parsing XML from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex); 
     117                throw new TemplateException("Error parsing " + usedMode + "(mode) from " + (sourceLocation != null ? sourceLocation : valueStr) + ": " + ex); 
    129118            } finally { 
    130119                if (source != null) { 
     
    145134 
    146135    } 
    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 
     138enum 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 
     176interface InsertStrategy { 
     177    void doInsert(Source src, String value, TemplateResult result, SAXParser parser) throws IOException, SAXException; 
     178} 
     179 
     180class 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 
     198class 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(); 
    158209            } 
    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 
     216class 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.