Example usage for javax.xml.stream.events XMLEvent asStartElement

List of usage examples for javax.xml.stream.events XMLEvent asStartElement

Introduction

In this page you can find the example usage for javax.xml.stream.events XMLEvent asStartElement.

Prototype

public StartElement asStartElement();

Source Link

Document

Returns this event as a start element event, may result in a class cast exception if this event is not a start element.

Usage

From source file:fr.openwide.talendalfresco.rest.client.RestAuthenticationTest.java

public void testRestLogin() {
    // create client and configure it
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);

    // instantiating a new method and configuring it
    GetMethod method = new GetMethod(restCommandUrlPrefix + "login");
    method.setFollowRedirects(true); // ?
    // Provide custom retry handler is necessary (?)
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    NameValuePair[] params = new NameValuePair[] { new NameValuePair("username", "admin"),
            new NameValuePair("password", "admin") };
    method.setQueryString(params);/*from w  w w. ja  v a 2  s  .  co  m*/

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody();
        System.out.println(new String(responseBody)); // TODO rm

        HashSet<String> defaultElementSet = new HashSet<String>(
                Arrays.asList(new String[] { RestConstants.TAG_COMMAND, RestConstants.TAG_CODE,
                        RestConstants.TAG_CONTENT, RestConstants.TAG_ERROR, RestConstants.TAG_MESSAGE }));
        HashMap<String, String> elementValueMap = new HashMap<String, String>(6);

        try {
            XMLEventReader xmlReader = XmlHelper.getXMLInputFactory()
                    .createXMLEventReader(new ByteArrayInputStream(responseBody));
            StringBuffer singleLevelTextBuf = null;
            while (xmlReader.hasNext()) {
                XMLEvent event = xmlReader.nextEvent();
                switch (event.getEventType()) {
                case XMLEvent.CHARACTERS:
                case XMLEvent.CDATA:
                    if (singleLevelTextBuf != null) {
                        singleLevelTextBuf.append(event.asCharacters().getData());
                    } // else element not meaningful
                    break;
                case XMLEvent.START_ELEMENT:
                    StartElement startElement = event.asStartElement();
                    String elementName = startElement.getName().getLocalPart();
                    if (defaultElementSet.contains(elementName)
                            // TODO another command specific level
                            || "ticket".equals(elementName)) {
                        // reinit buffer at start of meaningful elements
                        singleLevelTextBuf = new StringBuffer();
                    } else {
                        singleLevelTextBuf = null; // not useful
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (singleLevelTextBuf == null) {
                        break; // element not meaningful
                    }

                    // TODO or merely put it in the map since the element has been tested at start
                    EndElement endElement = event.asEndElement();
                    elementName = endElement.getName().getLocalPart();
                    if (defaultElementSet.contains(elementName)) {
                        String value = singleLevelTextBuf.toString();
                        elementValueMap.put(elementName, value);
                        // TODO test if it is code and it is not OK, break to error handling
                    }
                    // TODO another command specific level
                    else if ("ticket".equals(elementName)) {
                        ticket = singleLevelTextBuf.toString();
                    }
                    // singleLevelTextBuf = new StringBuffer(); // no ! in start
                    break;
                }
            }
        } catch (XMLStreamException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Throwable t) {
            // TODO Auto-generated catch block
            t.printStackTrace();
            //throw t;
        }

        String code = elementValueMap.get(RestConstants.TAG_CODE);
        assertTrue(RestConstants.CODE_OK.equals(code));
        System.out.println("got ticket " + ticket);

    } catch (HttpException e) {
        // TODO
        e.printStackTrace();
    } catch (IOException e) {
        // TODO
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:com.joliciel.frenchTreebank.upload.TreebankXmlReader.java

@Override
public boolean hasNextSentence() {
    MONITOR.startTask("hasNextSentence");
    try {/*ww w  . j a  v  a2 s .  c o m*/
        if (sentenceCount > 0 && currentSentenceCount == sentenceCount)
            return false;

        if (eventReader == null)
            this.getNextEventReader();

        boolean sentenceClosed = false;
        while (eventReader != null && !sentenceClosed) {
            while (eventReader.hasNext() && !sentenceClosed) {
                XMLEvent xmlEvent;
                try {
                    xmlEvent = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    LogUtils.logError(LOG, e);
                    throw new RuntimeException(e);
                }
                switch (xmlEvent.getEventType()) {
                case XMLEvent.START_ELEMENT:
                    StartElement startElementEvent = xmlEvent.asStartElement();
                    this.startElement(startElementEvent);
                    break;
                case XMLEvent.END_ELEMENT:
                    EndElement endElementEvent = xmlEvent.asEndElement();
                    sentenceClosed = this.endElement(endElementEvent);
                    break;
                case XMLEvent.PROCESSING_INSTRUCTION:
                    break;
                case XMLEvent.CHARACTERS:
                    Characters charactersEvent = xmlEvent.asCharacters();
                    this.characters(charactersEvent);
                    break;
                case XMLEvent.COMMENT:
                    break;
                case XMLEvent.START_DOCUMENT:
                    break;
                case XMLEvent.END_DOCUMENT:
                    break;
                case XMLEvent.ENTITY_REFERENCE:
                    break;
                case XMLEvent.ATTRIBUTE:
                    break;
                case XMLEvent.DTD:
                    break;
                case XMLEvent.CDATA:
                    break;
                case XMLEvent.SPACE:
                    break;
                }
            }
            if (!eventReader.hasNext()) {
                eventReader = null;
                this.getNextEventReader();
            }
            if (sentenceNumber != null && sentenceNumber.length() > 0 && sentenceClosed) {
                if (!sentenceNumber.equals(sentence.getSentenceNumber())) {
                    sentenceClosed = false;
                    sentence = null;
                }
            }
        }
        return sentenceClosed;
    } finally {
        MONITOR.endTask("hasNextSentence");
    }
}

From source file:com.amalto.core.load.io.XMLStreamUnwrapper.java

/**
 * Moves to next record in stream and stores it in {@link #stringWriter}.
 *///from   w w w  .j a  va  2s.c  om
private void moveToNext() {
    try {
        XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(stringWriter);
        boolean hasMadeChanges;
        do {
            if (!reader.hasNext()) {
                break;
            }
            hasMadeChanges = false; // Keep a state to skip line feeds
            final XMLEvent event = reader.nextEvent();
            if (event.isEndElement()) {
                level--;
            } else if (event.isStartElement()) {
                level++;
            } else if (event.isEndDocument()) {
                level--;
            }
            if (level >= RECORD_LEVEL) {
                if (event.isEndElement()) {
                    writer.writeEndElement();
                    hasMadeChanges = true;
                } else if (event.isStartElement()) {
                    final StartElement startElement = event.asStartElement();
                    final QName name = startElement.getName();
                    writer.writeStartElement(name.getNamespaceURI(), name.getLocalPart());
                    boolean isRecordRootElement = (RECORD_LEVEL == level - 1);
                    if (isRecordRootElement) {
                        for (int i = 0; i < rootNamespaceList.size(); i++) {
                            Namespace namespace = rootNamespaceList.get(i);
                            writer.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
                        }
                    }
                    // Declare namespaces (if any)
                    final Iterator elementNamespaces = startElement.getNamespaces();
                    while (elementNamespaces.hasNext()) {
                        Namespace elementNamespace = (Namespace) elementNamespaces.next();
                        if (isRecordRootElement) {
                            if (rootNamespaceList.size() > 0) {
                                for (int i = 0; i < rootNamespaceList.size(); i++) {
                                    Namespace namespace = rootNamespaceList.get(i);
                                    if (!namespace.getPrefix().equals(elementNamespace.getPrefix())
                                            || !namespace.getNamespaceURI()
                                                    .equals(elementNamespace.getNamespaceURI())) {
                                        writer.writeNamespace(elementNamespace.getPrefix(),
                                                elementNamespace.getNamespaceURI());
                                    }
                                }
                            } else {
                                writer.writeNamespace(elementNamespace.getPrefix(),
                                        elementNamespace.getNamespaceURI());
                            }
                        } else {
                            writer.writeNamespace(elementNamespace.getPrefix(),
                                    elementNamespace.getNamespaceURI());
                        }
                    }
                    // Write attributes
                    final Iterator attributes = startElement.getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = (Attribute) attributes.next();
                        QName attributeName = attribute.getName();
                        String value = attribute.getValue();
                        if (StringUtils.isEmpty(attributeName.getNamespaceURI())) {
                            writer.writeAttribute(attributeName.getLocalPart(), value);
                        } else {
                            writer.writeAttribute(attributeName.getNamespaceURI(), attributeName.getLocalPart(),
                                    value);
                        }
                    }
                    hasMadeChanges = true;
                } else if (event.isCharacters()) {
                    final String text = event.asCharacters().getData().trim();
                    if (!text.isEmpty()) {
                        writer.writeCharacters(text);
                        hasMadeChanges = true;
                    }
                }
            }
        } while (level > RECORD_LEVEL || !hasMadeChanges);
        writer.flush();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Unexpected parsing exception.", e);
    }
}

