List of usage examples for javax.xml.stream XMLStreamReader getAttributeCount
public int getAttributeCount();
From source file:org.pentaho.di.trans.steps.webservices.WebService.java
private void compatibleProcessRows(InputStream anXml, Object[] rowData, RowMetaInterface rowMeta, boolean ignoreNamespacePrefix, String encoding) throws KettleException { // First we should get the complete string // The problem is that the string can contain XML or any other format such as HTML saying the service is no longer // available. // We're talking about a WEB service here. // As such, to keep the original parsing scheme, we first read the content. // Then we create an input stream from the content again. // It's elaborate, but that way we can report on the failure more correctly. ///*from w ww.ja v a 2s. co m*/ String response = readStringFromInputStream(anXml, encoding); // Create a new reader to feed into the XML Input Factory below... // StringReader stringReader = new StringReader(response.toString()); // TODO Very empirical : see if we can do something better here try { XMLInputFactory vFactory = XMLInputFactory.newInstance(); XMLStreamReader vReader = vFactory.createXMLStreamReader(stringReader); Object[] outputRowData = RowDataUtil.allocateRowData(data.outputRowMeta.size()); int outputIndex = 0; boolean processing = false; boolean oneValueRowProcessing = false; for (int event = vReader.next(); vReader.hasNext(); event = vReader.next()) { switch (event) { case XMLStreamConstants.START_ELEMENT: // Start new code // START_ELEMENT= 1 // if (log.isRowLevel()) { logRowlevel("START_ELEMENT / " + vReader.getAttributeCount() + " / " + vReader.getNamespaceCount()); } // If we start the xml element named like the return type, // we start a new row // if (log.isRowLevel()) { logRowlevel("vReader.getLocalName = " + vReader.getLocalName()); } if (Const.isEmpty(meta.getOutFieldArgumentName())) { // getOutFieldArgumentName() == null if (oneValueRowProcessing) { WebServiceField field = meta.getFieldOutFromWsName(vReader.getLocalName(), ignoreNamespacePrefix); if (field != null) { outputRowData[outputIndex++] = getValue(vReader.getElementText(), field); putRow(data.outputRowMeta, outputRowData); oneValueRowProcessing = false; } else { if (meta.getOutFieldContainerName().equals(vReader.getLocalName())) { // meta.getOutFieldContainerName() = vReader.getLocalName() if (log.isRowLevel()) { logRowlevel("OutFieldContainerName = " + meta.getOutFieldContainerName()); } oneValueRowProcessing = true; } } } } else { // getOutFieldArgumentName() != null if (log.isRowLevel()) { logRowlevel("OutFieldArgumentName = " + meta.getOutFieldArgumentName()); } if (meta.getOutFieldArgumentName().equals(vReader.getLocalName())) { if (log.isRowLevel()) { logRowlevel("vReader.getLocalName = " + vReader.getLocalName()); } if (log.isRowLevel()) { logRowlevel("OutFieldArgumentName = "); } if (processing) { WebServiceField field = meta.getFieldOutFromWsName(vReader.getLocalName(), ignoreNamespacePrefix); if (field != null) { int index = data.outputRowMeta.indexOfValue(field.getName()); if (index >= 0) { outputRowData[index] = getValue(vReader.getElementText(), field); } } processing = false; } else { WebServiceField field = meta.getFieldOutFromWsName(vReader.getLocalName(), ignoreNamespacePrefix); if (meta.getFieldsOut().size() == 1 && field != null) { // This can be either a simple return element, or a complex type... // try { if (meta.isPassingInputData()) { for (int i = 0; i < rowMeta.getValueMetaList().size(); i++) { ValueMetaInterface valueMeta = getInputRowMeta().getValueMeta(i); outputRowData[outputIndex++] = valueMeta.cloneValueData(rowData[i]); } } outputRowData[outputIndex++] = getValue(vReader.getElementText(), field); putRow(data.outputRowMeta, outputRowData); } catch (WstxParsingException e) { throw new KettleStepException("Unable to get value for field [" + field.getName() + "]. Verify that this is not a complex data type by looking at the response XML.", e); } } else { for (WebServiceField curField : meta.getFieldsOut()) { if (!Const.isEmpty(curField.getName())) { outputRowData[outputIndex++] = getValue(vReader.getElementText(), curField); } } processing = true; } } } else { if (log.isRowLevel()) { logRowlevel("vReader.getLocalName = " + vReader.getLocalName()); } if (log.isRowLevel()) { logRowlevel("OutFieldArgumentName = " + meta.getOutFieldArgumentName()); } } } break; case XMLStreamConstants.END_ELEMENT: // END_ELEMENT= 2 if (log.isRowLevel()) { logRowlevel("END_ELEMENT"); } // If we end the xml element named as the return type, we // finish a row if ((meta.getOutFieldArgumentName() == null && meta.getOperationName().equals(vReader.getLocalName()))) { oneValueRowProcessing = false; } else if (meta.getOutFieldArgumentName() != null && meta.getOutFieldArgumentName().equals(vReader.getLocalName())) { putRow(data.outputRowMeta, outputRowData); processing = false; } break; case XMLStreamConstants.PROCESSING_INSTRUCTION: // PROCESSING_INSTRUCTION= 3 if (log.isRowLevel()) { logRowlevel("PROCESSING_INSTRUCTION"); } break; case XMLStreamConstants.CHARACTERS: // CHARACTERS= 4 if (log.isRowLevel()) { logRowlevel("CHARACTERS"); } break; case XMLStreamConstants.COMMENT: // COMMENT= 5 if (log.isRowLevel()) { logRowlevel("COMMENT"); } break; case XMLStreamConstants.SPACE: // PROCESSING_INSTRUCTION= 6 if (log.isRowLevel()) { logRowlevel("PROCESSING_INSTRUCTION"); } break; case XMLStreamConstants.START_DOCUMENT: // START_DOCUMENT= 7 if (log.isRowLevel()) { logRowlevel("START_DOCUMENT"); } if (log.isRowLevel()) { logRowlevel(vReader.getText()); } break; case XMLStreamConstants.END_DOCUMENT: // END_DOCUMENT= 8 if (log.isRowLevel()) { logRowlevel("END_DOCUMENT"); } break; case XMLStreamConstants.ENTITY_REFERENCE: // ENTITY_REFERENCE= 9 if (log.isRowLevel()) { logRowlevel("ENTITY_REFERENCE"); } break; case XMLStreamConstants.ATTRIBUTE: // ATTRIBUTE= 10 if (log.isRowLevel()) { logRowlevel("ATTRIBUTE"); } break; case XMLStreamConstants.DTD: // DTD= 11 if (log.isRowLevel()) { logRowlevel("DTD"); } break; case XMLStreamConstants.CDATA: // CDATA= 12 if (log.isRowLevel()) { logRowlevel("CDATA"); } break; case XMLStreamConstants.NAMESPACE: // NAMESPACE= 13 if (log.isRowLevel()) { logRowlevel("NAMESPACE"); } break; case XMLStreamConstants.NOTATION_DECLARATION: // NOTATION_DECLARATION= 14 if (log.isRowLevel()) { logRowlevel("NOTATION_DECLARATION"); } break; case XMLStreamConstants.ENTITY_DECLARATION: // ENTITY_DECLARATION= 15 if (log.isRowLevel()) { logRowlevel("ENTITY_DECLARATION"); } break; default: break; } } } catch (Exception e) { throw new KettleStepException( BaseMessages.getString(PKG, "WebServices.ERROR0010.OutputParsingError", response.toString()), e); } }
From source file:org.restcomm.connect.interpreter.rcml.Parser.java
private void start(final Stack<Tag.Builder> builders, final XMLStreamReader stream) { final Tag.Builder builder = Tag.builder(); // Read the next tag. builder.setName(stream.getLocalName()); // Read the attributes. final int limit = stream.getAttributeCount(); for (int index = 0; index < limit; index++) { final String name = stream.getAttributeLocalName(index); final String value = stream.getAttributeValue(index).trim(); final Attribute attribute = new Attribute(name, value); builder.addAttribute(attribute); }/* w w w. j a v a2 s .c o m*/ builders.push(builder); }
From source file:org.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java
private static void updateFile(File configFile, Map<String, PropertySimple> allProps) throws IOException, InterruptedException, XMLStreamException { InputStream in = null;//from w ww . j a va2 s. c om XMLStreamReader rdr = null; OutputStream out = null; XMLStreamWriter outWrt = null; try { Set<String> processedPropertyNames = new HashSet<String>(); in = new BufferedInputStream(new FileInputStream(configFile)); rdr = XML_INPUT_FACTORY.createXMLStreamReader(in); File tmpFile = File.createTempFile("hadoop-plugin", null); out = new FileOutputStream(tmpFile); outWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); ByteArrayOutputStream stash = new ByteArrayOutputStream(); XMLStreamWriter stashWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(stash); boolean outputActive = true; outWrt.writeStartDocument(); while (rdr.hasNext()) { int event = rdr.next(); XMLStreamWriter wrt = outputActive ? outWrt : stashWrt; switch (event) { case XMLStreamConstants.ATTRIBUTE: break; case XMLStreamConstants.CDATA: wrt.writeCData(rdr.getText()); break; case XMLStreamConstants.CHARACTERS: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.COMMENT: wrt.writeComment(rdr.getText()); break; case XMLStreamConstants.DTD: wrt.writeDTD(rdr.getText()); break; case XMLStreamConstants.END_DOCUMENT: wrt.writeEndDocument(); break; case XMLStreamConstants.END_ELEMENT: if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { String encoding = rdr.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } String propertyTagSoFar = Charset.forName(encoding) .decode(ByteBuffer.wrap(stash.toByteArray())).toString(); DetectedPropertyNameAndUpdatedTag propAndTag = updateProperty(propertyTagSoFar, allProps); //yes, we're intentionally circumventing the xml stream writer, because we already have the XML data we want to write. outWrt.flush(); out.write(propAndTag.updatedTag.getBytes("UTF-8")); processedPropertyNames.add(propAndTag.propertyName); //reset stuff stash.reset(); wrt = outWrt; outputActive = true; } else if (CONFIGURATION_TAG_NAME.equals(rdr.getName().getLocalPart())) { //now add the new props for (String prop : processedPropertyNames) { allProps.remove(prop); } for (Map.Entry<String, PropertySimple> e : allProps.entrySet()) { outWrt.writeStartElement(PROPERTY_TAG_NAME); outWrt.writeStartElement(NAME_TAG_NAME); outWrt.writeCharacters(e.getKey()); outWrt.writeEndElement(); outWrt.writeStartElement(VALUE_TAG_NAME); outWrt.writeCharacters(e.getValue().getStringValue()); outWrt.writeEndElement(); outWrt.writeEndElement(); } } wrt.writeEndElement(); break; case XMLStreamConstants.ENTITY_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.ENTITY_REFERENCE: wrt.writeEntityRef(rdr.getText()); break; case XMLStreamConstants.NAMESPACE: for (int i = 0; i < rdr.getNamespaceCount(); ++i) { wrt.writeNamespace(rdr.getNamespacePrefix(i), rdr.getNamespaceURI(i)); } break; case XMLStreamConstants.NOTATION_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.PROCESSING_INSTRUCTION: wrt.writeProcessingInstruction(rdr.getPITarget(), rdr.getPIData()); break; case XMLStreamConstants.SPACE: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.START_DOCUMENT: //this seems to be never called for some strange reason //wrt.writeStartDocument(); break; case XMLStreamConstants.START_ELEMENT: wrt.writeStartElement(rdr.getName().getPrefix(), rdr.getName().getLocalPart(), rdr.getName().getNamespaceURI()); for (int i = 0; i < rdr.getAttributeCount(); ++i) { wrt.writeAttribute(rdr.getAttributePrefix(i), rdr.getAttributeNamespace(i), rdr.getAttributeLocalName(i), rdr.getAttributeValue(i)); } if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { wrt.writeCharacters(""); outputActive = false; } break; } } outWrt.flush(); out.flush(); out.close(); in.close(); //now copy the temp file in the place of the original one FileUtil.copyFile(tmpFile, configFile); } finally { rdr.close(); outWrt.flush(); outWrt.close(); try { in.close(); } finally { out.flush(); out.close(); } } }
From source file:org.sakaiproject.nakamura.importer.ImportSiteArchiveServlet.java
private void processContentXml(InputStream in, String sitePath, Session session, ZipFile zip) throws XMLStreamException { Map<String, Resource> resources = new HashMap<String, Resource>(); String currentResourceId = null; XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(in); for (int event = reader.next(); event != XMLStreamReader.END_DOCUMENT; event = reader.next()) { String localName = null;/*from ww w . jav a 2 s. co m*/ switch (event) { case XMLStreamReader.START_ELEMENT: localName = reader.getLocalName(); if ("archive".equalsIgnoreCase(localName)) { final String system = reader.getAttributeValue(null, "system"); boolean supportedVersion = false; for (String version : supportedVersions) { if (version.equalsIgnoreCase(system)) { supportedVersion = true; } } if (!supportedVersion) { throw new Error("Not a supported version: " + system); } break; } if ("collection".equalsIgnoreCase(localName) || "resource".equalsIgnoreCase(localName)) { // grab the resource's attributes Resource resource = new Resource(); for (int i = 0; i < reader.getAttributeCount(); i++) { resource.attributes.put(reader.getAttributeLocalName(i).toLowerCase(), reader.getAttributeValue(i)); } currentResourceId = resource.getId(); resources.put(currentResourceId, resource); break; } if ("property".equalsIgnoreCase(localName)) { Resource resource = resources.get(currentResourceId); final String name = reader.getAttributeValue(null, "name"); String value = reader.getAttributeValue(null, "value"); if (value != null && !"".equals(value)) { if (reader.getAttributeValue(null, "enc").equalsIgnoreCase("BASE64")) { value = new String(base64.decode(value)); } resource.properties.put(name, value); } break; } break; case XMLStreamReader.END_ELEMENT: localName = reader.getLocalName(); if ("collection".equalsIgnoreCase(localName) || "resource".equalsIgnoreCase(localName)) { makeResource(resources.get(currentResourceId), sitePath, session, zip); } break; } // end switch } // end for reader.close(); }
From source file:org.talend.dataprep.schema.xls.XlsUtils.java
/** * transform all attributes to a Map (key: att name, value: att value) * * @param streamReader/*from w w w.j a va 2 s . co m*/ * @return */ private static Map<String, String> getAttributesNameValue(XMLStreamReader streamReader) { int size = streamReader.getAttributeCount(); if (size > 0) { Map<String, String> attributesValues = new HashMap<>(size); for (int i = 0; i < size; i++) { String attributeName = streamReader.getAttributeLocalName(i); String attributeValue = streamReader.getAttributeValue(i); if (StringUtils.isNotEmpty(attributeName)) { attributesValues.put(attributeName, attributeValue); } } return attributesValues; } return Collections.emptyMap(); }
From source file:org.unitedinternet.cosmo.util.DomReader.java
private static Element readElement(Document d, XMLStreamReader reader) throws XMLStreamException { Element e = null;/* ww w. ja va 2s .com*/ String local = reader.getLocalName(); String ns = reader.getNamespaceURI(); if (ns != null && !ns.equals("")) { String prefix = reader.getPrefix(); String qualified = prefix != null && !prefix.isEmpty() ? prefix + ":" + local : local; e = d.createElementNS(ns, qualified); } else { e = d.createElement(local); } //if (log.isDebugEnabled()) //log.debug("Reading element " + e.getTagName()); for (int i = 0; i < reader.getAttributeCount(); i++) { Attr a = readAttribute(i, d, reader); if (a.getNamespaceURI() != null) { e.setAttributeNodeNS(a); } else { e.setAttributeNode(a); } } return e; }
From source file:rjc.jplanner.model.Plan.java
/***************************************** loadXmlPlan *****************************************/ private int loadXmlPlan(XMLStreamReader xsr) throws XMLStreamException { // as calendars not yet loaded just keep calendar-id int calendarId = -1; // read XML plan attributes for (int i = 0; i < xsr.getAttributeCount(); i++) switch (xsr.getAttributeLocalName(i)) { case XmlLabels.XML_TITLE: m_title = xsr.getAttributeValue(i); break; case XmlLabels.XML_START: m_start = new DateTime(xsr.getAttributeValue(i)); break; case XmlLabels.XML_DT_FORMAT: m_datetimeFormat = xsr.getAttributeValue(i); break; case XmlLabels.XML_D_FORMAT: m_dateFormat = xsr.getAttributeValue(i); break; case XmlLabels.XML_CALENDAR: calendarId = Integer.parseInt(xsr.getAttributeValue(i)); break; case XmlLabels.XML_NOTES: m_notes = xsr.getAttributeValue(i); break; default:/*w ww. j a va2s . co m*/ JPlanner.trace("Unhandled attribute '" + xsr.getAttributeLocalName(i) + "'"); break; } // return calendar-id to be set as default calendar return calendarId; }
From source file:rjc.jplanner.model.Plan.java
/*************************************** loadXmlJPlanner ***************************************/ private void loadXmlJPlanner(XMLStreamReader xsr) throws XMLStreamException { // read XML JPlanner attributes for (int i = 0; i < xsr.getAttributeCount(); i++) switch (xsr.getAttributeLocalName(i)) { case XmlLabels.XML_SAVEUSER: m_savedBy = xsr.getAttributeValue(i); break; case XmlLabels.XML_SAVEWHEN: m_savedWhen = new DateTime(xsr.getAttributeValue(i)); break; case XmlLabels.XML_FORMAT: case XmlLabels.XML_SAVENAME: case XmlLabels.XML_SAVEWHERE: break; default:/*from www .j a va 2s . com*/ JPlanner.trace("Unhandled attribute '" + xsr.getAttributeLocalName(i) + "'"); break; } }
From source file:tpt.dbweb.cat.io.TaggedTextXMLReader.java
private Iterator<TaggedText> getIterator(InputStream is, String errorMessageInfo) { XMLStreamReader tmpxsr = null; try {//from ww w .j av a2 s . c om XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); xif.setProperty(XMLInputFactory.IS_VALIDATING, false); tmpxsr = xif.createXMLStreamReader(is); } catch (XMLStreamException | FactoryConfigurationError e) { e.printStackTrace(); return null; } final XMLStreamReader xsr = tmpxsr; return new PeekIterator<TaggedText>() { @Override protected TaggedText internalNext() { ArrayList<TextSpan> openMarks = new ArrayList<>(); StringBuilder pureTextSB = new StringBuilder(); ArrayList<TextSpan> marks = new ArrayList<>(); marks.add(new TextSpan(null, 0, 0)); TaggedText tt = null; try { loop: while (xsr.hasNext()) { xsr.next(); int event = xsr.getEventType(); switch (event) { case XMLStreamConstants.START_ELEMENT: if ("articles".equals(xsr.getLocalName())) { } else if ("article".equals(xsr.getLocalName())) { tt = new TaggedText(); for (int i = 0; i < xsr.getAttributeCount(); i++) { if ("id".equals(xsr.getAttributeLocalName(i))) { tt.id = xsr.getAttributeValue(i); } tt.info().put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } } else if ("mark".equals(xsr.getLocalName())) { TextSpan tr = new TextSpan(null, pureTextSB.length(), pureTextSB.length()); for (int i = 0; i < xsr.getAttributeCount(); i++) { tr.info().put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } openMarks.add(tr); } else if ("br".equals(xsr.getLocalName())) { // TODO: how to propagate tags from the input to the output? } else { log.warn("ignore tag " + xsr.getLocalName()); } break; case XMLStreamConstants.END_ELEMENT: if ("mark".equals(xsr.getLocalName())) { // search corresponding <mark ...> TextSpan tr = openMarks.remove(openMarks.size() - 1); if (tr == null) { log.warn("markend at " + xsr.getLocation().getCharacterOffset() + " has no corresponding mark tag"); break; } tr.end = pureTextSB.length(); marks.add(tr); } else if ("article".equals(xsr.getLocalName())) { tt.text = StringUtils.stripEnd(pureTextSB.toString().trim(), " \t\n"); pureTextSB = new StringBuilder(); tt.mentions = new ArrayList<>(); for (TextSpan mark : marks) { String entity = mark.info().get("entity"); if (entity == null) { entity = mark.info().get("annotation"); } if (entity != null) { EntityMention e = new EntityMention(tt.text, mark.start, mark.end, entity); String minMention = mark.info().get("min"); String mention = e.getMention(); if (minMention != null && !"".equals(minMention)) { Pattern p = Pattern.compile(Pattern.quote(minMention)); Matcher m = p.matcher(mention); if (m.find()) { TextSpan min = new TextSpan(e.text, e.start + m.start(), e.start + m.end()); e.min = min; if (m.find()) { log.warn("found " + minMention + " two times in \"" + mention + "\""); } } else { String prefix = Utility.findLongestPrefix(mention, minMention); log.warn("didn't find min mention '" + minMention + "' in text '" + mention + "', longest prefix found: '" + prefix + "' in article " + tt.id); } } mark.info().remove("min"); mark.info().remove("entity"); if (mark.info().size() > 0) { e.info().putAll(mark.info()); } tt.mentions.add(e); } } openMarks.clear(); marks.clear(); break loop; } break; case XMLStreamConstants.CHARACTERS: String toadd = xsr.getText(); if (pureTextSB.length() == 0) { toadd = StringUtils.stripStart(toadd, " \t\n"); } if (toadd.contains("thanks")) { log.info("test"); } pureTextSB.append(toadd); break; } } } catch (XMLStreamException e) { log.error("{}", errorMessageInfo); throw new RuntimeException(e); } if (tt != null && tt.mentions != null) { tt.mentions.sort(null); } return tt; } }; }