Java Code Examples for javax.xml.XMLConstants#DEFAULT_NS_PREFIX

The following examples show how to use javax.xml.XMLConstants#DEFAULT_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: XMLStreamWriterImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void writenamespace(String prefix, String namespaceURI)
    throws IOException {
    fWriter.write(" xmlns");

    if ((prefix != null) && (prefix != XMLConstants.DEFAULT_NS_PREFIX)) {
        fWriter.write(":");
        fWriter.write(prefix);
    }

    fWriter.write("=\"");
    writeXMLContent(
            namespaceURI,
            true,   // true = escapeChars
            true);  // true = escapeDoubleQuotes
    fWriter.write("\"");
}
 
Example 2
Source File: XMLStreamWriterImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void writeAttributeWithPrefix(String prefix, String localName,
    String value) throws IOException {
    fWriter.write(SPACE);

    if ((prefix != null) && (prefix != XMLConstants.DEFAULT_NS_PREFIX)) {
        fWriter.write(prefix);
        fWriter.write(":");
    }

    fWriter.write(localName);
    fWriter.write("=\"");
    writeXMLContent(value,
            true,   // true = escapeChars
            true);  // true = escapeDoubleQuotes
    fWriter.write("\"");
}
 
Example 3
Source File: ExtendedNamespaceContext.java    From XACML with MIT License 6 votes vote down vote up
@Override
public String toString() {
	Iterator<String> prefixIt = this.getAllPrefixes();
	StringBuffer sb = new StringBuffer("{[");
	while (prefixIt.hasNext()) {
		String prefix = prefixIt.next();
		String namespaceUri = this.getNamespaceURI(prefix);
		
		sb.append("{");
		if (prefix == XMLConstants.DEFAULT_NS_PREFIX) {
			sb.append(namespaceUri);
		} else {
			sb.append(prefix + "," + namespaceUri);
		}
		sb.append("}");
	}
	sb.append("]}");
	return sb.toString();
}
 
Example 4
Source File: XMLStreamWriterImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void writeAttributeWithPrefix(String prefix, String localName,
    String value) throws IOException {
    fWriter.write(SPACE);

    if ((prefix != null) && (prefix != XMLConstants.DEFAULT_NS_PREFIX)) {
        fWriter.write(prefix);
        fWriter.write(":");
    }

    fWriter.write(localName);
    fWriter.write("=\"");
    writeXMLContent(value,
            true,   // true = escapeChars
            true);  // true = escapeDoubleQuotes
    fWriter.write("\"");
}
 
Example 5
Source File: QNameParser.java    From web-feature-service with Apache License 2.0 6 votes vote down vote up
@Override
public QName parse(String key, String value) throws KVPParseException {
	String[] items = value.trim().split(":");
	if (items.length > 2)
		throw new KVPParseException("The value '" + value + "' of the parameter " + key + " is not a valid qualified property name.");
	
	String prefix = null;
	String name = null;
	
	if (items.length == 1) {
		prefix = XMLConstants.DEFAULT_NS_PREFIX;
		name = items[0];
	} else {
		prefix = items[0];
		name = items[1];
	}
	
	String namespaceURI = namespaceFilter.getNamespaceURI(prefix);
	if (namespaceURI == null)
		throw new KVPParseException("The prefix '" + prefix + "' used in the property name '" + value + "' of the parameter " + key + " is not bound to a namespace URI.");
			
	return new QName(namespaceURI, name);
}
 
Example 6
Source File: XMLStreamReaderImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
* @return the prefix of the current event, or null if the event does
* not have a prefix. For START_ELEMENT and END_ELEMENT, return
* XMLConstants.DEFAULT_NS_PREFIX when no prefix is available.
*/
public String getPrefix() {
    if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
        String prefix = fScanner.getElementQName().prefix;
        return prefix == null ? XMLConstants.DEFAULT_NS_PREFIX : prefix;
    }
    return null ;
}
 
Example 7
Source File: XMLStreamReaderImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
* @return the prefix of the current event, or null if the event does
* not have a prefix. For START_ELEMENT and END_ELEMENT, return
* XMLConstants.DEFAULT_NS_PREFIX when no prefix is available.
*/
public String getPrefix() {
    if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
        String prefix = fScanner.getElementQName().prefix;
        return prefix == null ? XMLConstants.DEFAULT_NS_PREFIX : prefix;
    }
    return null ;
}
 
