Changeset 3197

Show
Ignore:
Timestamp:
05/10/07 14:16:26 (3 years ago)
Author:
michael
Message:

Enable use of xtc:trim="true" to optionally trim preceeding whitespace to allow <f:attribute> to work (since whitespace will cause the opening tag to be closed to new attributes).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/java/org/sarugo/xtc/XMLWriter.java

    r2975 r3197  
    7676        public void writeAttribute(String name, Object value) throws IOException { 
    7777                if (!inOpenElement) 
    78                         throw new IllegalStateException("Openning tag has been closed."); 
     78                        throw new IllegalStateException("Opening tag has been closed."); 
    7979                write(" "); 
    8080                write(name); 
  • trunk/src/main/java/org/sarugo/xtc/compiler/CompilationUnit.java

    r2915 r3197  
    7575        return new CompositeTemplateHandler(fh); 
    7676    } 
     77     
     78    public boolean trimPreceedingWhitespace() { 
     79        return false; 
     80    } 
    7781 
    7882} 
  • trunk/src/main/java/org/sarugo/xtc/compiler/TagUnit.java

    r2915 r3197  
    1717import org.sarugo.xtc.TemplateHandler; 
    1818import org.sarugo.xtc.tag.Tag; 
     19import org.sarugo.xtc.tag.TagAttribute; 
     20import org.sarugo.xtc.tag.TagAttributeException; 
    1921import org.sarugo.xtc.tag.TagConfig; 
     22import org.sarugo.xtc.tag.TagHandler; 
    2023import org.sarugo.xtc.tag.TagLibrary; 
    21  
    2224 
    2325/** 
     
    2830class TagUnit extends CompilationUnit implements TagConfig { 
    2931 
     32    private static final String TRIM_NAMESPACE = "http://sarugo.org/xtc"; 
     33 
     34    private static final String TRIM_ATTRIBUTE = "trim"; 
     35 
    3036    private final TagLibrary library; 
    3137 
     
    3339 
    3440    private final Tag tag; 
    35      
     41 
    3642    private final String namespace; 
    37      
     43 
    3844    private final String name; 
    3945 
    40     public TagUnit(TagLibrary library, String namespace, String name, Tag tag, String id) { 
     46    private final boolean trimPreceedingWhitespace; 
     47 
     48    public TagUnit(TagLibrary library, String namespace, String name, Tag tag, 
     49            String id) { 
    4150        this.library = library; 
    4251        this.tag = tag; 
     
    4453        this.name = name; 
    4554        this.id = id; 
     55        TagAttribute trim = this.tag.getAttributes().get(TRIM_NAMESPACE, 
     56                TRIM_ATTRIBUTE); 
     57        if (trim != null) { 
     58            if (!trim.isLiteral()) { 
     59                throw new TagAttributeException(this.tag, trim, 
     60                        "Value must be literal."); 
     61            } 
     62            trimPreceedingWhitespace = trim.getBoolean(null); 
     63        } else { 
     64            trimPreceedingWhitespace = false; 
     65        } 
    4666    } 
    4767 
     
    6686    } 
    6787 
     88    @Override 
     89    public boolean trimPreceedingWhitespace() { 
     90        return this.trimPreceedingWhitespace; 
     91    } 
     92 
    6893} 
  • trunk/src/main/java/org/sarugo/xtc/compiler/TextUnit.java

    r3009 r3197  
    5959 
    6060        public TemplateHandler createTemplateHandler() { 
    61                 this.flushBufferToConfig(); 
     61                this.flushBufferToConfig(false); 
    6262 
    6363                if (this.children.size() == 0) { 
     
    8282 
    8383        private void addInstruction(Instruction instruction) { 
    84                 this.flushTextBuffer(); 
     84                this.flushTextBuffer(false); 
    8585                this.instructionBuffer.add(instruction); 
    8686        } 
    8787 
    88         private void flushTextBuffer() { 
     88        private void flushTextBuffer(boolean trimTrailingWhitespace) { 
    8989                if (this.textBuffer.length() > 0) { 
    9090                        String s = this.textBuffer.toString(); 
     91                         
     92                        if (trimTrailingWhitespace) { 
     93                                s = trimRight(s); 
     94                        } 
    9195 
    9296                        if (s.length() > 0) { 
     
    200204                // then we need to capture our buffer into a UITextHandler 
    201205                this.finishStartTag(); 
    202                 this.flushBufferToConfig(); 
     206                this.flushBufferToConfig(unit.trimPreceedingWhitespace()); 
    203207                this.children.add(unit); 
    204208        } 
    205209 
    206         protected void flushBufferToConfig() { 
    207  
    208                 this.flushTextBuffer(); 
     210        protected void flushBufferToConfig(boolean trimTrailingWhitespace) { 
     211 
     212                this.flushTextBuffer(trimTrailingWhitespace); 
    209213 
    210214                int size = this.instructionBuffer.size(); 
     
    238242        } 
    239243 
     244        private final static String trimRight(String s) { 
     245                int i = s.length() - 1; 
     246                while (i >= 0 && Character.isWhitespace(s.charAt(i))) { 
     247                        i--; 
     248                } 
     249                if (i == s.length() - 1) { 
     250                        return s; 
     251                } else { 
     252                        return s.substring(0, i + 1); 
     253                } 
     254        } 
     255 
    240256        public String toString() { 
    241257                return "TextUnit[" + this.children.size() + "]"; 
  • trunk/src/test/resources/jsf-core.xhtml

    r3009 r3197  
    22                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    33<html xmlns="http://www.w3.org/1999/xhtml" 
     4      xmlns:xtc="http://sarugo.org/xtc" 
    45      xmlns:c="http://java.sun.com/jsp/jstl/core" 
    56      xmlns:f="http://java.sun.com/jsf/core"> 
     
    89<div><f:attribute name="id" value="foo"/>The foo div.</div> 
    910<c:set var="bar" value="bar"/> 
    10 <div><f:attribute name="id" value="#{bar}"/>The bar div.</div> 
     11<div>   <f:attribute xtc:trim="true" name="id" value="#{bar}"/>The bar div.</div> 
    1112</body> 
    1213</html>