1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.namespace;
18
19 import javax.xml.namespace.QName;
20
21 import org.springframework.util.Assert;
22 import org.springframework.util.StringUtils;
23
24 import org.w3c.dom.Node;
25
26
27
28
29
30
31
32
33 public abstract class QNameUtils {
34
35
36 private static boolean qNameHasPrefix;
37
38 static {
39 try {
40 QName.class.getDeclaredConstructor(new Class[]{String.class, String.class, String.class});
41 qNameHasPrefix = true;
42 }
43 catch (NoSuchMethodException e) {
44 qNameHasPrefix = false;
45 }
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59 public static QName createQName(String namespaceUri, String localPart, String prefix) {
60 if (qNameHasPrefix) {
61 return new QName(namespaceUri, localPart, prefix);
62 }
63 else {
64 return new QName(namespaceUri, localPart);
65 }
66 }
67
68
69
70
71
72
73
74
75
76
77 public static String getPrefix(QName qName) {
78 return qNameHasPrefix ? qName.getPrefix() : "";
79 }
80
81
82
83
84
85
86
87 public static boolean validateQName(String text) {
88 if (!StringUtils.hasLength(text)) {
89 return false;
90 }
91 if (text.charAt(0) == '{') {
92 int i = text.indexOf('}');
93
94 if (i == -1 || i == text.length() - 1) {
95 return false;
96 }
97 }
98 return true;
99 }
100
101
102
103
104
105
106
107 public static QName getQNameForNode(Node node) {
108 if (node.getNamespaceURI() != null && node.getPrefix() != null && node.getLocalName() != null) {
109 return createQName(node.getNamespaceURI(), node.getLocalName(), node.getPrefix());
110 }
111 else if (node.getNamespaceURI() != null && node.getLocalName() != null) {
112 return new QName(node.getNamespaceURI(), node.getLocalName());
113 }
114 else if (node.getLocalName() != null) {
115 return new QName(node.getLocalName());
116 }
117 else {
118
119 return new QName(node.getNodeName());
120 }
121 }
122
123
124
125
126
127
128
129
130 public static String toQualifiedName(QName qName) {
131 String prefix = getPrefix(qName);
132 if (!StringUtils.hasLength(prefix)) {
133 return qName.getLocalPart();
134 }
135 else {
136 return prefix + ":" + qName.getLocalPart();
137 }
138 }
139
140
141
142
143
144
145
146
147
148 public static QName toQName(String namespaceUri, String qualifiedName) {
149 int idx = qualifiedName.indexOf(':');
150 if (idx == -1) {
151 return new QName(namespaceUri, qualifiedName);
152 }
153 else {
154 return createQName(namespaceUri, qualifiedName.substring(idx + 1), qualifiedName.substring(0, idx));
155 }
156 }
157
158
159
160
161
162
163
164
165
166
167 public static QName parseQNameString(String qNameString) {
168 Assert.hasLength(qNameString, "QName text may not be null or empty");
169 if (qNameString.charAt(0) != '{') {
170 return new QName(qNameString);
171 }
172 else {
173 int endOfNamespaceURI = qNameString.indexOf('}');
174 if (endOfNamespaceURI == -1) {
175 throw new IllegalArgumentException(
176 "Cannot create QName from \"" + qNameString + "\", missing closing \"}\"");
177 }
178 int prefixSeperator = qNameString.indexOf(':', endOfNamespaceURI + 1);
179 String namespaceURI = qNameString.substring(1, endOfNamespaceURI);
180 if (prefixSeperator == -1) {
181 return new QName(namespaceURI, qNameString.substring(endOfNamespaceURI + 1));
182 }
183 else {
184 return createQName(namespaceURI, qNameString.substring(prefixSeperator + 1),
185 qNameString.substring(endOfNamespaceURI + 1, prefixSeperator));
186 }
187 }
188
189 }
190
191 }