Java Code Examples for javax.xml.stream.XMLStreamReader#getNamespacePrefix()

The following examples show how to use javax.xml.stream.XMLStreamReader#getNamespacePrefix() . 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: TestDefaultNamespacePrefix.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testDefaultNamespacePrefixAsNull() throws Exception
    {
        String XML = "<blah xmlns=\"http://blah.org\"><foo>foo</foo></blah>";
//        System.setProperty("com.ctc.wstx.returnNullForDefaultNamespace", "true");
        XMLInputFactory factory = getNewInputFactory();

        assertEquals(Boolean.FALSE, factory.getProperty(WstxInputProperties.P_RETURN_NULL_FOR_DEFAULT_NAMESPACE));
        
        factory.setProperty(WstxInputProperties.P_RETURN_NULL_FOR_DEFAULT_NAMESPACE, true);
        XMLStreamReader r = factory.createXMLStreamReader(new StringReader(XML));
        assertTokenType(START_ELEMENT, r.next());
        String prefix = r.getNamespacePrefix(0);
        if (prefix != null) {
            fail("Null value is not returned for the default namespace prefix while "
                    + WstxInputProperties.P_RETURN_NULL_FOR_DEFAULT_NAMESPACE + " is set true");
        }
    }
 
Example 2
Source File: StAXSchemaParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** Fills in the list of declared prefixes. */
private void fillDeclaredPrefixes(XMLStreamReader reader) {
    fDeclaredPrefixes.clear();
    final int len = reader.getNamespaceCount();
    for (int i = 0; i < len; ++i) {
        String prefix = reader.getNamespacePrefix(i);
        fDeclaredPrefixes.add(prefix != null ? prefix : "");
    }
}
 
Example 3
Source File: XmlFeedConsumer.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param reader
 * @return 
 * @throws EntityProviderException
 */
private Map<String, String> extractNamespacesFromTag(final XMLStreamReader reader) throws EntityProviderException {
  // collect namespaces
  Map<String, String> foundPrefix2NamespaceUri = new HashMap<String, String>();
  int namespaceCount = reader.getNamespaceCount();
  for (int i = 0; i < namespaceCount; i++) {
    String namespacePrefix = reader.getNamespacePrefix(i);
    String namespaceUri = reader.getNamespaceURI(i);

    foundPrefix2NamespaceUri.put(namespacePrefix, namespaceUri);
  }
  return foundPrefix2NamespaceUri;
}
 
Example 4
Source File: XMLStreamReaderUtil.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public AttributesImpl(XMLStreamReader reader) {
    if (reader == null) {

        // this is the case when we call getAttributes() on the
        // reader when it is not on a start tag
        atInfos = new AttributeInfo[0];
    } else {

        // this is the normal case
        int index = 0;
        int namespaceCount = reader.getNamespaceCount();
        int attributeCount = reader.getAttributeCount();
        atInfos = new AttributeInfo[namespaceCount + attributeCount];
        for (int i=0; i<namespaceCount; i++) {
            String namespacePrefix = reader.getNamespacePrefix(i);

            // will be null if default prefix. QName can't take null
            if (namespacePrefix == null) {
                namespacePrefix = "";
            }
            atInfos[index++] = new AttributeInfo(
                new QName(XMLNS_NAMESPACE_URI,
                    namespacePrefix,
                    "xmlns"),
                reader.getNamespaceURI(i));
        }
        for (int i=0; i<attributeCount; i++) {
            atInfos[index++] = new AttributeInfo(
                reader.getAttributeName(i),
                reader.getAttributeValue(i));
        }
    }
}
 
Example 5
Source File: XmlEntryConsumer.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
private void extractNamespacesFromTag(final XMLStreamReader reader) throws EntityProviderException {
  // collect namespaces
  int namespaceCount = reader.getNamespaceCount();
  for (int i = 0; i < namespaceCount; i++) {
    String namespacePrefix = reader.getNamespacePrefix(i);
    String namespaceUri = reader.getNamespaceURI(i);

    foundPrefix2NamespaceUri.put(namespacePrefix, namespaceUri);
  }
}
 
