List of usage examples for javax.xml.stream.events XMLEvent isStartElement
public boolean isStartElement();
From source file:org.slc.sli.ingestion.parser.impl.EdfiRecordParserImpl.java
private static String extractTagName(XMLEvent e) { String result = ""; if (e.isEndElement()) { result = e.asEndElement().getName().getLocalPart(); } else if (e.isStartElement()) { result = e.asStartElement().getName().getLocalPart(); }//from ww w . jav a 2 s. c om return result; }
From source file:org.xmlsh.commands.internal.xml2json.java
private boolean parse(XMLEventReader reader, PrintWriter writer, boolean bComma) throws XMLStreamException, CoreException, UnsupportedEncodingException, IOException, TransformException, SaxonApiException { mLevel++;/* w w w . j av a 2 s .c om*/ while (reader.hasNext()) { XMLEvent e = reader.nextEvent(); if (e.isStartElement()) { StartElement start = e.asStartElement(); QName name = start.getName(); if (name.equals(kELEM_XJSON)) { if (mLevel != 1) throw new UnexpectedException("XJSON element must be at document root"); // Children become the new roots mLevel = 0; while (parse(reader, writer, bComma)) ; return false; } else if (name.equals(kELEM_FILE)) { if (!writeFile(start, reader, writer)) return false; } else if (bComma) writer.print(","); if (name.equals(kELEM_OBJECT)) writeObject(start, reader, writer); else if (name.equals(kELEM_ARRAY)) writeArray(start, reader, writer); else if (name.equals(kELEM_MEMBER)) writeMember(start, reader, writer); else if (name.equals(kELEM_NUMBER)) writeNumber(start, reader, writer); else if (name.equals(kELEM_BOOLEAN)) writeBoolean(start, reader, writer); else if (name.equals(kELEM_NULL)) writeNull(reader, writer); else if (name.equals(kELEM_STRING)) writeString(start, reader, writer); else readToEnd(reader); mLevel--; return true; } else if (e.isEndElement()) { mLevel--; return false; } } mLevel--; return false; }
From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.EncryptedCachedDiskStringsTable.java
/** * Reads from the original Excel document the string table and puts it into a * compressed and encrypted file.//w w w. j a v a 2s . c om * * @param is * @throws java.io.IOException */ @Override public void readFrom(InputStream is) throws IOException { this.currentItem = 0; // read from source and write into tempfile // open temp file depending on options compressed/encrypted OutputStream tempOS = null; if ((this.ca != null) || (this.compressTempFile)) { tempOS = new FileOutputStream(this.tempFile); if (this.ca != null) { // encrypt file if configured tempOS = new CipherOutputStream(tempOS, this.ciEncrypt); } if (this.compressTempFile) { // compress file if configured tempOS = new GZIPOutputStream(tempOS, EncryptedCachedDiskStringsTable.compressBufferSize); } } else { // not encrypted and not compressed: configure a random access file for // writing/reading = highest performance this.tempRAF = new RandomAccessFile(this.tempFile, "rw"); } // read from source // use Stax event reader XMLEventReader xer = null; try { xer = StaxHelper.newXMLInputFactory().createXMLEventReader(this.originalIS); while (xer.hasNext()) { XMLEvent xe = xer.nextEvent(); // check if it is a string item (entry in string table) if (xe.isStartElement() && xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("si")) { String siText = this.parseSIText(xer); // add string to temp file this.addString(siText, tempOS); this.count++; } } // close tempfile // make tempFile available as a reader } catch (XMLStreamException e) { LOG.error("Cannot read original SharedStringTable from document. Exception " + e); throw new IOException(e); } catch (FormatNotUnderstoodException e) { LOG.error("Cannot read properly SharedStringTable from document. Exception " + e); throw new IOException(e); } finally { // close temporary Stream (tempfile should be deleted using the close method of // this class and not here) if (tempOS != null) { tempOS.close(); } } // open the input stream towards the temp file this.accessTempFile(0L); }
From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.XSSFPullParser.java
public boolean hasNext() throws XMLStreamException { this.nextBeingCalled = true; if (this.finalized) { // we finished already - no more to read return false; }//from ww w. j a v a 2 s.c om if ((this.currentRow > 1) && (this.currentRow <= this.nextRow)) { // we still have to process an empty row return true; } // search for the next row while (this.xer.hasNext()) { XMLEvent xe = xer.nextEvent(); if (xe.isStartElement() && xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("row")) { // we // found // a row // get row number. Attribute at = xe.asStartElement().getAttributeByName(new QName("r")); String atValue = at.getValue(); this.nextRow = Integer.valueOf(atValue); return true; } } this.finalized = true; return false; }
From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.XSSFPullParser.java
public Object[] getNext() throws XMLStreamException, FormatNotUnderstoodException { Object[] result = null;/*from ww w . j av a 2s . c om*/ if (!this.nextBeingCalled) { // skip to the next tag if (this.hasNext() == false) { return null; } } if (this.finalized) { // no more to read return null; } // check ArrayList<SpreadSheetCellDAO> cells = new ArrayList<>(); if (this.currentRow == this.nextRow) { // only if we have a row to report // read through row, cf. // http://download.microsoft.com/download/3/E/3/3E3435BD-AA68-4B32-B84D-B633F0D0F90D/SpreadsheetMLBasics.ppt int currentCellCount = 0; while (this.xer.hasNext()) { XMLEvent xe = xer.nextEvent(); // read+ if (xe.isEndElement()) { if (xe.asEndElement().getName().getLocalPart().equalsIgnoreCase("row")) { break; // end of row } } else if (xe.isStartElement()) { if (xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("c")) { Attribute cellAddressAT = xe.asStartElement().getAttributeByName(new QName("r")); // check if cell is a subsequent cell and add null, if needed CellAddress currentCellAddress = new CellAddress(cellAddressAT.getValue()); for (int i = currentCellCount; i < currentCellAddress.getColumn(); i++) { cells.add(null); currentCellCount++; } currentCellCount++; // current cell Attribute cellTypeTAT = xe.asStartElement().getAttributeByName(new QName("t")); Attribute cellTypeSAT = xe.asStartElement().getAttributeByName(new QName("s")); String cellFormattedValue = ""; String cellFormula = ""; String cellAddress = cellAddressAT.getValue(); String cellComment = ""; String cellSheetName = this.sheetName; while ((!xe.isEndElement()) || !((xe.isEndElement()) && (xe.asEndElement().getName().getLocalPart().equalsIgnoreCase("c")))) { xe = xer.nextEvent(); if ((xe.isStartElement()) && (xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("v"))) { // if a cell data type is set (e.g. b boolean (covered?), d date in ISO8601 format, e // error, inlineStr (covered), n number (covered), s shared string (covered), str formula string (covered) // we return as string if (cellTypeTAT != null) { XMLEvent xeSubCharacters = xer.nextEvent(); if (!xeSubCharacters.isCharacters()) { LOG.error( "Error parsing excel file. Value attribute (v) of cell does not contains characters"); } else { cellFormattedValue = xeSubCharacters.asCharacters().getData(); if (XSSFPullParser.CELLTYPE_STRING.equals(cellTypeTAT.getValue())) { // need to // read // from // Shared // String // Table int strIdx = Integer.valueOf(cellFormattedValue); if ((this.sst != null) && (this.sst.getCount() > strIdx)) { cellFormattedValue = this.sst.getItemAt(strIdx).getString(); } else { cellFormattedValue = ""; } } else if (XSSFPullParser.CELLTYPE_NUMBER.equals(cellTypeTAT.getValue())) { // need to read number and format it (e.g. if it is a date etc.) according // to style int strStyleIdx = Integer.valueOf(cellTypeSAT.getValue()); XSSFCellStyle cellStyle = this.styles.getStyleAt(strStyleIdx); cellFormattedValue = this.dataFormatter.formatRawCellContents( Double.valueOf(cellFormattedValue), cellStyle.getDataFormat(), cellStyle.getDataFormatString(), this.isDate1904); } } } else { LOG.error("Cannot read celltype"); } } else if ((xe.isStartElement()) && (xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("f"))) { // read formula XMLEvent xeSubCharacters = xer.nextEvent(); if (!xeSubCharacters.isCharacters()) { LOG.error( "Error parsing excel file. Formula attribute (f) of cell does not contains characters"); } else { cellFormula = xeSubCharacters.asCharacters().getData(); } } else if ((xe.isStartElement()) && (xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("is"))) { // read inline string cellFormattedValue = this.parseCellInlineStringText(xer); } } cells.add(new SpreadSheetCellDAO(cellFormattedValue, cellComment, cellFormula, cellAddress, cellSheetName)); } } // else ignore (e.g. col) } } // convert to array result = new SpreadSheetCellDAO[cells.size()]; result = cells.toArray(result); // read all cells in row and create SpreadSheetCellDAOs this.nextBeingCalled = false; this.currentRow++; return result; }
From source file:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java
private String saxFilter(Node node) { try {/* w w w . j a v a2 s . c o m*/ final Transformer transformer = TransformerFactory.newInstance().newTransformer(); final StringWriter w1 = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(w1)); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLEventReader eventReader = xif .createXMLEventReader(new StreamSource(new StringReader(w1.toString()))); XMLEventReader filteredReader = xif.createFilteredReader(eventReader, new EventFilter() { @Override public boolean accept(XMLEvent event) { int type = event.getEventType(); if (type == XMLStreamConstants.START_DOCUMENT || type == XMLStreamConstants.END_DOCUMENT) { return false; } if (event.isStartElement()) { StartElement startElement = (StartElement) event; QName name = startElement.getName(); if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) { return false; } } if (event.isEndElement()) { EndElement endElement = (EndElement) event; QName name = endElement.getName(); if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) { return false; } } return true; } }); StringWriter sw = new StringWriter(); XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLEventWriter writer = xof.createXMLEventWriter(sw); while (filteredReader.hasNext()) { writer.add(filteredReader.nextEvent()); } return sw.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:tds.itemrenderer.processing.ITSDocumentHelper.java
/** * Gets the root element of XML/*w ww .j a va2 s .c o m*/ * * @param uri * @return xml root element * @throws FileNotFoundException * @throws UnsupportedEncodingException * @throws XMLStreamException */ public static String getRootElementName(URI uri) { XMLInputFactory inputFactory = TdsXmlInputFactory.newInstance(); try { InputStream input = null; String uriPath = getUriOriginalString(uri); if (isFtp(uri)) { input = new ByteArrayInputStream(FileFtpHandler.getBytes(uri)); } else { input = new FileInputStream(uriPath); } XMLEventReader inputEventReader = inputFactory.createXMLEventReader(input); while (inputEventReader.hasNext()) { XMLEvent event = inputEventReader.nextEvent(); if (event.isStartElement()) { return event.asStartElement().getName().getLocalPart(); } } } catch (FileNotFoundException | XMLStreamException e) { throw new ITSDocumentProcessingException(e); } return null; }