Example usage for javax.xml.stream XMLStreamReader getElementText

List of usage examples for javax.xml.stream XMLStreamReader getElementText

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getElementText.

Prototype

public String getElementText() throws XMLStreamException;

Source Link

Document

Reads the content of a text-only element, an exception is thrown if this is not a text-only element.

Usage

From source file:org.flowable.bpmn.converter.child.TimeCycleParser.java

public void parseChildElement(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model)
        throws Exception {
    if (parentElement instanceof TimerEventDefinition == false)
        return;/*from  w w  w  .  j  a v a2  s.  c  om*/

    TimerEventDefinition eventDefinition = (TimerEventDefinition) parentElement;

    if (StringUtils.isNotEmpty(BpmnXMLUtil.getAttributeValue(ATTRIBUTE_END_DATE, xtr))) {
        eventDefinition.setEndDate(BpmnXMLUtil.getAttributeValue(ATTRIBUTE_END_DATE, xtr));
    }
    eventDefinition.setTimeCycle(xtr.getElementText());
}

From source file:org.flowable.cmmn.converter.DocumentationXmlConverter.java

@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    try {//from ww  w.  j a va 2s .c  o m
        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);
    }
}

From source file:org.flowable.cmmn.converter.ExtensionElementsXMLConverter.java

protected void readFieldExtension(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    BaseElement cmmnElement = conversionHelper.getCurrentCmmnElement();

    FieldExtension extension = new FieldExtension();
    extension.setFieldName(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_NAME));

    String stringValueAttribute = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_FIELD_STRING);
    String expressionAttribute = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_FIELD_EXPRESSION);
    if (StringUtils.isNotEmpty(stringValueAttribute)) {
        extension.setStringValue(stringValueAttribute);

    } else if (StringUtils.isNotEmpty(expressionAttribute)) {
        extension.setExpression(expressionAttribute);

    } else {//ww  w .  ja  va 2  s .  co m
        boolean readyWithFieldExtension = false;
        try {
            while (!readyWithFieldExtension && xtr.hasNext()) {
                xtr.next();
                if (xtr.isStartElement()
                        && CmmnXmlConstants.ELEMENT_FIELD_STRING.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setStringValue(xtr.getElementText().trim());

                } else if (xtr.isStartElement()
                        && CmmnXmlConstants.ELEMENT_FIELD_EXPRESSION.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setExpression(xtr.getElementText().trim());

                } else if (xtr.isEndElement()
                        && CmmnXmlConstants.ELEMENT_FIELD.equalsIgnoreCase(xtr.getLocalName())) {
                    readyWithFieldExtension = true;
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Error parsing field extension child elements", e);
        }
    }

    CmmnElement currentCmmnElement = conversionHelper.getCurrentCmmnElement();
    if (currentCmmnElement instanceof ServiceTask) {
        ((ServiceTask) currentCmmnElement).getFieldExtensions().add(extension);

    } else if (currentCmmnElement instanceof DecisionTask) {
        ((DecisionTask) currentCmmnElement).getFieldExtensions().add(extension);
    } else if (currentCmmnElement instanceof FlowableListener) {
        ((FlowableListener) currentCmmnElement).getFieldExtensions().add(extension);

    } else {
        throw new FlowableException("Programmatic error: field added to unkown element " + currentCmmnElement);

    }
}

From source file:org.flowable.cmmn.converter.FieldExtensionXmlConverter.java

@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    CmmnElement cmmnElement = conversionHelper.getCurrentCmmnElement();
    if (!(cmmnElement instanceof ServiceTask || cmmnElement instanceof DecisionTask)) {
        return null;
    }/*w  ww .j  a v  a2 s  . c  om*/

    TaskWithFieldExtensions serviceTask = (TaskWithFieldExtensions) cmmnElement;

    FieldExtension extension = new FieldExtension();
    extension.setFieldName(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_NAME));

    String stringValueAttribute = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_FIELD_STRING);
    String expressionAttribute = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_FIELD_EXPRESSION);
    if (StringUtils.isNotEmpty(stringValueAttribute)) {
        extension.setStringValue(stringValueAttribute);

    } else if (StringUtils.isNotEmpty(expressionAttribute)) {
        extension.setExpression(expressionAttribute);

    } else {
        boolean readyWithFieldExtension = false;
        try {
            while (!readyWithFieldExtension && xtr.hasNext()) {
                xtr.next();
                if (xtr.isStartElement()
                        && CmmnXmlConstants.ELEMENT_FIELD_STRING.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setStringValue(xtr.getElementText().trim());

                } else if (xtr.isStartElement()
                        && CmmnXmlConstants.ELEMENT_FIELD_EXPRESSION.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setExpression(xtr.getElementText().trim());

                } else if (xtr.isEndElement() && getXMLElementName().equalsIgnoreCase(xtr.getLocalName())) {
                    readyWithFieldExtension = true;
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Error parsing field extension child elements", e);
        }
    }

    serviceTask.getFieldExtensions().add(extension);

    return null;
}

From source file:org.flowable.dmn.converter.child.OutputValuesParser.java

@Override
public void parseChildElement(XMLStreamReader xtr, DmnElement parentElement, DecisionTable decisionTable)
        throws Exception {
    if (!(parentElement instanceof OutputClause))
        return;/*from   w  ww. j  a  v  a  2  s  .  co  m*/

    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<Object>(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);
}

From source file:org.jasig.schedassist.impl.caldav.xml.ReportResponseHandlerImpl.java

/**
 * Extracts a {@link List} of {@link Calendar}s from the {@link InputStream}, if present.
 * /*from  w w  w.  ja v  a  2 s .  c  om*/
 * @param inputStream
 * @return a never null, but possibly empty {@link List} of {@link Calendar}s from the {@link InputStream}
 * @throws XmlParsingException in the event the stream could not be properly parsed
 */
public List<CalendarWithURI> extractCalendars(InputStream inputStream) {
    List<CalendarWithURI> results = new ArrayList<CalendarWithURI>();
    ByteArrayOutputStream capturedContent = null;
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try {
        InputStream localReference = inputStream;
        if (log.isDebugEnabled()) {
            capturedContent = new ByteArrayOutputStream();
            localReference = new TeeInputStream(inputStream, capturedContent);
        }
        BufferedInputStream buffered = new BufferedInputStream(localReference);
        buffered.mark(1);
        int firstbyte = buffered.read();
        if (-1 == firstbyte) {
            // short circuit on empty stream
            return results;
        }
        buffered.reset();
        XMLStreamReader parser = factory.createXMLStreamReader(buffered);

        String currentUri = null;
        String currentEtag = null;
        for (int eventType = parser.next(); eventType != XMLStreamConstants.END_DOCUMENT; eventType = parser
                .next()) {
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                QName name = parser.getName();
                if (isWebdavHrefElement(name)) {
                    currentUri = parser.getElementText();
                } else if (isWebdavEtagElement(name)) {
                    currentEtag = parser.getElementText();
                } else if (isCalendarDataElement(name)) {
                    Calendar cal = extractCalendar(parser.getElementText());
                    if (cal != null) {
                        CalendarWithURI withUri = new CalendarWithURI(cal, currentUri, currentEtag);
                        results.add(withUri);
                    } else if (log.isDebugEnabled()) {
                        log.debug("extractCalendar returned null for " + currentUri + ", skipping");
                    }
                }
                break;
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("extracted " + results.size() + " calendar from " + capturedContent.toString());
        }

    } catch (XMLStreamException e) {
        if (capturedContent != null) {
            log.error("caught XMLStreamException in extractCalendars, captured content: "
                    + capturedContent.toString(), e);
        } else {
            log.error("caught XMLStreamException in extractCalendars, no captured content available", e);
        }
        throw new XmlParsingException("caught XMLStreamException in extractCalendars", e);
    } catch (IOException e) {
        log.error("caught IOException in extractCalendars", e);
        throw new XmlParsingException("caught IOException in extractCalendars", e);
    }

    return results;
}

From source file:org.jbpm.designer.web.server.ProcessDiffServiceServlet.java

private List<String> getAssetVersions(String packageName, String assetName, String uuid,
        IDiagramProfile profile) {/*from www.java 2 s.co m*/
    try {
        String assetVersionURL = RepositoryInfo.getRepositoryProtocol(profile) + "://"
                + RepositoryInfo.getRepositoryHost(profile) + "/"
                + RepositoryInfo.getRepositorySubdomain(profile).substring(0,
                        RepositoryInfo.getRepositorySubdomain(profile).indexOf("/"))
                + "/rest/packages/" + URLEncoder.encode(packageName, "UTF-8") + "/assets/" + assetName
                + "/versions/";
        List<String> versionList = new ArrayList<String>();

        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(
                ServletUtil.getInputStreamForURL(assetVersionURL, "GET", profile), "UTF-8");
        boolean isFirstTitle = true;
        String title = "";
        while (reader.hasNext()) {
            int next = reader.next();
            if (next == XMLStreamReader.START_ELEMENT) {
                if ("title".equals(reader.getLocalName())) {
                    if (isFirstTitle) {
                        isFirstTitle = false;
                    } else {
                        versionList.add(reader.getElementText());
                    }
                }
            }
        }
        return versionList;
    } catch (Exception e) {
        _logger.error(e.getMessage());
        return null;
    }
}

From source file:org.mule.transport.sap.transformer.XmlToJcoFunctionTransformer.java

public JCoFunction transform(InputStream stream, String encoding) throws XMLStreamException {

    XMLStreamReader reader = null;
    XMLInputFactory factory = XMLInputFactory.newInstance();
    String functionName = null;/*from ww  w  .  j av a  2 s.c o  m*/
    String tableName = null;
    String structureName = null;
    String rowId = null;
    String fieldName = null;
    String value = null;

    JCoFunction function = null;

    try {
        reader = factory.createXMLStreamReader(stream);
        String localName = null;
        while (reader.hasNext()) {
            int eventType = reader.next();

            if (eventType == XMLStreamReader.START_DOCUMENT) {
                // find START_DOCUMENT

            } else if (eventType == XMLStreamReader.START_ELEMENT) {
                // find START_ELEMENT
                localName = reader.getLocalName();

                logger.debug("START ELEMENT IS FOUND");
                logger.debug("start localName = " + localName);

                if (localName.equals(MessageConstants.JCO)) {
                    functionName = getAttributeValue(MessageConstants.JCO_ATTR_NAME, reader);

                    try {
                        function = this.connector.getAdapter().getFunction(functionName);
                    } catch (JCoException e) {
                        throw new XMLStreamException(e);
                    }
                    logger.debug("function name:" + functionName);

                } else if (functionName != null) {
                    if (localName.equals(MessageConstants.IMPORT)) {
                        //recordType = IMPORT;

                        push(function.getImportParameterList());
                    } else if (localName.equals(MessageConstants.EXPORT)) {
                        //recordType = EXPORT;

                        push(function.getExportParameterList());
                    } else if (localName.equals(MessageConstants.TABLES)) {
                        //recordType = TABLES;
                        tableName = null;
                        push(function.getTableParameterList());

                    } else if (localName.equals(MessageConstants.TABLE)) {
                        if (tableName != null) {
                            pop();
                        }
                        tableName = getAttributeValue(MessageConstants.TABLE_ATTR_NAME, reader);

                        logger.debug("tableName = " + tableName);
                        push(this.record.getTable(tableName));
                    } else if (localName.equals(MessageConstants.STRUCTURE)) {
                        structureName = getAttributeValue(MessageConstants.STRUCTURE_ATTR_NAME, reader);
                        push(this.record.getStructure(structureName));
                    } else if (localName.equals(MessageConstants.ROW)) {
                        rowId = getAttributeValue(MessageConstants.ROW_ATTR_ID, reader);
                        logger.debug("rowId = " + rowId);
                        if (this.record instanceof JCoTable) {
                            ((JCoTable) this.record).appendRow();
                        }
                    } else if (localName.equals(MessageConstants.FIELD)) {
                        fieldName = getAttributeValue(MessageConstants.STRUCTURE_ATTR_NAME, reader);
                        value = reader.getElementText().trim(); // get an element value
                        logger.debug("FieldName = " + fieldName);
                        logger.debug("value = " + value);

                        this.record.setValue(fieldName, value);

                    }
                }

            } else if (eventType == XMLStreamReader.END_DOCUMENT) {

                // find END_DOCUMENT
                logger.debug("END DOCUMENT IS FOUND");
            } else if (eventType == XMLStreamReader.END_ELEMENT) {
                logger.debug("END ELEMENT IS FOUND");
                logger.debug("end localName = " + localName);
                // find END_ELEMENT
                if (localName.equals(MessageConstants.IMPORT) || localName.equals(MessageConstants.EXPORT)
                        || localName.equals(MessageConstants.TABLES) || localName.equals(MessageConstants.TABLE)
                        || localName.equals(MessageConstants.STRUCTURE)) {
                    pop();
                }
            }
        }
    } catch (Exception e) {
        logger.fatal(e);
        throw new TransformerException(this, e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (XMLStreamException ex) {
            }
        }
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException ex) {
            }
        }
        logger.debug("\n" + function.getImportParameterList().toXML());
        logger.debug("\n" + function.getExportParameterList().toXML());
        logger.debug("\n" + function.getTableParameterList().toXML());

        return function;
    }
}

