Example usage for javax.xml.stream XMLStreamReader getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the (local) name of the current event.

Usage

From source file:de.uzk.hki.da.cb.CreatePremisAction.java

/**
 * Accepts a premis.xml file and creates a new xml file for each jhove section  
 * //from w w w.j a  v  a  2s .  c om
 * @author Thomas Kleinke
 * Extract jhove data.
 *
 * @param premisFilePath the premis file path
 * @param outputFolder the output folder
 * @throws XMLStreamException the xML stream exception
 */
public void extractJhoveData(String premisFilePath, String outputFolder) throws XMLStreamException {

    outputFolder += "/premis_output/";

    FileInputStream inputStream = null;

    try {
        inputStream = new FileInputStream(premisFilePath);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("Couldn't find file " + premisFilePath, e);
    }

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(inputStream);

    boolean textElement = false;
    boolean jhoveSection = false;
    boolean objectIdentifierValue = false;
    int tab = 0;
    String fileId = "";

    while (streamReader.hasNext()) {
        int event = streamReader.next();

        switch (event) {

        case XMLStreamConstants.START_ELEMENT:

            if (streamReader.getLocalName().equals("jhove")) {
                jhoveSection = true;
                String outputFilePath = outputFolder + fileId.replace('/', '_').replace('.', '_') + ".xml";
                if (!new File(outputFolder).exists())
                    new File(outputFolder).mkdirs();
                writer = startNewDocument(outputFilePath);
            }

            if (streamReader.getLocalName().equals("objectIdentifierValue"))
                objectIdentifierValue = true;

            if (jhoveSection) {
                writer.writeDTD("\n");
                indent(tab);
                tab++;

                String prefix = streamReader.getPrefix();

                if (prefix != null && !prefix.equals("")) {
                    writer.setPrefix(prefix, streamReader.getNamespaceURI());
                    writer.writeStartElement(streamReader.getNamespaceURI(), streamReader.getLocalName());
                } else
                    writer.writeStartElement(streamReader.getLocalName());

                for (int i = 0; i < streamReader.getNamespaceCount(); i++)
                    writer.writeNamespace(streamReader.getNamespacePrefix(i), streamReader.getNamespaceURI(i));

                for (int i = 0; i < streamReader.getAttributeCount(); i++) {
                    QName qname = streamReader.getAttributeName(i);
                    String attributeName = qname.getLocalPart();
                    String attributePrefix = qname.getPrefix();
                    if (attributePrefix != null && !attributePrefix.equals(""))
                        attributeName = attributePrefix + ":" + attributeName;

                    writer.writeAttribute(attributeName, streamReader.getAttributeValue(i));
                }
            }

            break;

        case XMLStreamConstants.CHARACTERS:
            if (objectIdentifierValue) {
                fileId = streamReader.getText();
                objectIdentifierValue = false;
            }

            if (jhoveSection && !streamReader.isWhiteSpace()) {
                writer.writeCharacters(streamReader.getText());
                textElement = true;
            }
            break;

        case XMLStreamConstants.END_ELEMENT:
            if (jhoveSection) {
                tab--;

                if (!textElement) {
                    writer.writeDTD("\n");
                    indent(tab);
                }

                writer.writeEndElement();
                textElement = false;

                if (streamReader.getLocalName().equals("jhove")) {
                    jhoveSection = false;
                    finalizeDocument();
                }
            }
            break;

        case XMLStreamConstants.END_DOCUMENT:
            streamReader.close();
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException("Failed to close input stream", e);
            }
            break;

        default:
            break;
        }
    }
}

From source file:com.flexive.chemistry.webdav.TextDocumentResource.java

/**
 * Set the value of a property, stream points to the start of the property tag.
 *
 * @param parser    the XML parser/*from   ww  w. j  a v a2s  . co m*/
 * @throws XMLStreamException   on parsing errors
 */
