Example usage for javax.xml XMLConstants DEFAULT_NS_PREFIX

List of usage examples for javax.xml XMLConstants DEFAULT_NS_PREFIX

Introduction

In this page you can find the example usage for javax.xml XMLConstants DEFAULT_NS_PREFIX.

Prototype

String DEFAULT_NS_PREFIX

To view the source code for javax.xml XMLConstants DEFAULT_NS_PREFIX.

Click Source Link

Document

Prefix to use to represent the default XML Namespace.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String source = "<p xmlns='http://www.java2s.com/nfe' versao='2.00'></p>";

    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();

    NamespaceContext context = new NamespaceContext() {
        String PREFIX = "nfe";
        String URI = "http://www.java2s.com/nfe";

        @Override// w  w  w .  j av a  2 s  .  c o  m
        public String getNamespaceURI(String prefix) {
            return (PREFIX.equals(prefix)) ? URI : XMLConstants.NULL_NS_URI;
        }

        @Override
        public String getPrefix(String namespaceUri) {
            return (URI.equals(namespaceUri)) ? PREFIX : XMLConstants.DEFAULT_NS_PREFIX;
        }

        @Override
        public Iterator getPrefixes(String namespaceUri) {
            return Collections.singletonList(this.getPrefix(namespaceUri)).iterator();
        }
    };
    xPath.setNamespaceContext(context);
    InputSource inputSource = new InputSource(new StringReader(source));
    String versao = xPath.evaluate("//nfe:p/@versao", inputSource);
    System.out.println(versao.toString());
}

From source file:Main.java

public static String[] splitQName(String qualifiedName) {
    if (qualifiedName == null) {
        throw new IllegalArgumentException("Null QName");
    }/*from  w  ww .  jav a2  s . c om*/
    int idx = qualifiedName.indexOf(':');
    String[] parts = { null, null };
    if (idx >= 0) {
        parts[0] = qualifiedName.substring(0, idx);
        parts[1] = qualifiedName.substring(idx + 1);
    } else {
        parts[0] = XMLConstants.DEFAULT_NS_PREFIX;
        parts[1] = qualifiedName;
    }
    return parts;
}

From source file:Main.java

/**
 * Return the qualified name, that is to say the prefix -if any- with the
 * local name.//from w  w w.  j  a v  a 2 s.co m
 *
 * @param qName
 *            The QName.
 * @return A string that looks like "<tt>prefix:localName</tt>" or "
 *         <tt>NCName</tt>".
 */
public static String getQualifiedName(QName qName) {
    if (!XMLConstants.NULL_NS_URI.equals(qName.getNamespaceURI())
            && !XMLConstants.DEFAULT_NS_PREFIX.equals(qName.getPrefix())) {
        return qName.getPrefix() + ":" + qName.getLocalPart();
    } else {
        return qName.getLocalPart();
    }
}

From source file:Main.java

public static String getQName(QName qname) {
    if (qname.getPrefix() == null)
        throw new IllegalArgumentException("prefix is null in " + qname);
    return XMLConstants.DEFAULT_NS_PREFIX.equals(qname.getPrefix()) ? qname.getLocalPart()
            : qname.getPrefix() + ':' + qname.getLocalPart();
}

From source file:Main.java

public static String getDefaultNamespaceURI(final XMLStreamWriter writer) {
    return writer.getNamespaceContext().getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX);
}

From source file:Main.java

public String getNamespaceURI(String prefix) {
    if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX))
        return sourceDocument.lookupNamespaceURI(null);
    else//w  w  w. ja  v a 2 s.  co m
        return sourceDocument.lookupNamespaceURI(prefix);
}

From source file:eu.esdihumboldt.hale.io.wfs.ui.KVPUtil.java

/**
 * Add typename and namespace parameters for a WFS request to the given URI
 * builder.//  w  w w. ja  v  a  2  s .c om
 * 
 * @param builder the builder for the WFS request URI
 * @param selected the type names to include
 * @param version the targeted WFS version
 */
