1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
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
134
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 }