From source file:com.evolveum.polygon.connector.hcm.DocumentProcessing.java

public Map<String, Object> parseXMLData(HcmConnectorConfiguration conf, ResultsHandler handler,
        Map<String, Object> schemaAttributeMap, Filter query) {

    XMLInputFactory factory = XMLInputFactory.newInstance();
    try {//from w  ww .j a  v  a2  s .com

        String uidAttributeName = conf.getUidAttribute();
        String primariId = conf.getPrimaryId();
        String startName = "";
        String value = null;

        StringBuilder assignmentXMLBuilder = null;

        List<String> builderList = new ArrayList<String>();

        Integer nOfIterations = 0;
        Boolean isSubjectToQuery = false;
        Boolean isAssigment = false;
        Boolean evaluateAttr = true;
        Boolean specificAttributeQuery = false;

        XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(conf.getFilePath()));
        List<String> dictionary = populateDictionary(FIRSTFLAG);

        if (!attrsToGet.isEmpty()) {

            attrsToGet.add(uidAttributeName);
            attrsToGet.add(primariId);
            specificAttributeQuery = true;
            evaluateAttr = false;
            LOGGER.ok("The uid and primary id were added to the queried attribute list");

            schemaAttributeMap = modifySchemaAttributeMap(schemaAttributeMap);
        }

        while (eventReader.hasNext()) {

            XMLEvent event = eventReader.nextEvent();

            Integer code = event.getEventType();

            if (code == XMLStreamConstants.START_ELEMENT) {

                StartElement startElement = event.asStartElement();
                startName = startElement.getName().getLocalPart();

                if (!evaluateAttr && attrsToGet.contains(startName)) {

                    evaluateAttr = true;
                }

                if (!elementIsEmployeeData) {

                    if (startName.equals(EMPLOYEES)) {

                        if (dictionary.contains(nOfIterations.toString())) {
                            LOGGER.ok("The defined number of iterations has been hit: {0}",
                                    nOfIterations.toString());
                            break;
                        } else {
                            startName = "";
                            elementIsEmployeeData = true;
                            nOfIterations++;
                        }
                    }
                } else if (evaluateAttr) {

                    if (!isAssigment) {
                        if (!ASSIGNMENTTAG.equals(startName)) {

                        } else {
                            assignmentXMLBuilder = new StringBuilder();
                            isAssigment = true;
                        }
                    } else {

                        builderList = processAssignment(startName, null, START, builderList);
                    }

                    if (multiValuedAttributesList.contains(startName)) {

                        elementIsMultiValued = true;
                    }

                }

            } else if (elementIsEmployeeData) {

                if (code == XMLStreamConstants.CHARACTERS && evaluateAttr) {

                    Characters characters = event.asCharacters();

                    if (!characters.isWhiteSpace()) {

                        StringBuilder valueBuilder;
                        if (value != null) {
                            valueBuilder = new StringBuilder(value).append("")
                                    .append(characters.getData().toString());
                        } else {
                            valueBuilder = new StringBuilder(characters.getData().toString());
                        }
                        value = valueBuilder.toString();
                        // value = StringEscapeUtils.escapeXml10(value);
                        // LOGGER.info("The attribute value for: {0} is
                        // {1}", startName, value);
                    }
                } else if (code == XMLStreamConstants.END_ELEMENT) {

                    EndElement endElement = event.asEndElement();
                    String endName = endElement.getName().getLocalPart();

                    isSubjectToQuery = checkFilter(endName, value, query, uidAttributeName);

                    if (!isSubjectToQuery) {
                        attributeMap.clear();
                        elementIsEmployeeData = false;
                        value = null;

                        endName = EMPLOYEES;
                    }

                    if (endName.equals(EMPLOYEES)) {

                        attributeMap = handleEmployeeData(attributeMap, schemaAttributeMap, handler,
                                uidAttributeName, primariId);

                        elementIsEmployeeData = false;

                    } else if (evaluateAttr) {

                        if (endName.equals(startName)) {
                            if (value != null) {

                                if (!isAssigment) {
                                    if (!elementIsMultiValued) {

                                        attributeMap.put(startName, value);
                                    } else {

                                        multiValuedAttributeBuffer.put(startName, value);
                                    }
                                } else {

                                    value = StringEscapeUtils.escapeXml10(value);
                                    builderList = processAssignment(endName, value, VALUE, builderList);

                                    builderList = processAssignment(endName, null, END, builderList);
                                }
                                // LOGGER.info("Attribute name: {0} and the
                                // Attribute value: {1}", endName, value);
                                value = null;
                            }
                        } else {
                            if (endName.equals(ASSIGNMENTTAG)) {

                                builderList = processAssignment(endName, null, CLOSE, builderList);

                                // if (assigmentIsActive) {

                                for (String records : builderList) {
                                    assignmentXMLBuilder.append(records);

                                }
                                attributeMap.put(ASSIGNMENTTAG, assignmentXMLBuilder.toString());
                                // } else {
                                // }

                                builderList = new ArrayList<String>();
                                // assigmentIsActive = false;
                                isAssigment = false;

                            } else if (multiValuedAttributesList.contains(endName)) {
                                processMultiValuedAttributes(multiValuedAttributeBuffer);
                            }
                        }

                    }
                    if (specificAttributeQuery && evaluateAttr) {

                        evaluateAttr = false;
                    }
                }
            } else if (code == XMLStreamConstants.END_DOCUMENT) {
                handleBufferedData(uidAttributeName, primariId, handler);
            }
        }

    } catch (FileNotFoundException e) {
        StringBuilder errorBuilder = new StringBuilder("File not found at the specified path.")
                .append(e.getLocalizedMessage());
        LOGGER.error("File not found at the specified path: {0}", e);
        throw new ConnectorIOException(errorBuilder.toString());
    } catch (XMLStreamException e) {

        LOGGER.error("Unexpected processing error while parsing the .xml document : {0}", e);

        StringBuilder errorBuilder = new StringBuilder(
                "Unexpected processing error while parsing the .xml document. ")
                        .append(e.getLocalizedMessage());

        throw new ConnectorIOException(errorBuilder.toString());
    }
    return attributeMap;

}

