View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.millscript.loaders;
22  
23  import org.millscript.commons.alert.alerts.Fault;
24  import org.millscript.commons.xml.api.token.CharDataToken;
25  import org.millscript.commons.xml.api.token.CommentToken;
26  import org.millscript.commons.xml.api.token.EndTagToken;
27  import org.millscript.commons.xml.api.token.StartTagToken;
28  import org.millscript.millscript.datatypes.XmlComment;
29  import org.millscript.millscript.expr.ApplyExpr;
30  import org.millscript.millscript.expr.CommaExpr;
31  import org.millscript.millscript.expr.ConstantExpr;
32  import org.millscript.millscript.expr.ExplodeExpr;
33  import org.millscript.millscript.expr.NameExpr;
34  
35  /**
36   * This class is used when visiting an XML element inside a basic template and
37   * it handles the tokens visited in the elements content.
38   */
39  public class BasicTemplateLoaderElementTokenVisitor extends BasicTemplateLoaderTokenVisitor {
40  
41      /**
42       * Constructs a new element token visitor, with the specified
43       * controlling loader, parent token visitor and start tag.
44       *
45       * @param btl   the controlling loader
46       * @param ptv   the parent token visitor
47       * @param stt   the start tag token
48       */
49      public BasicTemplateLoaderElementTokenVisitor( final BasicTemplateLoader btl, final BasicTemplateLoaderTokenVisitor ptv, final StartTagToken stt ) {
50          super( btl, ptv, stt );
51      }
52  
53      /**
54       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.CharDataToken)
55       */
56      @Override
57      public void visit( final CharDataToken token ) {
58          if ( token.getCharData().length() == 0 ) {
59              // TODO - We might be able to remove this
60              throw new Fault(
61                  "Appending empty character data"
62              ).mishap();
63          }
64          // TODO - This needs to respect whitespace, CDATA and plain text
65          this.pushChildElement(
66              new ConstantExpr(
67                  token.getCharData()
68              )
69          );
70      }
71  
72      /**
73       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.CommentToken)
74       */
75      @Override
76      public void visit( final CommentToken token ) {
77          if ( token.getCommentText().length() == 0 ) {
78              // TODO - We might be able to remove this. I'm more certain about
79              // this than the one for character data
80              throw new Fault(
81                  "Appending empty comment"
82              ).mishap();
83          }
84          // NOTE - We allow XML comments to behave like format strings, so we
85          // must run each one through the format function.
86          this.pushChildElement(
87              new ApplyExpr(
88                  new NameExpr( "format" ),
89                  new CommaExpr(
90                      new ConstantExpr(
91                          new XmlComment(
92                              token.getCommentText()
93                          )
94                      ),
95                      new ExplodeExpr(
96                          new NameExpr( "arg" )
97                      )
98                  )
99              )
100         );
101     }
102 
103     /**
104      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.EndTagToken)
105      */
106     @Override
107     public void visit( final EndTagToken token ) {
108         this.basicTemplateLoader.compileEndTag( token );
109     }
110 
111 }