List of usage examples for javax.xml.stream XMLStreamReader getLocalName
public String getLocalName();
From source file:hudson.plugins.report.jck.parsers.JtregReportParser.java
private JtregBackwardCompatibileSuite parseTestSuite(XMLStreamReader in) throws XMLStreamException, Exception { final String name = findAttributeValue(in, "name"); final String failuresStr = findAttributeValue(in, "failures"); final String errorsStr = findAttributeValue(in, "errors"); final String totalStr = findAttributeValue(in, "tests"); final String totalSkip = findAttributeValue(in, "skipped"); final int failures = tryParseString(failuresStr); final int errors = tryParseString(errorsStr); final int total = tryParseString(totalStr); final int skipped = tryParseString(totalSkip); JtregBackwardCompatibileSuite suite = new JtregBackwardCompatibileSuite(name, failures, errors, total, skipped);/* w ww. java 2s.c o m*/ String statusLine = ""; String stdOutput = ""; String errOutput = ""; while (in.hasNext()) { int event = in.next(); if (event == START_ELEMENT && TESTCASE.equals(in.getLocalName())) { JtregBackwardCompatibileTest test = parseTestcase(in); suite.add(test); continue; } if (event == START_ELEMENT && PROPERTIES.equals(in.getLocalName())) { statusLine = findStatusLine(in); continue; } if (event == START_ELEMENT && SYSTEMOUT.equals(in.getLocalName())) { stdOutput = captureCharacters(in, SYSTEMOUT); continue; } if (event == START_ELEMENT && SYSTEMERR.equals(in.getLocalName())) { errOutput = captureCharacters(in, SYSTEMERR); continue; } if (event == END_ELEMENT && TESTSUITE.equals(in.getLocalName())) { break; } } //order imortant! see revalidateTests List<TestOutput> outputs = Arrays.asList(new TestOutput(SYSTEMOUT, stdOutput), new TestOutput(SYSTEMERR, errOutput)); suite.setStatusLine(statusLine); suite.setOutputs(outputs); return suite; }
From source file:davmail.exchange.ews.EWSMethod.java
protected void addExtendedPropertyValue(XMLStreamReader reader, Item item) throws XMLStreamException { String propertyTag = null;//from w ww . ja v a2s .c o m String propertyValue = null; while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "ExtendedProperty"))) { reader.next(); if (XMLStreamUtil.isStartTag(reader)) { String tagLocalName = reader.getLocalName(); if ("ExtendedFieldURI".equals(tagLocalName)) { propertyTag = getAttributeValue(reader, "PropertyTag"); // property name is in PropertyId or PropertyName with DistinguishedPropertySetId if (propertyTag == null) { propertyTag = getAttributeValue(reader, "PropertyId"); } if (propertyTag == null) { propertyTag = getAttributeValue(reader, "PropertyName"); } } else if ("Value".equals(tagLocalName)) { propertyValue = XMLStreamUtil.getElementText(reader); } else if ("Values".equals(tagLocalName)) { StringBuilder buffer = new StringBuilder(); while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Values"))) { reader.next(); if (XMLStreamUtil.isStartTag(reader)) { if (buffer.length() > 0) { buffer.append(','); } String singleValue = XMLStreamUtil.getElementText(reader); if (singleValue != null) { buffer.append(singleValue); } } } propertyValue = buffer.toString(); } } } if ((propertyTag != null) && (propertyValue != null)) { item.put(propertyTag, propertyValue); } }
From source file:de.uzk.hki.da.model.ObjectPremisXmlWriter.java
/** * Integrate jhove data./* ww w. ja va2 s . c om*/ * * @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:davmail.exchange.ews.EWSMethod.java
protected void handleAttendee(XMLStreamReader reader, Item item, String attendeeType) throws XMLStreamException { Attendee attendee = new Attendee(); if ("RequiredAttendees".equals(attendeeType)) { attendee.role = "REQ-PARTICIPANT"; } else {/*from w w w. jav a2 s.com*/ attendee.role = "OPT-PARTICIPANT"; } while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Attendee"))) { reader.next(); if (XMLStreamUtil.isStartTag(reader)) { String tagLocalName = reader.getLocalName(); if ("EmailAddress".equals(tagLocalName)) { attendee.email = reader.getElementText(); } else if ("Name".equals(tagLocalName)) { attendee.name = XMLStreamUtil.getElementText(reader); } else if ("ResponseType".equals(tagLocalName)) { String responseType = XMLStreamUtil.getElementText(reader); attendee.partstat = responseTypeToPartstat(responseType); } } } item.addAttendee(attendee); }
From source file:ddf.security.assertion.impl.SecurityAssertionImpl.java
/** * Parses the SecurityToken by wrapping within an AssertionWrapper. * * @param securityToken SecurityToken//from www . jav a 2 s .co m */ private void parseToken(SecurityToken securityToken) { XMLStreamReader xmlStreamReader = StaxUtils.createXMLStreamReader(securityToken.getToken()); try { AttrStatement attributeStatement = null; AuthenticationStatement authenticationStatement = null; Attr attribute = null; int attrs = 0; while (xmlStreamReader.hasNext()) { int event = xmlStreamReader.next(); switch (event) { case XMLStreamConstants.START_ELEMENT: { String localName = xmlStreamReader.getLocalName(); switch (localName) { case NameID.DEFAULT_ELEMENT_LOCAL_NAME: name = xmlStreamReader.getElementText(); for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { if (xmlStreamReader.getAttributeLocalName(i).equals(NameID.FORMAT_ATTRIB_NAME)) { nameIDFormat = xmlStreamReader.getAttributeValue(i); break; } } break; case AttributeStatement.DEFAULT_ELEMENT_LOCAL_NAME: attributeStatement = new AttrStatement(); attributeStatements.add(attributeStatement); break; case AuthnStatement.DEFAULT_ELEMENT_LOCAL_NAME: authenticationStatement = new AuthenticationStatement(); authenticationStatements.add(authenticationStatement); attrs = xmlStreamReader.getAttributeCount(); for (int i = 0; i < attrs; i++) { String name = xmlStreamReader.getAttributeLocalName(i); String value = xmlStreamReader.getAttributeValue(i); if (AuthnStatement.AUTHN_INSTANT_ATTRIB_NAME.equals(name)) { authenticationStatement.setAuthnInstant(DateTime.parse(value)); } } break; case AuthnContextClassRef.DEFAULT_ELEMENT_LOCAL_NAME: if (authenticationStatement != null) { String classValue = xmlStreamReader.getText(); classValue = classValue.trim(); AuthenticationContextClassRef authenticationContextClassRef = new AuthenticationContextClassRef(); authenticationContextClassRef.setAuthnContextClassRef(classValue); AuthenticationContext authenticationContext = new AuthenticationContext(); authenticationContext.setAuthnContextClassRef(authenticationContextClassRef); authenticationStatement.setAuthnContext(authenticationContext); } break; case Attribute.DEFAULT_ELEMENT_LOCAL_NAME: attribute = new Attr(); if (attributeStatement != null) { attributeStatement.addAttribute(attribute); } attrs = xmlStreamReader.getAttributeCount(); for (int i = 0; i < attrs; i++) { String name = xmlStreamReader.getAttributeLocalName(i); String value = xmlStreamReader.getAttributeValue(i); if (Attribute.NAME_ATTTRIB_NAME.equals(name)) { attribute.setName(value); } else if (Attribute.NAME_FORMAT_ATTRIB_NAME.equals(name)) { attribute.setNameFormat(value); } } break; case AttributeValue.DEFAULT_ELEMENT_LOCAL_NAME: XSString xsString = new XMLString(); xsString.setValue(xmlStreamReader.getElementText()); if (attribute != null) { attribute.addAttributeValue(xsString); } break; case Issuer.DEFAULT_ELEMENT_LOCAL_NAME: issuer = xmlStreamReader.getElementText(); break; case Conditions.DEFAULT_ELEMENT_LOCAL_NAME: attrs = xmlStreamReader.getAttributeCount(); for (int i = 0; i < attrs; i++) { String name = xmlStreamReader.getAttributeLocalName(i); String value = xmlStreamReader.getAttributeValue(i); if (Conditions.NOT_BEFORE_ATTRIB_NAME.equals(name)) { notBefore = DatatypeConverter.parseDateTime(value).getTime(); } else if (Conditions.NOT_ON_OR_AFTER_ATTRIB_NAME.equals(name)) { notOnOrAfter = DatatypeConverter.parseDateTime(value).getTime(); } } break; case SubjectConfirmation.DEFAULT_ELEMENT_LOCAL_NAME: attrs = xmlStreamReader.getAttributeCount(); for (int i = 0; i < attrs; i++) { String name = xmlStreamReader.getAttributeLocalName(i); String value = xmlStreamReader.getAttributeValue(i); if (SubjectConfirmation.METHOD_ATTRIB_NAME.equals(name)) { subjectConfirmations.add(value); } } case Assertion.DEFAULT_ELEMENT_LOCAL_NAME: attrs = xmlStreamReader.getAttributeCount(); for (int i = 0; i < attrs; i++) { String name = xmlStreamReader.getAttributeLocalName(i); String value = xmlStreamReader.getAttributeValue(i); if (Assertion.VERSION_ATTRIB_NAME.equals(name)) { if ("2.0".equals(value)) { tokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0"; } else if ("1.1".equals(value)) { tokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1"; } } } } break; } case XMLStreamConstants.END_ELEMENT: { String localName = xmlStreamReader.getLocalName(); switch (localName) { case AttributeStatement.DEFAULT_ELEMENT_LOCAL_NAME: attributeStatement = null; break; case Attribute.DEFAULT_ELEMENT_LOCAL_NAME: attribute = null; break; default: break; } break; } } } } catch (XMLStreamException e) { LOGGER.error("Unable to parse security token.", e); } finally { try { xmlStreamReader.close(); } catch (XMLStreamException ignore) { //ignore } } }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java
private void processCatgry(XMLStreamReader xmlr, Map<String, String> valueLabelPairs, List<String> missingValues, String variableName) throws XMLStreamException { boolean isMissing = "Y".equals(xmlr.getAttributeValue(null, "missing")); // (default is N, so null sets missing to false) // STORE -- (TODO NOW) //cat.setDataVariable(dv); //dv.getCategories().add(cat); String varValue = null;/*from w w w . j av a 2 s . c o m*/ String valueLabel = null; for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) { if (event == XMLStreamConstants.START_ELEMENT) { if (xmlr.getLocalName().equals("labl")) { valueLabel = processLabl(xmlr, LEVEL_CATEGORY); } else if (xmlr.getLocalName().equals("catValu")) { varValue = parseText(xmlr, false); } else if (xmlr.getLocalName().equals("catStat")) { // category statistics should not be present in the // TAB file + DDI card ingest: throw new XMLStreamException( "catStat (Category Statistics) section found in a variable section; not supported!"); } } else if (event == XMLStreamConstants.END_ELEMENT) { if (xmlr.getLocalName().equals("catgry")) { if (varValue != null && !varValue.equals("")) { if (valueLabel != null && !valueLabel.equals("")) { dbgLog.fine("DDI Reader: storing label " + valueLabel + " for value " + varValue); valueLabelPairs.put(varValue, valueLabel); } if (isMissing) { missingValues.add(varValue); } } return; } else if (xmlr.getLocalName().equals("catValu")) { // continue; } else if (xmlr.getLocalName().equals("labl")) { // continue; } else { throw new XMLStreamException( "Mismatched DDI Formatting: </catgry> expected, found " + xmlr.getLocalName()); } } } }
From source file:XmlStreamUtils.java
public static String printEvent(XMLStreamReader xmlr) { StringBuffer b = new StringBuffer(); b.append("EVENT:[" + xmlr.getLocation().getLineNumber() + "][" + xmlr.getLocation().getColumnNumber() + "] "); b.append(getName(xmlr.getEventType())); b.append(" ["); switch (xmlr.getEventType()) { case XMLStreamReader.START_ELEMENT: b.append("<"); printName(xmlr, b);//from w ww . j a va2s . co m for (int i = 0; i < xmlr.getNamespaceCount(); i++) { b.append(" "); String n = xmlr.getNamespacePrefix(i); if ("xmlns".equals(n)) { b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\""); } else { b.append("xmlns:" + n); b.append("=\""); b.append(xmlr.getNamespaceURI(i)); b.append("\""); } } for (int i = 0; i < xmlr.getAttributeCount(); i++) { b.append(" "); printName(xmlr.getAttributePrefix(i), xmlr.getAttributeNamespace(i), xmlr.getAttributeLocalName(i), b); b.append("=\""); b.append(xmlr.getAttributeValue(i)); b.append("\""); } b.append(">"); break; case XMLStreamReader.END_ELEMENT: b.append("</"); printName(xmlr, b); for (int i = 0; i < xmlr.getNamespaceCount(); i++) { b.append(" "); String n = xmlr.getNamespacePrefix(i); if ("xmlns".equals(n)) { b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\""); } else { b.append("xmlns:" + n); b.append("=\""); b.append(xmlr.getNamespaceURI(i)); b.append("\""); } } b.append(">"); break; case XMLStreamReader.SPACE: case XMLStreamReader.CHARACTERS: // b.append(xmlr.getText()); int start = xmlr.getTextStart(); int length = xmlr.getTextLength(); b.append(new String(xmlr.getTextCharacters(), start, length)); break; case XMLStreamReader.PROCESSING_INSTRUCTION: String target = xmlr.getPITarget(); if (target == null) target = ""; String data = xmlr.getPIData(); if (data == null) data = ""; b.append("<?"); b.append(target + " " + data); b.append("?>"); break; case XMLStreamReader.CDATA: b.append("<![CDATA["); if (xmlr.hasText()) b.append(xmlr.getText()); b.append("]]>"); break; case XMLStreamReader.COMMENT: b.append("<!--"); if (xmlr.hasText()) b.append(xmlr.getText()); b.append("-->"); break; case XMLStreamReader.ENTITY_REFERENCE: b.append(xmlr.getLocalName() + "="); if (xmlr.hasText()) b.append("[" + xmlr.getText() + "]"); break; case XMLStreamReader.START_DOCUMENT: b.append("<?xml"); b.append(" version='" + xmlr.getVersion() + "'"); b.append(" encoding='" + xmlr.getCharacterEncodingScheme() + "'"); if (xmlr.isStandalone()) b.append(" standalone='yes'"); else b.append(" standalone='no'"); b.append("?>"); break; } b.append("]"); return b.toString(); }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java
private void processVar(XMLStreamReader xmlr, SDIOMetadata smd, int variableNumber) throws XMLStreamException { // Attributes: // ID -- can be ignored (will be reassigned); // name:/* w w w . java2 s. c o m*/ String variableName = xmlr.getAttributeValue(null, "name"); if (variableName == null || variableName.equals("")) { throw new XMLStreamException("NULL or empty variable name attribute."); } variableNameList.add(variableName); // interval type: String varIntervalType = xmlr.getAttributeValue(null, "intrvl"); // OK if not specified; defaults to discrete: varIntervalType = (varIntervalType == null ? VAR_INTERVAL_DISCRETE : varIntervalType); // Number of decimal points: String dcmlAttr = xmlr.getAttributeValue(null, "dcml"); Long dcmlPoints = null; if (dcmlAttr != null && !dcmlAttr.equals("")) { try { dcmlPoints = new Long(dcmlAttr); } catch (NumberFormatException nfe) { throw new XMLStreamException("Invalid variable dcml attribute: " + dcmlAttr); } } // weighed variables not supported yet -- L.A. //dv.setWeighted( VAR_WEIGHTED.equals( xmlr.getAttributeValue(null, "wgt") ) ); // default is not-wgtd, so null sets weighted to false Map<String, String> valueLabelPairs = new LinkedHashMap<String, String>(); List<String> missingValues = new ArrayList<String>(); for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) { if (event == XMLStreamConstants.START_ELEMENT) { if (xmlr.getLocalName().equals("location")) { processLocation(xmlr, smd); } else if (xmlr.getLocalName().equals("labl")) { String _labl = processLabl(xmlr, LEVEL_VARIABLE); if (_labl != null && !_labl.equals("")) { variableLabelMap.put(variableName, _labl); } } else if (xmlr.getLocalName().equals("invalrng")) { processInvalrng(xmlr, smd, variableName); } else if (xmlr.getLocalName().equals("varFormat")) { int simpleVariableFormat = processVarFormat(xmlr, smd, variableName); variableTypeList.add(simpleVariableFormat); } else if (xmlr.getLocalName().equals("catgry")) { processCatgry(xmlr, valueLabelPairs, missingValues, variableName); } else if (xmlr.getLocalName().equals("universe")) { // Should not occur in TAB+DDI ingest (?) // ignore. } else if (xmlr.getLocalName().equals("concept")) { // Same deal. } else if (xmlr.getLocalName().equals("notes")) { // Same. } else if (xmlr.getLocalName().equals("sumStat")) { throw new XMLStreamException( "sumStat (Summary Statistics) section found in a variable section; not supported!"); } // the "todos" below are from DDIServiceBean -- L.A. // todo: qstnTxt: wait to handle until we know more of how we will use it // todo: wgt-var : waitng to see example } else if (event == XMLStreamConstants.END_ELEMENT) { if (xmlr.getLocalName().equals("var")) { // Before returning, set rich variable type metadata: if (variableTypeList.get(variableNumber) == 0) { // this is a numeric variable; // It could be discrete or continuous, with the number // of decimal points explicitely specified. unfVariableTypes.put(variableName, 0); // default printFormatList.add(5); formatCategoryTable.put(variableName, "other"); if ((dcmlPoints != null && dcmlPoints > new Long(0)) || (!varIntervalType.equals(VAR_INTERVAL_DISCRETE))) { unfVariableTypes.put(variableName, 1); decimalVariableSet.add(variableNumber); if (dcmlPoints == null || dcmlPoints == new Long(0)) { dcmlPoints = new Long(7); } printFormatNameTable.put(variableName, "F8." + dcmlPoints); } else { unfVariableTypes.put(variableName, 0); } } else { // this is a string variable. // (or Date; whih is stored as a string, for most purposes, // but requires some special treatment. unfVariableTypes.put(variableName, -1); printFormatList.add(1); if (!"date".equals(formatCategoryTable.get(variableName)) && !"time".equals(formatCategoryTable.get(variableName))) { formatCategoryTable.put(variableName, "other"); } } // Variable Label: // (if it's not supplied, we create an empty placeholder) if (variableLabelMap.get(variableName) == null || variableLabelMap.get(variableName).equals("")) { variableLabelMap.put(variableName, ""); } // Value Labels: if (!valueLabelPairs.isEmpty()) { valueLabelTable.put(variableName, valueLabelPairs); dbgLog.warning("valueLabelTable = " + valueLabelTable.toString()); dbgLog.warning("valueLabelTable = " + valueLabelTable); valueVariableMappingTable.put(variableName, variableName); } // Missing Values: if (!missingValues.isEmpty()) { missingValueTable.put(variableName, missingValues); } dbgLog.info("processed variable number " + variableNumber); return; } } } }
From source file:edu.harvard.iq.safe.lockss.impl.LOCKSSDaemonStatusTableXmlStreamParser.java
/** * * @param stream/*from w w w.j a v a 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:gima.neo4j.testsuite.osmcheck.OSMImporter.java
public void importFile(OSMWriter<?> osmWriter, String dataset, boolean allPoints, Charset charset) throws IOException, XMLStreamException { System.out.println("Importing with osm-writer: " + osmWriter); osmWriter.getOrCreateOSMDataset(layerName); osm_dataset = osmWriter.getDatasetId(); long startTime = System.currentTimeMillis(); long[] times = new long[] { 0L, 0L, 0L, 0L }; javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newInstance(); CountedFileReader reader = new CountedFileReader(dataset, charset); javax.xml.stream.XMLStreamReader parser = factory.createXMLStreamReader(reader); int countXMLTags = 0; beginProgressMonitor(100);/*from ww w. java2s .com*/ setLogContext(dataset); boolean startedWays = false; boolean startedRelations = false; try { ArrayList<String> currentXMLTags = new ArrayList<String>(); int depth = 0; Map<String, Object> wayProperties = null; ArrayList<Long> wayNodes = new ArrayList<Long>(); Map<String, Object> relationProperties = null; ArrayList<Map<String, Object>> relationMembers = new ArrayList<Map<String, Object>>(); LinkedHashMap<String, Object> currentNodeTags = new LinkedHashMap<String, Object>(); while (true) { updateProgressMonitor(reader.getPercentRead()); incrLogContext(); int event = parser.next(); if (event == javax.xml.stream.XMLStreamConstants.END_DOCUMENT) { break; } switch (event) { case javax.xml.stream.XMLStreamConstants.START_ELEMENT: currentXMLTags.add(depth, parser.getLocalName()); String tagPath = currentXMLTags.toString(); if (tagPath.equals("[osm]")) { osmWriter.setDatasetProperties(extractProperties(parser)); } else if (tagPath.equals("[osm, bounds]")) { osmWriter.addOSMBBox(extractProperties("bbox", parser)); } else if (tagPath.equals("[osm, node]")) { // <node id="269682538" lat="56.0420950" lon="12.9693483" user="sanna" uid="31450" visible="true" version="1" changeset="133823" timestamp="2008-06-11T12:36:28Z"/> osmWriter.createOSMNode(extractProperties("node", parser)); } else if (tagPath.equals("[osm, way]")) { // <way id="27359054" user="spull" uid="61533" visible="true" version="8" changeset="4707351" timestamp="2010-05-15T15:39:57Z"> if (!startedWays) { startedWays = true; times[0] = System.currentTimeMillis(); osmWriter.optimize(); times[1] = System.currentTimeMillis(); } wayProperties = extractProperties("way", parser); wayNodes.clear(); } else if (tagPath.equals("[osm, way, nd]")) { Map<String, Object> properties = extractProperties(parser); wayNodes.add(Long.parseLong(properties.get("ref").toString())); } else if (tagPath.endsWith("tag]")) { Map<String, Object> properties = extractProperties(parser); currentNodeTags.put(properties.get("k").toString(), properties.get("v").toString()); } else if (tagPath.equals("[osm, relation]")) { // <relation id="77965" user="Grillo" uid="13957" visible="true" version="24" changeset="5465617" timestamp="2010-08-11T19:25:46Z"> if (!startedRelations) { startedRelations = true; times[2] = System.currentTimeMillis(); osmWriter.optimize(); times[3] = System.currentTimeMillis(); } relationProperties = extractProperties("relation", parser); relationMembers.clear(); } else if (tagPath.equals("[osm, relation, member]")) { relationMembers.add(extractProperties(parser)); } if (startedRelations) { if (countXMLTags < 10) { log("Starting tag at depth " + depth + ": " + currentXMLTags.get(depth) + " - " + currentXMLTags.toString()); for (int i = 0; i < parser.getAttributeCount(); i++) { log("\t" + currentXMLTags.toString() + ": " + parser.getAttributeLocalName(i) + "[" + parser.getAttributeNamespace(i) + "," + parser.getAttributePrefix(i) + "," + parser.getAttributeType(i) + "," + "] = " + parser.getAttributeValue(i)); } } countXMLTags++; } depth++; break; case javax.xml.stream.XMLStreamConstants.END_ELEMENT: if (currentXMLTags.toString().equals("[osm, node]")) { osmWriter.addOSMNodeTags(allPoints, currentNodeTags); } else if (currentXMLTags.toString().equals("[osm, way]")) { osmWriter.createOSMWay(wayProperties, wayNodes, currentNodeTags); } else if (currentXMLTags.toString().equals("[osm, relation]")) { osmWriter.createOSMRelation(relationProperties, relationMembers, currentNodeTags); } depth--; currentXMLTags.remove(depth); // log("Ending tag at depth "+depth+": "+currentTags.get(depth)); break; default: break; } } } finally { endProgressMonitor(); parser.close(); osmWriter.finish(); this.osm_dataset = osmWriter.getDatasetId(); } describeTimes(startTime, times); osmWriter.describeMissing(); osmWriter.describeLoaded(); long stopTime = System.currentTimeMillis(); log("info | Elapsed time in seconds: " + (1.0 * (stopTime - startTime) / 1000.0)); stats.dumpGeomStats(); stats.printTagStats(); }