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.AttListDeclToken;
25  import org.millscript.commons.xml.api.token.CharDataToken;
26  import org.millscript.commons.xml.api.token.CommentToken;
27  import org.millscript.commons.xml.api.token.DTDToken;
28  import org.millscript.commons.xml.api.token.ElementDeclToken;
29  import org.millscript.commons.xml.api.token.EmptyElementToken;
30  import org.millscript.commons.xml.api.token.EndTagToken;
31  import org.millscript.commons.xml.api.token.EntityDeclToken;
32  import org.millscript.commons.xml.api.token.NotationDeclToken;
33  import org.millscript.commons.xml.api.token.PIToken;
34  import org.millscript.commons.xml.api.token.StartTagToken;
35  import org.millscript.commons.xml.api.token.TokenVisitor;
36  import org.millscript.commons.xml.api.token.XmlDeclToken;
37  import org.millscript.millscript.alert.Alerts;
38  import org.millscript.millscript.expr.CommaExpr;
39  import org.millscript.millscript.expr.Expr;
40  import org.millscript.millscript.expr.SkipExpr;
41  
42  /**
43   * This class provides validation and handling logic for loading an XML
44   * document as a MillScript template.
45   */
46  public class BasicTemplateLoaderTokenVisitor implements TokenVisitor {
47  
48      /**
49       * This body expression for the element.
50       */
51      private Expr body = new SkipExpr();
52  
53      /**
54       * The loader that is controlling the XML parse.
55       */
56      final BasicTemplateLoader basicTemplateLoader;
57  
58      /**
59       * The parent token visitor we should return to once this one has finished
60       * what it is supposed to do.
61       */
62      final BasicTemplateLoaderTokenVisitor parentTokenVisitor;
63  
64      /**
65       * The start tag that began the section this token visitor represents.
66       */
67      final StartTagToken startTagToken;
68  
69      /**
70       * Constructs a new basic template loader token visitor, with the specified
71       * controlling loader, parent token visitor and start tag.
72       *
73       * @param btl   the controlling loader
74       * @param ptv   the parent token visitor
75       * @param stt   the start tag token
76       */
77      public BasicTemplateLoaderTokenVisitor( final BasicTemplateLoader btl, final BasicTemplateLoaderTokenVisitor ptv, final StartTagToken stt ) {
78          this.basicTemplateLoader = btl;
79          this.parentTokenVisitor = ptv;
80          this.startTagToken = stt;
81      }
82  
83      /**
84       * Returns the body expression for this element.
85       *
86       * @return  an Expr for this elements body
87       */
88      Expr getBody() {
89          return this.body;
90      }
91  
92      /**
93       * Pushes the expression as part of this elements body.
94       *
95       * @param e the next body element for this element
96       */
97      void pushChildElement( final Expr e ) {
98          this.body = CommaExpr.make( this.body, e );
99      }
100 
101     /**
102      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.AttListDeclToken)
103      */
104     public void visit( final AttListDeclToken token ) {
105         // We don't handle processing instructions
106         throw(
107             Alerts.unimplemented(
108                 "Attribute list declarations cannot be compiled at the moment, sorry!"
109             ).decorate( token ).mishap()
110         );
111     }
112 
113     /**
114      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.CharDataToken)
115      */
116     public void visit( final CharDataToken token ) {
117         // TODO - In the future we might want to return this whitespace as part
118         // of the XML document
119         if ( ! token.isIgnorableWhitespace() ) {
120             throw(
121                 Alerts.unimplemented(
122                     "All character data must be within an element at the moment, sorry!"
123                 ).decorate( token ).mishap()
124             );
125         }
126     }
127 
128     /**
129      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.CommentToken)
130      */
131     public void visit( final CommentToken token ) {
132         throw(
133             Alerts.unimplemented(
134                 "Comments must be within an element at the moment, sorry!"
135             ).decorate( token ).mishap()
136         );
137     }
138 
139     /**
140      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.DTDToken)
141      */
142     public void visit( final DTDToken token ) {
143         throw(
144             Alerts.unimplemented(
145                 "Document type declarations cannot be compiled at the moment, sorry!"
146             ).decorate( token ).mishap()
147         );
148     }
149 
150     /**
151      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.ElementDeclToken)
152      */
153     public void visit( final ElementDeclToken token ) {
154         throw(
155             Alerts.unimplemented(
156                 "Element declarations cannot be compiled at the moment, sorry!"
157             ).decorate( token ).mishap()
158         );
159     }
160 
161     /**
162      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.EmptyElementToken)
163      */
164     public void visit( final EmptyElementToken token ) {
165         // TODO - Keep it simple for now, we can make it a tiny bit faster soon
166         this.visit( (StartTagToken) token );
167         this.visit( (EndTagToken)token );
168     }
169 
170     /**
171      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.EndTagToken)
172      */
173     public void visit( final EndTagToken token ) {
174         throw new Fault(
175             "EndTagToken should never be reported on BasicTemplateLoaderTokenVisitor"
176         ).mishap();
177     }
178 
179     /**
180      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.EntityDeclToken)
181      */
182     public void visit( final EntityDeclToken token ) {
183         throw(
184             Alerts.unimplemented(
185                 "Entity declarations cannot be compiled at the moment, sorry!"
186             ).decorate( token ).mishap()
187         );
188     }
189 
190     /**
191      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.NotationDeclToken)
192      */
193     public void visit( final NotationDeclToken token ) {
194         throw(
195             Alerts.unimplemented(
196                 "Notation declarations cannot be compiled at the moment, sorry!"
197             ).decorate( token ).mishap()
198         );
199     }
200 
201     /**
202      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.PIToken)
203      */
204     public void visit( final PIToken token ) {
205         throw(
206             Alerts.unimplemented(
207                 "Processing instructions cannot be compiled at the moment, sorry!"
208             ).decorate( token ).mishap()
209         );
210     }
211 
212     /**
213      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.StartTagToken)
214      */
215     public void visit( final StartTagToken token ) {
216         this.basicTemplateLoader.compileStartTag( token );
217     }
218 
219     /**
220      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.XmlDeclToken)
221      */
222     public void visit( final XmlDeclToken token ) {
223         // Ignore the XML declaration, we don't really care about it
224     }
225 
226 }