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

The following examples show how to use javax.xml.stream.XMLStreamReader#getElementText() . 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: DocumentationXmlConverter.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    try {
        String textFormat = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_TEXT_FORMAT);
        if (StringUtils.isEmpty(textFormat)) {
            textFormat = "text/plain";
        }
        
        String documentation = xtr.getElementText();
        if (StringUtils.isNotEmpty(documentation)) {
            conversionHelper.getCurrentCmmnElement().setDocumentation(documentation);
            conversionHelper.getCurrentCmmnElement().setDocumentationTextFormat(textFormat);
        }
        
        return null;
        
    } catch (Exception e) {
        throw new CmmnXMLException("Error reading documentation element", e);
    }
}
 
Example 2
Source File: ActivitiMapExceptionParser.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Override
public void parseChildElement(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model) throws Exception {
  if (!(parentElement instanceof Activity))
    return;

  String errorCode = xtr.getAttributeValue(null, MAP_EXCEPTION_ERRORCODE);
  String andChildren = xtr.getAttributeValue(null, MAP_EXCEPTION_ANDCHILDREN);
  String exceptionClass = xtr.getElementText();
  boolean hasChildrenBool = false;

  if (StringUtils.isEmpty(andChildren) || andChildren.toLowerCase().equals("false")) {
    hasChildrenBool = false;
  } else if (andChildren.toLowerCase().equals("true")) {
    hasChildrenBool = true;
  } else {
    throw new XMLException("'" + andChildren + "' is not valid boolean in mapException with errorCode=" + errorCode + " and class=" + exceptionClass);
  }
  
  if (StringUtils.isEmpty(errorCode) || StringUtils.isEmpty(errorCode.trim())) {
    throw new XMLException("No errorCode defined mapException with errorCode=" + errorCode + " and class=" + exceptionClass);
  }

  ((Activity) parentElement).getMapExceptions().add(new MapExceptionEntry(errorCode, exceptionClass, hasChildrenBool));
}
 
Example 3
Source File: Xxe_stax.java    From openrasp-testcases with MIT License 6 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String data = req.getParameter("data");
    String tmp = "";
    if (data != null) {
        try {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(data));
            while (reader.hasNext()) {
                int event = reader.next();
                if (event == XMLStreamConstants.START_ELEMENT) {
                    if (reader.getName().toString().equals("foo")) {
                        tmp = reader.getElementText();
                        resp.getWriter().println(tmp);
                    }
                }
            }
        } catch (Exception e) {
            resp.getWriter().println(e);
        }
    }
}
 
Example 4
Source File: XmlErrorDocumentConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void handleMessage(final XMLStreamReader reader, final ODataErrorContext errorContext)
    throws XMLStreamException {
  String lang = reader.getAttributeValue(Edm.NAMESPACE_XML_1998, FormatXml.XML_LANG);
  errorContext.setLocale(getLocale(lang));
  String message = reader.getElementText();
  errorContext.setMessage(message);
}
 
Example 5
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void readStAXSource() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.nextTag();
	String s = streamReader.getLocalName();
	assertEquals("root", s);
	s = streamReader.getElementText();
	assertEquals("Hello World", s);
	streamReader.close();
}
 
Example 6
Source File: FilmlistenSuchen.java    From MLib with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse the server XML file.
 * @param parser
 * @param listeFilmlistenUrls
 * @param art
 */
private void parseServerEntry(XMLStreamReader parser, ListeFilmlistenUrls listeFilmlistenUrls, String art) {
    String serverUrl = "";
    String prio = "";
    int event;
    try {
        while (parser.hasNext()) {
            event = parser.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                //parsername = parser.getLocalName();
                switch (parser.getLocalName()) {
                    case "URL":
                        serverUrl = parser.getElementText();
                        break;
                    case "Prio":
                        prio = parser.getElementText();
                        break;
                }
            }
            if (event == XMLStreamConstants.END_ELEMENT) {
                //parsername = parser.getLocalName();
                if (parser.getLocalName().equals("Server")) {
                    if (!serverUrl.equals("")) {
                        //public DatenFilmUpdate(String url, String prio, String zeit, String datum, String anzahl) {
                        if (prio.equals("")) {
                            prio = DatenFilmlisteUrl.FILM_UPDATE_SERVER_PRIO_1;
                        }
                        listeFilmlistenUrls.addWithCheck(new DatenFilmlisteUrl(serverUrl, prio, art));
                    }
                    break;
                }
            }
        }
    } catch (XMLStreamException ignored) {
    }

}
 