Example 6
Source File: XmlMetadataDeserializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void extractNamespaces(final XMLStreamReader reader) throws EntityProviderException {
  int namespaceCount = reader.getNamespaceCount();
  for (int i = 0; i < namespaceCount; i++) {
    String namespacePrefix = reader.getNamespacePrefix(i);
    String namespaceUri = reader.getNamespaceURI(i);
    if (namespacePrefix == null || isDefaultNamespace(namespacePrefix)) {
      namespacePrefix = Edm.PREFIX_EDM;
    }
    //Ignoring the duplicate tags, parent tag namespace takes precedence
    if (!xmlNamespaceMap.containsKey(namespacePrefix)) {
      xmlNamespaceMap.put(namespacePrefix, namespaceUri);
    }
  }
}
 
Example 7
Source File: DefinitionsXmlConverter.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    CmmnModel model = conversionHelper.getCmmnModel();
    model.setId(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_ID));
    model.setName(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_NAME));
    model.setExpressionLanguage(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_EXPRESSION_LANGUAGE));
    model.setExporter(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_EXPORTER));
    model.setExporterVersion(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_EXPORTER_VERSION));
    model.setAuthor(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_AUTHOR));
    model.setTargetNamespace(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_TARGET_NAMESPACE));
    
    String creationDateString = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_CREATION_DATE);
    if (StringUtils.isNotEmpty(creationDateString)) {
        try {
            Date creationDate = new SimpleDateFormat(XSD_DATETIME_FORMAT).parse(creationDateString);
            model.setCreationDate(creationDate);
        } catch (ParseException e) {
            LOGGER.warn("Ignoring creationDate attribute: invalid date format", e);
        }
    }

    for (int i = 0; i < xtr.getNamespaceCount(); i++) {
        String prefix = xtr.getNamespacePrefix(i);
        if (StringUtils.isNotEmpty(prefix)) {
            model.addNamespace(prefix, xtr.getNamespaceURI(i));
        }
    }
    
    return null;
}
 
Example 8
Source File: XMLStreamReaderUtil.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public AttributesImpl(XMLStreamReader reader) {
    if (reader == null) {

        // this is the case when we call getAttributes() on the
        // reader when it is not on a start tag
        atInfos = new AttributeInfo[0];
    } else {

        // this is the normal case
        int index = 0;
        int namespaceCount = reader.getNamespaceCount();
        int attributeCount = reader.getAttributeCount();
        atInfos = new AttributeInfo[namespaceCount + attributeCount];
        for (int i=0; i<namespaceCount; i++) {
            String namespacePrefix = reader.getNamespacePrefix(i);

            // will be null if default prefix. QName can't take null
            if (namespacePrefix == null) {
                namespacePrefix = "";
            }
            atInfos[index++] = new AttributeInfo(
                new QName(XMLNS_NAMESPACE_URI,
                    namespacePrefix,
                    "xmlns"),
                reader.getNamespaceURI(i));
        }
        for (int i=0; i<attributeCount; i++) {
            atInfos[index++] = new AttributeInfo(
                reader.getAttributeName(i),
                reader.getAttributeValue(i));
        }
    }
}
 
Example 9
Source File: StAXSchemaParser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Fills in the list of declared prefixes. */
private void fillDeclaredPrefixes(XMLStreamReader reader) {
    fDeclaredPrefixes.clear();
    final int len = reader.getNamespaceCount();
    for (int i = 0; i < len; ++i) {
        String prefix = reader.getNamespacePrefix(i);
        fDeclaredPrefixes.add(prefix != null ? prefix : "");
    }
}
 
Example 10
Source File: XMLStreamReaderUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public AttributesImpl(XMLStreamReader reader) {
    if (reader == null) {

        // this is the case when we call getAttributes() on the
        // reader when it is not on a start tag
        atInfos = new AttributeInfo[0];
    } else {

        // this is the normal case
        int index = 0;
        int namespaceCount = reader.getNamespaceCount();
        int attributeCount = reader.getAttributeCount();
        atInfos = new AttributeInfo[namespaceCount + attributeCount];
        for (int i=0; i<namespaceCount; i++) {
            String namespacePrefix = reader.getNamespacePrefix(i);

            // will be null if default prefix. QName can't take null
            if (namespacePrefix == null) {
                namespacePrefix = "";
            }
            atInfos[index++] = new AttributeInfo(
                new QName(XMLNS_NAMESPACE_URI,
                    namespacePrefix,
                    "xmlns"),
                reader.getNamespaceURI(i));
        }
        for (int i=0; i<attributeCount; i++) {
            atInfos[index++] = new AttributeInfo(
                reader.getAttributeName(i),
                reader.getAttributeValue(i));
        }
    }
}
 
Example 11
Source File: XmlFeedDeserializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Maps all all found namespaces of current xml tag into a map.
 * 
 * @param reader xml reader with current position at a xml tag
 * @return map with all found namespaces of current xml tag
 */
