001: /*
002: * Copyright 2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.namespace;
018:
019: import javax.xml.namespace.QName;
020:
021: import org.springframework.util.Assert;
022: import org.springframework.util.StringUtils;
023: import org.w3c.dom.Node;
024:
025: /**
026: * Helper class for using {@link QName}.
027: *
028: * @author Arjen Poutsma
029: * @see javax.xml.namespace.QName
030: * @since 1.0.0
031: */
032: public abstract class QNameUtils {
033:
034: /** Indicates whether {@link QName} has a prefix. The first release of the class did not have this. */
035: private static boolean qNameHasPrefix;
036:
037: static {
038: try {
039: QName.class.getDeclaredConstructor(new Class[] {
040: String.class, String.class, String.class });
041: qNameHasPrefix = true;
042: } catch (NoSuchMethodException e) {
043: qNameHasPrefix = false;
044: }
045: }
046:
047: /**
048: * Creates a new <code>QName</code> with the given parameters. Sets the prefix if possible, i.e. if the
049: * <code>QName(String, String, String)</code> constructor can be found. If this constructor is not available (as is
050: * the case on older implementations of JAX-RPC), the prefix is ignored.
051: *
052: * @param namespaceUri namespace URI of the <code>QName</code>
053: * @param localPart local part of the <code>QName</code>
054: * @param prefix prefix of the <code>QName</code>. May be ignored.
055: * @return the created <code>QName</code>
056: * @see QName#QName(String,String,String)
057: */
058: public static QName createQName(String namespaceUri,
059: String localPart, String prefix) {
060: if (qNameHasPrefix) {
061: return new QName(namespaceUri, localPart, prefix);
062: } else {
063: return new QName(namespaceUri, localPart);
064: }
065: }
066:
067: /**
068: * Returns the prefix of the given <code>QName</code>. Returns the prefix if available, i.e. if the
069: * <code>QName.getPrefix()</code> method can be found. If this method is not available (as is the case on older
070: * implementations of JAX-RPC), an empty string is returned.
071: *
072: * @param qName the <code>QName</code> to return the prefix from
073: * @return the prefix, if available, or an empty string
074: * @see javax.xml.namespace.QName#getPrefix()
075: */
076: public static String getPrefix(QName qName) {
077: return qNameHasPrefix ? qName.getPrefix() : "";
078: }
079:
080: /**
081: * Validates the given String as a QName
082: *
083: * @param text the qualified name
084: * @return <code>true</code> if valid, <code>false</code> otherwise
085: */
086: public static boolean validateQName(String text) {
087: if (!StringUtils.hasLength(text)) {
088: return false;
089: }
090: if (text.charAt(0) == '{') {
091: int i = text.indexOf('}');
092:
093: if (i == -1 || i == text.length() - 1) {
094: return false;
095: }
096: }
097: return true;
098: }
099:
100: /**
101: * Returns the qualified name of the given DOM Node.
102: *
103: * @param node the node
104: * @return the qualified name of the node
105: */
106: public static QName getQNameForNode(Node node) {
107: if (node.getNamespaceURI() != null && node.getPrefix() != null
108: && node.getLocalName() != null) {
109: return createQName(node.getNamespaceURI(), node
110: .getLocalName(), node.getPrefix());
111: } else if (node.getNamespaceURI() != null
112: && node.getLocalName() != null) {
113: return new QName(node.getNamespaceURI(), node
114: .getLocalName());
115: } else if (node.getLocalName() != null) {
116: return new QName(node.getLocalName());
117: } else {
118: // as a last resort, use the node name
119: return new QName(node.getNodeName());
120: }
121: }
122:
123: /**
124: * Convert a <code>QName</code> to a qualified name, as used by DOM and SAX. The returned string has a format of
125: * <code>prefix:localName</code> if the prefix is set, or just <code>localName</code> if not.
126: *
127: * @param qName the <code>QName</code>
128: * @return the qualified name
129: */
130: public static String toQualifiedName(QName qName) {
131: String prefix = getPrefix(qName);
132: if (!StringUtils.hasLength(prefix)) {
133: return qName.getLocalPart();
134: } else {
135: return prefix + ":" + qName.getLocalPart();
136: }
137: }
138:
139: /**
140: * Convert a namespace URI and DOM or SAX qualified name to a <code>QName</code>. The qualified name can have the
141: * form <code>prefix:localname</code> or <code>localName</code>.
142: *
143: * @param namespaceUri the namespace URI
144: * @param qualifiedName the qualified name
145: * @return a QName
146: */
147: public static QName toQName(String namespaceUri,
148: String qualifiedName) {
149: int idx = qualifiedName.indexOf(':');
150: if (idx == -1) {
151: return new QName(namespaceUri, qualifiedName);
152: } else {
153: return createQName(namespaceUri, qualifiedName
154: .substring(idx + 1), qualifiedName
155: .substring(0, idx));
156: }
157: }
158:
159: /**
160: * Parse the given qualified name string into a <code>QName</code>. Expects the syntax <code>localPart</code>,
161: * <code>{namespace}localPart</code>, or <code>{namespace}prefix:localPart</code>. This format resembles the
162: * <code>toString()</code> representation of <code>QName</code> itself, but allows for prefixes to be specified as
163: * well.
164: *
165: * @return a corresponding QName instance
166: * @throws IllegalArgumentException when the given string is <code>null</code> or empty.
167: */
168: public static QName parseQNameString(String qNameString) {
169: Assert.hasLength(qNameString,
170: "QName text may not be null or empty");
171: if (qNameString.charAt(0) != '{') {
172: return new QName(qNameString);
173: } else {
174: int endOfNamespaceURI = qNameString.indexOf('}');
175: if (endOfNamespaceURI == -1) {
176: throw new IllegalArgumentException(
177: "Cannot create QName from \"" + qNameString
178: + "\", missing closing \"}\"");
179: }
180: int prefixSeperator = qNameString.indexOf(':',
181: endOfNamespaceURI + 1);
182: String namespaceURI = qNameString.substring(1,
183: endOfNamespaceURI);
184: if (prefixSeperator == -1) {
185: return new QName(namespaceURI, qNameString
186: .substring(endOfNamespaceURI + 1));
187: } else {
188: return createQName(namespaceURI, qNameString
189: .substring(prefixSeperator + 1), qNameString
190: .substring(endOfNamespaceURI + 1,
191: prefixSeperator));
192: }
193: }
194:
195: }
196:
197: }
|