下面列出了javax.xml.stream.events.Namespace#getNamespaceURI ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Map<String,Namespace> buildByNsURIMap()
{
int len = mNamespaces.size();
if (len == 0) {
return Collections.emptyMap();
}
LinkedHashMap<String,Namespace> m = new LinkedHashMap<String,Namespace>(1 + len + (len>>1));
for (int i = 0; i < len; ++i) {
Namespace ns = mNamespaces.get(i);
String uri = ns.getNamespaceURI();
if (uri == null) { // shouldn't happen but...
uri = "";
}
m.put(uri, ns);
}
return m;
}
/**
* Returns the namespace to write in the XML document. This may imply a prefix change.
* If there is no namespace change, then this method returns the given instance as-is.
* To test if the returned namespace is a new one, callers should check if the size of
* {@link #uniqueNamespaces} changed.
*
* @param namespace the namespace to export.
*/
private Namespace exportIfNew(final Namespace namespace) {
notify(namespace);
String uri = namespace.getNamespaceURI();
if (uri != null && !uri.isEmpty()) {
/*
* Ignore trailing slash when checking if there is a namespace change.
* But if we do not find a namespace change, keep the trailing slash as
* given in the Namespace since that slash may be intentional.
*/
final String trimed = removeTrailingSlash(uri);
final String exported = relocate(trimed);
if (exported != trimed) {
return uniqueNamespaces.computeIfAbsent(exported, (k) -> {
return new TransformedEvent.NS(namespace, Namespaces.getPreferredPrefix(k, namespace.getPrefix()), k);
/*
* The new prefix selected by above line will be saved by out.add(Namespace)
* after this method has been invoked by the NAMESPACE case of add(XMLEvent).
*/
});
}
final Namespace c = uniqueNamespaces.put(uri, namespace); // No namespace change needed. Overwrite wrapper if any.
if (c != null) return c;
}
return namespace;
}
@SuppressWarnings("unchecked")
private String containsBaseNamespace(StartElement startElement) {
String localPart, prefix, qual = null;
Iterator<Namespace> namespaces = startElement.getNamespaces();
while (namespaces != null && namespaces.hasNext()) {
Namespace namespace = namespaces.next();
QName name = namespace.getName();
localPart = name.getLocalPart();
prefix = name.getPrefix();
if (prefix != null && ! prefix.isEmpty())
qual = (localPart != null && ! localPart.isEmpty()) ? prefix + ":" + localPart : prefix;
if (qual != null && qual.equals("xmlns"))
return namespace.getNamespaceURI();
}
return null;
}
@Override
public String doGetNamespaceURI(String prefix)
{
// Note: base class checks for 'known' problems and prefixes:
if (mNsByPrefix == null) {
mNsByPrefix = buildByPrefixMap();
}
Namespace ns = mNsByPrefix.get(prefix);
if (ns == null && mParentCtxt != null) {
return mParentCtxt.getNamespaceURI(prefix);
}
return (ns == null) ? null : ns.getNamespaceURI();
}
@Override
public Iterator<String> doGetPrefixes(String nsURI)
{
// Note: base class checks for 'known' problems and prefixes:
ArrayList<String> l = null;
for (int i = 0, len = mNamespaces.size(); i < len; ++i) {
Namespace ns = mNamespaces.get(i);
String uri = ns.getNamespaceURI();
if (uri == null) {
uri = "";
}
if (uri.equals(nsURI)) {
if (l == null) {
l = new ArrayList<String>();
}
String prefix = ns.getPrefix();
l.add((prefix == null) ? "" : prefix);
}
}
if (mParentCtxt != null) {
@SuppressWarnings("unchecked")
Iterator<String> it = /*(Iterator<String>)*/mParentCtxt.getPrefixes(nsURI);
if (l == null) {
return it;
}
while (it.hasNext()) {
l.add(it.next());
}
}
if (l == null) {
return DataUtil.emptyIterator();
}
return l.iterator();
}
public String getNamespaceURI(int arg0) {
List<Namespace> namespaces = eventProducer.getNamespaces();
if (namespaces == null) {
return null;
}
Namespace ns = namespaces.get(arg0);
if (ns != null) {
return ns.getNamespaceURI();
}
return null;
}
/**
* Combines a Namespace and local name into a QName.
*/
public static QName qname(Namespace ns, String name) {
return new QName(ns.getNamespaceURI(), name, ns.getPrefix());
}
public void add(XMLEvent event) throws XMLStreamException {
if (event.isStartDocument()) {
streamWriter.writeStartDocument();
} else if (event.isStartElement()) {
StartElement element = event.asStartElement();
QName elQName = element.getName();
if (elQName.getPrefix().length() > 0
&& elQName.getNamespaceURI().length() > 0)
streamWriter.writeStartElement(elQName.getPrefix(), elQName
.getLocalPart(), elQName.getNamespaceURI());
else if (elQName.getNamespaceURI().length() > 0)
streamWriter.writeStartElement(elQName.getNamespaceURI(),
elQName.getLocalPart());
else
streamWriter.writeStartElement(elQName.getLocalPart());
// Add element namespaces
Iterator namespaces = element.getNamespaces();
while (namespaces.hasNext()) {
Namespace ns = (Namespace) namespaces.next();
String prefix = ns.getPrefix();
String nsURI = ns.getNamespaceURI();
streamWriter.writeNamespace(prefix, nsURI);
}
// Add element attributes
Iterator attris = element.getAttributes();
while (attris.hasNext()) {
Attribute attr = (Attribute) attris.next();
QName atQName = attr.getName();
String value = attr.getValue();
if (atQName.getPrefix().length() > 0
&& atQName.getNamespaceURI().length() > 0)
streamWriter.writeAttribute(atQName.getPrefix(), atQName
.getNamespaceURI(), atQName.getLocalPart(), value);
else if (atQName.getNamespaceURI().length() > 0)
streamWriter.writeAttribute(atQName.getNamespaceURI(),
atQName.getLocalPart(), value);
else
streamWriter.writeAttribute(atQName.getLocalPart(), value);
}
} else if (event.isCharacters()) {
Characters chars = event.asCharacters();
streamWriter.writeCharacters(chars.getData());
} else if (event.isEndElement()) {
streamWriter.writeEndElement();
} else if (event.isEndDocument()) {
streamWriter.writeEndDocument();
} else {
throw new XMLStreamException("Unsupported event type: " + event);
}
}
/**
* Converts a namespace read from the XML document to the namespace used by JAXB annotations.
* This methods can convert the namespace for which there is a bijective mapping, for example
* {@code "http://www.isotc211.org/2005/gco"} to {@code "http://standards.iso.org/iso/19115/-3/gco/1.0"}.
* However some namespaces like {@code "http://www.isotc211.org/2005/gmd"} may be left unchanged,
* because that namespace from legacy ISO 19139:2007 can be mapped to many different namespaces
* in newer ISO 19115-3:2016 standard. However in some cases the context allows us to determines
* which newer namespace is used. In such case, that mapping is specified by the
* ({@code oldURI}, {@code newURI}) pair.
*
* @param namespace the namespace to import.
* @param oldURI an old URI which has been renamed as {@code newURI}, or {@code null} if none.
* @param newURI the new URI for {@code oldURI}, or {@code null} if {@code newURI} is null.
*/
private Namespace importNS(final Namespace namespace, final String oldURI, final String newURI) {
notify(namespace);
String uri = namespace.getNamespaceURI();
if (uri != null && !uri.isEmpty()) {
uri = removeTrailingSlash(uri);
final String imported = uri.equals(oldURI) ? newURI : relocate(uri);
if (imported != uri) {
final String prefix = prefixReplacement(namespace.getPrefix(), imported);
return new TransformedEvent.NS(namespace, prefix, imported);
}
}
return namespace;
}