1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
82 this.wellFormed.nextToken = token;
83
84 this.wellFormed.tokenVisitor = this.nextTokenVisitor;
85
86
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
113 this.wellFormed.nextToken = token;
114
115
116 this.wellFormed.tokenVisitor = new ContentTokenVisitor( this.wellFormed, this.nextTokenVisitor, token );
117 }
118
119 }