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

The following examples show how to use javax.xml.stream.XMLStreamReader#getNamespaceContext() . 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: Bug6472982Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNamespaceContext() {
    try {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes());
        XMLStreamReader sr = xif.createXMLStreamReader(is);
        NamespaceContext context = sr.getNamespaceContext();
        Assert.assertTrue(context.getPrefix("") == null);

    } catch (IllegalArgumentException iae) {
        Assert.fail("NamespacePrefix#getPrefix() should not throw an IllegalArgumentException for empty uri. ");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 2
Source File: NamespaceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNamespaceContext() {
    try {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes());
        XMLStreamReader sr = xif.createXMLStreamReader(is);
        while (sr.hasNext()) {
            int eventType = sr.next();
            if (eventType == XMLStreamConstants.START_ELEMENT) {
                if (sr.getLocalName().equals(childElement)) {
                    NamespaceContext context = sr.getNamespaceContext();
                    Assert.assertTrue(context.getPrefix(namespaceURI).equals(prefix));
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 3
Source File: Bug6481678.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNamespaceContext() {
    is = new java.io.ByteArrayInputStream(getXML().getBytes());
    try {
        XMLStreamReader sr = factory.createFilteredReader(factory.createXMLStreamReader(is), (StreamFilter) filter);
        while (sr.hasNext()) {
            int eventType = sr.next();
            if (eventType == XMLStreamConstants.START_ELEMENT) {
                if (sr.getLocalName().equals(childElement)) {
                    NamespaceContext context = sr.getNamespaceContext();
                    Assert.assertTrue(context.getPrefix(namespaceURIApple).equals(prefixApple));
                }
            }
        }
    } catch (Exception ex) {
        Assert.fail("Exception: " + ex.getMessage());
    }
}
 
Example 4
Source File: InTransformReader.java    From cxf with Apache License 2.0 6 votes vote down vote up
public InTransformReader(XMLStreamReader reader,
                         Map<String, String> inEMap,
                         Map<String, String> appendMap,
                         List<String> dropESet,
                         Map<String, String> inAMap,
                         boolean blockOriginalReader) {
    super(reader);
    inElementsMap = new QNamesMap(inEMap == null ? 0 : inEMap.size());
    inAttributesMap = new QNamesMap(inAMap == null ? 0 : inAMap.size());
    this.blockOriginalReader = blockOriginalReader;
    TransformUtils.convertToQNamesMap(inEMap, inElementsMap, nsMap);
    TransformUtils.convertToQNamesMap(inAMap, inAttributesMap, null);

    TransformUtils.convertToMapOfElementProperties(appendMap, inAppendMap);
    TransformUtils.convertToSetOfQNames(dropESet, inDropSet);
    namespaceContext = new DelegatingNamespaceContext(
        reader.getNamespaceContext(), nsMap);
}
 
Example 5
Source File: StaxSerializer.java    From cxf with Apache License 2.0 5 votes vote down vote up
private boolean addNamespaces(XMLStreamReader reader, Node ctx) {
    try {
        NamespaceContext nsctx = reader.getNamespaceContext();
        if (nsctx instanceof com.ctc.wstx.sr.InputElementStack) {
            com.ctc.wstx.sr.InputElementStack ies = (com.ctc.wstx.sr.InputElementStack)nsctx;
            com.ctc.wstx.util.InternCache ic = com.ctc.wstx.util.InternCache.getInstance();

            Map<String, String> storedNamespaces = new HashMap<>();
            Node wk = ctx;
            while (wk != null) {
                NamedNodeMap atts = wk.getAttributes();
                if (atts != null) {
                    for (int i = 0; i < atts.getLength(); ++i) {
                        Node att = atts.item(i);
                        String nodeName = att.getNodeName();
                        if (("xmlns".equals(nodeName) || nodeName.startsWith("xmlns:"))
                            && !storedNamespaces.containsKey(att.getNodeName())) {

                            String prefix = att.getLocalName();
                            if ("xmlns".equals(prefix)) {
                                prefix = "";
                            }
                            prefix = ic.intern(prefix);
                            ies.addNsBinding(prefix, att.getNodeValue());
                            storedNamespaces.put(nodeName, att.getNodeValue());
                        }
                    }
                }
                wk = wk.getParentNode();
            }
        }
        return true;
    } catch (Throwable t) {
        //ignore, not much we can do but hope the decrypted XML is stand alone ok
    }
    return false;
}
 
Example 6
Source File: StaEDIXMLStreamReaderTest.java    From staedi with Apache License 2.0 4 votes vote down vote up
@Test
void testNamespaces() throws Exception {
    XMLStreamReader xmlReader = getXmlReader(TINY_X12);

    assertEquals(XMLStreamConstants.START_DOCUMENT, xmlReader.getEventType());
    assertNull(xmlReader.getNamespaceURI());
    assertThrows(IllegalStateException.class, () -> xmlReader.getName());

    assertEquals(XMLStreamConstants.START_ELEMENT, xmlReader.next());
    assertEquals(EDINamespaces.LOOPS, xmlReader.getNamespaceURI("l"));
    assertEquals("INTERCHANGE", xmlReader.getLocalName());
    assertEquals(EDINamespaces.LOOPS, xmlReader.getName().getNamespaceURI());
    assertEquals("l", xmlReader.getName().getPrefix());
    assertEquals(4, xmlReader.getNamespaceCount());
    assertEquals(EDINamespaces.LOOPS, xmlReader.getNamespaceURI());

    assertSegmentBoundaries(xmlReader, "ISA", 16);

    NamespaceContext context = xmlReader.getNamespaceContext();
    assertEquals("s", context.getPrefix(EDINamespaces.SEGMENTS));
    assertEquals("s", context.getPrefixes(EDINamespaces.SEGMENTS).next());
    assertEquals(EDINamespaces.SEGMENTS, context.getNamespaceURI("s"));
    assertNull(context.getNamespaceURI("x"));
    assertEquals(EDINamespaces.SEGMENTS, xmlReader.getNamespaceURI());

    assertEquals(XMLStreamConstants.START_ELEMENT, xmlReader.next());
    assertEquals("IEA", xmlReader.getLocalName());
    assertEquals(EDINamespaces.SEGMENTS, xmlReader.getNamespaceURI());

    // No namespaces declared on the segment
    assertEquals(0, xmlReader.getNamespaceCount());
    assertNull(xmlReader.getNamespacePrefix(0));
    assertNull(xmlReader.getNamespaceURI(0));

    assertElement(xmlReader, "IEA01", "1");
    assertEquals(EDINamespaces.ELEMENTS, xmlReader.getNamespaceURI());

    // No namespaces declared on the element
    assertEquals(0, xmlReader.getNamespaceCount());
    assertNull(xmlReader.getNamespacePrefix(0));
    assertNull(xmlReader.getNamespaceURI(0));

    assertElement(xmlReader, "IEA02", "508121953");
    assertEquals(EDINamespaces.ELEMENTS, xmlReader.getNamespaceURI());

    assertEquals(XMLStreamConstants.END_ELEMENT, xmlReader.next());
    assertEquals("IEA", xmlReader.getLocalName());
    assertEquals(EDINamespaces.SEGMENTS, xmlReader.getNamespaceURI());

    assertEquals(XMLStreamConstants.END_ELEMENT, xmlReader.next());
    assertEquals("INTERCHANGE", xmlReader.getLocalName());
    assertEquals(EDINamespaces.LOOPS, xmlReader.getName().getNamespaceURI());
    assertEquals("l", xmlReader.getName().getPrefix());
    assertEquals(4, xmlReader.getNamespaceCount());
    assertEquals(EDINamespaces.LOOPS, xmlReader.getNamespaceURI());

    assertEquals(XMLStreamConstants.END_DOCUMENT, xmlReader.next());
    assertNull(xmlReader.getNamespaceURI("l"));
}
 
Example 7
Source File: XmlEntryConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private void readCustomElement(final XMLStreamReader reader, final String tagName, final EntityInfoAggregator eia,
    final EntityProviderReadProperties readProperties)
    throws EdmException, EntityProviderException, XMLStreamException {
  EntityPropertyInfo targetPathInfo = eia.getTargetPathInfo(tagName);
  NamespaceContext nsctx = reader.getNamespaceContext();

  boolean skipTag = true;
  if (!Edm.NAMESPACE_ATOM_2005.equals(reader.getName().getNamespaceURI())) {

    if (targetPathInfo != null) {
      final String customPrefix = targetPathInfo.getCustomMapping().getFcNsPrefix();
      final String customNamespaceURI = targetPathInfo.getCustomMapping().getFcNsUri();

      if (customPrefix != null && customNamespaceURI != null) {
        String xmlPrefix = nsctx.getPrefix(customNamespaceURI);
        String xmlNamespaceUri = reader.getNamespaceURI(customPrefix);

        if (customNamespaceURI.equals(xmlNamespaceUri) && customPrefix.equals(xmlPrefix)) {
          skipTag = false;
          reader.require(XMLStreamConstants.START_ELEMENT, customNamespaceURI, tagName);
          final String text = reader.getElementText();
          reader.require(XMLStreamConstants.END_ELEMENT, customNamespaceURI, tagName);

          final EntityPropertyInfo propertyInfo = getValidatedPropertyInfo(eia, tagName);
          final Class<?> typeMapping = typeMappings.getMappingClass(propertyInfo.getName());
          final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
          final Object value = type.valueOfString(text, EdmLiteralKind.DEFAULT,
              readProperties == null || readProperties.isValidatingFacets() ? propertyInfo.getFacets() : null,
              typeMapping == null ? type.getDefaultType() : typeMapping);
          properties.put(tagName, value);
        }
      }
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(tagName));
    }
  }

  if (skipTag) {
    skipStartedTag(reader);
  }
}
 
Example 8
Source File: XmlEntryDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private void readCustomElement(final XMLStreamReader reader, final String tagName, //NOSONAR
    final EntityInfoAggregator eia,
    final DeserializerProperties readProperties)
    throws EdmException, EntityProviderException, XMLStreamException { //NOSONAR
  EntityPropertyInfo targetPathInfo = eia.getTargetPathInfo(tagName);
  NamespaceContext nsctx = reader.getNamespaceContext();

  boolean skipTag = true;
  if (!Edm.NAMESPACE_ATOM_2005.equals(reader.getName().getNamespaceURI())) {

    if (targetPathInfo != null) {
      final String customPrefix = targetPathInfo.getCustomMapping().getFcNsPrefix();
      final String customNamespaceURI = targetPathInfo.getCustomMapping().getFcNsUri();

      if (customPrefix != null && customNamespaceURI != null) {
        String xmlPrefix = nsctx.getPrefix(customNamespaceURI);
        String xmlNamespaceUri = reader.getNamespaceURI(customPrefix);

        if (customNamespaceURI.equals(xmlNamespaceUri) && customPrefix.equals(xmlPrefix)) { //NOSONAR
          skipTag = false;
          reader.require(XMLStreamConstants.START_ELEMENT, customNamespaceURI, tagName);
          final String text = reader.getElementText();
          reader.require(XMLStreamConstants.END_ELEMENT, customNamespaceURI, tagName);

          final EntityPropertyInfo propertyInfo = getValidatedPropertyInfo(eia, tagName);
          final Class<?> typeMapping = typeMappings.getMappingClass(propertyInfo.getName());
          final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
          final Object value = type.valueOfString(text, EdmLiteralKind.DEFAULT,
              readProperties == null || readProperties.isValidatingFacets() ? propertyInfo.getFacets() : null,
              typeMapping == null ? type.getDefaultType() : typeMapping);
          properties.put(tagName, value);
        }
      }
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(tagName));
    }
  }

  if (skipTag) {
    skipStartedTag(reader);
  }
}
 
Example 9
Source File: XmlEntryConsumer.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
private void readCustomElement(final XMLStreamReader reader, final String tagName, final EntityInfoAggregator eia) throws EdmException, EntityProviderException, XMLStreamException {
  EntityPropertyInfo targetPathInfo = eia.getTargetPathInfo(tagName);
  NamespaceContext nsctx = reader.getNamespaceContext();

  boolean skipTag = true;
  if (!Edm.NAMESPACE_ATOM_2005.equals(reader.getName().getNamespaceURI())) {

    if (targetPathInfo != null) {
      final String customPrefix = targetPathInfo.getCustomMapping().getFcNsPrefix();
      final String customNamespaceURI = targetPathInfo.getCustomMapping().getFcNsUri();

      if (customPrefix != null && customNamespaceURI != null) {
        String xmlPrefix = nsctx.getPrefix(customNamespaceURI);
        String xmlNamespaceUri = reader.getNamespaceURI(customPrefix);

        if (customNamespaceURI.equals(xmlNamespaceUri) && customPrefix.equals(xmlPrefix)) {
          skipTag = false;
          reader.require(XMLStreamConstants.START_ELEMENT, customNamespaceURI, tagName);
          reader.next();
          reader.require(XMLStreamConstants.CHARACTERS, null, null);
          final String text = reader.getText();
          reader.nextTag();
          reader.require(XMLStreamConstants.END_ELEMENT, customNamespaceURI, tagName);

          final EntityPropertyInfo propertyInfo = getValidatedPropertyInfo(eia, tagName);
          final Class<?> typeMapping = typeMappings.getMappingClass(propertyInfo.getName());
          final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
          final Object value = type.valueOfString(text, EdmLiteralKind.DEFAULT, propertyInfo.getFacets(),
              typeMapping == null ? type.getDefaultType() : typeMapping);
          properties.put(tagName, value);
        }
      }
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(tagName));
    }
  }

  if (skipTag) {
    skipStartedTag(reader);
  }
}