Changeset 778:b35f4c6bf4f3 in livinglogic.java.ul4

Show
Ignore:
Timestamp:
01/14/13 19:01:12 (7 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Don't preformat Text nodes.

Location:
src/main/java/com/livinglogic
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • src/main/java/com/livinglogic/ul4/CompiledTemplate.java

    r772 r778  
    5858    } 
    5959 
    60     public void render(Writer out, Map<String, Object> variables) throws java.io.IOException 
     60    public void render(Writer out, Map<String, Object> variables, boolean keepWhitespace) throws java.io.IOException 
    6161    { 
    62         render(new EvaluationContext(out, variables)); 
     62        render(new EvaluationContext(out, variables, keepWhitespace)); 
    6363    } 
    6464 
     
    9696    } 
    9797 
    98     public String renders(Map<String, Object> variables) 
     98    public String renders(Map<String, Object> variables, boolean keepWhitespace) 
    9999    { 
    100         return renders(new EvaluationContext(null, variables)); 
     100        return renders(new EvaluationContext(null, variables, keepWhitespace)); 
    101101    } 
    102102 
  • src/main/java/com/livinglogic/ul4/EvaluationContext.java

    r775 r778  
    3535 
    3636    /** 
     37     * Should whitespace in literal text be kept? 
     38     */ 
     39    protected boolean keepWhitespace; 
     40 
     41    /** 
    3742     * The stack of currently executing templates. 
    3843     */ 
     
    5156     * @param writer The output stream where the template output will be written 
    5257     */ 
    53     public EvaluationContext(Writer writer) 
    54     { 
    55         this(writer, null); 
     58    public EvaluationContext(Writer writer, boolean keepWhitespace) 
     59    { 
     60        this(writer, null, keepWhitespace); 
    5661    } 
    5762 
     
    6267     *                  template code (or {@code null} for no variables) 
    6368     */ 
    64     public EvaluationContext(Writer writer, Map<String, Object> variables) 
     69    public EvaluationContext(Writer writer, Map<String, Object> variables, boolean keepWhitespace) 
    6570    { 
    6671        this.writer = writer; 
     
    6873            variables = new HashMap<String, Object>(); 
    6974        this.variables = variables; 
     75        this.keepWhitespace = keepWhitespace; 
    7076        this.stack = new Stack<Template>(); 
    7177        this.allVariables = new MapChain<String, Object>(variables, makeMap("stack", stack)); 
     
    112118    { 
    113119        return allVariables; 
     120    } 
     121 
     122    /** 
     123     * Return whether whitespace should be kept in output of literal text. 
     124     */ 
     125    boolean getKeepWhitespace() 
     126    { 
     127        return keepWhitespace; 
    114128    } 
    115129 
  • src/main/java/com/livinglogic/ul4/InterpretedTemplate.java

    r777 r778  
    2626import org.apache.commons.lang.StringEscapeUtils; 
    2727 
    28 import static com.livinglogic.utils.StringUtils.removeWhiteSpace; 
     28import static com.livinglogic.utils.StringUtils.removeWhitespace; 
    2929import com.livinglogic.ul4on.Decoder; 
    3030import com.livinglogic.ul4on.Encoder; 
     
    4646 
    4747    /** 
    48      * Should whitespace be stripped from text nodes? 
    49      * (i.e. linefeed an the whitespace after the linefeed will be removed. Other spaces/tabs etc. will not be removed) 
    50      */ 
    51     public boolean keepWhiteSpace = true; 
     48     * Should whitespace be skipped when outputting text nodes? 
     49     * (i.e. linefeed and the whitespace after the linefeed will be skipped. Other spaces/tabs etc. will not be skipped) 
     50     */ 
     51    public boolean keepWhitespace = true; 
    5252 
    5353    /** 
     
    6969     * Creates an empty template object. Must be filled in later (use for creating subtemplates) 
    7070     */ 
    71     public InterpretedTemplate(Location location, String name, boolean keepWhiteSpace, String startdelim, String enddelim) 
     71    public InterpretedTemplate(Location location, String name, boolean keepWhitespace, String startdelim, String enddelim) 
    7272    { 
    7373        super(location); 
    7474        this.source = null; 
    7575        this.name = name; 
    76         this.keepWhiteSpace = keepWhiteSpace; 
     76        this.keepWhitespace = keepWhitespace; 
    7777        this.startdelim = startdelim; 
    7878        this.enddelim = enddelim; 
     
    8484    } 
    8585 
    86     public InterpretedTemplate(String source, boolean keepWhiteSpace) throws RecognitionException 
    87     { 
    88         this(source, null, keepWhiteSpace, "<?", "?>"); 
     86    public InterpretedTemplate(String source, boolean keepWhitespace) throws RecognitionException 
     87    { 
     88        this(source, null, keepWhitespace, "<?", "?>"); 
    8989    } 
    9090 
     
    9494    } 
    9595 
    96     public InterpretedTemplate(String source, String name, boolean keepWhiteSpace) throws RecognitionException 
    97     { 
    98         this(source, name, keepWhiteSpace, "<?", "?>"); 
     96    public InterpretedTemplate(String source, String name, boolean keepWhitespace) throws RecognitionException 
     97    { 
     98        this(source, name, keepWhitespace, "<?", "?>"); 
    9999    } 
    100100 
     
    104104    } 
    105105 
    106     public InterpretedTemplate(String source, boolean keepWhiteSpace, String startdelim, String enddelim) throws RecognitionException 
    107     { 
    108         this(source, null, keepWhiteSpace, startdelim, enddelim); 
    109     } 
    110  
    111     public InterpretedTemplate(String source, String name, boolean keepWhiteSpace, String startdelim, String enddelim) throws RecognitionException 
     106    public InterpretedTemplate(String source, boolean keepWhitespace, String startdelim, String enddelim) throws RecognitionException 
     107    { 
     108        this(source, null, keepWhitespace, startdelim, enddelim); 
     109    } 
     110 
     111    public InterpretedTemplate(String source, String name, boolean keepWhitespace, String startdelim, String enddelim) throws RecognitionException 
    112112    { 
    113113        super((Location)null); 
    114114        this.source = source; 
    115115        this.name = name; 
    116         this.keepWhiteSpace = keepWhiteSpace; 
     116        this.keepWhitespace = keepWhitespace; 
    117117        this.startdelim = startdelim; 
    118118        this.enddelim = enddelim; 
     
    136136                if (type == null) 
    137137                { 
    138                     if (keepWhiteSpace) 
    139                         innerBlock.append(new Text(location, null)); 
    140                     else 
    141                     { 
    142                         String originalText = location.getCode(); 
    143                         String text = removeWhiteSpace(originalText); 
    144                         if (text.length() != 0) 
    145                         { 
    146                             innerBlock.append(new Text(location, text.equals(originalText) ? null : text)); 
    147                         } 
    148                     } 
     138                    innerBlock.append(new Text(location)); 
    149139                } 
    150140                else if (type.equals("print")) 
     
    232222                { 
    233223                    // Copy over the attributes that we know now, the source is set once the <?end?> tag is encountered 
    234                     InterpretedTemplate subtemplate = new InterpretedTemplate(location, location.getCode(), keepWhiteSpace, startdelim, enddelim); 
     224                    InterpretedTemplate subtemplate = new InterpretedTemplate(location, location.getCode(), keepWhitespace, startdelim, enddelim); 
    235225                    innerBlock.append(subtemplate); 
    236226                    stack.push(subtemplate); 
     
    285275    { 
    286276        return source; 
     277    } 
     278 
     279    public boolean getKeepWhitespace() 
     280    { 
     281        return keepWhitespace; 
    287282    } 
    288283 
     
    410405    public void render(java.io.Writer writer, Map<String, Object> variables) throws IOException 
    411406    { 
    412         render(new EvaluationContext(writer, variables)); 
     407        render(new EvaluationContext(writer, variables, keepWhitespace)); 
    413408    } 
    414409 
     
    605600    { 
    606601        Utils.register("de.livinglogic.ul4.location", new ObjectFactory(){ public UL4ONSerializable create() { return new com.livinglogic.ul4.Location(null, null, -1, -1, -1, -1); }}); 
    607         Utils.register("de.livinglogic.ul4.text", new ObjectFactory(){ public UL4ONSerializable create() { return new com.livinglogic.ul4.Text(null, null); }}); 
     602        Utils.register("de.livinglogic.ul4.text", new ObjectFactory(){ public UL4ONSerializable create() { return new com.livinglogic.ul4.Text(null); }}); 
    608603        Utils.register("de.livinglogic.ul4.const", new ObjectFactory(){ public UL4ONSerializable create() { return new com.livinglogic.ul4.Const(null); }}); 
    609604        Utils.register("de.livinglogic.ul4.list", new ObjectFactory(){ public UL4ONSerializable create() { return new com.livinglogic.ul4.List(); }}); 
     
    661656        encoder.dump(source); 
    662657        encoder.dump(name); 
    663         encoder.dump(keepWhiteSpace); 
     658        encoder.dump(keepWhitespace); 
    664659        encoder.dump(startdelim); 
    665660        encoder.dump(enddelim); 
     
    676671        source = (String)decoder.load(); 
    677672        name = (String)decoder.load(); 
    678         keepWhiteSpace = (Boolean)decoder.load(); 
     673        keepWhitespace = (Boolean)decoder.load(); 
    679674        startdelim = (String)decoder.load(); 
    680675        enddelim = (String)decoder.load(); 
     
    695690            HashMap<String, ValueMaker> v = new HashMap<String, ValueMaker>(super.getValueMakers()); 
    696691            v.put("name", new ValueMaker(){public Object getValue(Object object){return ((InterpretedTemplate)object).name;}}); 
    697             v.put("keepws", new ValueMaker(){public Object getValue(Object object){return ((InterpretedTemplate)object).keepWhiteSpace;}}); 
     692            v.put("keepws", new ValueMaker(){public Object getValue(Object object){return ((InterpretedTemplate)object).keepWhitespace;}}); 
    698693            v.put("startdelim", new ValueMaker(){public Object getValue(Object object){return ((InterpretedTemplate)object).startdelim;}}); 
    699694            v.put("enddelim", new ValueMaker(){public Object getValue(Object object){return ((InterpretedTemplate)object).enddelim;}}); 
  • src/main/java/com/livinglogic/ul4/TemplateClosure.java

    r774 r778  
    2424public class TemplateClosure extends ObjectAsMap implements Template, UL4Type 
    2525{ 
    26     private Template template; 
     26    private InterpretedTemplate template; 
    2727    private Map<String, Object> variables; 
    2828 
    29     public TemplateClosure(Template template, Map<String, Object> variables) 
     29    public TemplateClosure(InterpretedTemplate template, Map<String, Object> variables) 
    3030    { 
    3131        this.template = template; 
     
    3333    } 
    3434 
    35     public Template getTemplate() 
     35    public InterpretedTemplate getTemplate() 
    3636    { 
    3737        return template; 
     
    8282    public void render(Writer out, Map<String, Object> variables) throws java.io.IOException 
    8383    { 
    84         render(new EvaluationContext(out, variables)); 
     84        render(new EvaluationContext(out, variables, template.getKeepWhitespace())); 
    8585    } 
    8686 
     
    120120    public String renders(Map<String, Object> variables) 
    121121    { 
    122         return renders(new EvaluationContext(null, variables)); 
     122        return renders(new EvaluationContext(null, variables, template.getKeepWhitespace())); 
    123123    } 
    124124 
  • src/main/java/com/livinglogic/ul4/Text.java

    r777 r778  
    99import java.io.IOException; 
    1010 
     11import static com.livinglogic.utils.StringUtils.removeWhitespace; 
     12 
    1113import com.livinglogic.ul4on.Decoder; 
    1214import com.livinglogic.ul4on.Encoder; 
     
    1416class Text extends Tag 
    1517{ 
    16     private String text; 
    17  
    18     public Text(Location location, String text) 
     18    public Text(Location location) 
    1919    { 
    2020        super(location); 
    21         this.text = text; 
    2221    } 
    2322 
    24     public String getText() 
     23    public String getText(boolean keepWhitespace) 
    2524    { 
    26         return text != null ? text : location.getCode(); 
     25        String text = location.getCode(); 
     26        if (!keepWhitespace) 
     27            text = removeWhitespace(text); 
     28        return text; 
    2729    } 
    2830 
     
    3436            buffer.append("\t"); 
    3537        buffer.append("text("); 
    36         buffer.append(FunctionRepr.call(getText())); 
     38        buffer.append(FunctionRepr.call(getText(true))); 
    3739        buffer.append(")\n"); 
    3840        return buffer.toString(); 
     
    4648    public Object evaluate(EvaluationContext context) throws IOException 
    4749    { 
    48         context.write(getText()); 
     50        context.write(getText(context.getKeepWhitespace())); 
    4951        return null; 
    5052    } 
    51  
    52     public void dumpUL4ON(Encoder encoder) throws IOException 
    53     { 
    54         super.dumpUL4ON(encoder); 
    55         encoder.dump(text); 
    56     } 
    57  
    58     public void loadUL4ON(Decoder decoder) throws IOException 
    59     { 
    60         super.loadUL4ON(decoder); 
    61         text = (String)decoder.load(); 
    62     } 
    6353} 
  • src/main/java/com/livinglogic/utils/StringUtils.java

    r777 r778  
    1515public class StringUtils 
    1616{ 
    17     public static String removeWhiteSpace(String string) 
     17    public static String removeWhitespace(String string) 
    1818    { 
    1919        String[] lines = string.split("\n");