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.alert.alerts.Fault;
24  import org.millscript.commons.xml.alerts.XmlWellFormednessAlert;
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.EmptyElementToken;
29  import org.millscript.commons.xml.api.token.EndTagToken;
30  import org.millscript.commons.xml.api.token.PIToken;
31  import org.millscript.commons.xml.api.token.StartTagToken;
32  
33  /**
34   * This class provides a token visitor the for <code>element</code> part of an
35   * XML document. This is the fifth token visitor in a chain that provides the
36   * well formedness checks required of an XML processor.
37   */
38  public class ElementTokenVisitor extends AbsTokenVisitor {
39  
40      /**
41       * Constructs a new <code>element</code> section token visitor, with the
42       * specified token visitor.
43       *
44       * @param xt    the well-formed XML tokenizer our tokens come from
45       */
46      public ElementTokenVisitor( final WellFormedXmlTokenizer xt ) {
47          super( xt, new Misc3TokenVisitor( xt ) );
48      }
49  
50      /**
51       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.CharDataToken)
52       */
53      public void visit( final CharDataToken token ) {
54          throw new XmlWellFormednessAlert(
55              "Character data must not appear before the document element"
56          ).decorate( token ).mishap();
57      }
58  
59      /**
60       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.CommentToken)
61       */
62      public void visit( final CommentToken token ) {
63          throw new Fault(
64              "This comment should have been handled in the Misc2 section"
65          ).decorate( token ).mishap();
66      }
67  
68      /**
69       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.DTDToken)
70       */
71      public void visit( final DTDToken token ) {
72          throw new XmlWellFormednessAlert(
73              "Only one document type declaration can be used in a single XML document"
74          ).decorate( token ).mishap();
75      }
76  
77      /**
78       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.EmptyElementToken)
79       */
80      public void visit( final EmptyElementToken token ) {
81          // this token is valid in this section
82          this.wellFormed.nextToken = token;
83          // but next time round we are in the next section
84          this.wellFormed.tokenVisitor = this.nextTokenVisitor;
85          // reset the namespace scope to what it was just before this element
86          // was read
87          this.wellFormed.setNamespaces( token.getPreviousNamespaceScope() );
88      }
89  
90      /**
91       * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.EndTagToken)
92       */
93      public void visit( final EndTagToken token ) {
94          throw new XmlWellFormednessAlert(
95              "The start tag for this document element appears to be missing"
96          ).decorate( token ).mishap();
97      }
98  
99      /**
100      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.PIToken)
101      */
102     public void visit( final PIToken token ) {
103         throw new Fault(
104             "This processing instruction should have been handled in the Misc2 section"
105         ).decorate( token ).mishap();
106     }
107 
108     /**
109      * @see org.millscript.commons.xml.api.token.TokenVisitor#visit(org.millscript.commons.xml.api.token.StartTagToken)
110      */
111     public void visit( final StartTagToken token ) {
112         // this token is valid in this section
113         this.wellFormed.nextToken = token;
114         // but we override the visitor for the next section, but after the
115         // content we must return to the next token visitor
116         this.wellFormed.tokenVisitor = new ContentTokenVisitor( this.wellFormed, this.nextTokenVisitor, token );
117     }
118 
119 }