View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.functions;
23  
24  import org.millscript.commons.xml.api.tokenizer.XmlTokenizer;
25  import org.millscript.commons.xml.tokenizer.XmlTokenizerFactory;
26  import org.millscript.millscript.vm.Machine;
27  
28  import java.io.StringReader;
29  
30  /**
31   * This class implements the MillScript <code>stringToFragment</code> function.
32   */
33  public class StringToXMLFunction extends Function {
34  
35      /**
36       * The XML tokenizer factory we can use to get an XML tokenizer for the
37       * XML.
38       */
39      private final XmlTokenizerFactory factory = new XmlTokenizerFactory();
40  
41      /**
42       * The current token visitor we can use to construct our internal
43       * representations.
44       */
45      StringToXMLTokenVisitor tokenVisitor;
46  
47      /**
48       * @see org.millscript.millscript.functions.Function#apply(org.millscript.millscript.vm.Machine, int)
49       */
50      @Override
51      public void apply( final Machine mc, final int nargs ) {
52          checkNargs( mc, 1, nargs );
53          // We need a well-formed XmlTokenizer
54          final XmlTokenizer xmlTokenizer = this.factory.getTokenizer(
55              new StringReader( mc.popString() )
56          );
57          // Make the special string to XML token visitor
58          this.tokenVisitor = new StringToXMLTokenVisitor( this, null, null, mc );
59          // Loop through all the tokens in the XML and return the related values
60          while ( xmlTokenizer.hasNextToken() ) {
61              xmlTokenizer.nextToken().visit( this.tokenVisitor );
62          }
63          // Report any left over errors, e.g. unclosed tags
64          this.tokenVisitor.reportAnyErrors();
65      }
66  
67  }