protected void processProperty(XMLStreamReader parser) throws XMLStreamException {
    int level = 0;
    String name = null;
    for (int i = 0; i < parser.getAttributeCount(); i++) {
        if ("name".equals(parser.getAttributeName(i).getLocalPart())) {
            name = parser.getAttributeValue(i);
            break;
        }
    }
    if (name == null) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("property without name attribute encountered");
        }
        return;
    }

    String value = null;
    for (int event = parser.nextTag(); event != XMLStreamConstants.END_DOCUMENT
            && level >= 0; event = parser.nextTag()) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            if ("value".equals(parser.getLocalName())) {
                value = parser.getElementText().trim();
            } else if ("name".equals(parser.getLocalName())) {
                name = parser.getElementText();
            } else {
                level++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            level--;
            break;
        }
    }

    if (value != null) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Setting field " + name + " to " + value);
        }
        try {
            object.setValue(name, value);
        } catch (Exception e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Failed to set field " + name + " (ignored): " + e.getMessage());
            }
        }
    }
}

From source file:com.hp.alm.ali.rest.client.AliRestClient.java

private List<String> getAttributeValues(InputStream xml, String elemName, String attrName) {
    try {//  ww w . j  a v a  2 s.  c o  m
        XMLInputFactory factory = XmlUtils.createBasicInputFactory();
        XMLStreamReader parser;
        parser = factory.createXMLStreamReader(xml);
        List<String> result = new LinkedList<String>();
        while (true) {
            int event = parser.next();
            if (event == XMLStreamConstants.END_DOCUMENT) {
                parser.close();
                break;
            }
            if (event == XMLStreamConstants.START_ELEMENT && elemName.equals(parser.getLocalName())) {

                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    String localName = parser.getAttributeLocalName(i);
                    if (attrName.equals(localName)) {
                        result.add(parser.getAttributeValue(i));
                        break;
                    }
                }
            }
        }
        return result;
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            xml.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:com.clustercontrol.agent.winevent.WinEventMonitor.java

/**
 * XMLStAX???EventLogRecord????/*from   w w w  .  j  av a 2  s  .co  m*/
 * @param eventXmlStream
 * @return EventLogRecord?
 */
private ArrayList<EventLogRecord> parseEventXML(InputStream eventXmlStream) {
    ArrayList<EventLogRecord> eventlogs = new ArrayList<EventLogRecord>();

    try {
        XMLInputFactory xmlif = XMLInputFactory.newInstance();
        /**
         * OpenJDK7/OracleJDK7??"]"?2?????????????????????????????
         * ?XML?????????OpenJDK7/OracleJDK7???????/??????????
         * URL???????????????
         * 
         * URL
         * http://docs.oracle.com/javase/jp/6/api/javax/xml/stream/XMLStreamReader.html#next()
         */
        String xmlCoalescingKey = "javax.xml.stream.isCoalescing";// TODO JRE???????????????????
        if (m_log.isDebugEnabled()) {
            m_log.debug(xmlCoalescingKey + " = true");
        }
        xmlif.setProperty(xmlCoalescingKey, true);
        XMLStreamReader xmlr = xmlif.createXMLStreamReader(eventXmlStream);

        while (xmlr.hasNext()) {
            switch (xmlr.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                m_log.trace("EventType : XMLStreamConstants.START_ELEMENT");

                String localName = xmlr.getLocalName();
                m_log.trace("local name : " + localName);

                if ("Event".equals(localName)) {
                    EventLogRecord eventlog = new EventLogRecord();
                    eventlogs.add(eventlog);
                    m_log.debug("create new EventLogRecord");
                } else {
                    String attrLocalName = null;
                    String attrValue = null;

                    if (xmlr.getAttributeCount() != 0) {
                        attrLocalName = xmlr.getAttributeLocalName(0);
                        attrValue = xmlr.getAttributeValue(0);
                        m_log.trace("attribute local name : " + attrLocalName);
                        m_log.trace("attribute local value : " + attrValue);
                    }

                    if ("Provider".equals(localName)) {
                        if ("Name".equals(attrLocalName)) {
                            m_log.trace("target value : " + attrValue);

                            EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1);
                            eventlog.setProviderName(attrValue);
                            m_log.debug("set ProviderName : " + eventlog.getProviderName());
                        }
                    }
                    // Get-WinEvent/wevtutil.exe
                    else if ("TimeCreated".equals(localName) && "SystemTime".equals(attrLocalName)) {
                        m_log.trace("target value : " + attrValue);

                        // "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'"???S????????????
                        String formatedDateString = attrValue.replaceAll("\\..*Z", "");
                        m_log.trace("formatted target value : " + formatedDateString);
                        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

                        EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1);
                        ;
                        try {
                            eventlog.setTimeCreated(sdf.parse(formatedDateString));
                        } catch (ParseException e) {
                            // do nothing
                            m_log.error("set TimeCreated Error", e);
                        }
                        m_log.debug("set TimeCreated : " + eventlog.getTimeCreated());
                    }
                    // Get-EventLog
                    if ("TimeGenerated".equals(localName) && "SystemTime".equals(attrLocalName)) {
                        m_log.trace("target value : " + attrValue);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'");
                        sdf.setTimeZone(HinemosTime.getTimeZone());

                        EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1);
                        ;
                        try {
                            eventlog.setTimeCreated(sdf.parse(attrValue));
                        } catch (ParseException e) {
                            // do nothing
                            m_log.error("set TimeCreated Error", e);
                        }
                        m_log.debug("set TimeCreated : " + eventlog.getTimeCreated());
                    } else {
                        targetProperty = localName;
                        m_log.trace("target property : " + targetProperty);
                    }
                }

                break;
            case XMLStreamConstants.SPACE:
            case XMLStreamConstants.CHARACTERS:
                m_log.trace("EventType : XMLStreamConstants.CHARACTERS, length=" + xmlr.getTextLength());
                if (targetProperty != null) {
                    try {
                        EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1);
                        ;
                        if ("EventID".equals(targetProperty)) {
                            eventlog.setId(Integer.parseInt(new String(xmlr.getTextCharacters(),
                                    xmlr.getTextStart(), xmlr.getTextLength())));
                            m_log.debug("set EventID : " + eventlog.getId());
                        }
                        // Get-WinEvent/wevtutil.exe
                        else if ("Level".equals(targetProperty)) {
                            if (eventlog.getLevel() == WinEventConstant.UNDEFINED) {
                                eventlog.setLevel(Integer.parseInt(new String(xmlr.getTextCharacters(),
                                        xmlr.getTextStart(), xmlr.getTextLength())));
                                m_log.debug("set Level : " + eventlog.getLevel());
                            }
                        } else if ("Task".equals(targetProperty)) {
                            if (eventlog.getTask() == WinEventConstant.UNDEFINED) {
                                eventlog.setTask(Integer.parseInt(new String(xmlr.getTextCharacters(),
                                        xmlr.getTextStart(), xmlr.getTextLength())));
                                m_log.debug("set Task : " + eventlog.getTask());
                            }
                        } else if ("Keywords".equals(targetProperty)) {
                            // TODO ????????0x8080000000000000
                            //eventlog.setKeywords(Long.decode(new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength())));
                            //m_log.debug("set Keywords : " + eventlog.getKeywords());
                        } else if ("EventRecordId".equals(targetProperty)) {
                            eventlog.setRecordId(Long.parseLong(new String(xmlr.getTextCharacters(),
                                    xmlr.getTextStart(), xmlr.getTextLength())));
                            m_log.debug("set RecordId : " + eventlog.getRecordId());
                        } else if ("Channel".equals(targetProperty)) {
                            eventlog.setLogName(new String(xmlr.getTextCharacters(), xmlr.getTextStart(),
                                    xmlr.getTextLength()));
                            m_log.debug("set LogName : " + eventlog.getLogName());
                        } else if ("Computer".equals(targetProperty)) {
                            eventlog.setMachineName(new String(xmlr.getTextCharacters(), xmlr.getTextStart(),
                                    xmlr.getTextLength()));
                            m_log.debug("set MachineName : " + eventlog.getMachineName());
                        } else if ("Message".equals(targetProperty)) {
                            String message = new String(xmlr.getTextCharacters(), xmlr.getTextStart(),
                                    xmlr.getTextLength());
                            message = message.replaceAll(tmpReturnCode, "\r\n");
                            message = message.replaceAll(tmpLtCode, "<");
                            message = message.replaceAll(tmpGtCode, ">");
                            eventlog.setMessage(message);
                            m_log.debug("set Message : " + eventlog.getMessage());
                        } else if ("Data".equals(targetProperty)) {
                            String data = new String(xmlr.getTextCharacters(), xmlr.getTextStart(),
                                    xmlr.getTextLength());
                            eventlog.getData().add(data);
                            m_log.debug("set Data : " + data);
                        } else {
                            m_log.debug("unknown target property : " + targetProperty);
                        }
                    } catch (NumberFormatException e) {
                        m_log.debug("number parse error", e);
                    }
                }
                targetProperty = null;
                break;
            default: // 
                break;
            }
            xmlr.next();
        }
        xmlr.close();
    } catch (XMLStreamException e) {
        m_log.warn("parseEvent() xmlstream error", e);
    }

    return eventlogs;

}

