View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-XML: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript-XML.
6   //
7   // MillScript-XML 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-XML 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-XML; 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.commons.xml.tokenizer.wellformed;
22  
23  import org.millscript.commons.xml.alerts.XmlWellFormednessAlert;
24  import org.millscript.commons.xml.api.token.CharDataToken;
25  import org.millscript.commons.xml.api.token.EmptyElementToken;
26  import org.millscript.commons.xml.api.token.EndTagToken;
27  import org.millscript.commons.xml.api.token.StartTagToken;
28  
29  /**
30   * This class provides a token visitor the for third <code>Misc</code> section
31   * part of an XML document. This is the sixth token visitor in a chain that
32   * provides the well formedness checks required of an XML processor.
33   */
34  public class Misc3TokenVisitor extends Misc2TokenVisitor {
35  
36      /**
37       * Constructs a new <code>Misc</code> section token visitor, with the
38       * specified token visitor.
39       *
40       * @param xt    the well-formed XML tokenizer our tokens come from
41       */
42      public Misc3TokenVisitor( final WellFormedXmlTokenizer xt ) {
43          // We're at the end of the document, so there isn't a next
44          // token visitor. This shouldn't be a problem
45          // TODO - what should it be?
46          super( xt, null );
47      }
48  
49      /**
50       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.CharDataToken)
51       */
52      @Override
53      public void visit( final CharDataToken token ) {
54          // this token is only valid in this section if it is whitespace
55          if ( token.isIgnorableWhitespace() ) {
56              // this token is valid in this section
57              this.wellFormed.nextToken = token;
58          } else {
59              // not valid in this section, check it in the next one
60              throw new XmlWellFormednessAlert(
61                  "Character data must not appear after the document element"
62              ).decorate( token ).mishap();
63          }
64      }
65  
66      /**
67       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.EmptyElementToken)
68       */
69      @Override
70      public void visit( final EmptyElementToken token ) {
71          throw new XmlWellFormednessAlert(
72              "An XML document can only have a single document element"
73          ).decorate( token ).mishap();
74      }
75  
76      /**
77       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.EndTagToken)
78       */
79      @Override
80      public void visit( final EndTagToken token ) {
81          throw new XmlWellFormednessAlert(
82              "An XML document can only have a single document element"
83          ).decorate( token ).mishap();
84      }
85  
86      /**
87       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.StartTagToken)
88       */
89      @Override
90      public void visit( final StartTagToken token ) {
91          throw new XmlWellFormednessAlert(
92              "An XML document can only have a single document element"
93          ).decorate( token ).mishap();
94      }
95  
96  }