public static void addTypeNameParameter(URIBuilder builder, Iterable<QName> selected, WFSVersion version) {
    // namespaces mapped to prefixes
    Map<String, String> namespaces = new HashMap<>();
    // type names with updated prefix
    Set<QName> typeNames = new HashSet<>();

    for (QName type : selected) {
        String prefix;
        if (type.getNamespaceURI() != null && !type.getNamespaceURI().isEmpty()) {
            prefix = namespaces.get(type.getNamespaceURI());
            if (prefix == null) {
                // no mapping yet for namespace
                String candidate = type.getPrefix();
                prefix = addPrefix(candidate, type.getNamespaceURI(), namespaces);
            }
        } else {
            // default namespace
            prefix = XMLConstants.DEFAULT_NS_PREFIX;
        }

        // add updated type
        typeNames.add(new QName(type.getNamespaceURI(), type.getLocalPart(), prefix));
    }

    final String paramNamespaces;
    final String paramTypenames;
    final String prefixNamespaceDelim;
    switch (version) {
    case V1_1_0:
        paramNamespaces = "NAMESPACE";
        paramTypenames = "TYPENAME";
        prefixNamespaceDelim = "=";
        break;
    case V2_0_0:
    case V2_0_2:
        /*
         * XXX below are the values as defined in the WFS 2 specification.
         * There have been problems with some GeoServer instances if used in
         * that manner.
         */
        paramNamespaces = "NAMESPACES";
        paramTypenames = "TYPENAMES";
        prefixNamespaceDelim = ",";
        break;
    default:
        // fall-back to WFS 1.1
        paramNamespaces = "NAMESPACE";
        paramTypenames = "TYPENAME";
        prefixNamespaceDelim = "=";
    }

    // add namespace prefix definitions
    if (!namespaces.isEmpty()) {
        builder.addParameter(paramNamespaces, Joiner.on(',')
                .join(Maps.transformEntries(namespaces, new EntryTransformer<String, String, String>() {

                    @Override
                    public String transformEntry(String namespace, String prefix) {
                        StringBuilder sb = new StringBuilder();
                        sb.append("xmlns(");
                        sb.append(prefix);
                        sb.append(prefixNamespaceDelim);
                        sb.append(namespace);
                        sb.append(")");
                        return sb.toString();
                    }

                }).values()));
    }
    // add type names
    if (!typeNames.isEmpty()) {
        builder.addParameter(paramTypenames,
                Joiner.on(',').join(Iterables.transform(typeNames, new Function<QName, String>() {

                    @Override
                    public String apply(QName typeName) {
                        String prefix = typeName.getPrefix();
                        if (prefix == null || prefix.isEmpty()) {
                            return typeName.getLocalPart();
                        }
                        return prefix + ":" + typeName.getLocalPart();
                    }
                })));
    }
}

From source file:Main.java

/**
 * Get the namespace prefix of the passed element in a safe way.
 *
 * @param aElement/*from www  .  j  av a 2 s  .c  o m*/
 *        The element to be queried. May be <code>null</code>.
 * @return {@link XMLConstants#DEFAULT_NS_PREFIX} or the provided prefix.
 *         Never <code>null</code>.
 * @since 8.4.1
 */
@Nonnull
public static String getPrefix(@Nullable final Element aElement) {
    final String sPrefix = aElement == null ? null : aElement.getPrefix();
    return sPrefix == null ? XMLConstants.DEFAULT_NS_PREFIX : sPrefix;
}

From source file:edu.internet2.middleware.openid.message.encoding.EncodingUtils.java

/**
 * Flatten the parameter map. Each parameter QName is converted into it's dotted namespace qualified form, and
 * namespace declarations are added to the resulting parameter map. The resulting map is suitable for passing to
 * {@link MessageCodec#encode(Map<String, String>)}.
 * /*  w ww  .  j a  va2 s.c o m*/
 * @param parameterMap parameter map to flatten
 * @return parameter map with flattened parameter names and namespace declarations
 */
public static Map<String, String> flattenParameterNames(ParameterMap parameterMap) {
    Map<String, String> parameters = new LinkedHashMap<String, String>();
    NamespaceMap namespaces = parameterMap.getNamespaces();

    // add namespaces to key-value parameter map
    for (String namespaceURI : namespaces.getURIs()) {
        String key = OpenIDConstants.MESSAGE_NAMESPACE_PREFIX;
        String namespaceAlias = namespaces.getAlias(namespaceURI);

        if (namespaceAlias != XMLConstants.DEFAULT_NS_PREFIX) {
            key += "." + namespaceAlias;
        }

        parameters.put(key, namespaceURI);
    }

    // add parameters to key-value parameter map
    for (QName qname : parameterMap.keySet()) {
        String key = encodeParameterName(qname, namespaces);
        parameters.put(key, parameterMap.get(qname));
    }

    return parameters;
}

From source file:org.javelin.sws.ext.bind.internal.model.AttributePattern.java

@Override
protected void replayNonNullString(String value, XMLEventWriter eventWriter, MarshallingContext context)
        throws XMLStreamException {
    if (!XMLConstants.NULL_NS_URI.equals(this.attributeName.getNamespaceURI())) {
        // we MUST create second namespace declaration if the current one has default ("") prefix, otherwise the attribute will have absent
        // namespace!
        // see: http://www.w3.org/TR/xml-names11/#defaulting - "The namespace name for an unprefixed attribute name always has no value."
        String prefix = this.safeRegisterNamespace(context, eventWriter, this.attributeName);
        if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
            Namespace namespace = this.safePrepareNamespace(
                    context, eventWriter, new QName(this.attributeName.getNamespaceURI(),
                            this.attributeName.getLocalPart(), context.newPrefix()),
                    NamespaceRegistration.IF_DEFAULT_PREFIX);
            prefix = namespace.getPrefix();
        }/*from   w  w w  . j  a  va2 s.c o  m*/
        eventWriter.add(XML_EVENTS_FACTORY.createAttribute(prefix, this.attributeName.getNamespaceURI(),
                this.attributeName.getLocalPart(), value));
    } else {
        // attribute is unqalified and has no namespace
        eventWriter.add(XML_EVENTS_FACTORY.createAttribute(this.attributeName.getLocalPart(), value));
    }
}