|
Apache CXF example source code file (NamespaceHelper.java)
The Apache CXF NamespaceHelper.java source code/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.cxf.aegis.util;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.StringTokenizer;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import org.w3c.dom.Element;
import org.apache.cxf.aegis.DatabindingException;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.helpers.JavaUtils;
import org.apache.cxf.staxutils.StaxUtils;
/**
* Namespace utilities.
*/
public final class NamespaceHelper {
private NamespaceHelper() {
//utility class
}
/**
* Create a unique namespace uri/prefix combination.
*
* @param nsUri
* @return The namespace with the specified URI. If one doesn't exist, one
* is created.
*/
public static String getUniquePrefix(Element element, String namespaceURI) {
String prefix = getPrefix(element, namespaceURI);
// it is OK to have both namespace URI and prefix be empty.
if (prefix == null) {
if ("".equals(namespaceURI)) {
return "";
}
prefix = DOMUtils.createNamespace(element, namespaceURI);
}
return prefix;
}
public static String getPrefix(Element element, String namespaceURI) {
return DOMUtils.getPrefixRecursive(element, namespaceURI);
}
public static void getPrefixes(Element element, String namespaceURI, List<String> prefixes) {
DOMUtils.getPrefixesRecursive(element, namespaceURI, prefixes);
}
/**
* Create a unique namespace uri/prefix combination.
*
* @param nsUri
* @return The namespace with the specified URI. If one doesn't exist, one
* is created.
* @throws XMLStreamException
*/
public static String getUniquePrefix(XMLStreamWriter writer, String namespaceURI, boolean declare)
throws XMLStreamException {
return getUniquePrefix(writer, namespaceURI, null, declare);
}
/**
* Make a unique prefix.
* @param writer target writer.
* @param namespaceURI namespace
* @param preferred if there's a proposed prefix (e.g. xsi), here it is.
* @param declare whether to declare to the stream.
* @return the prefix.
* @throws XMLStreamException
*/
public static String getUniquePrefix(XMLStreamWriter writer,
String namespaceURI,
String preferred,
boolean declare)
throws XMLStreamException {
if (preferred != null) {
String existing = writer.getNamespaceContext().getNamespaceURI(preferred);
if (namespaceURI.equals(existing)) {
return preferred;
}
}
String prefix = preferred;
if (prefix == null) {
prefix = writer.getNamespaceContext().getPrefix(namespaceURI);
if (prefix != null) {
declare = false;
}
}
if (prefix == null) {
prefix = StaxUtils.getUniquePrefix(writer);
}
if (declare) {
writer.setPrefix(prefix, namespaceURI);
writer.writeNamespace(prefix, namespaceURI);
}
return prefix;
}
/**
* Generates the name of a XML namespace from a given class name and
* protocol. The returned namespace will take the form
* <code>protocol://domain, where
Other Apache CXF examples (source code examples)Here is a short list of links related to this Apache CXF NamespaceHelper.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.