Java Code Examples for javax.xml.XMLConstants#XML_NS_PREFIX

The following examples show how to use javax.xml.XMLConstants#XML_NS_PREFIX . 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: QNameComparatorTest.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompare() {
    List<QName> names = new LinkedList<>();
    QName name1 = new QName("localonly");
    names.add(name1);
    QName name2 = new QName(XMLConstants.XML_NS_URI, "xhtml", XMLConstants.XML_NS_PREFIX);
    names.add(name2);
    QName name3 = new QName(NamespaceRegistry.NAMESPACE_JCR, "UpperCase", NamespaceRegistry.PREFIX_JCR);
    names.add(name3);
    QName name4 = new QName(NamespaceRegistry.NAMESPACE_JCR, "primaryType", NamespaceRegistry.PREFIX_JCR);
    names.add(name4);
    QName name5 = new QName(NamespaceRegistry.NAMESPACE_JCR, "PrimaryType", NamespaceRegistry.PREFIX_JCR);
    names.add(name5);
    Collections.sort(names, new QNameComparator());
    
    Assert.assertThat(names, Matchers.contains(name2, name5, name4, name3, name1));
}
 
Example 2
Source File: ModuleNamespaceContext.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public String getPrefix(final String namespaceURI) {
    checkArgument(namespaceURI != null);

    switch (namespaceURI) {
        case YangConstants.RFC6020_YIN_NAMESPACE_STRING:
            return XMLConstants.DEFAULT_NS_PREFIX;
        case XMLConstants.XML_NS_URI:
            return XMLConstants.XML_NS_PREFIX;
        case XMLConstants.XMLNS_ATTRIBUTE_NS_URI:
            return XMLConstants.XMLNS_ATTRIBUTE;
        default:
            final List<@NonNull String> prefixes = namespaceToPrefix.get(namespaceURI);
            return prefixes.isEmpty() ? null : prefixes.get(0);
    }
}
 
Example 3
Source File: ModuleNamespaceContext.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public String getNamespaceURI(final String prefix) {
    checkArgument(prefix != null);

    switch (prefix) {
        case XMLConstants.DEFAULT_NS_PREFIX:
            return YangConstants.RFC6020_YIN_NAMESPACE_STRING;
        case XMLConstants.XML_NS_PREFIX:
            return XMLConstants.XML_NS_URI;
        case XMLConstants.XMLNS_ATTRIBUTE:
            return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
        default:
            final ModuleEffectiveStatement module = prefixToModule.get(prefix);
            return module != null ? module.localQNameModule().getNamespace().toString()
                    : XMLConstants.NULL_NS_URI;
    }
}
 
Example 4
Source File: BaseNsContext.java    From woodstox with Apache License 2.0 6 votes vote down vote up
@Override
public final String getPrefix(String nsURI)
{
    /* First the known offenders; invalid args, 2 predefined xml namespace
     * prefixes
     */
    if (nsURI == null || nsURI.length() == 0) {
        throw new IllegalArgumentException("Illegal to pass null/empty prefix as argument.");
    }
    if (nsURI.equals(XMLConstants.XML_NS_URI)) {
        return XMLConstants.XML_NS_PREFIX;
    }
    if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
        return XMLConstants.XMLNS_ATTRIBUTE;
    }
    return doGetPrefix(nsURI);
}
 
