001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.soap.util;
018
019 import java.util.Collection;
020 import java.util.Iterator;
021
022 import javax.xml.namespace.QName;
023
024 import org.w3c.dom.Attr;
025 import org.w3c.dom.Element;
026
027 /**
028 * Utilities for converting QNames into different representations
029 *
030 * @author Alex Boisvert
031 * @version $Revision: 1.5 $
032 * @since 3.0
033 */
034 public class QNameUtil {
035
036 /**
037 * Convert QName to the Clark notation, e.g., {namespace}localName
038 */
039 public static String toString(QName qname) {
040 if (qname.getNamespaceURI() == null) {
041 return "{}" + qname.getLocalPart();
042 } else {
043 return "{" + qname.getNamespaceURI() + "}" + qname.getLocalPart();
044 }
045 }
046
047 /**
048 * Convert QName to the Clark notation, e.g., {namespace}localName
049 */
050 public static String toString(Element element) {
051 if (element.getNamespaceURI() == null) {
052 return "{}" + element.getLocalName();
053 } else {
054 return "{" + element.getNamespaceURI() + "}" + element.getLocalName();
055 }
056 }
057
058 /**
059 * Convert QName to the Clark notation, e.g., {namespace}localName
060 */
061 public static String toString(Attr attr) {
062 if (attr.getNamespaceURI() == null) {
063 return "{}" + attr.getLocalName();
064 } else {
065 return "{" + attr.getNamespaceURI() + "}" + attr.getLocalName();
066 }
067 }
068
069 public static String toString(Collection collection) {
070 StringBuffer buf = new StringBuffer();
071 Iterator iter = collection.iterator();
072 while (iter.hasNext()) {
073 QName qname = (QName) iter.next();
074 buf.append(toString(qname));
075 if (iter.hasNext()) {
076 buf.append(", ");
077 }
078 }
079 return buf.toString();
080 }
081
082 /**
083 * Convert a String back into a QName following the Clark notation
084 */
085 public static QName parse(String name) {
086 int pos = name.indexOf('}');
087 if (name.startsWith("{") && pos > 0) {
088 String ns = name.substring(1, pos);
089 String lname = name.substring(pos + 1, name.length());
090 return new QName(ns, lname);
091 }
092 return null;
093 }
094
095 }