Example 7
Source File: W3CAddressingWSDLParserExtension.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean bindingOperationElements(EditableWSDLBoundOperation operation, XMLStreamReader reader) {
    EditableWSDLBoundOperation edit = (EditableWSDLBoundOperation) operation;

    QName anon = reader.getName();
    if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
        try {
            String value = reader.getElementText();
            if (value == null || value.trim().equals("")) {
                throw new WebServiceException("Null values not permitted in wsaw:Anonymous.");
                // TODO: throw exception only if wsdl:required=true
                // TODO: is this the right exception ?
            } else if (value.equals("optional")) {
                edit.setAnonymous(ANONYMOUS.optional);
            } else if (value.equals("required")) {
                edit.setAnonymous(ANONYMOUS.required);
            } else if (value.equals("prohibited")) {
                edit.setAnonymous(ANONYMOUS.prohibited);
            } else {
                throw new WebServiceException("wsaw:Anonymous value \"" + value + "\" not understood.");
                // TODO: throw exception only if wsdl:required=true
                // TODO: is this the right exception ?
            }
        } catch (XMLStreamException e) {
            throw new WebServiceException(e);       // TODO: is this the correct behavior ?
        }

        return true;        // consumed the element
    }

    return false;
}
 
Example 8
Source File: SchemaReaderBase.java    From staedi with Apache License 2.0 5 votes vote down vote up
String getElementText(XMLStreamReader reader, String context) {
    try {
        return reader.getElementText();
    } catch (XMLStreamException xse) {
        throw schemaException("XMLStreamException reading element text: " + context, reader, xse);
    }
}
 
Example 9
Source File: W3CAddressingWSDLParserExtension.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean bindingOperationElements(EditableWSDLBoundOperation operation, XMLStreamReader reader) {
    EditableWSDLBoundOperation edit = (EditableWSDLBoundOperation) operation;

    QName anon = reader.getName();
    if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
        try {
            String value = reader.getElementText();
            if (value == null || value.trim().equals("")) {
                throw new WebServiceException("Null values not permitted in wsaw:Anonymous.");
                // TODO: throw exception only if wsdl:required=true
                // TODO: is this the right exception ?
            } else if (value.equals("optional")) {
                edit.setAnonymous(ANONYMOUS.optional);
            } else if (value.equals("required")) {
                edit.setAnonymous(ANONYMOUS.required);
            } else if (value.equals("prohibited")) {
                edit.setAnonymous(ANONYMOUS.prohibited);
            } else {
                throw new WebServiceException("wsaw:Anonymous value \"" + value + "\" not understood.");
                // TODO: throw exception only if wsdl:required=true
                // TODO: is this the right exception ?
            }
        } catch (XMLStreamException e) {
            throw new WebServiceException(e);       // TODO: is this the correct behavior ?
        }

        return true;        // consumed the element
    }

    return false;
}
 
Example 10
Source File: OutputValuesParser.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void parseChildElement(XMLStreamReader xtr, DmnElement parentElement, Decision decision) throws Exception {
    if (!(parentElement instanceof OutputClause))
        return;

    OutputClause clause = (OutputClause) parentElement;
    UnaryTests outputValues = new UnaryTests();

    boolean readyWithOutputValues = false;
    try {
        while (!readyWithOutputValues && xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && ELEMENT_TEXT.equalsIgnoreCase(xtr.getLocalName())) {
                String outputValuesText = xtr.getElementText();

                if (StringUtils.isNotEmpty(outputValuesText)) {
                    String[] outputValuesSplit = outputValuesText.replaceAll("^\"", "").split("\"?(,|$)(?=(([^\"]*\"){2})*[^\"]*$) *\"?");
                    outputValues.setTextValues(new ArrayList<>(Arrays.asList(outputValuesSplit)));
                }


            } else if (xtr.isEndElement() && getElementName().equalsIgnoreCase(xtr.getLocalName())) {
                readyWithOutputValues = true;
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Error parsing output values", e);
    }

    clause.setOutputValues(outputValues);
}
 
Example 11
Source File: XmlLinkConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private String readTag(final XMLStreamReader reader, final String namespaceURI, final String localName)
    throws XMLStreamException {
  reader.require(XMLStreamConstants.START_ELEMENT, namespaceURI, localName);
  final String result = reader.getElementText();
  reader.require(XMLStreamConstants.END_ELEMENT, namespaceURI, localName);

  return result;
}
 
Example 12
Source File: ActivitiFailedjobRetryParser.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public void parseChildElement(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model) throws Exception {
  if (!(parentElement instanceof Activity))
    return;
  String cycle = xtr.getElementText();
  if (cycle == null || cycle.isEmpty()) {
    return;
  }
  ((Activity) parentElement).setFailedJobRetryTimeCycleValue(cycle);
}
 
Example 13
Source File: W3CAddressingWSDLParserExtension.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean bindingOperationElements(EditableWSDLBoundOperation operation, XMLStreamReader reader) {
    EditableWSDLBoundOperation edit = (EditableWSDLBoundOperation) operation;

    QName anon = reader.getName();
    if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
        try {
            String value = reader.getElementText();
            if (value == null || value.trim().equals("")) {
                throw new WebServiceException("Null values not permitted in wsaw:Anonymous.");
                // TODO: throw exception only if wsdl:required=true
                // TODO: is this the right exception ?
            } else if (value.equals("optional")) {
                edit.setAnonymous(ANONYMOUS.optional);
            } else if (value.equals("required")) {
                edit.setAnonymous(ANONYMOUS.required);
            } else if (value.equals("prohibited")) {
                edit.setAnonymous(ANONYMOUS.prohibited);
            } else {
                throw new WebServiceException("wsaw:Anonymous value \"" + value + "\" not understood.");
                // TODO: throw exception only if wsdl:required=true
                // TODO: is this the right exception ?
            }
        } catch (XMLStreamException e) {
            throw new WebServiceException(e);       // TODO: is this the correct behavior ?
        }

        return true;        // consumed the element
    }

    return false;
}
 