Example 8
Source File: XMLEventAllocatorImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected void fillNamespaceAttributes(EndElementEvent event,XMLStreamReader xmlr){
    int count = xmlr.getNamespaceCount();
    String uri = null;
    String prefix = null;
    NamespaceImpl attr = null;
    for(int i=0;i< count;i++){
        uri = xmlr.getNamespaceURI(i);
        prefix = xmlr.getNamespacePrefix(i);
        if(prefix == null){
            prefix = XMLConstants.DEFAULT_NS_PREFIX;
        }
        attr = new NamespaceImpl(prefix,uri);
        event.addNamespace(attr);
    }
}
 
Example 9
Source File: JAXPNamespaceContextWrapper.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public String getPrefix(String uri) {
    if (fNamespaceContext != null) {
        if (uri == null) {
            uri = XMLConstants.NULL_NS_URI;
        }
        String prefix = fNamespaceContext.getPrefix(uri);
        if (prefix == null) {
            prefix = XMLConstants.DEFAULT_NS_PREFIX;
        }
        return (fSymbolTable != null) ? fSymbolTable.addSymbol(prefix) : prefix.intern();
    }
    return null;
}
 
Example 10
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 11
Source File: Bpmn2ResourceImpl.java    From fixflow with Apache License 2.0 5 votes vote down vote up
/**
 * Partly stolen from XmlHelperImpl.setPrefixToNamespaceMap().
 * Ensuring that namespace declaration is saved seems to be really tricky.
 * We will necessarily create a dummy package to ensure that later XmlSaveImpl.addNamespaceDeclarations() writes the ns declaration for us
 * @param namespace
 * @return
 */
private String getPrefixDuringSave(String namespace) {
    if (urisToPrefixes.containsKey(namespace))
        return urisToPrefixes.get(namespace).get(0);

    EPackage ePackage = extendedMetaData.getPackage(namespace);
    if (ePackage == null) {
        ePackage = extendedMetaData.demandPackage(namespace);
        // This will internally create a nice prefix
    }

    String prefix;
    if (namespace.equals(getDefinitions().getTargetNamespace()))
        // try to use the default namespace (xmlns="...") for local references
        prefix = XMLConstants.DEFAULT_NS_PREFIX;
    else
        prefix = ePackage.getNsPrefix();

    // Make prefix unique
    String originalPrefix = prefix + "_";
    int discr = 0;
    while (prefixesToURIs.containsKey(prefix)
            && !prefixesToURIs.get(prefix).equals(namespace))
        prefix = originalPrefix + discr++;

    // I'm not sure if the following code is needed, but I keep it to avoid inconsistencies
    if (!packages.containsKey(ePackage)) {
        packages.put(ePackage, prefix);
    }
    prefixesToURIs.put(prefix, namespace);
    return prefix;
}
 
Example 12
Source File: XMLStreamReaderImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
* @return the prefix of the current event, or null if the event does
* not have a prefix. For START_ELEMENT and END_ELEMENT, return
* XMLConstants.DEFAULT_NS_PREFIX when no prefix is available.
*/
public String getPrefix() {
    if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
        String prefix = fScanner.getElementQName().prefix;
        return prefix == null ? XMLConstants.DEFAULT_NS_PREFIX : prefix;
    }
    return null ;
}
 
Example 13
Source File: NamespaceResolver.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public String getNamespaceURI(String prefix) {
    if (prefix == null) {
        throw new IllegalArgumentException("No prefix provided!");
    } else if (namespaces.containsKey(prefix)) {
        return namespaces.get(prefix);
    } else {
        return XMLConstants.DEFAULT_NS_PREFIX;
    }
}
 
Example 14
Source File: XMLEventAllocatorImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected void fillNamespaceAttributes(StartElementEvent event,XMLStreamReader xmlr){
    int count = xmlr.getNamespaceCount();
    String uri = null;
    String prefix = null;
    NamespaceImpl attr = null;
    for(int i=0;i< count;i++){
        uri = xmlr.getNamespaceURI(i);
        prefix = xmlr.getNamespacePrefix(i);
        if(prefix == null){
            prefix = XMLConstants.DEFAULT_NS_PREFIX;
        }
        attr = new NamespaceImpl(prefix,uri);
        event.addNamespaceAttribute(attr);
    }
}
 
Example 15
Source File: XMLStreamReaderImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
* @return the prefix of the current event, or null if the event does
* not have a prefix. For START_ELEMENT and END_ELEMENT, return
* XMLConstants.DEFAULT_NS_PREFIX when no prefix is available.
*/
public String getPrefix() {
    if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
        String prefix = fScanner.getElementQName().prefix;
        return prefix == null ? XMLConstants.DEFAULT_NS_PREFIX : prefix;
    }
    return null ;
}
 