Example 5
Source File: AbstractNamespaceContext.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public final String getPrefix (@Nonnull final String sNamespaceURI)
{
  // According to JavaDoc
  if (sNamespaceURI == null)
    throw new IllegalArgumentException ();

  // special "xml:" namespace handling
  if (sNamespaceURI.equals (XMLConstants.XML_NS_URI))
    return XMLConstants.XML_NS_PREFIX;
  // special "xmlns:" namespace handling
  if (sNamespaceURI.equals (XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
    return XMLConstants.XMLNS_ATTRIBUTE;
  // special "" namespace handling
  if (sNamespaceURI.equals (getDefaultNamespaceURI ()))
    return XMLConstants.DEFAULT_NS_PREFIX;

  return getCustomPrefix (sNamespaceURI);
}
 
Example 6
Source File: EditableNamespaceContext.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the prefix for a namespace binding.
 */
@Override
public String getPrefix(String namespaceURI) {
  // per javax.xml.namespace.NamespaceContext doc
  if (namespaceURI == null)
    throw new IllegalArgumentException("Cannot find prefix for null namespace URI");
  if (XMLConstants.XML_NS_URI.equals(namespaceURI))
    return XMLConstants.XML_NS_PREFIX;
  if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI))
    return XMLConstants.XMLNS_ATTRIBUTE;

  for (Map.Entry<String, String> entry: bindings.entrySet()) {
    if (namespaceURI.equals(entry.getValue()))
      return entry.getKey();
  }

  return null;
}
 
Example 7
Source File: UnmarshallingContext.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getPrefix(String uri) {
    if( uri==null )
        throw new IllegalArgumentException();
    if( uri.equals(XMLConstants.XML_NS_URI) )
        return XMLConstants.XML_NS_PREFIX;
    if( uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) )
        return XMLConstants.XMLNS_ATTRIBUTE;

    for( int i=nsLen-2; i>=0; i-=2 )
        if(uri.equals(nsBind[i+1]))
            if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) )
                // make sure that this prefix is still effective.
                return nsBind[i];

    if(environmentNamespaceContext!=null)
        return environmentNamespaceContext.getPrefix(uri);

    return null;
}
 
Example 8
Source File: DOMNamespaceContext.java    From jlibs with Apache License 2.0 6 votes vote down vote up
@Override
public String getPrefix(String namespaceURI){
    if(namespaceURI==null)
        throw new IllegalArgumentException("namespaceURI is null");
    else if(XMLConstants.XML_NS_URI.equals(namespaceURI))
        return XMLConstants.XML_NS_PREFIX;
    else if(XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI))
        return XMLConstants.XMLNS_ATTRIBUTE;
    else{
        Attr attr = DOMUtil.findNamespaceDeclarationForURI(node, namespaceURI);
        if(attr!=null)
            return attr.getLocalName();
        else if(XMLConstants.NULL_NS_URI.equals(namespaceURI))
            return XMLConstants.DEFAULT_NS_PREFIX;
        else
            return null;
    }
}
 
Example 9
Source File: UnmarshallingContext.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getPrefix(String uri) {
    if( uri==null )
        throw new IllegalArgumentException();
    if( uri.equals(XMLConstants.XML_NS_URI) )
        return XMLConstants.XML_NS_PREFIX;
    if( uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) )
        return XMLConstants.XMLNS_ATTRIBUTE;

    for( int i=nsLen-2; i>=0; i-=2 )
        if(uri.equals(nsBind[i+1]))
            if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) )
                // make sure that this prefix is still effective.
                return nsBind[i];

    if(environmentNamespaceContext!=null)
        return environmentNamespaceContext.getPrefix(uri);

    return null;
}
 
Example 10
Source File: StringNamespaceContext.java    From XACML with MIT License 6 votes vote down vote up
@Override
public String getPrefix(String namespaceURI) {
	if (namespaceURI == null) {
		throw new IllegalArgumentException();
	}
	if (defaultNamespace != null && namespaceURI.equals(defaultNamespace.getNamespace())) {
		return XMLConstants.DEFAULT_NS_PREFIX;
	}
	if (namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
		return XMLConstants.XMLNS_ATTRIBUTE;
	} else if (namespaceURI.equals(XMLConstants.XML_NS_URI)) {
		return XMLConstants.XML_NS_PREFIX;
	}
	//  search the map looking for an entry that matches
	for (String key : namespaceMap.keySet()) {
		Namespace namespace = namespaceMap.get(key);
		if (namespace.getNamespace().equals(namespaceURI)) {
			return namespace.getPrefix();
		}
	}
	// if we get here then the URI was not in the map
	return null;
}
 