private Map<String, String> extractNamespacesFromTag(final XMLStreamReader reader) {
  // collect namespaces
  Map<String, String> foundPrefix2NamespaceUri = new HashMap<String, String>();
  int namespaceCount = reader.getNamespaceCount();
  for (int i = 0; i < namespaceCount; i++) {
    String namespacePrefix = reader.getNamespacePrefix(i);
    String namespaceUri = reader.getNamespaceURI(i);

    foundPrefix2NamespaceUri.put(namespacePrefix, namespaceUri);
  }
  return foundPrefix2NamespaceUri;
}
 
Example 12
Source File: StAXSchemaParser.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** Fills in the list of declared prefixes. */
private void fillDeclaredPrefixes(XMLStreamReader reader) {
    fDeclaredPrefixes.clear();
    final int len = reader.getNamespaceCount();
    for (int i = 0; i < len; ++i) {
        String prefix = reader.getNamespacePrefix(i);
        fDeclaredPrefixes.add(prefix != null ? prefix : "");
    }
}
 
Example 13
Source File: XMLStreamReaderUtil.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public AttributesImpl(XMLStreamReader reader) {
    if (reader == null) {

        // this is the case when we call getAttributes() on the
        // reader when it is not on a start tag
        atInfos = new AttributeInfo[0];
    } else {

        // this is the normal case
        int index = 0;
        int namespaceCount = reader.getNamespaceCount();
        int attributeCount = reader.getAttributeCount();
        atInfos = new AttributeInfo[namespaceCount + attributeCount];
        for (int i=0; i<namespaceCount; i++) {
            String namespacePrefix = reader.getNamespacePrefix(i);

            // will be null if default prefix. QName can't take null
            if (namespacePrefix == null) {
                namespacePrefix = "";
            }
            atInfos[index++] = new AttributeInfo(
                new QName(XMLNS_NAMESPACE_URI,
                    namespacePrefix,
                    "xmlns"),
                reader.getNamespaceURI(i));
        }
        for (int i=0; i<attributeCount; i++) {
            atInfos[index++] = new AttributeInfo(
                reader.getAttributeName(i),
                reader.getAttributeValue(i));
        }
    }
}
 
Example 14
Source File: StAXSchemaParser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** Fills in the list of declared prefixes. */
private void fillDeclaredPrefixes(XMLStreamReader reader) {
    fDeclaredPrefixes.clear();
    final int len = reader.getNamespaceCount();
    for (int i = 0; i < len; ++i) {
        String prefix = reader.getNamespacePrefix(i);
        fDeclaredPrefixes.add(prefix != null ? prefix : "");
    }
}
 
Example 15
Source File: StAXSchemaParser.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/** Fills in the list of declared prefixes. */
private void fillDeclaredPrefixes(XMLStreamReader reader) {
    fDeclaredPrefixes.clear();
    final int len = reader.getNamespaceCount();
    for (int i = 0; i < len; ++i) {
        String prefix = reader.getNamespacePrefix(i);
        fDeclaredPrefixes.add(prefix != null ? prefix : "");
    }
}
 
Example 16
Source File: TransformTestUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static void verifyNamespaceDeclarations(XMLStreamReader teacher, XMLStreamReader reader) {
    int dcount = teacher.getNamespaceCount();
    for (int i = 0; i < dcount; i++) {
        String p = teacher.getNamespacePrefix(i);
        Assert.assertEquals("nsdecl prefix " + p + " is incorrectly bound.",
                            teacher.getNamespaceURI(i), reader.getNamespaceURI(p));
    }
}
 
Example 17
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void extractNamespaces(final XMLStreamReader reader) throws EntityProviderException {
  int namespaceCount = reader.getNamespaceCount();
  for (int i = 0; i < namespaceCount; i++) {
    String namespacePrefix = reader.getNamespacePrefix(i);
    String namespaceUri = reader.getNamespaceURI(i);
    if (namespacePrefix == null || isDefaultNamespace(namespacePrefix)) {
      namespacePrefix = Edm.PREFIX_EDM;
    }
    //Ignoring the duplicate tags, parent tag namespace takes precedence
    if (!xmlNamespaceMap.containsKey(namespacePrefix)) {
      xmlNamespaceMap.put(namespacePrefix, namespaceUri);
    }
  }
}
 
