View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2005 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.loaders;
23  
24  import org.millscript.commons.util.map.EHashMap;
25  import org.millscript.commons.xml.api.Name;
26  import org.millscript.commons.xml.api.token.EndTagToken;
27  import org.millscript.commons.xml.api.token.StartTagToken;
28  import org.millscript.commons.xml.tokenizer.NoNamespacesName;
29  import org.millscript.millscript.expr.ConstantExpr;
30  import org.millscript.millscript.expr.Expr;
31  import org.millscript.millscript.expr.LambdaExpr;
32  import org.millscript.millscript.expr.NameExpr;
33  
34  /**
35   * This class implements the MillScript template loader. The specified file is
36   * loaded as an XHTML file, but the additional elements <code>arg</code> and
37   * <code>item</code> and attribute values starting with <code>?</code> take on
38   * special meaning.
39   */
40  public final class TemplateLoader extends BasicXHTMLTemplateLoader {
41  
42      /**
43       * A mapping from special element names to their handler.
44       */
45      private final EHashMap< Name, SpecialTemplateName > specialTemplateElements = new EHashMap< Name, SpecialTemplateName >();
46  
47      /**
48       * Constructs a new XHTML template loader which does not support XML
49       * namespaces, with the default set of special elements.
50       */
51      public TemplateLoader() {
52          super( false );
53          specialTemplateElements.insert(
54              new NoNamespacesName( "item" ),
55              new ItemName( this )
56          );
57          specialTemplateElements.insert(
58              new NoNamespacesName( "arg" ),
59              new ArgName( this )
60          );
61          specialTemplateElements.insert(
62              new NoNamespacesName( "arg1" ),
63              new ShortcutArgName( this, 1 )
64          );
65          specialTemplateElements.insert(
66              new NoNamespacesName( "arg2" ),
67              new ShortcutArgName( this, 2 )
68          );
69          specialTemplateElements.insert(
70              new NoNamespacesName( "arg3" ),
71              new ShortcutArgName( this, 3 )
72          );
73          specialTemplateElements.insert(
74              new NoNamespacesName( "arg4" ),
75              new ShortcutArgName( this, 4 )
76          );
77          specialTemplateElements.insert(
78              new NoNamespacesName( "arg5" ),
79              new ShortcutArgName( this, 5 )
80          );
81          specialTemplateElements.insert(
82              new NoNamespacesName( "arg6" ),
83              new ShortcutArgName( this, 6 )
84          );
85          specialTemplateElements.insert(
86              new NoNamespacesName( "arg7" ),
87              new ShortcutArgName( this, 7 )
88          );
89          specialTemplateElements.insert(
90              new NoNamespacesName( "arg8" ),
91              new ShortcutArgName( this, 8 )
92          );
93          specialTemplateElements.insert(
94              new NoNamespacesName( "arg9" ),
95              new ShortcutArgName( this, 9 )
96          );
97      }
98  
99      /**
100      * @see org.millscript.millscript.loaders.BasicTemplateLoader#compAttributeValue(java.lang.String)
101      */
102     @Override
103     public Expr compAttributeValue( final String value ) {
104         if ( value.length() > 0 && value.charAt( 0 ) == '?' ) {
105             // Compile everything but the leading '?'
106             return compSpecialString( value.substring( 1, value.length() ) );
107         } else {
108             return new ConstantExpr( value );
109         }
110     }
111 
112     /**
113      * @see org.millscript.millscript.loaders.BasicTemplateLoader#compileEndTag(org.millscript.commons.xml.api.token.EndTagToken)
114      */
115     @Override
116     public void compileEndTag( final EndTagToken token ) {
117         final SpecialTemplateName specialName = this.specialTemplateElements.get( token.getName() );
118         if ( specialName == null ) {
119             super.compileEndTag( token );
120         } else {
121             specialName.handle();
122             // End this element
123             this.tokenVisitor = this.tokenVisitor.parentTokenVisitor;
124         }
125     }
126 
127     /**
128      * @see org.millscript.millscript.loaders.BasicTemplateLoader#compileStartTag(org.millscript.commons.xml.api.token.StartTagToken)
129      */
130     @Override
131     public void compileStartTag( final StartTagToken token ) {
132         if ( this.specialTemplateElements.containsKey( token.getName() ) ) {
133             // ok, we allow these special elements through, just start another
134             // normal element
135             this.tokenVisitor = new BasicTemplateLoaderElementTokenVisitor( this, this.tokenVisitor, token );
136         } else {
137             super.compileStartTag( token );
138         }
139     }
140 
141     /**
142      * @see org.millscript.millscript.loaders.BasicTemplateLoader#makeLambda(java.lang.String, org.millscript.millscript.expr.Expr)
143      */
144     @Override
145     public Expr makeLambda( final String theName, final Expr body ) {
146         return (
147             new LambdaExpr(
148                 theName,
149                 null,
150                 new NameExpr( "arg" ),
151                 body
152             )
153         );
154     }
155 
156 }