01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.ws.soap.axiom.support;
18:
19: import java.util.Locale;
20: import javax.xml.namespace.QName;
21:
22: import org.apache.axiom.om.OMElement;
23: import org.apache.axiom.om.OMException;
24: import org.apache.axiom.om.OMNamespace;
25: import org.springframework.util.StringUtils;
26: import org.springframework.xml.namespace.QNameUtils;
27:
28: /**
29: * Collection of generic utility methods to work with Axiom. Includes conversion from <code>OMNamespace</code>s to
30: * <code>QName</code>s.
31: *
32: * @author Arjen Poutsma
33: * @see org.apache.axiom.om.OMNamespace
34: * @see javax.xml.namespace.QName
35: * @since 1.0.0
36: */
37: public abstract class AxiomUtils {
38:
39: /**
40: * Converts a <code>javax.xml.namespace.QName</code> to a <code>org.apache.axiom.om.OMNamespace</code>. A
41: * <code>OMElement</code> is used to resolve the namespace, or to declare a new one.
42: *
43: * @param qName the <code>QName</code> to convert
44: * @param resolveElement the element used to resolve the Q
45: * @return the converted SAAJ Name
46: * @throws OMException if conversion is unsuccessful
47: * @throws IllegalArgumentException if <code>qName</code> is not fully qualified
48: */
49: public static OMNamespace toNamespace(QName qName,
50: OMElement resolveElement) throws OMException {
51: String prefix = QNameUtils.getPrefix(qName);
52: if (StringUtils.hasLength(qName.getNamespaceURI())
53: && StringUtils.hasLength(prefix)) {
54: return resolveElement.declareNamespace(qName
55: .getNamespaceURI(), prefix);
56: } else if (StringUtils.hasLength(qName.getNamespaceURI())) {
57: // check for existing namespace, and declare if necessary
58: return resolveElement.declareNamespace(qName
59: .getNamespaceURI(), "");
60: } else {
61: throw new IllegalArgumentException("qName [" + qName
62: + "] does not contain a namespace");
63: }
64: }
65:
66: /**
67: * Converts the given locale to a <code>xml:lang</code> string, as used in Axiom Faults.
68: *
69: * @param locale the locale
70: * @return the language string
71: */
72: public static String toLanguage(Locale locale) {
73: return locale.toString().replace('_', '-');
74: }
75:
76: /**
77: * Converts the given locale to a <code>xml:lang</code> string, as used in Axiom Faults.
78: *
79: * @param language the language string
80: * @return the locale
81: */
82: public static Locale toLocale(String language) {
83: language = language.replace('-', '_');
84: return StringUtils.parseLocaleString(language);
85: }
86:
87: }
|