From source file:ca.phon.session.io.xml.v12.XMLSessionReader_v12.java

@Override
public boolean canRead(File file) throws IOException {
    // open file and make sure the first
    // element is 'session' with the correct version
    boolean canRead = false;

    // use StAX to read only first element
    // create StAX reader
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = null;
    try (FileInputStream source = new FileInputStream(file)) {
        //BufferedReader in = new BufferedReader(new InputStreamReader(source, "UTF-8"));
        XMLEventReader xmlReader = factory.createXMLEventReader(source, "UTF-8");
        reader = factory.createFilteredReader(xmlReader, new XMLWhitespaceFilter());

        XMLEvent evt;
        while (!(evt = reader.nextEvent()).isStartElement())
            ;/*  ww w  .j  a v a 2 s.  co  m*/
        canRead = evt.asStartElement().getName().getLocalPart().equals("session")
                && evt.asStartElement().getAttributeByName(new QName("version")).getValue().equals("PB1.2");
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }

    return canRead;
}

From source file:fr.openwide.talendalfresco.rest.client.importer.RestImportFileTest.java

public void login() {
    // create client and configure it
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);

    // instantiating a new method and configuring it
    GetMethod method = new GetMethod(restCommandUrlPrefix + "login");
    method.setFollowRedirects(true); // ?
    // Provide custom retry handler is necessary (?)
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    NameValuePair[] params = new NameValuePair[] { new NameValuePair("username", "admin"),
            new NameValuePair("password", "admin") };
    method.setQueryString(params);/* w ww  .  j a  va  2s. c o m*/

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody();
        System.out.println(new String(responseBody)); // TODO rm

        HashSet<String> defaultElementSet = new HashSet<String>(
                Arrays.asList(new String[] { RestConstants.TAG_COMMAND, RestConstants.TAG_CODE,
                        RestConstants.TAG_CONTENT, RestConstants.TAG_ERROR, RestConstants.TAG_MESSAGE }));
        HashMap<String, String> elementValueMap = new HashMap<String, String>(6);

        try {
            XMLEventReader xmlReader = XmlHelper.getXMLInputFactory()
                    .createXMLEventReader(new ByteArrayInputStream(responseBody));
            StringBuffer singleLevelTextBuf = null;
            while (xmlReader.hasNext()) {
                XMLEvent event = xmlReader.nextEvent();
                switch (event.getEventType()) {
                case XMLEvent.CHARACTERS:
                case XMLEvent.CDATA:
                    if (singleLevelTextBuf != null) {
                        singleLevelTextBuf.append(event.asCharacters().getData());
                    } // else element not meaningful
                    break;
                case XMLEvent.START_ELEMENT:
                    StartElement startElement = event.asStartElement();
                    String elementName = startElement.getName().getLocalPart();
                    if (defaultElementSet.contains(elementName)
                            // TODO another command specific level
                            || "ticket".equals(elementName)) {
                        // reinit buffer at start of meaningful elements
                        singleLevelTextBuf = new StringBuffer();
                    } else {
                        singleLevelTextBuf = null; // not useful
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (singleLevelTextBuf == null) {
                        break; // element not meaningful
                    }

                    // TODO or merely put it in the map since the element has been tested at start
                    EndElement endElement = event.asEndElement();
                    elementName = endElement.getName().getLocalPart();
                    if (defaultElementSet.contains(elementName)) {
                        String value = singleLevelTextBuf.toString();
                        elementValueMap.put(elementName, value);
                        // TODO test if it is code and it is not OK, break to error handling
                    }
                    // TODO another command specific level
                    else if ("ticket".equals(elementName)) {
                        ticket = singleLevelTextBuf.toString();
                    }
                    // singleLevelTextBuf = new StringBuffer(); // no ! in start
                    break;
                }
            }
        } catch (XMLStreamException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Throwable t) {
            // TODO Auto-generated catch block
            t.printStackTrace();
            //throw t;
        }

        String code = elementValueMap.get(RestConstants.TAG_CODE);
        assertTrue(RestConstants.CODE_OK.equals(code));
        System.out.println("got ticket " + ticket);

    } catch (HttpException e) {
        // TODO
        e.printStackTrace();
    } catch (IOException e) {
        // TODO
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:fr.openwide.talendalfresco.rest.client.importer.RestImportFileTest.java

public void testSingleFileImport() {
    // create client and configure it
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);

    // instantiating a new method and configuring it
    PostMethod method = new PostMethod(restCommandUrlPrefix + "import");
    // Provide custom retry handler is necessary (?)
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    NameValuePair[] params = new NameValuePair[] { new NameValuePair("path", "/app:company_home"),
            new NameValuePair("ticket", ticket) };
    method.setQueryString(params);/*from w  w  w .  j  a  v  a  2s.  c  o  m*/

    try {
        //method.setRequestBody(new NameValuePair[] {
        //      new NameValuePair("path", "/app:company_home") });
        FileInputStream acpXmlIs = new FileInputStream(SAMPLE_SINGLE_FILE_PATH);
        InputStreamRequestEntity entity = new InputStreamRequestEntity(acpXmlIs);
        //InputStreamRequestEntity entity = new InputStreamRequestEntity(acpXmlIs, "text/xml; charset=ISO-8859-1");
        method.setRequestEntity(entity);
    } catch (IOException ioex) {
        fail("ACP XML file not found " + ioex.getMessage());
    }

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody();
        System.out.println(new String(responseBody)); // TODO rm

        HashSet<String> defaultElementSet = new HashSet<String>(
                Arrays.asList(new String[] { RestConstants.TAG_COMMAND, RestConstants.TAG_CODE,
                        RestConstants.TAG_CONTENT, RestConstants.TAG_ERROR, RestConstants.TAG_MESSAGE }));
        HashMap<String, String> elementValueMap = new HashMap<String, String>(6);

        try {
            XMLEventReader xmlReader = XmlHelper.getXMLInputFactory()
                    .createXMLEventReader(new ByteArrayInputStream(responseBody));
            StringBuffer singleLevelTextBuf = null;
            while (xmlReader.hasNext()) {
                XMLEvent event = xmlReader.nextEvent();
                switch (event.getEventType()) {
                case XMLEvent.CHARACTERS:
                case XMLEvent.CDATA:
                    if (singleLevelTextBuf != null) {
                        singleLevelTextBuf.append(event.asCharacters().getData());
                    } // else element not meaningful
                    break;
                case XMLEvent.START_ELEMENT:
                    StartElement startElement = event.asStartElement();
                    String elementName = startElement.getName().getLocalPart();
                    if (defaultElementSet.contains(elementName)
                            // TODO another command specific level
                            || "ticket".equals(elementName)) {
                        // reinit buffer at start of meaningful elements
                        singleLevelTextBuf = new StringBuffer();
                    } else {
                        singleLevelTextBuf = null; // not useful
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (singleLevelTextBuf == null) {
                        break; // element not meaningful
                    }

                    // TODO or merely put it in the map since the element has been tested at start
                    EndElement endElement = event.asEndElement();
                    elementName = endElement.getName().getLocalPart();
                    if (defaultElementSet.contains(elementName)) {
                        String value = singleLevelTextBuf.toString();
                        elementValueMap.put(elementName, value);
                        // TODO test if it is code and it is not OK, break to error handling
                    }
                    // TODO another command specific level
                    else if ("ticket".equals(elementName)) {
                        ticket = singleLevelTextBuf.toString();
                    }
                    // singleLevelTextBuf = new StringBuffer(); // no ! in start
                    break;
                }
            }
        } catch (XMLStreamException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Throwable t) {
            // TODO Auto-generated catch block
            t.printStackTrace();
            //throw t;
        }

        String code = elementValueMap.get(RestConstants.TAG_CODE);
        assertTrue(RestConstants.CODE_OK.equals(code));
        System.out.println("got ticket " + ticket);

    } catch (HttpException e) {
        // TODO
        e.printStackTrace();
    } catch (IOException e) {
        // TODO
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:edu.monash.merc.system.parser.xml.HPAWSXmlParser.java

public List<HPAEntryBean> parseHPAXml(String fileName, XMLInputFactory2 factory2) {
    xmlif2 = factory2;//from w w w.j  a va2 s  .c  o  m
    logger.info("Starting to parse " + fileName);
    List<HPAEntryBean> hpaEntryBeans = new ArrayList<HPAEntryBean>();
    XMLEventReader2 xmlEventReader = null;
    try {
        xmlEventReader = (XMLEventReader2) xmlif2.createXMLEventReader(new FileInputStream(fileName));

        QName entryQN = new QName(ELE_ENTRY);
        QName versionQN = new QName(ATTR_VERSION);
        QName urlQN = new QName(ATTR_URL);
        QName nameQN = new QName(ELE_NAME);
        QName identiferQN = new QName(ELE_IDENTIFIER);
        QName idQN = new QName(ATTR_ID);
        QName xrefQN = new QName(ELE_XREF);
        QName dbQN = new QName(ATTR_DB);
        QName tissueExpQN = new QName(ELE_TISSUE_EXPRESSION);
        QName typeQN = new QName(ATTR_TYPE);
        QName verificationQN = new QName(ELE_VERIFICATION);
        QName dataQN = new QName(ELE_DATA);
        QName tissueQN = new QName(ELE_TISSUE);
        QName statusQN = new QName(ATTR_STATUS);
        QName cellTypeQN = new QName(ELE_CELLTYPE);
        QName levelQN = new QName(ELE_LEVEL);
        QName antibodyQN = new QName(ELE_ANTIBODY);

        String version = null;
        String url = null;

        String geneName = null;
        String geneAccession = null;
        String dbNameForIdentifier = null;

        String xrefAc = null;
        String xrefDb = null;
        boolean tissueExpressionPresent = false;

        boolean antibodyPresent = false;

        String tissueStatus = null;
        String tissue = null;
        String cellType = null;
        String levelType = null;
        String level = null;

        String verificationType = null;
        String verification = null;

        HPAEntryBean hpaEntryBean = null;

        GeneBean geneBean = null;

        List<DbSourceAcEntryBean> dbSourceAcEntryBeans = new ArrayList<DbSourceAcEntryBean>();

        List<PEEvidenceBean> peAntiIHCNormEvidenceBeans = new ArrayList<PEEvidenceBean>();

        PEEvidenceBean antiIHCNormEvidenceBean = null;

        AccessionBean identifiedAcBean = null;

        while (xmlEventReader.hasNextEvent()) {
            //eventType = reader.next();
            XMLEvent event = xmlEventReader.nextEvent();
            if (event.isStartElement()) {
                StartElement element = event.asStartElement();

                //hpa entry
                if (element.getName().equals(entryQN)) {
                    //start to create a hpaEntryBean
                    hpaEntryBean = new HPAEntryBean();
                    //create a GeneBean
                    geneBean = new GeneBean();
                    //create a list of DbSourceAcEntryBean to store all DbSource and Ac
                    dbSourceAcEntryBeans = new ArrayList<DbSourceAcEntryBean>();
                    //create a list of PEEvidenceBean to store all antibody evidencs for the current gene
                    peAntiIHCNormEvidenceBeans = new ArrayList<PEEvidenceBean>();

                    //get the version attribute
                    Attribute versionAttr = element.getAttributeByName(versionQN);
                    if (versionAttr != null) {
                        version = versionAttr.getValue();
                    }
                    //get the url attribute
                    Attribute urlAttr = element.getAttributeByName(urlQN);
                    if (urlAttr != null) {
                        url = urlAttr.getValue();
                    }
                }

                //parse the gene name in the name element
                if (element.getName().equals(nameQN)) {
                    if (xmlEventReader.peek().isCharacters()) {
                        event = xmlEventReader.nextEvent();
                        Characters geneCharacters = event.asCharacters();
                        if (geneCharacters != null) {
                            geneName = geneCharacters.getData();
                        }
                    }
                }

                //parse the ensg accession and db in the identifier element
                if (element.getName().equals(identiferQN)) {
                    Attribute idAttr = element.getAttributeByName(idQN);
                    if (idAttr != null) {
                        geneAccession = idAttr.getValue();
                    }
                    Attribute dbAttr = element.getAttributeByName(dbQN);
                    if (dbAttr != null) {
                        dbNameForIdentifier = dbAttr.getValue();
                    }
                }

                //parse all db and accession pair in xref element
                if (element.getName().equals(xrefQN)) {
                    Attribute idAttr = element.getAttributeByName(idQN);
                    if (idAttr != null) {
                        xrefAc = idAttr.getValue();
                    }
                    Attribute dbAttr = element.getAttributeByName(dbQN);
                    if (dbAttr != null) {
                        xrefDb = dbAttr.getValue();
                    }
                }

                //parse tissueExpression
                if (element.getName().equals(tissueExpQN)) {
                    //we only focus on the tissueExpression element in the path /entry/tissueExpression
                    if (!antibodyPresent) {
                        //set the tissueExpression present flag into true;
                        tissueExpressionPresent = true;
                        //create a list of PEEvidenceBean to store the PEEvidence for antibody
                        peAntiIHCNormEvidenceBeans = new ArrayList<PEEvidenceBean>();
                    }
                }

                //parse the verification element to get reliability or validation value
                if (element.getName().equals(verificationQN)) {
                    //we only focus on the verification element in the path /entry/tissueExpression/verification
                    if (!antibodyPresent && tissueExpressionPresent) {
                        Attribute verifAttr = element.getAttributeByName(typeQN);
                        if (verifAttr != null) {
                            verificationType = element.getAttributeByName(typeQN).getValue();
                        }
                        if (xmlEventReader.peek().isCharacters()) {
                            event = xmlEventReader.nextEvent();
                            verification = event.asCharacters().getData();
                        }
                    }
                }
                //start of the data element
                if (element.getName().equals(dataQN)) {
                    //we only focus on the data element in the path /entry/tissueExpression/data
                    if (!antibodyPresent && tissueExpressionPresent) {
                        antiIHCNormEvidenceBean = new PEEvidenceBean();
                        TPBDataTypeBean dataTypeBean = createTPBDataTypeBeanForPEANTIIHCNORM();
                        antiIHCNormEvidenceBean.setTpbDataTypeBean(dataTypeBean);
                    }
                }

                //start of tissue
                if (element.getName().equals(tissueQN)) {
                    //we only focus on the tissue element in the path /entry/tissueExpression/data/tissue
                    if (!antibodyPresent && tissueExpressionPresent) {
                        Attribute tissueStatusAttr = element.getAttributeByName(statusQN);
                        if (tissueStatusAttr != null) {
                            tissueStatus = tissueStatusAttr.getValue();
                        }
                        if (xmlEventReader.peek().isCharacters()) {
                            event = xmlEventReader.nextEvent();
                            tissue = event.asCharacters().getData();
                        }
                    }
                }

                //start of cellType
                if (element.getName().equals(cellTypeQN)) {
                    //we only focus on the cellType element in the path /entry/tissueExpression/data/cellType
                    if (!antibodyPresent && tissueExpressionPresent) {
                        if (xmlEventReader.peek().isCharacters()) {
                            event = xmlEventReader.nextEvent();
                            cellType = event.asCharacters().getData();
                        }
                    }
                }

                //start of level
                if (element.getName().equals(levelQN)) {
                    //we only focus on the level element in the path /entry/tissueExpression/data/level
                    if (!antibodyPresent && tissueExpressionPresent) {
                        Attribute typeAttr = element.getAttributeByName(typeQN);
                        if (typeAttr != null) {
                            levelType = typeAttr.getValue();
                        }
                        if (xmlEventReader.peek().isCharacters()) {
                            event = xmlEventReader.nextEvent();
                            level = event.asCharacters().getData();
                        }
                    }
                }
                //start of antibody element
                if (element.getName().equals(antibodyQN)) {
                    //we have to setup antibodyPresent flag as true
                    antibodyPresent = true;
                }
            }

            //End of element
            if (event.isEndElement()) {
                EndElement endElement = event.asEndElement();
                //hpa entry end
                if (endElement.getName().equals(entryQN)) {
                    //set hpa version
                    hpaEntryBean.setHpaVersion(version);
                    //hpaEntryBean set gene bean
                    hpaEntryBean.setGeneBean(geneBean);
                    //create the primary dbsource bean
                    DBSourceBean primaryDbSourceBean = createPrimaryDBSourceBeanForHPA();

                    //set the primary DBSourceBean
                    hpaEntryBean.setPrimaryDbSourceBean(primaryDbSourceBean);

                    //set the identified accesion bean
                    hpaEntryBean.setIdentifiedAccessionBean(identifiedAcBean);

                    //set DbSourceAcEntryBean list
                    hpaEntryBean.setDbSourceAcEntryBeans(dbSourceAcEntryBeans);

                    //set all the PeAntiIHCBody evidences
                    if (peAntiIHCNormEvidenceBeans.size() == 0) {
                        peAntiIHCNormEvidenceBeans.add(createNonePEEvidence(url));
                    }

                    hpaEntryBean.setPeAntiIHCNormEvidencesBeans(peAntiIHCNormEvidenceBeans);

                    //add the current hpa entry bean into list
                    hpaEntryBeans.add(hpaEntryBean);
                    //reset version and url
                    version = null;
                    url = null;
                    identifiedAcBean = null;
                }

                //end of gene name, populate the gene name
                if (endElement.getName().equals(nameQN)) {
                    //set gene name
                    geneBean.setDisplayName(geneName);
                }

                //end of identifier, populating for gene accession, db and accessions if any
                if (endElement.getName().equals(identiferQN)) {
                    //set the gene accession
                    geneBean.setEnsgAccession(geneAccession);

                    identifiedAcBean = createIdentifiedAcBean(geneAccession, dbNameForIdentifier);
                    //create a DbSourceAcEntryBean based on the identifier element
                    DbSourceAcEntryBean dbSourceAcEntryBean = createDbSourceAcEntry(dbNameForIdentifier,
                            geneAccession);
                    //add this DbSourceAcEntryBean into list
                    dbSourceAcEntryBeans.add(dbSourceAcEntryBean);
                }

                //end of xref element.  populate for db and accessions if any
                if (endElement.getName().equals(xrefQN)) {
                    //create a DbSourceAcEntryBean based on the xref element
                    DbSourceAcEntryBean dbSourceAcEntryBean = createDbSourceAcEntry(xrefDb, xrefAc);
                    //add this DbSoureAcEntryBean into list
                    dbSourceAcEntryBeans.add(dbSourceAcEntryBean);
                    //set rest of db and accession values
                    xrefDb = null;
                    xrefAc = null;
                }
                //end of the tissueExpression
                if (endElement.getName().equals(tissueExpQN)) {
                    //we only focus on the tissueExpression element in the path /entry/tissueExpression
                    if (!antibodyPresent) {
                        //the tissueExpression is end. we have to reset tissueExpressionPresent,
                        //verificationType and verification values under the tissueExpression element level
                        //reset tissueExpression present flag into false
                        tissueExpressionPresent = false;
                        //reset verification type
                        verificationType = null;
                        //reset verification value
                        verification = null;
                    }
                }

                //end of data element
                if (endElement.getName().equals(dataQN)) {
                    //we only focus on the data element in the path /entry/tissueExpression/data
                    if (!antibodyPresent && tissueExpressionPresent) {
                        //we only consider the tissue status is normal one
                        if (StringUtils.endsWithIgnoreCase(tissueStatus, TISSUE_STATUS_NORMAL)) {
                            setAntiEvidence(antiIHCNormEvidenceBean, url, verification, tissue, cellType, level,
                                    levelType);
                            //add anti evidence
                            peAntiIHCNormEvidenceBeans.add(antiIHCNormEvidenceBean);
                        }
                        //the data element is end. we have to reset the tissueStatus, tissue, cellType and level values under the data element level
                        tissueStatus = null;
                        tissue = null;
                        cellType = null;
                        level = null;
                        levelType = null;
                    }
                }
                //end of antibody
                if (endElement.getName().equals(antibodyQN)) {
                    //we have to reset antibodyPresent flag as false
                    antibodyPresent = false;
                }
            }
            //End of XML document
            if (event.isEndDocument()) {
                // finished to parse the whole document;
                break;
            }
        }
    } catch (Exception ex) {
        logger.error(ex);
        throw new DMXMLParserException(ex);
    } finally {
        if (xmlEventReader != null) {
            try {
                xmlEventReader.close();
            } catch (Exception e) {
                //ignore whatever caught.
            }
        }
    }
    return hpaEntryBeans;
}

From source file:edu.jhu.hlt.concrete.ingesters.webposts.WebPostIngester.java

@Override
public Communication fromCharacterBasedFile(final Path path) throws IngestException {
    if (!Files.exists(path))
        throw new IngestException("No file at: " + path.toString());

    AnalyticUUIDGeneratorFactory f = new AnalyticUUIDGeneratorFactory();
    AnalyticUUIDGenerator g = f.create();
    Communication c = new Communication();
    c.setUuid(g.next());/*from www . j  a  v a2 s .c  om*/
    c.setType(this.getKind());
    c.setMetadata(TooledMetadataConverter.convert(this));

    try {
        ExistingNonDirectoryFile ef = new ExistingNonDirectoryFile(path);
        c.setId(ef.getName().split("\\.")[0]);
    } catch (NoSuchFileException | NotFileException e) {
        // might throw if path is a directory.
        throw new IngestException(path.toString() + " is not a file, or is a directory.");
    }

    String content;
    try (InputStream is = Files.newInputStream(path);
            BufferedInputStream bin = new BufferedInputStream(is, 1024 * 8 * 8);) {
        content = IOUtils.toString(bin, StandardCharsets.UTF_8);
        c.setText(content);
    } catch (IOException e) {
        throw new IngestException(e);
    }

    try (InputStream is = Files.newInputStream(path);
            BufferedInputStream bin = new BufferedInputStream(is, 1024 * 8 * 8);
            BufferedReader reader = new BufferedReader(new InputStreamReader(bin, StandardCharsets.UTF_8));) {
        XMLEventReader rdr = null;
        try {
            rdr = inF.createXMLEventReader(reader);

            // Below method moves the reader
            // to the headline end element.
            Section headline = this.handleBeginning(rdr, content, c);
            headline.setUuid(g.next());
            c.addToSectionList(headline);
            TextSpan sts = headline.getTextSpan();
            LOGGER.debug("headline text: {}", c.getText().substring(sts.getStart(), sts.getEnding()));

            int sectNumber = 1;
            int subSect = 0;

            int currOff = -1;
            // Big amounts of characters.
            while (rdr.hasNext()) {
                XMLEvent nextEvent = rdr.nextEvent();
                currOff = nextEvent.getLocation().getCharacterOffset();

                // First: see if document is going to end.
                // If yes: exit.
                if (nextEvent.isEndDocument())
                    break;

                // region
                // enables ingestion of quotes inside a usenet webpost.
                // by Tongfei Chen
                if (nextEvent.isStartElement()
                        && nextEvent.asStartElement().getName().equals(QName.valueOf("QUOTE"))) {
                    Attribute attrQuote = nextEvent.asStartElement()
                            .getAttributeByName(QName.valueOf("PREVIOUSPOST"));
                    String quote = StringEscapeUtils.escapeXml(attrQuote.getValue());
                    int location = attrQuote.getLocation().getCharacterOffset()
                            + "<QUOTE PREVIOUSPOST=\"".length();
                    Section quoteSection = new Section(g.next(), "quote")
                            .setTextSpan(new TextSpan(location, location + quote.length()));
                    c.addToSectionList(quoteSection);
                }
                // endregion

                // Check if start element.
                if (nextEvent.isCharacters()) {
                    Characters chars = nextEvent.asCharacters();
                    if (!chars.isWhiteSpace()) {
                        String fpContent = chars.getData();
                        LOGGER.debug("Character offset: {}", currOff);
                        LOGGER.debug("Character based data: {}", fpContent);

                        SimpleImmutableEntry<Integer, Integer> pads = trimSpacing(fpContent);
                        final int tsb = currOff + pads.getKey();

                        final int tse = currOff + fpContent.replace("\"", "&quot;").replace("<", "&lt;")
                                .replace(">", "&gt;").length() - (pads.getValue());
                        // MAINTAIN CORRECT TEXT SPAN
                        // CANNOT USE StringEscapeUtils.escapeXml because it will escape "'", which
                        // is not escaped in the data
                        // @tongfei

                        LOGGER.debug("Section text: {}", content.substring(tsb, tse));
                        TextSpan ts = new TextSpan(tsb, tse);
                        String sk;
                        if (subSect == 0)
                            sk = "poster";
                        else if (subSect == 1)
                            sk = "postdate";
                        else
                            sk = "post";

                        Section s = new Section();
                        s.setKind(sk);
                        s.setTextSpan(ts);
                        s.setUuid(g.next());
                        List<Integer> intList = new ArrayList<>();
                        intList.add(sectNumber);
                        intList.add(subSect);
                        s.setNumberList(intList);
                        c.addToSectionList(s);

                        subSect++;
                    }
                } else if (nextEvent.isEndElement()) {
                    EndElement ee = nextEvent.asEndElement();
                    currOff = ee.getLocation().getCharacterOffset();
                    QName name = ee.getName();
                    String localName = name.getLocalPart();
                    LOGGER.debug("Hit end element: {}", localName);
                    if (localName.equalsIgnoreCase(POST_LOCAL_NAME)) {
                        LOGGER.debug("Switching to new post.");
                        sectNumber++;
                        subSect = 0;
                    } else if (localName.equalsIgnoreCase(TEXT_LOCAL_NAME)) {
                        // done with document.
                        break;
                    }
                }
            }

            return c;

        } catch (XMLStreamException | ConcreteException | StringIndexOutOfBoundsException
                | ClassCastException x) {
            throw new IngestException(x);
        } finally {
            if (rdr != null)
                try {
                    rdr.close();
                } catch (XMLStreamException e) {
                    // not likely.
                    LOGGER.info("Error closing XMLReader.", e);
                }
        }
    } catch (IOException e) {
        throw new IngestException(e);
    }
}

From source file:json_to_xml_1.java

public int execute(String args[]) throws ProgramTerminationException {
    this.getInfoMessages().clear();

    if (args.length < 2) {
        throw constructTermination("messageArgumentsMissing", null,
                getI10nString("messageArgumentsMissingUsage") + "\n\tjson_to_xml_1 "
                        + getI10nString("messageParameterList") + "\n");
    }/*www.  j  a  v a 2s  .  c o  m*/

    File resultInfoFile = new File(args[1]);

    try {
        resultInfoFile = resultInfoFile.getCanonicalFile();
    } catch (SecurityException ex) {
        throw constructTermination("messageResultInfoFileCantGetCanonicalPath", ex, null,
                resultInfoFile.getAbsolutePath());
    } catch (IOException ex) {
        throw constructTermination("messageResultInfoFileCantGetCanonicalPath", ex, null,
                resultInfoFile.getAbsolutePath());
    }

    if (resultInfoFile.exists() == true) {
        if (resultInfoFile.isFile() == true) {
            if (resultInfoFile.canWrite() != true) {
                throw constructTermination("messageResultInfoFileIsntWritable", null, null,
                        resultInfoFile.getAbsolutePath());
            }
        } else {
            throw constructTermination("messageResultInfoPathIsntAFile", null, null,
                    resultInfoFile.getAbsolutePath());
        }
    }

    json_to_xml_1.resultInfoFile = resultInfoFile;

    File jobFile = new File(args[0]);

    try {
        jobFile = jobFile.getCanonicalFile();
    } catch (SecurityException ex) {
        throw constructTermination("messageJobFileCantGetCanonicalPath", ex, null, jobFile.getAbsolutePath());
    } catch (IOException ex) {
        throw constructTermination("messageJobFileCantGetCanonicalPath", ex, null, jobFile.getAbsolutePath());
    }

    if (jobFile.exists() != true) {
        throw constructTermination("messageJobFileDoesntExist", null, null, jobFile.getAbsolutePath());
    }

    if (jobFile.isFile() != true) {
        throw constructTermination("messageJobPathIsntAFile", null, null, jobFile.getAbsolutePath());
    }

    if (jobFile.canRead() != true) {
        throw constructTermination("messageJobFileIsntReadable", null, null, jobFile.getAbsolutePath());
    }

    System.out.println("json_to_xml_1: " + getI10nStringFormatted("messageCallDetails",
            jobFile.getAbsolutePath(), resultInfoFile.getAbsolutePath()));

    File inputFile = null;
    File outputFile = null;

    try {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        InputStream in = new FileInputStream(jobFile);
        XMLEventReader eventReader = inputFactory.createXMLEventReader(in);

        while (eventReader.hasNext() == true) {
            XMLEvent event = eventReader.nextEvent();

            if (event.isStartElement() == true) {
                String tagName = event.asStartElement().getName().getLocalPart();

                if (tagName.equals("json-input-file") == true) {
                    StartElement inputFileElement = event.asStartElement();
                    Attribute pathAttribute = inputFileElement.getAttributeByName(new QName("path"));

                    if (pathAttribute == null) {
                        throw constructTermination("messageJobFileEntryIsMissingAnAttribute", null, null,
                                jobFile.getAbsolutePath(), tagName, "path");
                    }

                    String inputFilePath = pathAttribute.getValue();

                    if (inputFilePath.isEmpty() == true) {
                        throw constructTermination("messageJobFileAttributeValueIsEmpty", null, null,
                                jobFile.getAbsolutePath(), tagName, "path");
                    }

                    inputFile = new File(inputFilePath);

                    if (inputFile.isAbsolute() != true) {
                        inputFile = new File(
                                jobFile.getAbsoluteFile().getParent() + File.separator + inputFilePath);
                    }

                    try {
                        inputFile = inputFile.getCanonicalFile();
                    } catch (SecurityException ex) {
                        throw constructTermination("messageInputFileCantGetCanonicalPath", ex, null,
                                new File(inputFilePath).getAbsolutePath(), jobFile.getAbsolutePath());
                    } catch (IOException ex) {
                        throw constructTermination("messageInputFileCantGetCanonicalPath", ex, null,
                                new File(inputFilePath).getAbsolutePath(), jobFile.getAbsolutePath());
                    }

                    if (inputFile.exists() != true) {
                        throw constructTermination("messageInputFileDoesntExist", null, null,
                                inputFile.getAbsolutePath(), jobFile.getAbsolutePath());
                    }

                    if (inputFile.isFile() != true) {
                        throw constructTermination("messageInputPathIsntAFile", null, null,
                                inputFile.getAbsolutePath(), jobFile.getAbsolutePath());
                    }

                    if (inputFile.canRead() != true) {
                        throw constructTermination("messageInputFileIsntReadable", null, null,
                                inputFile.getAbsolutePath(), jobFile.getAbsolutePath());
                    }
                } else if (tagName.equals("xml-output-file") == true) {
                    StartElement outputFileElement = event.asStartElement();
                    Attribute pathAttribute = outputFileElement.getAttributeByName(new QName("path"));

                    if (pathAttribute == null) {
                        throw constructTermination("messageJobFileEntryIsMissingAnAttribute", null, null,
                                jobFile.getAbsolutePath(), tagName, "path");
                    }

                    String outputFilePath = pathAttribute.getValue();

                    if (outputFilePath.isEmpty() == true) {
                        throw constructTermination("messageJobFileAttributeValueIsEmpty", null, null,
                                jobFile.getAbsolutePath(), tagName, "path");
                    }

                    outputFile = new File(outputFilePath);

                    if (outputFile.isAbsolute() != true) {
                        outputFile = new File(
                                jobFile.getAbsoluteFile().getParent() + File.separator + outputFilePath);
                    }

                    try {
                        outputFile = outputFile.getCanonicalFile();
                    } catch (SecurityException ex) {
                        throw constructTermination("messageOutputFileCantGetCanonicalPath", ex, null,
                                new File(outputFilePath).getAbsolutePath(), jobFile.getAbsolutePath());
                    } catch (IOException ex) {
                        throw constructTermination("messageOutputFileCantGetCanonicalPath", ex, null,
                                new File(outputFilePath).getAbsolutePath(), jobFile.getAbsolutePath());
                    }

                    if (outputFile.exists() == true) {
                        if (outputFile.isFile() == true) {
                            if (outputFile.canWrite() != true) {
                                throw constructTermination("messageOutputFileIsntWritable", null, null,
                                        outputFile.getAbsolutePath());
                            }
                        } else {
                            throw constructTermination("messageOutputPathIsntAFile", null, null,
                                    outputFile.getAbsolutePath());
                        }
                    }
                }
            }
        }
    } catch (XMLStreamException ex) {
        throw constructTermination("messageJobFileErrorWhileReading", ex, null, jobFile.getAbsolutePath());
    } catch (SecurityException ex) {
        throw constructTermination("messageJobFileErrorWhileReading", ex, null, jobFile.getAbsolutePath());
    } catch (IOException ex) {
        throw constructTermination("messageJobFileErrorWhileReading", ex, null, jobFile.getAbsolutePath());
    }

    if (inputFile == null) {
        throw constructTermination("messageJobFileNoInputFile", null, null, jobFile.getAbsolutePath());
    }

    if (outputFile == null) {
        throw constructTermination("messageJobFileNoOutputFile", null, null, jobFile.getAbsolutePath());
    }

    StringBuilder stringBuilder = new StringBuilder();

    try {
        JSONObject json = new JSONObject(new JSONTokener(new BufferedReader(new FileReader(inputFile))));

        stringBuilder.append(XML.toString(json));
    } catch (Exception ex) {
        throw constructTermination("messageConversionError", ex, null, inputFile.getAbsolutePath());
    }

    try {
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));

        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        writer.write(
                "<!-- This file was created by json_to_xml_1, which is free software licensed under the GNU Affero General Public License 3 or any later version (see https://github.com/publishing-systems/digital_publishing_workflow_tools/ and http://www.publishing-systems.org). -->\n");
        writer.write(stringBuilder.toString());

        writer.flush();
        writer.close();
    } catch (FileNotFoundException ex) {
        throw constructTermination("messageOutputFileWritingError", ex, null, outputFile.getAbsolutePath());
    } catch (UnsupportedEncodingException ex) {
        throw constructTermination("messageOutputFileWritingError", ex, null, outputFile.getAbsolutePath());
    } catch (IOException ex) {
        throw constructTermination("messageOutputFileWritingError", ex, null, outputFile.getAbsolutePath());
    }

    return 0;
}