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;
22  
23  import org.millscript.commons.alert.Alert;
24  import org.millscript.commons.util.IMap;
25  import org.millscript.commons.xml.alerts.XmlNamespaceWellFormednessAlert;
26  
27  /**
28   * This class provides a <code>Name</code> implementation for prefixed, or
29   * namespace aware, names.
30   */
31  public class PrefixedName extends AbstractName {
32  
33      /**
34       * This is the ID from the release 0.3.0 for future compatibility.
35       */
36      private static final long serialVersionUID = -2766284273553889013L;
37  
38      /**
39       * The local part of the prefixed name.
40       */
41      private final String localName;
42  
43      /**
44       * The namespace this name belongs to.
45       */
46      private final String namespace;
47  
48      /**
49       * The prefix part of the prefixed name.
50       */
51      private final String prefix;
52  
53      /**
54       * The full qualified name.
55       */
56      private final String qName;
57  
58      /**
59       * Constructs a new prefixed name, in the specified namespace, with the
60       * specified qualified name and prefix length within the qualified name.
61       *
62       * @param n the namespace this name belongs in
63       * @param q the fully qualified name
64       * @param prefixLength  the length of the prefix part of the qualified name
65       */
66      public PrefixedName( final String n, final String q, final int prefixLength ) {
67          // If the following changes such that name could be null, the equals
68          // and hashCode methods will need updating.
69          if ( n == null ) {
70              throw new NullPointerException( "namespace cannot be null" );
71  //        } else if ( n.length() == 0 ) {
72  //            throw new XmlNamespaceWellFormednessAlert(
73  //                "Namespace cannot be an empty string"
74  //            );
75          }
76          this.namespace = n;
77          // If the following changes such that qName could be null, the equals
78          // and hashCode methods will need updating.
79          this.qName = q;
80          this.prefix = this.qName.substring( 0, prefixLength );
81          this.localName = this.qName.substring( prefixLength + 1 );
82      }
83  
84      /**
85       * Constructs a new prefixed name, in the specified namespace, with the
86       * specified qualified name and prefix length within the qualified name.
87       *
88       * @param n the namespace this name belongs in
89       * @param l the local name
90       * @param p the namespace prefix
91       * @param q the fully qualified name
92       */
93      private PrefixedName( final String n, final String l, final String p, final String q ) {
94          this.localName = l;
95          this.namespace = n;
96          this.prefix = p;
97          this.qName = q;
98      }
99  
100     /**
101      * @see org.millscript.commons.alert.AlertDecorator#decorate(org.millscript.commons.alert.Alert)
102      */
103     public Alert decorate( final Alert alert ) {
104         final StringBuffer buffer = new StringBuffer( this.getQName() );
105         if ( this.namespace != null ) {
106             buffer.append( "(" );
107             buffer.append( this.namespace );
108             buffer.append( ")" );
109         }
110         return alert.culprit( "xml name", buffer );
111     }
112 
113     /**
114      * @see org.millscript.commons.xml.api.Name#getLocalName()
115      */
116     public String getLocalName() {
117         return this.localName;
118     }
119 
120     /**
121      * @see org.millscript.commons.xml.api.Name#getNamespace()
122      */
123     public String getNamespace() {
124         return this.namespace;
125     }
126 
127     /**
128      * @see org.millscript.commons.xml.api.Name#getPrefix()
129      */
130     public String getPrefix() {
131         return this.prefix;
132     }
133 
134     /**
135      * @see org.millscript.commons.xml.api.Name#getQName()
136      */
137     public String getQName() {
138         return this.qName;
139     }
140 
141     /**
142      * @see org.millscript.commons.xml.tokenizer.ProtoName#setNamespace(IMap)
143      */
144     @Override
145     public PrefixedName setNamespace( final IMap< String, String > scope ) {
146         final String ns = scope.get( this.getPrefix() );
147         // When handling a prefixed name, the prefix MUST be declared in the
148         // current scope and it is an error otherwise
149         if ( "".equals( ns ) ) {
150             throw new XmlNamespaceWellFormednessAlert(
151                 "A Namespace prefix must be declared and in scope before it can be used"
152             ).culprit( "prefix", this.getPrefix() ).mishap();
153         }
154         // Return a new prefixed name with the namespace for this names prefix
155         // from the specified scope
156         return new PrefixedName(
157             ns,
158             this.getLocalName(),
159             this.getPrefix(),
160             this.getQName()
161         );
162     }
163 
164     /**
165      * @see java.lang.Object#toString()
166      */
167     @Override
168     public String toString() {
169         return this.prefix + ':' + this.localName + "(" + this.namespace + ")";
170     }
171 
172 }