Java XML QName Parse parseQNameString(String qNameString)

Here you can find the source of parseQNameString(String qNameString)

Description

Parse the given qualified name string into a QName.

License

Apache License

Exception

Parameter Description
IllegalArgumentException when the given string is <code>null</code> or empty.

Return

a corresponding QName instance

Declaration

public static QName parseQNameString(String qNameString) 

Method Source Code


//package com.java2s;
/*//from   w  w w  .  j av a 2 s.co m
 * Copyright 2005-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.xml.namespace.QName;
import org.springframework.util.Assert;

public class Main {
    /** Indicates whether {@link QName} has a prefix. The first release of the class did not have this. */
    private static boolean qNameHasPrefix;

    /**
     * Parse the given qualified name string into a <code>QName</code>. Expects the syntax <code>localPart</code>,
     * <code>{namespace}localPart</code>, or <code>{namespace}prefix:localPart</code>. This format resembles the
     * <code>toString()</code> representation of <code>QName</code> itself, but allows for prefixes to be specified as
     * well.
     *
     * @return a corresponding QName instance
     * @throws IllegalArgumentException when the given string is <code>null</code> or empty.
     */
    public static QName parseQNameString(String qNameString) {
        Assert.hasLength(qNameString, "QName text may not be null or empty");
        if (qNameString.charAt(0) != '{') {
            return new QName(qNameString);
        } else {
            int endOfNamespaceURI = qNameString.indexOf('}');
            if (endOfNamespaceURI == -1) {
                throw new IllegalArgumentException(
                        "Cannot create QName from \"" + qNameString + "\", missing closing \"}\"");
            }
            int prefixSeperator = qNameString.indexOf(':', endOfNamespaceURI + 1);
            String namespaceURI = qNameString.substring(1, endOfNamespaceURI);
            if (prefixSeperator == -1) {
                return new QName(namespaceURI, qNameString.substring(endOfNamespaceURI + 1));
            } else {
                return createQName(namespaceURI, qNameString.substring(prefixSeperator + 1),
                        qNameString.substring(endOfNamespaceURI + 1, prefixSeperator));
            }
        }

    }

    /**
     * Creates a new <code>QName</code> with the given parameters. Sets the prefix if possible, i.e. if the
     * <code>QName(String, String, String)</code> constructor can be found. If this constructor is not available (as is
     * the case on older implementations of JAX-RPC), the prefix is ignored.
     *
     * @param namespaceUri namespace URI of the <code>QName</code>
     * @param localPart    local part of the <code>QName</code>
     * @param prefix       prefix of the <code>QName</code>. May be ignored.
     * @return the created <code>QName</code>
     * @see QName#QName(String,String,String)
     */
    public static QName createQName(String namespaceUri, String localPart, String prefix) {
        if (qNameHasPrefix) {
            return new QName(namespaceUri, localPart, prefix);
        } else {
            return new QName(namespaceUri, localPart);
        }
    }
}

Related

  1. parseQName(String qname, Element namespaceContext)
  2. parseQName(URI u)
  3. parseQNameString(String qns)