/** * Returns the namespace prefix of the element or an empty string if none * exists. * * @return the namespace prefix */ public String getNamespacePrefix() { return namespace.getPrefix(); }
/** * This will retrieve the namespace prefix of the * <code>Attribute</code>. For any XML attribute * which appears as * <code>[namespacePrefix]:[attributeName]</code>, * the namespace prefix of the attribute would be * <code>[namespacePrefix]</code>. When the attribute * has no namespace, an empty <code>String</code> is returned. * * @return <code>String</code> - namespace prefix of this * attribute. */ public String getNamespacePrefix() { return namespace.getPrefix(); }
/** * This will handle adding any <code>{@link Namespace}</code> attributes to * the DOM tree. * * @param ns * <code>Namespace</code> to add definition of */ private static String getXmlnsTagFor(Namespace ns) { String attrName = "xmlns"; if (!ns.getPrefix().equals("")) { attrName += ":"; attrName += ns.getPrefix(); } return attrName; }
@Override public String toString() { return ns.getPrefix() + "=" + ns.getURI(); } }
@Override public final String getNamespacePrefix(Object namespace) { return ((NamespaceContainer)namespace).getNamespace().getPrefix(); }
void includeNamespace(Namespace namespace) { nsFromUser.put(namespace.getPrefix(), namespace.getURI()); }
/** * Define a Namespace to be available for the XPath expression. * * @param namespace * The namespace to define. * @return true if this Namespace prefix was newly defined, false if the * prefix was previously defined and has now been redefined. * @throws NullPointerException * if the namespace is null. */ public boolean setNamespace(Namespace namespace) { if (namespace == null) { throw new NullPointerException("Null Namespace"); } if ("".equals(namespace.getPrefix())) { if (Namespace.NO_NAMESPACE != namespace) { throw new IllegalArgumentException( "Cannot set a Namespace URI in XPath for the \"\" prefix."); } // NO_NAMESPACE is always defined... return false; } if (namespaces == null) { namespaces = new HashMap<String, Namespace>(); } return namespaces.put(namespace.getPrefix(), namespace) == null; }
private static final List<Namespace> orderFirst(final Namespace nsa, final List<Namespace> nsl) { if (nsl.get(0) == nsa) { return nsl; } // OK, we have our namespace list, but our's is not the first. // we need the Attribute's Namespace to be up front. TreeMap<String,Namespace> tm = new TreeMap<String, Namespace>(); for (Namespace ns : nsl) { if (ns != nsa) { tm.put(ns.getPrefix(), ns); } } ArrayList<Namespace> ret = new ArrayList<Namespace>(tm.size() + 1); ret.add(nsa); ret.addAll(tm.values()); return Collections.unmodifiableList(ret); }
/** * Returns the full name of the element, in the form * [namespacePrefix]:[localName]. If the element does not have a namespace * prefix, then the local name is returned. * * @return qualified name of the element (including * namespace prefix) */ public String getQualifiedName() { // Note: Any changes here should be reflected in // XMLOutputter.printQualifiedName() if ("".equals(namespace.getPrefix())) { return getName(); } return new StringBuilder(namespace.getPrefix()) .append(':') .append(name) .toString(); }
/** * This will handle printing of any needed <code>{@link Namespace}</code> * declarations. * * @param out * <code>XMLStreamWriter</code> to use. * @param fstack * The current FormatStack * @param ns * <code>Namespace</code> to print definition of * @throws XMLStreamException * if the output fails */ protected void printNamespace(final XMLStreamWriter out, final FormatStack fstack, final Namespace ns) throws XMLStreamException { final String prefix = ns.getPrefix(); final String uri = ns.getURI(); out.writeNamespace(prefix, uri); }
/** * Check if two namespaces collide. * * @param namespace <code>Namespace</code> to check. * @param other <code>Namespace</code> to check against. * @return <code>String</code> reason for collision, or * <code>null</code> if no collision. */ public static String checkNamespaceCollision(final Namespace namespace, final Namespace other) { String p1,p2,u1,u2,reason; reason = null; p1 = namespace.getPrefix(); u1 = namespace.getURI(); p2 = other.getPrefix(); u2 = other.getURI(); if (p1.equals(p2) && !u1.equals(u2)) { reason = "The namespace prefix \"" + p1 + "\" collides"; } return reason; }
@Override public javax.xml.stream.events.Namespace next() { Namespace ns = source.next(); return fac.createNamespace(ns.getPrefix(), ns.getURI()); }
/** * This will create an <code>Exception</code> indicating * that the addition of the <code>{@link Namespace}</code> * to the <code>{@link Element}</code> is illegal. * * @param base <code>Element</code> that the <code>Namespace</code> * couldn't be added to * @param added <code>Namespace</code> that could not be added * @param reason cause of the problem */ IllegalAddException(Element base, Namespace added, String reason) { super(new StringBuilder() .append("The namespace xmlns") .append(added.getPrefix().equals("") ? "=" : ":" + added.getPrefix() + "=") .append("\"") .append(added.getURI()) .append("\" could not be added as a namespace to \"") .append(base.getQualifiedName()) .append("\": ") .append(reason) .toString()); }
/** * Check if <code>{@link Attribute}</code>'s namespace collides with a * <code>{@link Element}</code>'s namespace. * * @param attribute <code>Attribute</code> to check. * @param element <code>Element</code> to check against. * @return <code>String</code> reason for collision, or * <code>null</code> if no collision. */ public static String checkNamespaceCollision(final Attribute attribute, final Element element) { final Namespace namespace = attribute.getNamespace(); final String prefix = namespace.getPrefix(); if ("".equals(prefix)) { return null; } return checkNamespaceCollision(namespace, element); }
void setContext(Object node) { nsFromContext.clear(); List<Namespace> nsl = null; if (node instanceof NamespaceAware) { nsl = ((NamespaceAware)node).getNamespacesInScope(); } else if (node instanceof NamespaceContainer) { nsl = ((NamespaceContainer)node).getParentElement().getNamespacesInScope(); } if (nsl != null) { for (Namespace ns : nsl) { nsFromContext.put(ns.getPrefix(), ns.getURI()); } } }
@Override public final String getElementQName(Object element) { Element e = (Element)element; if (e.getNamespace().getPrefix().length() == 0) { return e.getName(); } return e.getNamespacePrefix() + ":" + e.getName(); }
@Override public javax.xml.stream.events.Attribute next() { final Attribute att = source.next(); final Namespace ns = att.getNamespace(); if (ns == Namespace.NO_NAMESPACE) { return fac.createAttribute(att.getName(), att.getValue()); } return fac.createAttribute(ns.getPrefix(), ns.getURI(), att.getName(), att.getValue()); }
/** * This will handle printing of an <code>{@link Attribute}</code>. * * @param out * <code>XMLStreamWriter</code> to use. * @param fstack * The current FormatStack * @param attribute * <code>Attribute</code> to output * @throws XMLStreamException * if the output fails */ protected void printAttribute(final XMLStreamWriter out, final FormatStack fstack, final Attribute attribute) throws XMLStreamException { if (!attribute.isSpecified() && fstack.isSpecifiedAttributesOnly()) { return; } final Namespace ns = attribute.getNamespace(); if (ns == Namespace.NO_NAMESPACE) { out.writeAttribute(attribute.getName(), attribute.getValue()); } else { out.writeAttribute(ns.getPrefix(), ns.getURI(), attribute.getName(), attribute.getValue()); } }