Example 11
Source File: UnmarshallingContext.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getPrefix(String uri) {
    if( uri==null )
        throw new IllegalArgumentException();
    if( uri.equals(XMLConstants.XML_NS_URI) )
        return XMLConstants.XML_NS_PREFIX;
    if( uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) )
        return XMLConstants.XMLNS_ATTRIBUTE;

    for( int i=nsLen-2; i>=0; i-=2 )
        if(uri.equals(nsBind[i+1]))
            if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) )
                // make sure that this prefix is still effective.
                return nsBind[i];

    if(environmentNamespaceContext!=null)
        return environmentNamespaceContext.getPrefix(uri);

    return null;
}
 
Example 12
Source File: UnmarshallingContext.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getPrefix(String uri) {
    if( uri==null )
        throw new IllegalArgumentException();
    if( uri.equals(XMLConstants.XML_NS_URI) )
        return XMLConstants.XML_NS_PREFIX;
    if( uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) )
        return XMLConstants.XMLNS_ATTRIBUTE;

    for( int i=nsLen-2; i>=0; i-=2 )
        if(uri.equals(nsBind[i+1]))
            if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) )
                // make sure that this prefix is still effective.
                return nsBind[i];

    if(environmentNamespaceContext!=null)
        return environmentNamespaceContext.getPrefix(uri);

    return null;
}
 
Example 13
Source File: UnmarshallingContext.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getPrefix(String uri) {
    if( uri==null )
        throw new IllegalArgumentException();
    if( uri.equals(XMLConstants.XML_NS_URI) )
        return XMLConstants.XML_NS_PREFIX;
    if( uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) )
        return XMLConstants.XMLNS_ATTRIBUTE;

    for( int i=nsLen-2; i>=0; i-=2 )
        if(uri.equals(nsBind[i+1]))
            if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) )
                // make sure that this prefix is still effective.
                return nsBind[i];

    if(environmentNamespaceContext!=null)
        return environmentNamespaceContext.getPrefix(uri);

    return null;
}
 
Example 14
Source File: MapNamespaceContext.java    From cxf with Apache License 2.0 6 votes vote down vote up
public String getPrefix(String namespaceURI) {
    if (namespaceURI == null) {
        throw new IllegalArgumentException("Null namespace to getPrefix");
    }
    if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
        return XMLConstants.XML_NS_PREFIX;
    }
    if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
        return XMLConstants.XMLNS_ATTRIBUTE;
    }

    for (Map.Entry<String, String> e : namespaces.entrySet()) {
        if (e.getValue().equals(namespaceURI)) {
            return e.getKey();
        }
    }
    return null;
}
 
Example 15
Source File: UnmarshallingContext.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getPrefix(String uri) {
    if( uri==null )
        throw new IllegalArgumentException();
    if( uri.equals(XMLConstants.XML_NS_URI) )
        return XMLConstants.XML_NS_PREFIX;
    if( uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) )
        return XMLConstants.XMLNS_ATTRIBUTE;

    for( int i=nsLen-2; i>=0; i-=2 )
        if(uri.equals(nsBind[i+1]))
            if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) )
                // make sure that this prefix is still effective.
                return nsBind[i];

    if(environmentNamespaceContext!=null)
        return environmentNamespaceContext.getPrefix(uri);

    return null;
}
 
Example 16
Source File: MapBasedNamespaceContext.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public String getPrefix(String uri) {
	if (defaultNamespaceURI != null && defaultNamespaceURI.equals(uri)) {
		return XMLConstants.DEFAULT_NS_PREFIX;
	} else if (uri == null) {
		throw new IllegalArgumentException("Null not allowed as prefix");
	} else if (uri.equals(XMLConstants.XML_NS_URI)) {
		return XMLConstants.XML_NS_PREFIX;
	} else if (uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
		return XMLConstants.XMLNS_ATTRIBUTE;
	} else {
		return namespaceIdsMap.get(uri);
	}
}
 
