1 /*
2 * Copyright (C) 2002 Carsten Krebs (Team-Konzept GmbH & Co.KG)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package com.teamkonzept.dom4jb.dom;
19
20 import org.apache.xerces.util.XMLChar;
21
22 public final class NodeNameNS extends NodeName {
23
24 private final String namespaceURI;
25 private final String prefix;
26 private String qualifiedName;
27
28 /*** Creates new NodeName */
29 private NodeNameNS(
30 final String namespaceURI,
31 final String prefix,
32 final String localName) {
33 super(localName);
34 this.namespaceURI = namespaceURI;
35 this.prefix = prefix;
36 }
37
38 public String getQualifiedName() {
39 if (qualifiedName != null) {
40 return qualifiedName;
41 }
42
43 if (prefix == null) {
44 qualifiedName = localName;
45 } else {
46 qualifiedName =
47 (new StringBuffer(prefix))
48 .append(':')
49 .append(localName)
50 .toString();
51 }
52 return qualifiedName;
53 }
54
55 public String getLocalName() {
56 return localName;
57 }
58
59 public String getNamespaceURI() {
60 return namespaceURI;
61 }
62
63 public String getPrefix() {
64 return prefix;
65 }
66
67 public NodeName setPrefix(String prefix) {
68 prefix = prefix.trim();
69 if (prefix.length() <= 0) {
70 prefix = null;
71 }
72 return getInstance(prefix, namespaceURI, localName);
73 }
74
75 private static void assertNamespace(
76 final String prefix,
77 final String namespaceURI,
78 final String localName) {
79 if (prefix != null) {
80 if (prefix.equals(XML_PREFIX)
81 && namespaceURI != XML_NAMESPACE_URI) {
82 throw new DOMException(
83 DOMException.NAMESPACE_ERR,
84 "namespaceURI is different from " + XML_NAMESPACE_URI);
85 }
86 if (prefix.equals(NS_PREFIX) && namespaceURI != NS_NAMESPACE_URI) {
87 throw new DOMException(
88 DOMException.NAMESPACE_ERR,
89 "namespaceURI is different from " + NS_NAMESPACE_URI);
90 }
91
92 return;
93 }
94
95 if (localName == NS_PREFIX && namespaceURI != NS_NAMESPACE_URI) {
96 throw new DOMException(
97 DOMException.NAMESPACE_ERR,
98 "namespaceURI is different from " + NS_NAMESPACE_URI);
99 }
100 }
101
102 public static NodeName getInstance(
103 String namespaceURI,
104 final String qualifiedName) {
105 if (qualifiedName == null) {
106 throw new DOMException(
107 DOMException.NAMESPACE_ERR,
108 "qualified name missing!");
109 }
110 if (!qualifiedName.equals("*")
111 && !XMLChar.isValidName(qualifiedName)) {
112 throw new DOMException(
113 DOMException.INVALID_CHARACTER_ERR,
114 "invalid qualifiedName!");
115 }
116
117 final int ndx = qualifiedName.indexOf(':');
118 final String prefix, localName;
119 if (ndx > 0) {
120 if (namespaceURI == null) {
121 throw new DOMException(
122 DOMException.NAMESPACE_ERR,
123 "namespaceURI is missing!");
124 }
125 prefix = qualifiedName.substring(0, ndx).trim().intern();
126 localName = qualifiedName.substring(ndx + 1).trim().intern();
127 } else {
128 prefix = null;
129 localName = qualifiedName.intern();
130 }
131 if (namespaceURI != null) {
132 if (!namespaceURI.equals("*")
133 && !XMLChar.isValidNCName(namespaceURI)) {
134 throw new DOMException(
135 DOMException.NAMESPACE_ERR,
136 "invalid namespaceURI!");
137 }
138 namespaceURI = namespaceURI.trim().intern();
139 } else {
140 namespaceURI = null;
141 }
142
143 return getInstance(prefix, namespaceURI, localName);
144 }
145
146 private static NodeName getInstance(
147 final String namespaceURI,
148 final String prefix,
149 final String localName) {
150 assertNamespace(prefix, namespaceURI, localName);
151
152 final String key = getKey(prefix, namespaceURI, localName);
153
154 synchronized (names) {
155 final Object name = names.get(key);
156 if (name == null) {
157 final NodeName newName =
158 new NodeNameNS(prefix, namespaceURI, localName);
159 names.put(key, newName);
160 return newName;
161 }
162 return (NodeName) name;
163 }
164 }
165
166 /***
167 * @see java.lang.Object#toString()
168 */
169 public String toString() {
170 final StringBuffer buf = new StringBuffer();
171 if (namespaceURI != null) {
172 buf.append(namespaceURI);
173 }
174 buf.append(':');
175 if (prefix != null) {
176 buf.append(prefix);
177 }
178 buf.append(':');
179 buf.append(localName);
180 return buf.toString();
181 }
182
183 }
This page was automatically generated by Maven