Example 14
Source File: W3CAddressingWSDLParserExtension.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean bindingOperationElements(EditableWSDLBoundOperation operation, XMLStreamReader reader) {
    EditableWSDLBoundOperation edit = (EditableWSDLBoundOperation) operation;

    QName anon = reader.getName();
    if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
        try {
            String value = reader.getElementText();
            if (value == null || value.trim().equals("")) {
                throw new WebServiceException("Null values not permitted in wsaw:Anonymous.");
                // TODO: throw exception only if wsdl:required=true
                // TODO: is this the right exception ?
            } else if (value.equals("optional")) {
                edit.setAnonymous(ANONYMOUS.optional);
            } else if (value.equals("required")) {
                edit.setAnonymous(ANONYMOUS.required);
            } else if (value.equals("prohibited")) {
                edit.setAnonymous(ANONYMOUS.prohibited);
            } else {
                throw new WebServiceException("wsaw:Anonymous value \"" + value + "\" not understood.");
                // TODO: throw exception only if wsdl:required=true
                // TODO: is this the right exception ?
            }
        } catch (XMLStreamException e) {
            throw new WebServiceException(e);       // TODO: is this the correct behavior ?
        }

        return true;        // consumed the element
    }

    return false;
}
 
Example 15
Source File: XMLStreamReaderUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static String getElementText(XMLStreamReader reader) {
    try {
        return reader.getElementText();
    } catch (XMLStreamException e) {
        throw wrapException(e);
    }
}
 
Example 16
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 17
Source File: XhtmlCollectionFormat.java    From cosmo with Apache License 2.0 4 votes vote down vote up
public CollectionItem parse(String source, EntityFactory entityFactory) throws ParseException {
    CollectionItem collection = entityFactory.createCollection();

    try {
        if (source == null) {
            throw new ParseException("Source has no XML data", -1);
        }
        StringReader sr = new StringReader(source);
        XMLStreamReader reader = createXmlReader(sr);

        boolean inCollection = false;
        while (reader.hasNext()) {
            reader.next();
            if (!reader.isStartElement()) {
                continue;
            }

            if (hasClass(reader, "collection")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Found collection element");
                }
                inCollection = true;
                continue;
            }

            if (inCollection && hasClass(reader, "name")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Found name element");
                }

                String name = reader.getElementText();
                if (StringUtils.isBlank(name)) {
                    throw new ParseException("Empty name not allowed", reader.getLocation().getCharacterOffset());
                }
                collection.setDisplayName(name);

                continue;
            }

            if (inCollection && hasClass(reader, "uuid")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Found uuid element");
                }

                String uuid = reader.getElementText();
                if (StringUtils.isBlank(uuid)) {
                    throw new ParseException("Empty uuid not allowed", reader.getLocation().getCharacterOffset());
                }
                collection.setUid(uuid);

                continue;
            }
        }

        reader.close();
    } catch (XMLStreamException e) {
        handleXmlException("Error reading XML", e);
    }

    return collection;
}
 
Example 18
Source File: XmlErrorDocumentDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private void handleCode(final XMLStreamReader reader, final ODataErrorContext errorContext)
    throws XMLStreamException {
  String code = reader.getElementText();
  errorContext.setErrorCode(code);
}
 
Example 19
Source File: AtomServiceDocumentConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private TitleImpl parseTitle(final XMLStreamReader reader) throws XMLStreamException {
  reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_TITLE);
  String text = reader.getElementText();
  reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_TITLE);
  return new TitleImpl().setText(text);
}
 
Example 20
Source File: FileServiceApp.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
private void download() {
    String strData = null;
    try {
        final long size = Long.parseLong(getFileSize(fileName));
        XMLStreamReader reader = getXMLStreamFromInput(contactURL(
                generateURL("getFileRecords?fileName=" + URLEncoder.encode(fileName, DEF_CHAR_SET))));
        long count = 0;
        byte[] buff;
        int progValue = 0;
        while (!stop && reader.hasNext()) {
            if (reader.next() == XMLStreamReader.START_ELEMENT && reader.getName().getLocalPart()
                    .equals("record")) {
                strData = reader.getElementText();
                buff = Base64.decode(strData.getBytes(DEF_CHAR_SET));
                count += buff.length;
                final long c = count;
                this.out.write(buff);
                final int tmp = (int) ((c / (double) size) * 100);
                if (tmp > progValue) {
                    progValue = tmp;
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            progressBar.setValue(tmp);
                        }
                    });
                }
            }
        }
        if (this.downloadNotifyId != -1 && !stop) {
            final long id = this.downloadNotifyId;
            final String xtype = this.type;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    downloadFinished(id, fileName, xtype);
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
        showError(e.getMessage());
    }
}