List of usage examples for javax.xml.stream XMLStreamConstants CHARACTERS
int CHARACTERS
To view the source code for javax.xml.stream XMLStreamConstants CHARACTERS.
Click Source Link
From source file:davmail.exchange.ews.EWSMethod.java
protected String handleTag(XMLStreamReader reader, String localName) throws XMLStreamException { String result = null;/*from ww w. j a v a 2 s . c o m*/ int event = reader.getEventType(); if (event == XMLStreamConstants.START_ELEMENT && localName.equals(reader.getLocalName())) { while (reader.hasNext() && !((event == XMLStreamConstants.END_ELEMENT && localName.equals(reader.getLocalName())))) { event = reader.next(); if (event == XMLStreamConstants.CHARACTERS) { result = reader.getText(); } } } return result; }
From source file:com.clustercontrol.agent.winevent.WinEventMonitor.java
/** * XMLStAX???EventLogRecord????/* w w w. j av a2s. 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:ddf.catalog.source.solr.DynamicSchemaResolver.java
/** * Given xml as a string, this method will parse out element text and CDATA text. It separates * each by one space character./*from ww w .j av a2 s. co m*/ * * @param xmlDatas List of XML as {@code String} * @return parsed CDATA and element text */ protected List<String> parseTextFrom(List<Serializable> xmlDatas) { StringBuilder builder = new StringBuilder(); List<String> parsedTexts = new ArrayList<>(); XMLStreamReader xmlStreamReader = null; StringReader sr = null; long starttime = System.currentTimeMillis(); try { for (Serializable xmlData : xmlDatas) { // xml parser does not handle leading whitespace sr = new StringReader(xmlData.toString()); xmlStreamReader = XML_INPUT_FACTORY.createXMLStreamReader(sr); while (xmlStreamReader.hasNext()) { int event = xmlStreamReader.next(); if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) { String text = xmlStreamReader.getText(); if (StringUtils.isNotBlank(text)) { builder.append(" ").append(text.trim()); } } if (event == XMLStreamConstants.START_ELEMENT) { for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { String text = xmlStreamReader.getAttributeValue(i); if (StringUtils.isNotBlank(text)) { builder.append(" ").append(text.trim()); } } } } parsedTexts.add(builder.toString()); builder.setLength(0); } } catch (XMLStreamException e1) { LOGGER.warn("Failure occurred in parsing the xml data. No data has been stored or indexed.", e1); } finally { IOUtils.closeQuietly(sr); if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (XMLStreamException e) { LOGGER.debug("Exception closing XMLStreamReader", e); } } } long endTime = System.currentTimeMillis(); LOGGER.debug("Parsing took {} ms", endTime - starttime); return parsedTexts; }
From source file:dk.statsbiblioteket.util.xml.XMLStepperTest.java
public void testPipePositionOnIgnoredFail() throws XMLStreamException { ByteArrayOutputStream os = new ByteArrayOutputStream(); XMLStreamWriter out = xmlOutFactory.createXMLStreamWriter(os); XMLStreamReader in = xmlFactory.createXMLStreamReader(new StringReader(SAMPLE)); assertTrue("The first 'bar' should be findable", XMLStepper.findTagStart(in, "bar")); XMLStepper.pipeXML(in, out, false); // until first </bar> assertEquals(// w ww. j av a2 s. c o m "The reader should be positioned at a character tag (newline) but was positioned at " + XMLUtil.eventID2String(in.getEventType()), XMLStreamConstants.CHARACTERS, in.getEventType()); }
From source file:de.uzk.hki.da.model.ObjectPremisXmlWriter.java
/** * Integrate jhove data.// w ww . j a va 2 s . c o m * * @param jhoveFilePath the jhove file path * @param tab the tab * @throws XMLStreamException the xML stream exception * @author Thomas Kleinke * @throws FileNotFoundException */ private void integrateJhoveData(String jhoveFilePath, int tab) throws XMLStreamException, FileNotFoundException { File jhoveFile = new File(jhoveFilePath); if (!jhoveFile.exists()) throw new FileNotFoundException("file does not exist. " + jhoveFile); FileInputStream inputStream = null; inputStream = new FileInputStream(jhoveFile); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(inputStream); boolean textElement = false; while (streamReader.hasNext()) { int event = streamReader.next(); switch (event) { case XMLStreamConstants.START_ELEMENT: 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 (!streamReader.isWhiteSpace()) { writer.writeCharacters(streamReader.getText()); textElement = true; } break; case XMLStreamConstants.END_ELEMENT: tab--; if (!textElement) { writer.writeDTD("\n"); indent(tab); } writer.writeEndElement(); textElement = false; break; default: break; } } streamReader.close(); try { inputStream.close(); } catch (IOException e) { throw new RuntimeException("Failed to close input stream", e); } }
From source file:edu.harvard.iq.safe.lockss.impl.LOCKSSDaemonStatusTableXmlStreamParser.java
/** * * @param stream//from ww w. ja va 2 s .c om * @param encoding */ @Override public void read(InputStream stream, String encoding) { // logger.setLevel(Level.FINE); // 1. create Input factory XMLInputFactory xmlif = XMLInputFactory.newInstance(); xmlif.setProperty("javax.xml.stream.isCoalescing", java.lang.Boolean.TRUE); xmlif.setProperty("javax.xml.stream.isNamespaceAware", java.lang.Boolean.TRUE); long startTime = System.currentTimeMillis(); int noAUs = 0; String aus = null; String currentTableId = null; String currentTableTitle = null; String currentTableKey = null; boolean hasErrorsColumn = false; String siAuId = null; XMLStreamReader xmlr = null; try { // create reader xmlr = xmlif.createXMLStreamReader(new BufferedInputStream(stream), encoding); String curElement = ""; boolean isLastTagnameTable = false; String targetTagName = "row"; String cellTagName = "columnname"; boolean withinSummaryinfo = false; boolean withinColumndescriptor = false; boolean withinRow = false; boolean withinCell = false; boolean withinReference = false; boolean isCrawlStatusActive = false; boolean isCrawlStatusColumn = false; int valueTagCounter = 0; String currentColumnName = null; String currentCellValue = null; String currentCellKey = null; SummaryInfo si = null; List<String> rowData = null; Map<String, String> rowDataH = null; w1: while (xmlr.hasNext()) { int eventType = xmlr.next(); switch (eventType) { case XMLStreamConstants.START_ELEMENT: curElement = xmlr.getLocalName(); // note: getName() -> // QName logger.log(Level.FINE, "--------- start tag = <{0}> ---------", curElement); // check the table name first if (curElement.equals("table")) { isLastTagnameTable = true; } else if (curElement.equals("error")) { isTargetPageValid = false; break w1; } if (isLastTagnameTable) { if (curElement.equals("name")) { currentTableId = xmlr.getElementText(); logger.log(Level.FINE, "########### table Id = [{0}] ###########", currentTableId); tableId = currentTableId; if (belongsInclusionTableList.contains(currentTableId)) { logger.log(Level.FINE, "!!!!! Table ({0}) belongs to the target list !!!!!", currentTableId); } else { logger.log(Level.FINE, "XXXXXXXXXXX Table ({0}) does not belong to the target list XXXXXXXXXXX", currentTableId); break w1; } } else if (curElement.equals("key")) { currentTableKey = xmlr.getElementText(); logger.log(Level.FINE, "---------- table key = ({0}) ----------", currentTableKey); tableKey = currentTableKey; } else if (curElement.equals("title")) { currentTableTitle = xmlr.getElementText(); logger.log(Level.FINE, "+++++++++ table Title = ({0}) +++++++++", currentTableTitle); if (currentTableId.equals("PeerRepair")) { if (currentTableTitle.startsWith("Repair candidates for AU: ")) { currentTableTitle = currentTableTitle.replaceFirst("Repair candidates for AU: ", ""); logger.log(Level.FINE, "save this modified table-Title as auName={0}", currentTableTitle); this.tableTitle = currentTableTitle; } else { logger.log(Level.WARNING, "The table-Title does not start with the expected token={0}", currentTableTitle); } } isLastTagnameTable = false; } } if (curElement.equals("columndescriptor")) { withinColumndescriptor = true; } else if (curElement.equals("row")) { withinRow = true; rowCounter++; logger.log(Level.FINE, "================== {0}-th row starts here ==================", rowCounter); // set-up the table storage //if (rowCounter == 1) { // 1st row rowData = new ArrayList<String>(); rowDataH = new LinkedHashMap<String, String>(); //} } else if (curElement.equals("cell")) { logger.log(Level.FINE, "entering a cell"); withinCell = true; } else if (curElement.equals("reference")) { withinReference = true; logger.log(Level.FINE, "within reference on"); } else if (curElement.equals("summaryinfo")) { withinSummaryinfo = true; si = new SummaryInfo(); } else if (curElement.equals("value")) { logger.log(Level.FINE, "entering a value"); valueTagCounter++; } //---- columndescriptor tag --------------------------------------------------- if (withinColumndescriptor) { if (curElement.equals("name")) { String nameText = xmlr.getElementText(); logger.log(Level.FINE, "\tcolumndescriptor: name = {0}", nameText); columndescriptorList.add(nameText); } else if (curElement.equals("title")) { String titleText = xmlr.getElementText(); logger.log(Level.FINE, "\tcolumndescriptor: title = {0}", titleText); } else if (curElement.equals("type")) { String typeText = xmlr.getElementText(); logger.log(Level.FINE, "\tcolumndescriptor: type = {0}", typeText); getTypeList().add(typeText); } } //---- cell tag ---------------------------------------------------------------- if (withinCell) { logger.log(Level.FINE, "parsing withinCell"); if (curElement.equals("columnname")) { String columnname = xmlr.getElementText(); logger.log(Level.FINE, "\t\tcolumnname = {0}", columnname); currentColumnName = columnname; if (columnname.equals("crawl_status")) { isCrawlStatusColumn = true; } else { isCrawlStatusColumn = false; } if (columnname.equals("Errors")) { hasErrorsColumn = true; } } else { // value tag block: either value-tag WO a child element // or with a child element /* * <value><reference>...<value>xxxx</value> * <value>xxxx</value> */ if ((curElement.equals("value")) && (!withinReference)) { logger.log(Level.FINE, "entering el:value/WO-REF block"); if (!hasReferenceTag.contains(currentColumnName)) { logger.log(Level.FINE, "No child reference tag is expected for this value tag"); logger.log(Level.FINEST, "xmlr.getEventType():pre-parsing={0}", xmlr.getEventType()); String cellValue = xmlr.getElementText(); // note: the above parsing action moves the // cursor to the end-tag, i.e., </value> // therefore, the end-element-switch-block below // cannot catch this </value> tag logger.log(Level.FINE, "\t\t\t[No ref: value] {0} = {1}", new Object[] { currentColumnName, cellValue }); currentCellValue = cellValue; logger.log(Level.FINEST, "xmlr.getEventType():post-parsing={0}", xmlr.getEventType()); // store this value // rowData logger.log(Level.FINE, "current column name={0}", currentColumnName); logger.log(Level.FINE, "valueTagCounter={0}", valueTagCounter); if (currentColumnName.endsWith("Damaged")) { if (valueTagCounter <= 1) { // 2nd value tag is footnot for this column // ignore this value rowData.add(cellValue); rowDataH.put(currentColumnName, currentCellValue); } } else { rowData.add(cellValue); rowDataH.put(currentColumnName, currentCellValue); } } else { // previously this block was unthinkable, but // it was found that there are columns that // temporarily have a <reference> tag in // crawl_status_table; these columns are // included in hasReferenceTag by default; // thus, for such unstable columns, // when they hava a <reference tag, // data are caputred in another within- // reference block; however, when these // columns no longer have <reference> tag, // text data would be left uncaptured unless // some follow-up processing takes place here logger.log(Level.FINE, "May have to capture data: column={0}", currentColumnName); if (mayHaveReferenceTag.contains(currentColumnName) && !isCrawlStatusActive) { // because the crawling is not active, // it is safely assume that the maybe columns have no reference tag // 2011-10-24 the above assumption was found wrong // a crawling cell does not say active but // subsequent columns have a reference logger.log(Level.FINE, "a text or a reference tag : try to parse it as a text"); String cellValue = null; try { cellValue = xmlr.getElementText(); } catch (javax.xml.stream.XMLStreamException ex) { continue; } finally { } logger.log(Level.FINE, "\t\t\t[value WO-ref(crawling_NOT_active case)={0}]", currentColumnName + " = " + cellValue); currentCellValue = cellValue; // store this value // rowData logger.log(Level.FINE, "\t\t\tcurrent columnName={0}", currentColumnName); rowData.add(cellValue); rowDataH.put(currentColumnName, currentCellValue); } else { logger.log(Level.FINE, "WO-Ref: no processing items now:{0}", curElement); } } } else if (withinReference) { // reference tag exists logger.log(Level.FINE, "WR:curElement={0}", curElement); if (curElement.equals("key")) { String cellKey = xmlr.getElementText(); logger.log(Level.FINE, "\t\tcurrentCellKey is set to={0}", cellKey); currentCellKey = cellKey; } else if (curElement.equals("value")) { String cellValue = xmlr.getElementText(); logger.log(Level.FINE, "\t\twr: {0} = {1}", new Object[] { currentColumnName, cellValue }); // exception cases follow: if (currentColumnName.equals("AuName")) { logger.log(Level.FINE, "\t\tAuName is replaced with the key[=AuId]= {0}", currentCellKey); // rowData // This block is for ArchivalUnitStatusTable // add the key as a new datum (auId) // ahead of its value rowData.add(currentCellKey); rowDataH.put("AuId", currentCellKey); currentCellValue = cellValue; } else if (currentColumnName.equals("auId")) { // This block is for V3PollerTable logger.log(Level.FINE, "\t\tnew value for auId(V3PollerTable)={0}", currentCellKey); // deprecated after 2012-02-02: use key as data // currentCellValue = currentCellKey; // add auName as a new column ahead of auId rowData.add(cellValue); rowDataH.put("auName", cellValue); logger.log(Level.FINE, "\t\tauName(V3PollerTable)={0}", cellValue); currentCellValue = currentCellKey; } else if (currentColumnName.equals("pollId")) { // this block is for V3PollerTable logger.log(Level.FINE, "\t\tFull string (key) is used={0}", currentCellKey); // The key has the complete string whereas // the value is its truncated copy currentCellValue = currentCellKey; } else if (currentColumnName.equals("au")) { logger.log(Level.FINE, "\t\tauId is used instead for au(crawl_status_table)={0}", currentCellKey); // 2012-02-02: add auName ahead of au rowData.add(cellValue); rowDataH.put("auName", cellValue); logger.log(Level.FINE, "\t\tauName={0}", cellValue); // rowData // This block is for crawl_status_table // save the key(auId) instead of value currentCellValue = currentCellKey; } else if (currentColumnName.equals("Peers")) { logger.log(Level.FINE, "\t\tURL (key) is used={0}", currentCellKey); currentCellValue = DaemonStatusDataUtil.escapeHtml(currentCellKey); logger.log(Level.FINE, "\t\tAfter encoding ={0}", currentCellValue); } else { if (isCrawlStatusColumn) { // if the craw status column is // "active", some later columns // may have a reference tag // so turn on the switch if (cellValue.equals("Active") || (cellValue.equals("Pending"))) { isCrawlStatusActive = true; } else { isCrawlStatusActive = false; } } // the default processing currentCellValue = cellValue; } // store currentCellValue logger.log(Level.FINE, "currentCellValue={0}", currentCellValue); // rowData rowData.add(currentCellValue); rowDataH.put(currentColumnName, currentCellValue); } // Within ref tag: key and valu processing } // value with text or value with ref tag } // columnname or value } // within cell // ---- summaryinfo tag -------------------------------------------------------- if (withinSummaryinfo) { logger.log(Level.FINE, "============================ Within SummaryInfo ============================ "); if (curElement.equals("title")) { String text = xmlr.getElementText(); si.setTitle(text); logger.log(Level.FINE, "\tsi:titile={0}", si.getTitle()); } else if (curElement.equals("type")) { String text = xmlr.getElementText(); si.setType(Integer.parseInt(text)); logger.log(Level.FINE, "\tsi:type={0}", si.getType()); } else if (curElement.equals("key")) { if (withinReference && si.getTitle().equals("Volume")) { String text = xmlr.getElementText(); logger.log(Level.FINE, "\tsi:key contents(Volume case)={0}", text); siAuId = text; // si.setValue(text); logger.log(Level.FINE, "\tsi:value(Volume case)={0}", siAuId); } } else if (curElement.equals("value")) { if (withinReference) { if (hasRefTitileTagsSI.contains(si.getTitle())) { if (si.getTitle().equals("Volume")) { // 2012-02-02 use the au name String text = xmlr.getElementText(); si.setValue(text); logger.log(Level.FINE, "\tsi:value(Volume case)={0}", si.getValue()); } else { String text = xmlr.getElementText(); si.setValue(text); logger.log(Level.FINE, "\tsi:value={0}", si.getValue()); } } } else { // note: 2012-02-07 // daemon 1.59.2 uses the new layout for AU page // this layout includes a summaryinfo tag // that now contains a reference tag String text = null; try { text = xmlr.getElementText(); if (!hasRefTitileTagsSI.contains(si.getTitle())) { si.setValue(text); logger.log(Level.FINE, "\tsi:value={0}", si.getValue()); } } catch (javax.xml.stream.XMLStreamException ex) { logger.log(Level.WARNING, "encounter a reference tag rather than text"); continue; } finally { } } } /* * aus = xmlr.getElementText(); * out.println("found token=[" + aus + "]"); if * (currentTableId.equals("ArchivalUnitStatusTable")) { * m = pau.matcher(aus); if (m.find()) { * out.println("How many AUs=" + m.group(1)); noAUs = * Integer.parseInt(m.group(1)); } else { * out.println("not found within[" + aus + "]"); } } */ } break; case XMLStreamConstants.CHARACTERS: break; case XMLStreamConstants.ATTRIBUTE: break; case XMLStreamConstants.END_ELEMENT: if (xmlr.getLocalName().equals("columndescriptor")) { withinColumndescriptor = false; logger.log(Level.FINE, "leaving columndescriptor"); } else if (xmlr.getLocalName().equals("row")) { if (withinRow) { logger.log(Level.FINE, "========= end of the target row element"); withinRow = false; } if (!isCrawlStatusActive) { tabularData.add(rowData); tableData.add(rowDataH); } else { rowIgnored++; rowCounter--; } rowData = null; rowDataH = null; isCrawlStatusActive = false; } else if (xmlr.getLocalName().equals("cell")) { // rowDataH.add(cellDatum); cellCounter++; withinCell = false; currentColumnName = null; currentCellValue = null; currentCellKey = null; isCrawlStatusColumn = false; valueTagCounter = 0; logger.log(Level.FINE, "leaving cell"); } else if (xmlr.getLocalName().equals("columnname")) { logger.log(Level.FINE, "leaving columnname"); } else if (xmlr.getLocalName().equals("reference")) { withinReference = false; } else if (xmlr.getLocalName().equals("summaryinfo")) { logger.log(Level.FINE, "si={0}", si.toString()); summaryInfoList.add(si); si = null; withinSummaryinfo = false; } else if (xmlr.getLocalName().equals("value")) { logger.log(Level.FINE, "leaving value"); } else { logger.log(Level.FINE, "--------- end tag = <{0}> ---------", curElement); } break; case XMLStreamConstants.END_DOCUMENT: logger.log(Level.FINE, "Total of {0} row occurrences", rowCounter); } // end: switch } // end:while } catch (XMLStreamException ex) { logger.log(Level.WARNING, "XMLStreamException occurs", ex); this.isTargetPageValid = false; } catch (RuntimeException re) { logger.log(Level.WARNING, "some RuntimeException occurs", re); this.isTargetPageValid = false; } catch (Exception e) { logger.log(Level.WARNING, "some Exception occurs", e); this.isTargetPageValid = false; } finally { // 5. close reader/IO if (xmlr != null) { try { xmlr.close(); } catch (XMLStreamException ex) { logger.log(Level.WARNING, "XMLStreamException occurs during close()", ex); } } if (!this.isTargetPageValid) { logger.log(Level.WARNING, "This parsing session may not be complete due to some exception reported earlier"); } } // end of try if (currentTableId.equals("V3PollerDetailTable")) { summaryInfoList.add(new SummaryInfo("auId", 4, siAuId)); summaryInfoMap = new LinkedHashMap<String, String>(); for (SummaryInfo si : summaryInfoList) { summaryInfoMap.put(si.getTitle(), si.getValue()); } } // parsing summary logger.log(Level.FINE, "###################### parsing summary ######################"); logger.log(Level.FINE, "currentTableId={0}", currentTableId); logger.log(Level.FINE, "currentTableTitle={0}", currentTableTitle); logger.log(Level.FINE, "currentTableKey={0}", currentTableKey); logger.log(Level.FINE, "columndescriptorList={0}", columndescriptorList); logger.log(Level.FINE, "# of columndescriptors={0}", columndescriptorList.size()); logger.log(Level.FINE, "typeList={0}", typeList); logger.log(Level.FINE, "# of rows counted={0}", rowCounter); logger.log(Level.FINE, "# of rows excluded[active ones are excluded]={0}", rowIgnored); logger.log(Level.FINE, "summaryInfoList:size={0}", summaryInfoList.size()); logger.log(Level.FINE, "summaryInfoList={0}", summaryInfoList); logger.log(Level.FINE, "table: cell counts = {0}", cellCounter); logger.log(Level.FINE, "tableData[map]=\n{0}", tableData); logger.log(Level.FINE, "tabularData[list]=\n{0}", tabularData); /* * if (currentTableId.equals("ArchivalUnitStatusTable")) { if * (rowCounter == noAUs) { out.println("au counting is OK=" + * rowCounter); } else { err.println("au counting disagreement"); throw * new RuntimeException("parsing error is suspected"); } } */ logger.log(Level.FINE, " completed in {0} ms\n\n", (System.currentTimeMillis() - startTime)); if (!columndescriptorList.isEmpty()) { int noCols = columndescriptorList.size(); if (currentTableId.equals("V3PollerTable") && !hasErrorsColumn) { noCols--; } int noCellsExpd = rowCounter * noCols; if (noCols > 0) { // this table has a table logger.log(Level.FINE, "checking parsing results: table dimmensions"); if (noCellsExpd == cellCounter) { logger.log(Level.FINE, "table dimensions and cell-count are consistent"); } else { int diff = noCellsExpd - cellCounter; logger.log(Level.FINE, "The table has {0} incomplete cells", diff); hasIncompleteRows = true; setIncompleteRowList(); logger.log(Level.FINE, "incomplete rows: {0}", incompleteRows); } } } }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java
private String getElementText(XMLStreamReader xmlr) throws XMLStreamException { if (xmlr.getEventType() != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("parser must be on START_ELEMENT to read next text", xmlr.getLocation()); }//from w w w.j av a 2 s.c o m int eventType = xmlr.next(); StringBuffer content = new StringBuffer(); while (eventType != XMLStreamConstants.END_ELEMENT) { if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA || eventType == XMLStreamConstants.SPACE /* || eventType == XMLStreamConstants.ENTITY_REFERENCE*/) { content.append(xmlr.getText()); } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT || eventType == XMLStreamConstants.ENTITY_REFERENCE) { // skipping } else if (eventType == XMLStreamConstants.END_DOCUMENT) { throw new XMLStreamException("unexpected end of document when reading element text content"); } else if (eventType == XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("element text content may not contain START_ELEMENT", xmlr.getLocation()); } else { throw new XMLStreamException("Unexpected event type " + eventType, xmlr.getLocation()); } eventType = xmlr.next(); } return content.toString(); }
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();// www .j ava 2s . 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.microsoft.windowsazure.storage.table.TableParser.java
/** * Reserved for internal use. Parses the operation response as an entity. Parses the result returned in the * specified stream in AtomPub format into a {@link TableResult} containing an entity of the specified class type * projected using the specified resolver. * /*from w ww . j a v a 2 s .c om*/ * @param xmlr * An <code>XMLStreamReader</code> on the input stream. * @param clazzType * The class type <code>T</code> implementing {@link TableEntity} for the entity returned. Set to * <code>null</code> to ignore the returned entity and copy only response properties into the * {@link TableResult} object. * @param resolver * An {@link EntityResolver} instance to project the entity into an instance of type <code>R</code>. Set * to <code>null</code> to return the entity as an instance of the class type <code>T</code>. * @param opContext * An {@link OperationContext} object used to track the execution of the operation. * @return * A {@link TableResult} containing the parsed entity result of the operation. * * @throws XMLStreamException * if an error occurs while accessing the stream. * @throws ParseException * if an error occurs while parsing the stream. * @throws InstantiationException * if an error occurs while constructing the result. * @throws IllegalAccessException * if an error occurs in reflection while parsing the result. * @throws StorageException * if a storage service error occurs. */ private static <T extends TableEntity, R> TableResult parseAtomEntity(final XMLStreamReader xmlr, final Class<T> clazzType, final EntityResolver<R> resolver, final OperationContext opContext) throws XMLStreamException, ParseException, InstantiationException, IllegalAccessException, StorageException { int eventType = xmlr.getEventType(); final TableResult res = new TableResult(); xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.ENTRY); String etag = StringEscapeUtils.unescapeHtml4( xmlr.getAttributeValue(ODataConstants.DATA_SERVICES_METADATA_NS, ODataConstants.ETAG)); res.setEtag(etag); while (xmlr.hasNext()) { eventType = xmlr.next(); if (eventType == XMLStreamConstants.CHARACTERS) { xmlr.getText(); continue; } final String name = xmlr.getName().toString(); if (eventType == XMLStreamConstants.START_ELEMENT) { if (name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.ID)) { Utility.readElementFromXMLReader(xmlr, ODataConstants.ID); } else if (name .equals(ODataConstants.BRACKETED_DATA_SERVICES_METADATA_NS + ODataConstants.PROPERTIES)) { // Do read properties if (resolver == null && clazzType == null) { return res; } else { res.setProperties(readAtomProperties(xmlr, opContext)); break; } } } } // Move to end Content eventType = xmlr.next(); if (eventType == XMLStreamConstants.CHARACTERS) { eventType = xmlr.next(); } xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.CONTENT); eventType = xmlr.next(); if (eventType == XMLStreamConstants.CHARACTERS) { eventType = xmlr.next(); } xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.ENTRY); String rowKey = null; String partitionKey = null; Date timestamp = null; // Remove core properties from map and set individually EntityProperty tempProp = res.getProperties().remove(TableConstants.PARTITION_KEY); if (tempProp != null) { partitionKey = tempProp.getValueAsString(); } tempProp = res.getProperties().remove(TableConstants.ROW_KEY); if (tempProp != null) { rowKey = tempProp.getValueAsString(); } tempProp = res.getProperties().remove(TableConstants.TIMESTAMP); if (tempProp != null) { timestamp = tempProp.getValueAsDate(); } if (resolver != null) { // Call resolver res.setResult(resolver.resolve(partitionKey, rowKey, timestamp, res.getProperties(), res.getEtag())); } else if (clazzType != null) { // Generate new entity and return final T entity = clazzType.newInstance(); entity.setEtag(res.getEtag()); entity.setPartitionKey(partitionKey); entity.setRowKey(rowKey); entity.setTimestamp(timestamp); entity.readEntity(res.getProperties(), opContext); res.setResult(entity); } return res; }
From source file:com.microsoft.windowsazure.storage.table.TableParser.java
/** * Reserved for internal use. Parses the operation response as a collection of entities. Reads entity data from the * specified input stream using the specified class type and optionally projects each entity result with the * specified resolver into an {@link ODataPayload} containing a collection of {@link TableResult} objects. * //from w w w . j av a2s . com * @param inStream * The <code>InputStream</code> to read the data to parse from. * @param clazzType * The class type <code>T</code> implementing {@link TableEntity} for the entities returned. Set to * <code>null</code> to ignore the returned entities and copy only response properties into the * {@link TableResult} objects. * @param resolver * An {@link EntityResolver} instance to project the entities into instances of type <code>R</code>. Set * to <code>null</code> to return the entities as instances of the class type <code>T</code>. * @param opContext * An {@link OperationContext} object used to track the execution of the operation. * @return * An {@link ODataPayload} containing a collection of {@link TableResult} objects with the parsed operation * response. * * @throws XMLStreamException * if an error occurs while accessing the stream. * @throws ParseException * if an error occurs while parsing the stream. * @throws InstantiationException * if an error occurs while constructing the result. * @throws IllegalAccessException * if an error occurs in reflection while parsing the result. * @throws StorageException * if a storage service error occurs. */ @SuppressWarnings("unchecked") private static <T extends TableEntity, R> ODataPayload<?> parseAtomQueryResponse(final InputStream inStream, final Class<T> clazzType, final EntityResolver<R> resolver, final OperationContext opContext) throws XMLStreamException, ParseException, InstantiationException, IllegalAccessException, StorageException { ODataPayload<T> corePayload = null; ODataPayload<R> resolvedPayload = null; ODataPayload<?> commonPayload = null; if (resolver != null) { resolvedPayload = new ODataPayload<R>(); commonPayload = resolvedPayload; } else { corePayload = new ODataPayload<T>(); commonPayload = corePayload; } final XMLStreamReader xmlr = Utility.createXMLStreamReaderFromStream(inStream); int eventType = xmlr.getEventType(); xmlr.require(XMLStreamConstants.START_DOCUMENT, null, null); eventType = xmlr.next(); xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.FEED); // skip feed chars eventType = xmlr.next(); while (xmlr.hasNext()) { eventType = xmlr.next(); if (eventType == XMLStreamConstants.CHARACTERS) { xmlr.getText(); continue; } final String name = xmlr.getName().toString(); if (eventType == XMLStreamConstants.START_ELEMENT) { if (name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.ENTRY)) { final TableResult res = parseAtomEntity(xmlr, clazzType, resolver, opContext); if (corePayload != null) { corePayload.tableResults.add(res); } if (resolver != null) { resolvedPayload.results.add((R) res.getResult()); } else { corePayload.results.add((T) res.getResult()); } } } else if (eventType == XMLStreamConstants.END_ELEMENT && name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.FEED)) { break; } } xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.FEED); return commonPayload; }