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;
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
68
69 if ( n == null ) {
70 throw new NullPointerException( "namespace cannot be null" );
71
72
73
74
75 }
76 this.namespace = n;
77
78
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
148
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
155
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 }