Example 16
Source File: QName.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p><code>QName</code> derived from parsing the formatted
 * <code>String</code>.</p>
 *
 * <p>If the <code>String</code> is <code>null</code> or does not conform to
 * {@link #toString() QName.toString()} formatting, an
 * <code>IllegalArgumentException</code> is thrown.</p>
 *
 * <p><em>The <code>String</code> <strong>MUST</strong> be in the
 * form returned by {@link #toString() QName.toString()}.</em></p>
 *
 * <p>The commonly accepted way of representing a <code>QName</code>
 * as a <code>String</code> was
 * <a href="http://jclark.com/xml/xmlns.htm">defined</a>
 * by James Clark.  Although this is not a <em>standard</em>
 * specification, it is in common use, e.g. {@link
 * javax.xml.transform.Transformer#setParameter(String name, Object value)}.
 * This implementation parses a <code>String</code> formatted
 * as: "{" + Namespace URI + "}" + local part.  If the Namespace
 * URI <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the
 * local part should be provided.</p>
 *
 * <p>The prefix value <strong><em>CANNOT</em></strong> be
 * represented in the <code>String</code> and will be set to
 * {@link javax.xml.XMLConstants#DEFAULT_NS_PREFIX
 * XMLConstants.DEFAULT_NS_PREFIX}.</p>
 *
 * <p>This method does not do full validation of the resulting
 * <code>QName</code>.
 * <p>The Namespace URI is not validated as a
 * <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.
 * The local part is not validated as a
 * <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
 * as specified in
 * <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a>.</p>
 *
 * @param qNameAsString <code>String</code> representation
 * of the <code>QName</code>
 *
 * @throws IllegalArgumentException When <code>qNameAsString</code> is
 *   <code>null</code> or malformed
 *
 * @return <code>QName</code> corresponding to the given <code>String</code>
 * @see #toString() QName.toString()
 */
public static QName valueOf(String qNameAsString) {

    // null is not valid
    if (qNameAsString == null) {
        throw new IllegalArgumentException(
                "cannot create QName from \"null\" or \"\" String");
    }

    // "" local part is valid to preserve compatible behavior with QName 1.0
    if (qNameAsString.length() == 0) {
        return new QName(
            XMLConstants.NULL_NS_URI,
            qNameAsString,
            XMLConstants.DEFAULT_NS_PREFIX);
    }

    // local part only?
    if (qNameAsString.charAt(0) != '{') {
        return new QName(
            XMLConstants.NULL_NS_URI,
            qNameAsString,
            XMLConstants.DEFAULT_NS_PREFIX);
    }

    // Namespace URI improperly specified?
    if (qNameAsString.startsWith("{" + XMLConstants.NULL_NS_URI + "}")) {
        throw new IllegalArgumentException(
            "Namespace URI .equals(XMLConstants.NULL_NS_URI), "
            + ".equals(\"" + XMLConstants.NULL_NS_URI + "\"), "
            + "only the local part, "
            + "\""
            + qNameAsString.substring(2 + XMLConstants.NULL_NS_URI.length())
            + "\", "
            + "should be provided.");
    }

    // Namespace URI and local part specified
    int endOfNamespaceURI = qNameAsString.indexOf('}');
    if (endOfNamespaceURI == -1) {
        throw new IllegalArgumentException(
            "cannot create QName from \""
                + qNameAsString
                + "\", missing closing \"}\"");
    }
    return new QName(
        qNameAsString.substring(1, endOfNamespaceURI),
        qNameAsString.substring(endOfNamespaceURI + 1),
        XMLConstants.DEFAULT_NS_PREFIX);
}
 
Example 17
Source File: NamespaceImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/** Creates a new instance of NamespaceImpl */
public NamespaceImpl(String namespaceURI) {
    super(XMLConstants.XMLNS_ATTRIBUTE,XMLConstants.XMLNS_ATTRIBUTE_NS_URI,XMLConstants.DEFAULT_NS_PREFIX,namespaceURI,null);
    init();
}
 
Example 18
Source File: XMLStreamWriterImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param prefix
 * @param localName
 * @param namespaceURI
 * @throws XMLStreamException
 */
public void writeStartElement(String prefix, String localName,
    String namespaceURI) throws XMLStreamException {
    try {
        if (localName == null) {
            throw new XMLStreamException("Local Name cannot be null");
        }

        if (namespaceURI == null) {
            throw new XMLStreamException("NamespaceURI cannot be null");
        }

        if (!fIsRepairingNamespace) {
            if (prefix == null) {
                throw new XMLStreamException("Prefix cannot be null");
            }
        }

        if (fStartTagOpened) {
            closeStartTag();
        }

        openStartTag();
        namespaceURI = fSymbolTable.addSymbol(namespaceURI);

        if (prefix != null) {
            prefix = fSymbolTable.addSymbol(prefix);
        }

        fElementStack.push(prefix, localName, null, namespaceURI, false);
        fInternalNamespaceContext.pushContext();

        String tmpPrefix = fNamespaceContext.getPrefix(namespaceURI);


        if ((prefix != null) &&
                ((tmpPrefix == null) || !prefix.equals(tmpPrefix))) {
            fInternalNamespaceContext.declarePrefix(prefix, namespaceURI);

        }

        if (fIsRepairingNamespace) {
            if ((prefix == null) ||
                    ((tmpPrefix != null) && prefix.equals(tmpPrefix))) {
                return;
            }

            QName qname = new QName();
            qname.setValues(prefix, XMLConstants.XMLNS_ATTRIBUTE, null,
                namespaceURI);
            fNamespaceDecls.add(qname);

            return;
        }

        if ((prefix != null) && (prefix != XMLConstants.DEFAULT_NS_PREFIX)) {
            fWriter.write(prefix);
            fWriter.write(":");
        }

        fWriter.write(localName);

    } catch (IOException ex) {
        throw new XMLStreamException(ex);
    }
}
 
Example 19
Source File: QName.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * <p><code>QName</code> constructor specifying the local part.</p>
 *
 * <p>If the local part is <code>null</code> an
 * <code>IllegalArgumentException</code> is thrown.
 * A local part of "" is allowed to preserve
 * compatible behavior with QName 1.0. </p>
 *
 * <p>When using this constructor, the Namespace URI is set to
 * {@link javax.xml.XMLConstants#NULL_NS_URI
 * XMLConstants.NULL_NS_URI} and the prefix is set to {@link
 * javax.xml.XMLConstants#DEFAULT_NS_PREFIX
 * XMLConstants.DEFAULT_NS_PREFIX}.</p>
 *
 * <p><em>In an XML context, all Element and Attribute names exist
 * in the context of a Namespace.  Making this explicit during the
 * construction of a <code>QName</code> helps prevent hard to
 * diagnosis XML validity errors.  The constructors {@link
 * #QName(String namespaceURI, String localPart) QName(String
 * namespaceURI, String localPart)} and
 * {@link #QName(String namespaceURI, String localPart, String prefix)}
 * are preferred.</em></p>
 *
 * <p>The local part is not validated as a
 * <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
 * as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces
 * in XML</a>.</p>
 *
 * @param localPart local part of the <code>QName</code>
 *
 * @throws IllegalArgumentException When <code>localPart</code> is
 *   <code>null</code>
 *
 * @see #QName(String namespaceURI, String localPart) QName(String
 * namespaceURI, String localPart)
 * @see #QName(String namespaceURI, String localPart, String
 * prefix) QName(String namespaceURI, String localPart, String
 * prefix)
 */
public QName(String localPart) {
    this(
        XMLConstants.NULL_NS_URI,
        localPart,
        XMLConstants.DEFAULT_NS_PREFIX);
}
 
Example 20
Source File: QName.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * <p><code>QName</code> constructor specifying the local part.</p>
 *
 * <p>If the local part is <code>null</code> an
 * <code>IllegalArgumentException</code> is thrown.
 * A local part of "" is allowed to preserve
 * compatible behavior with QName 1.0. </p>
 *
 * <p>When using this constructor, the Namespace URI is set to
 * {@link javax.xml.XMLConstants#NULL_NS_URI
 * XMLConstants.NULL_NS_URI} and the prefix is set to {@link
 * javax.xml.XMLConstants#DEFAULT_NS_PREFIX
 * XMLConstants.DEFAULT_NS_PREFIX}.</p>
 *
 * <p><em>In an XML context, all Element and Attribute names exist
 * in the context of a Namespace.  Making this explicit during the
 * construction of a <code>QName</code> helps prevent hard to
 * diagnosis XML validity errors.  The constructors {@link
 * #QName(String namespaceURI, String localPart) QName(String
 * namespaceURI, String localPart)} and
 * {@link #QName(String namespaceURI, String localPart, String prefix)}
 * are preferred.</em></p>
 *
 * <p>The local part is not validated as a
 * <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
 * as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces
 * in XML</a>.</p>
 *
 * @param localPart local part of the <code>QName</code>
 *
 * @throws IllegalArgumentException When <code>localPart</code> is
 *   <code>null</code>
 *
 * @see #QName(String namespaceURI, String localPart) QName(String
 * namespaceURI, String localPart)
 * @see #QName(String namespaceURI, String localPart, String
 * prefix) QName(String namespaceURI, String localPart, String
 * prefix)
 */
public QName(String localPart) {
    this(
        XMLConstants.NULL_NS_URI,
        localPart,
        XMLConstants.DEFAULT_NS_PREFIX);
}