001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package jsx;
021 
022 import js.ArrayLike;
023 import js.Disposable;
024 import js.Js;
025 import js.Value;
026 import js.Vars;
027 
028 /**
029  * <p>A utility class, with its static methods, providing facilities to operate on 
030  * HTML templates.</p>
031  * <p>A template in JScripter(JS) Class Library is actually an array of strings.</p>
032  * 
033  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
034  */
035 public final class Template extends Disposable
036 {
037     private Template() {}
038 
039     /**
040      * <p>Applies a template by itself.</p>
041      * @return The result string.
042      * @since 1.0
043      */
044     public static final String apply(ArrayLike<String> tmplt) {
045         return apply(tmplt, new Vars<String>().var());
046     }
047 
048     /**
049      * <p>Applies a template to an argument string.</p>
050      * @param tmplt The template (an array of strings) to apply.
051      * @param arg The argument string.
052      * @return The result string.
053      * @since 1.0
054      */
055     public static final String apply(ArrayLike<String> tmplt, String arg) {
056         return apply(tmplt, create(arg));
057     }
058 
059     /**
060      * <p>Applies a template to an array of parameter strings.</p>
061      * @param tmplt The template (an array of strings) to apply.
062      * @param paras The parameters, which is also an array of strings.
063      * @return The result string.
064      * @since 1.0
065      */
066     public static final String apply(ArrayLike<String> tmplt, ArrayLike<String> paras) {
067         Value.String html = new Value.String("");
068         int tlen = tmplt.length();
069         int plen = paras.length();
070         if (tlen < plen) {
071             for (int i = 0; i < tlen; i++) {
072                 html.aadd(paras.get(i));
073                 html.aadd(tmplt.get(i));
074             }
075             for (int i = tlen; i < plen; i++) {
076                 html.aadd(paras.get(i));
077             }
078         } else {
079             for (int i = 0; i < plen; i++) {
080                 html.aadd(tmplt.get(i));
081                 html.aadd(paras.get(i));
082             }
083             for (int i = plen; i < tlen; i++) {
084                 html.aadd(tmplt.get(i));
085             }
086         }
087         return html.var();
088     }
089 
090     /**
091      * <p>Creates a template with a single argument string.</p>
092      * @param s The argument string.
093      * @return The result template.
094      * @since 1.0
095      */
096     public static final ArrayLike<String> create(String s) {
097         return new Vars<String>().add(s).var();
098     }
099 
100     /**
101      * <p>The template for creating XML starting tags.</p>
102      * @since 1.0
103      */
104     public final static ArrayLike<String> START = new Vars<String>().add("<").add(">").var();
105     /**
106      * <p>The template for creating XML closing tags.</p>
107      * @since 1.0
108      */
109     public final static ArrayLike<String> CLOSE = new Vars<String>().add(Js.add("<", "/")).add(">").var();
110 
111     /**
112      * <p>Creates a template for creating XML elements with starting and closing tag names.</p>
113      * @param start The start tag name.
114      * @param close The close tag name.
115      * @return The result template.
116      * @since 1.0
117      */
118     public static final ArrayLike<String> element(String start, String close) {
119         return new Vars<String>().add(Template.apply(START, Template.create(start)))
120                                  .add(Template.apply(CLOSE, Template.create(close)))
121                                  .var();
122     }
123     /**
124      * <p>Creates a template for creating XML elements with the tag name.</p>
125      * @param tag The tag name.
126      * @return The result template.
127      * @since 1.0
128      */
129     public static final ArrayLike<String> element(String tag) {
130         return element(tag, tag);
131     }
132     /**
133      * <p>Creates a template for creating XML attributes with the attribute name.</p>
134      * @param attr The attribute name.
135      * @return The result template.
136      * @since 1.0
137      */
138     public static final ArrayLike<String> attr(String attr) {
139         return new Vars<String>().add(
140                 Code.join(" ", attr, "=")
141         ).var();
142     }
143 
144     /**
145      * <p>The template for creating HTML &lt;button&gt;&lt;/button&gt; elements.</p>
146      * @since 1.0
147      */
148     public final static ArrayLike<String> BUTTON = element("button");
149     /**
150      * <p>The template for creating HTML &lt;div&gt;&lt;/div&gt; elements.</p>
151      * @since 1.0
152      */
153     public final static ArrayLike<String> DIV    = element("div"   );
154     /**
155      * <p>The template for creating HTML &lt;label&gt;&lt;/label&gt; elements.</p>
156      * @since 1.0
157      */
158     public final static ArrayLike<String> LABEL  = element("label" );
159     /**
160      * <p>The template for creating HTML &lt;li&gt;&lt;/li&gt; elements.</p>
161      * @since 1.0
162      */
163     public final static ArrayLike<String> LI     = element("li"    );
164     /**
165      * <p>The template for creating HTML &lt;ul&gt;&lt;/ul&gt; elements.</p>
166      * @since 1.0
167      */
168     public final static ArrayLike<String> UL     = element("ul"    );
169     /**
170      * <p>The template for creating HTML &lt;span&gt;&lt;/span&gt; elements.</p>
171      * @since 1.0
172      */
173     public final static ArrayLike<String> SPAN   = element("span"  );
174 }