Example 18
Source File: XMLTree.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/** Should be invoked on ELEMENT_START event */
private void putNamespaces(XMLStreamReader reader) {
  for (int i = 0; i < reader.getNamespaceCount(); i++) {
    final String prefix = reader.getNamespacePrefix(i);
    if (prefix != null) {
      putNamespace(prefix, reader.getNamespaceURI(i));
    }
  }
}
 
Example 19
Source File: XMLStreamReaderUtil.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public AttributesImpl(XMLStreamReader reader) {
    if (reader == null) {

        // this is the case when we call getAttributes() on the
        // reader when it is not on a start tag
        atInfos = new AttributeInfo[0];
    } else {

        // this is the normal case
        int index = 0;
        int namespaceCount = reader.getNamespaceCount();
        int attributeCount = reader.getAttributeCount();
        atInfos = new AttributeInfo[namespaceCount + attributeCount];
        for (int i=0; i<namespaceCount; i++) {
            String namespacePrefix = reader.getNamespacePrefix(i);

            // will be null if default prefix. QName can't take null
            if (namespacePrefix == null) {
                namespacePrefix = "";
            }
            atInfos[index++] = new AttributeInfo(
                new QName(XMLNS_NAMESPACE_URI,
                    namespacePrefix,
                    "xmlns"),
                reader.getNamespaceURI(i));
        }
        for (int i=0; i<attributeCount; i++) {
            atInfos[index++] = new AttributeInfo(
                reader.getAttributeName(i),
                reader.getAttributeValue(i));
        }
    }
}
 
Example 20
Source File: StAXEncoder.java    From exificient with MIT License 4 votes vote down vote up
public void encode(XMLStreamReader xmlStream) throws XMLStreamException,
		EXIException, IOException {

	// StartDocument should be initial state
	assert (xmlStream.getEventType() == XMLStreamConstants.START_DOCUMENT);

	writeStartDocument();

	while (xmlStream.hasNext()) {
		int event = xmlStream.next();
		switch (event) {
		case XMLStreamConstants.START_DOCUMENT:
			// should have happened beforehand
			throw new EXIException("Unexpected START_DOCUMENT event");
		case XMLStreamConstants.END_DOCUMENT:
			this.writeEndDocument();
			break;
		case XMLStreamConstants.START_ELEMENT:
			QName qn = xmlStream.getName();
			String pfx = qn.getPrefix();
			writeStartElement(pfx, qn.getLocalPart(), qn.getNamespaceURI());
			// parse NS declarations
			int nsCnt = xmlStream.getNamespaceCount();
			for (int i = 0; i < nsCnt; i++) {
				String nsPfx = xmlStream.getNamespacePrefix(i);
				nsPfx = nsPfx == null ? Constants.XML_DEFAULT_NS_PREFIX
						: nsPfx;
				String nsUri = xmlStream.getNamespaceURI(i);
				this.writeNamespace(nsPfx, nsUri);
			}
			// parse attributes
			int atCnt = xmlStream.getAttributeCount();
			for (int i = 0; i < atCnt; i++) {
				QName atQname = xmlStream.getAttributeName(i);
				this.writeAttribute(atQname.getPrefix(),
						atQname.getNamespaceURI(), atQname.getLocalPart(),
						xmlStream.getAttributeValue(i));
			}

			break;
		case XMLStreamConstants.END_ELEMENT:
			writeEndElement();
			break;
		case XMLStreamConstants.NAMESPACE:
			break;
		case XMLStreamConstants.CHARACTERS:
			this.writeCharacters(xmlStream.getTextCharacters(),
					xmlStream.getTextStart(), xmlStream.getTextLength());
			break;
		case XMLStreamConstants.SPACE:
			// @SuppressWarnings("unused")
			String ignorableSpace = xmlStream.getText();
			writeCharacters(ignorableSpace);
			break;
		case XMLStreamConstants.ATTRIBUTE:
			// @SuppressWarnings("unused")
			// int attsX = xmlStream.getAttributeCount();
			break;
		case XMLStreamConstants.PROCESSING_INSTRUCTION:
			this.writeProcessingInstruction(xmlStream.getPITarget(),
					xmlStream.getPIData());
			break;
		case XMLStreamConstants.COMMENT:
			this.writeComment(xmlStream.getText());
			break;
		case XMLStreamConstants.DTD:
			// TODO DTD
			break;
		case XMLStreamConstants.ENTITY_REFERENCE:
			// TODO ER
			break;
		default:
			System.out.println("Event '" + event + "' not supported!");
		}
	}

	// this.flush();
}