From source file:com.cedarsoft.serialization.test.performance.XmlParserPerformance.java

private void benchParse(XMLInputFactory inputFactory, @Nonnull String contentSample) throws XMLStreamException {
    for (int i = 0; i < BIG; i++) {
        XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(contentSample));

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getLocalName());
        assertEquals("fileType", parser.getName().getLocalPart());

        boolean dependent = Boolean.parseBoolean(parser.getAttributeValue(null, "dependent"));

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("id", parser.getName().getLocalPart());
        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String id = parser.getText();

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getName().getLocalPart());

        boolean isDefault = Boolean.parseBoolean(parser.getAttributeValue(null, "default"));
        String delimiter = parser.getAttributeValue(null, "delimiter");

        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String extension = parser.getText();
        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getName().getLocalPart());

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getName().getLocalPart());
        assertEquals(XMLStreamReader.END_DOCUMENT, parser.next());

        parser.close();// ww w .ja v a  2  s .  c  o m

        FileType type = new FileType(id, new Extension(delimiter, extension, isDefault), dependent);
        assertNotNull(type);
    }
}

From source file:edu.indiana.d2i.htrc.portal.HTRCPersistenceAPIClient.java

private AlgorithmDetailsBean parseAlgorithmDetailBean(InputStream stream) throws XMLStreamException {
    AlgorithmDetailsBean res = new AlgorithmDetailsBean();
    XMLStreamReader parser = factory.createXMLStreamReader(stream);

    List<AlgorithmDetailsBean.Parameter> parameters = new ArrayList<AlgorithmDetailsBean.Parameter>();
    List<String> authors = new ArrayList<String>();
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.hasName()) {
                // only parse the info tag!
                if (parser.getLocalName().equals(AlgorithmDetailsBean.NAME)) {
                    res.setName(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.VERSION)) {
                    res.setVersion(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.DESCRIPTION)) {
                    res.setDescription(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.SUPPORTURL)) {
                    res.setSupportUrl(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.PARAMETER)) {
                    AlgorithmDetailsBean.Parameter parameter = new AlgorithmDetailsBean.Parameter();
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        if (parser.getAttributeLocalName(i).equals("required"))
                            parameter.setRequired(Boolean.valueOf(parser.getAttributeValue(i)));
                        if (parser.getAttributeLocalName(i).equals("type"))
                            parameter.setType(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("name"))
                            parameter.setName(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("defaultValue"))
                            parameter.setDefaultValue(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("validation"))
                            parameter.setValidation(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("validationError"))
                            parameter.setValidationError(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("readOnly"))
                            parameter.setReadOnly(Boolean.parseBoolean(parser.getAttributeValue(i)));
                    }// w  ww  .  ja  va 2  s  .c  o  m
                    parser.nextTag();
                    if (parser.getLocalName().equals("label"))
                        parameter.setLabel(parser.getElementText());
                    parser.nextTag();
                    if (parser.getLocalName().equals("description"))
                        parameter.setDescription(parser.getElementText());
                    parameters.add(parameter);
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.AUTHOR)) {
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        if (parser.getAttributeLocalName(i).equals("name"))
                            authors.add(parser.getAttributeValue(i));
                    }
                }
            }
        }
    }
    res.setParameters(parameters);
    res.setAuthors(authors);
    return res;
}

