View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
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.expr;
22  
23  import org.millscript.commons.util.IList;
24  import org.millscript.millscript.action.Action;
25  import org.millscript.millscript.action.XMLAction;
26  import org.millscript.millscript.vm.CompilerState;
27  
28  /**
29   * This class implements an XML expression. An XML expression always returns one
30   * result, which is the XML fragment it represents. An XML expression has a set
31   * of attribute name and associated value expressions, plus a body expressions.
32   *
33   * @see org.millscript.millscript.syntax.XMLElementSyntax
34   * @see XMLAction
35   */
36  public final class XMLExpr extends Expr< XMLAction > implements OneResult {
37  
38      /**
39       * This class implements an XML attribute. An XML attribute is a name-value
40       * pair, so this class contains a name and a value expression.
41       */
42      public static final class XMLAttr {
43  
44          /**
45           * The attribute name expression.
46           */
47          final Expr< ? > name;
48  
49          /**
50           * The attribute value expression.
51           */
52          final Expr< ? > value;
53  
54          /**
55           * Creates a new XML attribute with the specified name and value
56           * expressions.
57           *
58           * @param   a   the attribute name expression.
59           * @param   b   the attribute value expression.
60           */
61          public XMLAttr( final Expr< ? > a, final Expr< ? > b ) {
62              name = CheckExpr.make( a );
63              value = CheckExpr.make( b );
64          }
65      }
66  
67      /**
68       * The XML tag name expression.
69       */
70      private final ConstantExpr tagName;
71  
72      /**
73       * The XML attribute expressions. This should be a list of {@link XMLAttr}s.
74       */
75      private final IList< XMLAttr > attrs;
76  
77      /**
78       * The XML elements body expression.
79       */
80      private final Expr< ? > body;
81  
82      /**
83       * Creates a new XML element expression with the specified tag name,
84       * attributes and body.
85       *
86       * @param   a   the XML tag name expression
87       * @param   b   a list of XMLAttrs for the attributes
88       * @param   c   the XML tag body expression
89       */
90      public XMLExpr( final ConstantExpr a, final IList< XMLAttr > b, final Expr< ? > c ) {
91          tagName = a;
92          attrs = b;
93          body = c;
94      }
95  
96      /**
97       * @see org.millscript.millscript.expr.Expr#compileIt()
98       */
99      @Override
100     public XMLAction compileIt() {
101         return (
102             new XMLAction(
103                 tagName.compile(),
104                 compileNames(),
105                 compileValues(),
106                 body.compile()
107             )
108         );
109     }
110 
111     /**
112      * Returns an array of actions for this XML elements attribute names.
113      *
114      * @return  an array of actions for this elements attribute names.
115      */
116     Action[] compileNames() {
117         Action[] names = new Action[ attrs.size() ];
118         for ( int i = 0; i < names.length; i++ ) {
119             names[ i ] = this.attrs.get0( i ).name.compile();
120         }
121         return names;
122     }
123 
124     /**
125      * Returns an array of actions for this XML elements attribute values.
126      *
127      * @return  an array of actions for this elements attribute values.
128      */
129     Action[] compileValues() {
130         Action[] vals = new Action[ attrs.size() ];
131         for ( int i = 0; i < vals.length; i++ ) {
132             vals[ i ] = this.attrs.get0( i ).value.compile();
133         }
134         return vals;
135     }
136 
137     /**
138      * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
139      */
140     @Override
141     public void resolve( final CompilerState state ) {
142         this.tagName.resolve( state );
143         for ( int i = 1; i <= this.attrs.size(); i++ ) {
144             final XMLAttr attr = this.attrs.get( i );
145             attr.name.resolve( state );
146             attr.value.resolve( state );
147         }
148         this.body.resolve( state );
149     }
150 
151     /**
152      * @see org.millscript.millscript.expr.Expr#showComponents(int)
153      */
154     @Override
155     void showComponents( final int n ) {
156         this.tagName.show( n );
157         for ( int i = 1; i <= this.attrs.size(); i++ ) {
158             final XMLAttr attr = this.attrs.get( i );
159             attr.name.show( n );
160             attr.value.show( n );
161         }
162         this.body.show( n );
163     }
164 
165 }