View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Helper class for using [email protected] QName}.
28   *
29   * @author Arjen Poutsma
30   * @see javax.xml.namespace.QName
31   * @since 1.0.0
32   */
33  public abstract class QNameUtils {
34  
35      /** Indicates whether [email protected] QName} has a prefix. The first release of the class did not have this. */
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       * Creates a new <code>QName</code> with the given parameters. Sets the prefix if possible, i.e. if the
50       * <code>QName(String, String, String)</code> constructor can be found. If this constructor is not available (as is
51       * the case on older implementations of JAX-RPC), the prefix is ignored.
52       *
53       * @param namespaceUri namespace URI of the <code>QName</code>
54       * @param localPart    local part of the <code>QName</code>
55       * @param prefix       prefix of the <code>QName</code>. May be ignored.
56       * @return the created <code>QName</code>
57       * @see QName#QName(String,String,String)
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       * Returns the prefix of the given <code>QName</code>. Returns the prefix if available, i.e. if the
70       * <code>QName.getPrefix()</code> method can be found. If this method is not available (as is the case on older
71       * implementations of JAX-RPC), an empty string is returned.
72       *
73       * @param qName the <code>QName</code> to return the prefix from
74       * @return the prefix, if available, or an empty string
75       * @see javax.xml.namespace.QName#getPrefix()
76       */
77      public static String getPrefix(QName qName) {
78          return qNameHasPrefix ? qName.getPrefix() : "";
79      }
80  
81      /**
82       * Validates the given String as a QName
83       *
84       * @param text the qualified name
85       * @return <code>true</code> if valid, <code>false</code> otherwise
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      * Returns the qualified name of the given DOM Node.
103      *
104      * @param node the node
105      * @return the qualified name of the node
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             // 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         }
135         else {
136             return prefix + ":" + qName.getLocalPart();
137         }
138     }
139 
140     /**
141      * Convert a namespace URI and DOM or SAX qualified name to a <code>QName</code>. The qualified name can have the
142      * form <code>prefix:localname</code> or <code>localName</code>.
143      *
144      * @param namespaceUri  the namespace URI
145      * @param qualifiedName the qualified name
146      * @return a QName
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      * Parse the given qualified name string into a <code>QName</code>. Expects the syntax <code>localPart</code>,
160      * <code>{namespace}localPart</code>, or <code>{namespace}prefix:localPart</code>. This format resembles the
161      * <code>toString()</code> representation of <code>QName</code> itself, but allows for prefixes to be specified as
162      * well.
163      *
164      * @return a corresponding QName instance
165      * @throws IllegalArgumentException when the given string is <code>null</code> or empty.
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 }