From source file:com.cedarsoft.serialization.test.performance.XmlParserPerformance.java

private void benchParse(javolution.xml.stream.XMLInputFactory inputFactory)
        throws XMLStreamException, javolution.xml.stream.XMLStreamException {
    for (int i = 0; i < BIG; i++) {
        javolution.xml.stream.XMLStreamReader parser = inputFactory
                .createXMLStreamReader(new StringReader(CONTENT_SAMPLE));

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getLocalName().toString());

        boolean dependent = Boolean.parseBoolean(parser.getAttributeValue(null, "dependent").toString());

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("id", parser.getLocalName().toString());
        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String id = parser.getText().toString();

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("id", parser.getLocalName().toString());

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getLocalName().toString());

        boolean isDefault = Boolean.parseBoolean(parser.getAttributeValue(null, "default").toString());
        String delimiter = parser.getAttributeValue(null, "delimiter").toString();

        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String extension = parser.getText().toString();

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getLocalName().toString());

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getLocalName().toString());
        assertEquals(XMLStreamReader.END_DOCUMENT, parser.next());

        parser.close();//w  w  w. jav a2s.c o  m

        FileType type = new FileType(id, new Extension(delimiter, extension, isDefault), dependent);
        assertNotNull(type);
    }
}

From source file:net.cloudkit.toolkit.DownloadTest.java

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@Transactional/*  w w w  .  j  a  va2s  .com*/
@Test
public void test() throws Exception {

    Map<String, Object> cdl = new HashMap<>();
    Map<String, String> cdlBillHead = null;
    Map<String, String> cdlBillList = null;
    List<Map<String, String>> cdlBillLists = new ArrayList<>();
    Map<String, String> cdlBillDoc = null;
    List<Map<String, String>> cdlBillDocs = new ArrayList<>();

    // MESSAGE_CONFIG
    // InputStream
    InputStream in = null;
    try {

        // StAX?
        XMLInputFactory xif = XMLInputFactory.newInstance();
        // ?
        XMLStreamReader reader = xif
                .createXMLStreamReader(new FileInputStream(new File("D:\\temp\\bill\\CDL100000006531646.xml")));

        // 
        while (reader.hasNext()) {
            // ?
            int event = reader.next();

            // 
            // if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
            if (event == XMLStreamReader.START_ELEMENT) {
                // CdlBill/CdlBillHead
                if ("CdlBillHead".equals(reader.getLocalName())) {
                    cdlBillHead = new LinkedHashMap<>();
                }
                {
                    // BillSeq
                    if ("BillSeq".equals(reader.getLocalName())) {
                        cdlBillHead.put("BillSeq", reader.getElementText());
                    }
                    // ListNo
                    if ("ListNo".equals(reader.getLocalName())) {
                        cdlBillHead.put("ListNo", reader.getElementText());
                    }
                    // IEFlag
                    if ("IEFlag".equals(reader.getLocalName())) {
                        cdlBillHead.put("IEFlag", reader.getElementText());
                    }
                    // IEPort
                    if ("IEPort".equals(reader.getLocalName())) {
                        cdlBillHead.put("IEPort", reader.getElementText());
                    }
                    // IEDate
                    if ("IEDate".equals(reader.getLocalName())) {
                        cdlBillHead.put("IEDate", reader.getElementText());
                    }
                    // RecordsNo
                    if ("RecordsNo".equals(reader.getLocalName())) {
                        cdlBillHead.put("RecordsNo", reader.getElementText());
                    }
                    // TradeCode
                    if ("TradeCode".equals(reader.getLocalName())) {
                        cdlBillHead.put("TradeCode", reader.getElementText());
                    }
                    // TradeName
                    if ("TradeName".equals(reader.getLocalName())) {
                        cdlBillHead.put("TradeName", reader.getElementText());
                    }
                    // OwnerCode
                    if ("OwnerCode".equals(reader.getLocalName())) {
                        cdlBillHead.put("OwnerCode", reader.getElementText());
                    }
                    // OwnerName
                    if ("OwnerName".equals(reader.getLocalName())) {
                        cdlBillHead.put("OwnerName", reader.getElementText());
                    }
                    // TrafMode
                    if ("TrafMode".equals(reader.getLocalName())) {
                        cdlBillHead.put("TrafMode", reader.getElementText());
                    }
                    // ShipName
                    if ("ShipName".equals(reader.getLocalName())) {
                        cdlBillHead.put("ShipName", reader.getElementText());
                    }
                    // VoyageNo
                    if ("VoyageNo".equals(reader.getLocalName())) {
                        cdlBillHead.put("VoyageNo", reader.getElementText());
                    }
                    // BillNo
                    if ("BillNo".equals(reader.getLocalName())) {
                        cdlBillHead.put("BillNo", reader.getElementText());
                    }
                    // TradeMode
                    if ("TradeMode".equals(reader.getLocalName())) {
                        cdlBillHead.put("TradeMode", reader.getElementText());
                    }
                    // TradeCountry
                    if ("TradeCountry".equals(reader.getLocalName())) {
                        cdlBillHead.put("TradeCountry", reader.getElementText());
                    }
                    // DestinationPort
                    if ("DestinationPort".equals(reader.getLocalName())) {
                        cdlBillHead.put("DestinationPort", reader.getElementText());
                    }
                    // DistrictCode
                    if ("DistrictCode".equals(reader.getLocalName())) {
                        cdlBillHead.put("DistrictCode", reader.getElementText());
                    }
                    // PackNum
                    if ("PackNum".equals(reader.getLocalName())) {
                        cdlBillHead.put("PackNum", reader.getElementText());
                    }
                    // WrapType
                    if ("WrapType".equals(reader.getLocalName())) {
                        cdlBillHead.put("WrapType", reader.getElementText());
                    }
                    // GrossWt
                    if ("GrossWt".equals(reader.getLocalName())) {
                        cdlBillHead.put("GrossWt", reader.getElementText());
                    }
                    // NetWt
                    if ("NetWt".equals(reader.getLocalName())) {
                        cdlBillHead.put("NetWt", reader.getElementText());
                    }
                    // ListType
                    if ("ListType".equals(reader.getLocalName())) {
                        cdlBillHead.put("ListType", reader.getElementText());
                    }
                    // ListStat
                    if ("ListStat".equals(reader.getLocalName())) {
                        cdlBillHead.put("ListStat", reader.getElementText());
                    }
                    // DocuCodeCom
                    if ("DocuCodeCom".equals(reader.getLocalName())) {
                        cdlBillHead.put("DocuCodeCom", reader.getElementText());
                    }
                    // AgentCode
                    if ("AgentCode".equals(reader.getLocalName())) {
                        cdlBillHead.put("AgentCode", reader.getElementText());
                    }
                    // AgentName
                    if ("AgentName".equals(reader.getLocalName())) {
                        cdlBillHead.put("AgentName", reader.getElementText());
                    }
                    // DeclCustom
                    if ("DeclCustom".equals(reader.getLocalName())) {
                        cdlBillHead.put("DeclCustom", reader.getElementText());
                    }
                    // DeclDate
                    if ("DeclDate".equals(reader.getLocalName())) {
                        cdlBillHead.put("DeclDate", reader.getElementText());
                    }
                    // GType
                    if ("GType".equals(reader.getLocalName())) {
                        cdlBillHead.put("GType", reader.getElementText());
                    }

                }

                // CdlBill/CdlBillLists/CdlBillList
                if ("CdlBillList".equals(reader.getLocalName())) {
                    cdlBillList = new LinkedHashMap<>();
                }
                {
                    // GNo
                    if ("GNo".equals(reader.getLocalName())) {
                        cdlBillList.put("GNo", reader.getElementText());
                    }
                    // ItemNo
                    if ("ItemNo".equals(reader.getLocalName())) {
                        cdlBillList.put("ItemNo", reader.getElementText());
                    }
                    // CodeTs
                    if ("CodeTs".equals(reader.getLocalName())) {
                        cdlBillList.put("CodeTs", reader.getElementText());
                    }
                    // GName
                    if ("GName".equals(reader.getLocalName())) {
                        cdlBillList.put("GName", reader.getElementText());
                    }
                    // GModel
                    if ("GModel".equals(reader.getLocalName())) {
                        cdlBillList.put("GModel", reader.getElementText());
                    }
                    // GQty
                    if ("GQty".equals(reader.getLocalName())) {
                        cdlBillList.put("GQty", reader.getElementText());
                    }
                    // GUnit
                    if ("GUnit".equals(reader.getLocalName())) {
                        cdlBillList.put("GUnit", reader.getElementText());
                    }
                    // DeclPrice
                    if ("DeclPrice".equals(reader.getLocalName())) {
                        cdlBillList.put("DeclPrice", reader.getElementText());
                    }
                    // DeclTotal
                    if ("DeclTotal".equals(reader.getLocalName())) {
                        cdlBillList.put("DeclTotal", reader.getElementText());
                    }
                    // TradeCurr
                    if ("TradeCurr".equals(reader.getLocalName())) {
                        cdlBillList.put("TradeCurr", reader.getElementText());
                    }
                    // OriginalalCountry
                    if ("OriginalalCountry".equals(reader.getLocalName())) {
                        cdlBillList.put("OriginalalCountry", reader.getElementText());
                    }
                    // DutyMode
                    if ("DutyMode".equals(reader.getLocalName())) {
                        cdlBillList.put("DutyMode", reader.getElementText());
                    }
                    // Unit1
                    if ("Unit1".equals(reader.getLocalName())) {
                        cdlBillList.put("Unit1", reader.getElementText());
                    }
                    // Unit2
                    if ("Unit2".equals(reader.getLocalName())) {
                        cdlBillList.put("Unit2", reader.getElementText());
                    }
                }

                // CdlBill/CdlBillDocs/CdlBillDoc
                if ("CdlBillDoc".equals(reader.getLocalName())) {
                    cdlBillDoc = new LinkedHashMap<>();
                }
                {
                    // DocCode
                    if ("DocCode".equals(reader.getLocalName())) {
                        cdlBillDoc.put("DocCode", reader.getElementText());
                    }
                    // CertNo
                    if ("CertNo".equals(reader.getLocalName())) {
                        cdlBillDoc.put("CertNo", reader.getElementText());
                    }
                }
            }

            if (event == XMLStreamReader.END_ELEMENT) {
                // CdlBill/CdlBillHead
                if ("CdlBillHead".equals(reader.getLocalName())) {

                }

                // CdlBill/CdlBillLists/CdlBillList
                if ("CdlBillList".equals(reader.getLocalName())) {
                    cdlBillLists.add(cdlBillList);
                }

                // CdlBill/CdlBillDocs/CdlBillDoc
                if ("CdlBillDoc".equals(reader.getLocalName())) {
                    cdlBillDocs.add(cdlBillDoc);
                }
            }
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    cdl.put("CdlBillHead", cdlBillHead);
    cdl.put("CdlBillLists", cdlBillLists);
    cdl.put("CdlBillDocs", cdlBillDocs);
    System.out.println(cdlBillLists.size() + " " + cdlBillDocs.size());
    persistentRepositoryService.save(cdl);

}

From source file:davmail.exchange.ews.EWSMethod.java

protected String getTagContent(XMLStreamReader reader) throws XMLStreamException {
    String tagLocalName = reader.getLocalName();
    while (reader.hasNext() && !(reader.getEventType() == XMLStreamConstants.END_ELEMENT)) {
        reader.next();//from   ww w.  j av a 2  s. c  o m
        if (reader.getEventType() == XMLStreamConstants.CHARACTERS) {
            return reader.getText();
        }
    }
    // empty tag
    if (reader.hasNext()) {
        return null;
    } else {
        throw new XMLStreamException("End element for " + tagLocalName + " not found");
    }
}

From source file:com.rapidminer.gui.properties.OperatorPropertyPanel.java

/**
 * Starts a progress thread which parses the parameter descriptions for the provided operator ,
 * cleans the {@link #parameterDescriptionCache}, and stores parsed descriptions in the
 * {@link #parameterDescriptionCache}.//from  w w  w  .  j  ava  2s  .co  m
 */
private void parseParameterDescriptions(final Operator operator) {
    parameterDescriptionCache.clear();
    URL documentationURL = OperatorDocumentationBrowser.getDocResourcePath(operator);
    if (documentationURL != null) {
        try (InputStream documentationStream = documentationURL.openStream()) {
            XMLStreamReader reader = XML_STREAM_FACTORY.createXMLStreamReader(documentationStream);
            String parameterKey = null;

            // The builder that stores the parameter description text
            StringBuilder parameterTextBuilder = null;
            boolean inParameters = false;
            while (reader.hasNext()) {
                switch (reader.next()) {
                case XMLStreamReader.START_ELEMENT:
                    if (!inParameters && reader.getLocalName().equals(TAG_PARAMETERS)) {
                        inParameters = true;
                    } else {
                        AttributesImpl attributes = new AttributesImpl();
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            attributes.addAttribute("", reader.getAttributeLocalName(i),
                                    reader.getAttributeName(i).toString(), reader.getAttributeType(i),
                                    reader.getAttributeValue(i));
                        }

                        // Check if no parameter was found
                        if (reader.getLocalName().equals(TAG_PARAMETER)) {
                            parameterKey = attributes.getValue(ATTRIBUTE_PARAMETER_KEY);

                            // In case a parameter key was found, create a new string
                            // builder
                            if (parameterKey != null) {
                                parameterTextBuilder = new StringBuilder();
                            }
                        }

                        if (parameterTextBuilder != null) {
                            appendParameterStartTag(reader.getLocalName(), attributes, parameterTextBuilder);
                        }
                    }
                    break;
                case XMLStreamReader.END_ELEMENT:
                    // end parsing when end of parameters element is reached
                    if (reader.getLocalName().equals(TAG_PARAMETERS)) {
                        return;
                    }

                    if (parameterTextBuilder != null) {

                        // otherwise add element to description text
                        parameterTextBuilder.append("</");
                        parameterTextBuilder.append(reader.getLocalName());
                        parameterTextBuilder.append(">");

                        // Store description when parameter element ends
                        if (reader.getLocalName().equals(TAG_PARAMETER)) {
                            final String parameterDescription = parameterTextBuilder.toString();
                            final String key = parameterKey;
                            if (!parameterDescriptionCache.containsKey(parameterKey)) {
                                Source xmlSource = new StreamSource(new StringReader(parameterDescription));
                                try {
                                    String desc = OperatorDocToHtmlConverter.applyXSLTTransformation(xmlSource);
                                    parameterDescriptionCache.put(key, StringEscapeUtils.unescapeHtml(desc));
                                } catch (TransformerException e) {
                                    // ignore
                                }
                            }
                        }
                    }
                    break;
                case XMLStreamReader.CHARACTERS:
                    if (parameterTextBuilder != null) {
                        parameterTextBuilder.append(StringEscapeUtils.escapeHtml(reader.getText()));
                    }
                    break;
                default:
                    // ignore other events
                    break;
                }
            }
        } catch (IOException | XMLStreamException e) {
            // ignore
        }
    }
}