From source file:org.openo.nfvo.emsdriver.collector.TaskThread.java

private boolean processCMXml(File tempfile, String nename, String type) {

    String csvpath = localPath + nename + "/" + type + "/";
    File csvpathfile = new File(csvpath);
    if (!csvpathfile.exists()) {
        csvpathfile.mkdirs();/*from  w ww . j  a  v a  2  s.  c om*/
    }
    String csvFileName = nename + dateFormat.format(new Date()) + System.nanoTime();
    String csvpathAndFileName = csvpath + csvFileName + ".csv";
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(csvpathAndFileName, false);
        bos = new BufferedOutputStream(fos, 10240);
    } catch (FileNotFoundException e1) {
        log.error("FileNotFoundException " + StringUtil.getStackTrace(e1));
    }

    boolean FieldNameFlag = false;
    boolean FieldValueFlag = false;
    //line num
    int countNum = 0;
    String xmlPathAndFileName = null;
    String localName = null;
    String endLocalName = null;
    String rmUID = null;
    int index = -1;
    ArrayList<String> names = new ArrayList<String>();// colname
    LinkedHashMap<String, String> nameAndValue = new LinkedHashMap<String, String>();

    FileInputStream fis = null;
    InputStreamReader isr = null;
    XMLStreamReader reader = null;
    try {
        fis = new FileInputStream(tempfile);
        isr = new InputStreamReader(fis, Constant.ENCODING_UTF8);
        XMLInputFactory fac = XMLInputFactory.newInstance();
        reader = fac.createXMLStreamReader(isr);
        int event = -1;
        boolean setcolum = true;
        while (reader.hasNext()) {
            try {
                event = reader.next();
                switch (event) {
                case XMLStreamConstants.START_ELEMENT:
                    localName = reader.getLocalName();
                    if ("FieldName".equalsIgnoreCase(localName)) {
                        FieldNameFlag = true;
                    }
                    if (FieldNameFlag) {
                        if ("N".equalsIgnoreCase(localName)) {
                            String colName = reader.getElementText().trim();
                            names.add(colName);
                        }
                    }
                    if ("FieldValue".equalsIgnoreCase(localName)) {
                        FieldValueFlag = true;

                    }
                    if (FieldValueFlag) {
                        if (setcolum) {
                            xmlPathAndFileName = this.setColumnNames(nename, names, type);
                            setcolum = false;
                        }

                        if ("Object".equalsIgnoreCase(localName)) {
                            int ac = reader.getAttributeCount();
                            for (int i = 0; i < ac; i++) {
                                if ("rmUID".equalsIgnoreCase(reader.getAttributeLocalName(i))) {
                                    rmUID = reader.getAttributeValue(i).trim();
                                }
                            }
                            nameAndValue.put("rmUID", rmUID);
                        }
                        if ("V".equalsIgnoreCase(localName)) {
                            index = Integer.parseInt(reader.getAttributeValue(0)) - 1;
                            String currentName = names.get(index);
                            String v = reader.getElementText().trim();
                            nameAndValue.put(currentName, v);
                        }
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    endLocalName = reader.getLocalName();

                    if ("FieldName".equalsIgnoreCase(endLocalName)) {
                        FieldNameFlag = false;
                    }
                    if ("FieldValue".equalsIgnoreCase(endLocalName)) {
                        FieldValueFlag = false;
                    }
                    if ("Object".equalsIgnoreCase(endLocalName)) {
                        countNum++;
                        this.appendLine(nameAndValue, bos);
                        nameAndValue.clear();
                    }
                    break;
                }
            } catch (Exception e) {
                log.error("" + StringUtil.getStackTrace(e));
                event = reader.next();
            }
        }

        if (bos != null) {
            bos.close();
            bos = null;
        }
        if (fos != null) {
            fos.close();
            fos = null;
        }

        String[] fileKeys = this.createZipFile(csvpathAndFileName, xmlPathAndFileName, nename);
        //ftp store
        Properties ftpPro = configurationInterface.getProperties();
        String ip = ftpPro.getProperty("ftp_ip");
        String port = ftpPro.getProperty("ftp_port");
        String ftp_user = ftpPro.getProperty("ftp_user");
        String ftp_password = ftpPro.getProperty("ftp_password");

        String ftp_passive = ftpPro.getProperty("ftp_passive");
        String ftp_type = ftpPro.getProperty("ftp_type");
        String remoteFile = ftpPro.getProperty("ftp_remote_path");
        this.ftpStore(fileKeys, ip, port, ftp_user, ftp_password, ftp_passive, ftp_type, remoteFile);
        //create Message
        String message = this.createMessage(fileKeys[1], ftp_user, ftp_password, ip, port, countNum, nename);

        //set message
        this.setMessage(message);
    } catch (Exception e) {
        log.error("" + StringUtil.getStackTrace(e));
        return false;
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
            if (bos != null) {
                bos.close();
            }

            if (fos != null) {
                fos.close();
            }
        } catch (Exception e) {
            log.error(e);
        }
    }
    return true;
}

From source file:org.osaf.cosmo.model.text.XhtmlCollectionFormat.java

public CollectionItem parse(String source, EntityFactory entityFactory) throws ParseException {
    CollectionItem collection = entityFactory.createCollection();

    try {//from  w  w w .java2  s .  co m
        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;
}