001 
002 /*
003  *  Descripter 1.0 - Java Script Engines
004  *  Copyright (C) 2010-2015  Jianjun Liu (J.J.Liu)
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 org.descripter.js.ss;
021 
022 import java.io.Reader;
023 import java.io.StringReader;
024 
025 import org.descripter.js.Visitor;
026 import org.descripter.js.ss.parse.JSSPHtml;
027 import org.descripter.js.ss.parse.JSSPPage;
028 import org.descripter.js.ss.parse.JSSPParser;
029 import org.descripter.js.ss.parse.JSSPParserVisitor;
030 import org.descripter.js.ss.parse.JSSPScript;
031 import org.descripter.js.ss.parse.SimpleNode;
032 
033 /**
034  * <p>Converts server-pages to JavaScript.</p>
035  * 
036  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.descripter.org" target="_blank">http://www.descripter.org</a>
037  * @since Descripter 1.0
038  */
039 public class Scriptizer extends Visitor<Scriptlet<? extends Daemon>> implements JSSPParserVisitor
040 {
041     /**
042      * <p>Constructs a visitor of this type.</p>
043      * @param scriptlet A {@link Scriptlet} context.
044      * @since Descripter 1.0
045      */
046     public Scriptizer(Scriptlet<? extends Daemon> scriptlet) {
047         super(scriptlet);
048     }
049 
050     /**
051      * <p>Converts JavaScript Server Page into JavaScript.</p>
052      * @param jssp The text of the JavaScript Server Page.
053      * @return The converted JavaScript code
054      * @since Descripter 1.0
055      */
056     public String scriptize(String jssp) {
057         return scriptize(new StringReader(jssp));
058     }
059 
060     /**
061      * <p>Converts JavaScript Server Page into JavaScript.</p>
062      * @param reader A {@link Reader} to read the text of the JavaScript Server Page.
063      * @return The converted JavaScript code
064      * @since Descripter 1.0
065      */
066     public String scriptize(Reader reader) {
067         try {
068             return new JSSPParser(reader).Page().jjtAccept(
069                     this,
070                     ""
071             ).toString();
072         } catch (Exception e) {
073             throw new RuntimeException(e);
074         }
075     }
076 
077     /**
078      * <p>Visits a parsed node of the specified type.</p>
079      * @param node The node to visit.
080      * @param data An {@link Object} datum passed to this method.
081      * @return An {@link Object} datum.
082      * @since Descripter 1.0
083      */
084     @Override
085     public Object visit(SimpleNode node, Object data) {
086         return node.jjtAccept(this, data);
087     }
088 
089     /**
090      * <p>Visits a parsed node of the specified type.</p>
091      * @param node The node to visit.
092      * @param data An {@link Object} datum passed to this method.
093      * @return An {@link Object} datum.
094      * @since Descripter 1.0
095      */
096     @Override
097     public Object visit(JSSPPage node, Object data) {
098         return node.acceptChildren(this, data);
099     }
100 
101     /**
102      * <p>Visits a parsed node of the specified type.</p>
103      * @param node The node to visit.
104      * @param data An {@link Object} datum passed to this method.
105      * @return An {@link Object} datum.
106      * @since Descripter 1.0
107      */
108     @Override
109     public Object visit(JSSPScript node, Object data) {
110         return cat(data, node.jjtGetValue());
111     }
112 
113     /**
114      * <p>Visits a parsed node of the specified type.</p>
115      * @param node The node to visit.
116      * @param data An {@link Object} datum passed to this method.
117      * @return An {@link Object} datum.
118      * @since Descripter 1.0
119      */
120     @Override
121     public Object visit(JSSPHtml node, Object data) {
122         return cat(
123                 data,
124                 stmt(
125                         inv("print",
126                                 qt(
127                                         esc(
128                                                 node.jjtGetValue().toString()
129                                         )
130                                 )
131                         )
132                 )
133         );
134     }
135 }