Example 17
Source File: AbstractNamespaceContext.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@DevelopersNote ("Java 8: Iterator; Java 10: Iterator<String>")
public final Iterator getPrefixes (@Nonnull final String sNamespaceURI)
{
  // According to JavaDoc
  if (sNamespaceURI == null)
    throw new IllegalArgumentException ();
  if (sNamespaceURI.equals (XMLConstants.XML_NS_URI))
    return new SingleElementIterator <> (XMLConstants.XML_NS_PREFIX);
  if (sNamespaceURI.equals (XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
    return new SingleElementIterator <> (XMLConstants.XMLNS_ATTRIBUTE);

  final Iterator <String> ret = getCustomPrefixes (sNamespaceURI);
  return ret != null ? ret : new EmptyIterator <> ();
}
 
Example 18
Source File: DomXPathNamespaceResolver.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
public String getPrefix(String namespaceURI) {
  ensureNotNull("Namespace URI", namespaceURI);

  if(namespaceURI.equals(XMLConstants.XML_NS_URI)) {
    return XMLConstants.XML_NS_PREFIX;
  }

  if(namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
    return XMLConstants.XMLNS_ATTRIBUTE;
  }

  /**
   * TODO: This only works for the root element. Every child element with a 'xmlns'-attribute will be ignored.
   */
  if (namespaceURI.equals(element.name())) {
    return XMLConstants.DEFAULT_NS_PREFIX;
  }

  String key = null;
  if(namespaces.containsValue(namespaceURI)) {
    for(Map.Entry<String, String> entry : namespaces.entrySet()) {
      if(namespaceURI.equals(entry.getValue())) {
        key = entry.getKey();
        break;
      }
    }
  }
  return key;

}
 
Example 19
Source File: BijectiveNsMap.java    From woodstox with Apache License 2.0 5 votes vote down vote up
public static BijectiveNsMap createEmpty()
{
    String[] strs = new String[DEFAULT_ARRAY_SIZE];

    strs[0] = XMLConstants.XML_NS_PREFIX;
    strs[1] = XMLConstants.XML_NS_URI;
    strs[2] = XMLConstants.XMLNS_ATTRIBUTE;
    strs[3] = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;

    // Let's consider pre-defined ones to be 'out of scope', i.e.
    // conceptually be part of (missing) parent's mappings.
    return new BijectiveNsMap(4, strs);
}
 
Example 20
Source File: InputElementStack.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public final String getPrefix(String nsURI)
{
    if (nsURI == null || nsURI.length() == 0) {
        throw new IllegalArgumentException("Illegal to pass null/empty prefix as argument.");
    }
    if (nsURI.equals(XMLConstants.XML_NS_URI)) {
        return XMLConstants.XML_NS_PREFIX;
    }
    if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
        return XMLConstants.XMLNS_ATTRIBUTE;
    }
    /* Ok, need to find the match, if any; starting from end of the
     * list of active namespaces. Note that we can not count on prefix
     * being interned/canonicalized.
     */
    String prefix = null;

    // 29-Sep-2004, TSa: Need to check for namespace masking, too...
    String[] strs = mNamespaces.getInternalArray();
    int len = mNamespaces.size();

    main_loop:
    for (int index = len-1; index > 0; index -= 2) {
        if (nsURI.equals(strs[index])) {
            // Ok, is prefix masked?
            prefix = strs[index-1];
            for (int j = index+1; j < len; j += 2) {
                if (strs[j] == prefix) { // masked!
                    prefix = null;
                    continue main_loop;
                }
            }
            // nah, it's good
            // 17-Mar-2006, TSa: ... but default NS has prefix null...
            if (prefix == null) {
                prefix = "";
            }
            break main_loop;
        }
    }

    return prefix;
}