Java Code Examples for javax.xml.namespace.NamespaceContext#getNamespaceURI()

The following examples show how to use javax.xml.namespace.NamespaceContext#getNamespaceURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestNamespaces.java    From woodstox with Apache License 2.0 6 votes vote down vote up
private void checkValidityOfNs1(NamespaceContext nc)
    throws XMLStreamException
{
    // Ok, we have just 2 bindings here.
    // First, let's check some non-existing bindings
    assertNull(nc.getPrefix("someurl"));
    assertNull(nc.getPrefix("whatever"));
    assertNull(nc.getNamespaceURI("c"));
    // default can be empty or null
    String defNs = nc.getNamespaceURI("");
    if (defNs != null && defNs.length() > 0) {
        fail("Expected default namespace to be null or empty, was '"+defNs+"'");
    }
    // And then the ones that do exist
    assertEquals("a", nc.getPrefix("myurl"));
    assertEquals("myurl", nc.getNamespaceURI("a"));
    assertEquals("b", nc.getPrefix("urlforb"));
    assertEquals("urlforb", nc.getNamespaceURI("b"));
}
 
Example 2
Source File: ElementReader.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void extractXsiType() {
    /*
     * We're making a conscious choice here -- garbage in == garbage out.
     */
    String xsiTypeQname = root.getAttributeValue(Constants.URI_2001_SCHEMA_XSI, "type");
    if (xsiTypeQname != null) {
        Matcher m = QNAME_PATTERN.matcher(xsiTypeQname);
        if (m.matches()) {
            NamespaceContext nc = root.getNamespaceContext();
            this.xsiType = new QName(nc.getNamespaceURI(m.group(1)), m.group(2), m.group(1));
        } else {
            this.xsiType = new QName(this.namespace, xsiTypeQname, "");
        }
    }
}
 
Example 3
Source File: SimpleOutputElement.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 * Note: this method can and will only be called before outputting
 * the root element.
 */
@Override
protected final void setRootNsContext(NamespaceContext ctxt)
{
    mRootNsContext = ctxt;
    // Let's also figure out the default ns binding, if any:
    String defURI = ctxt.getNamespaceURI("");
    if (defURI != null && defURI.length() > 0) {
        mDefaultNsURI = defURI;
    }
}
 
Example 4
Source File: StaxXmlPullParser.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public String getNamespace(String prefix) {
    if (prefix == null) {
        prefix = XMLConstants.DEFAULT_NS_PREFIX;
    }
    NamespaceContext namespaceContext = xmlStreamReader.getNamespaceContext();
    return namespaceContext.getNamespaceURI(prefix);
}
 
Example 5
Source File: CompoundNamespaceContext.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
 */
public String getNamespaceURI(String prefix) {
    if (prefix == null) {
        throw new IllegalArgumentException("The prefix cannot be null.");
    }
    for (NamespaceContext nc: contexts) {
        String uri = nc.getNamespaceURI(prefix);
        if (uri != null) {
            return uri;
        }
    }
    return null;
}
 
Example 6
Source File: DOMOutputElement.java    From woodstox with Apache License 2.0 5 votes vote down vote up
@Override
protected void setRootNsContext(NamespaceContext ctxt)
{
    mRootNsContext = ctxt;
    /* Let's also see if we have an active default ns mapping:
     * (provided it hasn't yet explicitly been set for this element)
     */
    if (!mDefaultNsSet) {
        String defURI = ctxt.getNamespaceURI("");
        if (defURI != null && defURI.length() > 0) {
            mDefaultNsURI = defURI;
        }
    }
}
 
Example 7
Source File: SimpleXPathParser.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
private QName getQName(String prefix, String name, NamespaceContext namespaceContext) throws XPathException {
	if (prefix == null)
		prefix = XMLConstants.DEFAULT_NS_PREFIX;

	String namespaceURI = namespaceContext.getNamespaceURI(prefix);
	if (namespaceURI == null || namespaceURI.isEmpty())
		throw new XPathException("Failed to find namespace URI for prefix '" + prefix + "' used with '" + name + "'.");

	return new QName(namespaceURI, name);
}
 
Example 8
Source File: DatatypeConverterImpl.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 9
Source File: DatatypeConverterImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 10
Source File: DatatypeConverterImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 11
Source File: DatatypeConverterImpl.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 12
Source File: DatatypeConverterImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 13
Source File: DatatypeConverterImpl.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 14
Source File: DatatypeConverterImpl.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 15
Source File: StaxXmlPullParser.java    From Smack with Apache License 2.0 4 votes vote down vote up
@Override
public String getNamespace() {
    NamespaceContext namespaceContext = xmlStreamReader.getNamespaceContext();
    String prefix = getPrefix();
    return namespaceContext.getNamespaceURI(prefix);
}
 
Example 16
Source File: DatatypeConverterImpl.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 17
Source File: DatatypeConverterImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 18
Source File: DatatypeConverterImpl.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 19
Source File: DatatypeConverterImpl.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}
 
Example 20
Source File: BijectiveNsMap.java    From woodstox with Apache License 2.0 4 votes vote down vote up
/**
 * Method used to add a dynamic binding, and return the prefix
 * used to bind the specified namespace URI.
 */
public String addGeneratedMapping(String prefixBase, NamespaceContext ctxt,
                                  String uri, int[] seqArr)
{
    String[] strs = mNsStrings;
    int seqNr = seqArr[0];
    String prefix;
    int attempts = 0;

    main_loop:
    while (true) {
        // We better intern the resulting prefix? Or not?
        prefix = (prefixBase + seqNr).intern();
        ++seqNr;

        /* Ok, let's see if we have a mapping (masked or not) for
         * the prefix. If we do, let's just not use it: we could
         * of course mask it (unless it's in current scope), but
         * it's easier to just get a "virgin" prefix...
         */
        int phash = prefix.hashCode();
        
        for (int ix = mScopeEnd - 2; ix >= 0; ix -= 2) {
            String thisP = strs[ix];
            if (thisP == prefix ||
                (thisP.hashCode() == phash && thisP.equals(prefix))) {
                continue main_loop;
            }
        }
        // So far so good... but do we have a root context that might
        // have something too?

        // [woodstox-core#74]: had infinite loop for certain Namespace implementations
        if (ctxt != null) {
            String existing = ctxt.getNamespaceURI(prefix);
            if (existing != null && !existing.isEmpty()) {
                continue;
            }
        }
        // also... guard against infinite loops in general, just in case
        if (++attempts > MAX_LOOP_FOR_NEW_PREFIX) {
            throw new IllegalStateException("Internal error: failed to find a mapping prefix for URI '"+uri
                    +" in "+MAX_LOOP_FOR_NEW_PREFIX+" attempts");
        }
        
        break;
    }
    seqArr[0] = seqNr;

    // Ok, good; then let's just add it in...
    if (mScopeEnd >= strs.length) {
        // let's just double the array sizes...
        strs = DataUtil.growArrayBy(strs, strs.length);
        mNsStrings = strs;
    }
    strs[mScopeEnd++] = prefix;
    strs[mScopeEnd++] = uri;

    return prefix;
}