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.token;
22  
23  import org.millscript.commons.alert.Alert;
24  import org.millscript.commons.alert.AlertDecorator;
25  import org.millscript.commons.util.IMap;
26  import org.millscript.commons.util.MapIterator;
27  import org.millscript.commons.xml.api.Attributes;
28  import org.millscript.commons.xml.api.Name;
29  import org.millscript.commons.xml.api.token.StartTagToken;
30  import org.millscript.commons.xml.api.token.TokenVisitor;
31  
32  /**
33   * This class provides a default implementation of the start tag token.
34   */
35  public class StartTagTokenImpl implements AlertDecorator, StartTagToken {
36  
37      /**
38       * The element <code>Attributes</code>.
39       */
40      private final Attributes attributes;
41  
42      /**
43       * This namespaces that are in scope with this start tag.
44       */
45      private final IMap< String, String > namespaceScope;
46  
47      /**
48       * This namespaces that were in scope just before this start tag.
49       */
50      private final IMap< String, String > previousNamespaceScope;
51  
52      /**
53       * The element <code>Name</code>.
54       */
55      private final Name tagName;
56  
57      /**
58       * Contructs a new start tag token with the specified <code>Name</code> and
59       * <code>Attributes</code>.
60       *
61       * @param previousNamespace the mapping of namespace prefix to namespace
62       * IRI that were in scope just before this element
63       * @param namespace the mapping of namespace prefix to namespace IRI for
64       * the scope of this element
65       * @param name  the element <code>Name</code>
66       * @param attrs the element <code>Attributes</code>
67       */
68      public StartTagTokenImpl( final IMap< String, String > previousNamespace, final IMap< String, String > namespace, final Name name, final Attributes attrs ) {
69          this.attributes = attrs;
70          this.namespaceScope = namespace;
71          this.previousNamespaceScope = previousNamespace;
72          this.tagName = name;
73      }
74  
75      /**
76       * @see org.millscript.commons.alert.AlertDecorator#decorate(org.millscript.commons.alert.Alert)
77       */
78      public Alert decorate( final Alert alert ) {
79          final StringBuffer buffer = new StringBuffer( "<" );
80          buffer.append( this.tagName );
81          if ( this.attributes.size() != 0 ) {
82              MapIterator< Name, String > it = this.attributes.iterator( true );
83              buffer.append( ' ' );
84              buffer.append( it.nextKey() );
85              buffer.append( "=\"" );
86              buffer.append( it.currentValue() );
87              buffer.append( '"' );
88          }
89          buffer.append( '>' );
90          return alert.culprit(
91              "start tag",
92              buffer.toString()
93          );
94      }
95  
96      /**
97       * @see org.millscript.commons.xml.api.token.StartTagToken#getAttributes()
98       */
99      public Attributes getAttributes() {
100         return this.attributes;
101     }
102 
103     /**
104      * @see org.millscript.commons.xml.api.token.StartTagToken#getName()
105      */
106     public Name getName() {
107         return this.tagName;
108     }
109 
110     /**
111      * @see org.millscript.commons.xml.api.token.StartTagToken#getNamespaceScope()
112      */
113     public IMap< String, String > getNamespaceScope() {
114         return this.namespaceScope;
115     }
116 
117     /**
118      * @see org.millscript.commons.xml.api.token.StartTagToken#getPreviousNamespaceScope()
119      */
120     public IMap< String, String > getPreviousNamespaceScope() {
121         return this.previousNamespaceScope;
122     }
123 
124     /**
125      * @see org.millscript.commons.xml.api.token.Token#visit(org.millscript.commons.xml.api.token.TokenVisitor)
126      */
127     public void visit( final TokenVisitor tokenVisitor ) {
128         tokenVisitor.visit( this );
129     }
130 
131 }