List of usage examples for javax.xml.stream XMLStreamReader getAttributeValue
public String getAttributeValue(int index);
From source file:org.neo4j.gis.spatial.osm.OSMImporter.java
private Map<String, Object> extractProperties(String name, XMLStreamReader parser) { // <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"/> // <way id="27359054" user="spull" uid="61533" visible="true" // version="8" changeset="4707351" timestamp="2010-05-15T15:39:57Z"> // <relation id="77965" user="Grillo" uid="13957" visible="true" // version="24" changeset="5465617" timestamp="2010-08-11T19:25:46Z"> LinkedHashMap<String, Object> properties = new LinkedHashMap<String, Object>(); for (int i = 0; i < parser.getAttributeCount(); i++) { String prop = parser.getAttributeLocalName(i); String value = parser.getAttributeValue(i); if (name != null && prop.equals("id")) { prop = name + "_osm_id"; name = null;/*from w w w. j ava2 s . com*/ } if (prop.equals("lat") || prop.equals("lon")) { properties.put(prop, Double.parseDouble(value)); } else if (name != null && prop.equals("version")) { properties.put(prop, Integer.parseInt(value)); } else if (prop.equals("visible")) { if (!value.equals("true") && !value.equals("1")) { properties.put(prop, false); } } else if (prop.equals("timestamp")) { try { Date timestamp = timestampFormat.parse(value); properties.put(prop, timestamp.getTime()); } catch (ParseException e) { error("Error parsing timestamp", e); } } else { properties.put(prop, value); } } if (name != null) { properties.put("name", name); } return properties; }
From source file:org.netbeans.jbatch.modeler.specification.model.job.util.JobUtil.java
void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) { try {//from w w w .ja v a 2 s. com // TransformerFactory tf = TransformerFactory.newInstance(); // Transformer t = tf.newTransformer(); // StAXSource source = new StAXSource(xmlStreamReader); // StAXResult result = new StAXResult(xmlStreamWriter); // t.transform(source, result); boolean finish = false; while (xmlStreamReader.hasNext() && !finish) { switch (xmlStreamReader.getEventType()) { case XMLEvent.START_ELEMENT: String prefix = xmlStreamReader.getPrefix(); String namespaceURI = xmlStreamReader.getNamespaceURI(); if (namespaceURI != null) { if (prefix != null) { xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(), xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI()); } else { xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(), xmlStreamReader.getLocalName()); } } else { xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName()); } for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) { xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i), xmlStreamReader.getNamespaceURI(i)); } int count = xmlStreamReader.getAttributeCount(); for (int i = 0; i < count; i++) { // xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i), // xmlStreamReader.getAttributeNamespace(i), // xmlStreamReader.getAttributeLocalName(i), // xmlStreamReader.getAttributeValue(i)); String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i), attrPrefix = xmlStreamReader.getAttributePrefix(i); if (attrNamespaceURI != null) { if (attrPrefix != null) { xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI, xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } else { xmlStreamWriter.writeAttribute(attrNamespaceURI, xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } } else { xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } } break; case XMLEvent.END_ELEMENT: xmlStreamWriter.writeEndElement(); if (xmlStreamReader.getLocalName().equals("definitions")) { finish = true; } break; case XMLEvent.SPACE: case XMLEvent.CHARACTERS: xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(), xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength()); break; case XMLEvent.PROCESSING_INSTRUCTION: xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(), xmlStreamReader.getPIData()); break; case XMLEvent.CDATA: xmlStreamWriter.writeCData(xmlStreamReader.getText()); break; case XMLEvent.COMMENT: xmlStreamWriter.writeComment(xmlStreamReader.getText()); break; case XMLEvent.ENTITY_REFERENCE: xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName()); break; case XMLEvent.START_DOCUMENT: String encoding = xmlStreamReader.getCharacterEncodingScheme(); String version = xmlStreamReader.getVersion(); if (encoding != null && version != null) { xmlStreamWriter.writeStartDocument(encoding, version); } else if (version != null) { xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion()); } break; case XMLEvent.END_DOCUMENT: xmlStreamWriter.writeEndDocument(); break; case XMLEvent.DTD: xmlStreamWriter.writeDTD(xmlStreamReader.getText()); break; } if (!finish) { xmlStreamReader.next(); } } } catch (XMLStreamException ex) { Exceptions.printStackTrace(ex); } }
From source file:org.opendatakit.briefcase.export.SubmissionParser.java
private static Optional<String> parseAttribute(Path submission, Reader ioReader, String attributeName, SubmissionExportErrorCallback onParsingError) { try {//from w w w .j a v a 2 s . c o m XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(ioReader); while (reader.hasNext()) if (reader.next() == START_ELEMENT) for (int i = 0, c = reader.getAttributeCount(); i < c; ++i) if (reader.getAttributeLocalName(i).equals(attributeName)) return Optional.of(reader.getAttributeValue(i)); } catch (XMLStreamException e) { log.error("Can't parse submission", e); onParsingError.accept(submission, "parsing error"); } return Optional.empty(); }
From source file:org.openo.nfvo.emsdriver.collector.TaskThread.java
private boolean processCMXml(File tempfile, String nename, String type) { String csvpath = localPath + nename + "/" + type + "/"; File csvpathfile = new File(csvpath); if (!csvpathfile.exists()) { csvpathfile.mkdirs();/*from www .j a v a 2 s . c o m*/ } String csvFileName = nename + dateFormat.format(new Date()) + System.nanoTime(); String csvpathAndFileName = csvpath + csvFileName + ".csv"; BufferedOutputStream bos = null; FileOutputStream fos = null; try { fos = new FileOutputStream(csvpathAndFileName, false); bos = new BufferedOutputStream(fos, 10240); } catch (FileNotFoundException e1) { log.error("FileNotFoundException " + StringUtil.getStackTrace(e1)); } boolean FieldNameFlag = false; boolean FieldValueFlag = false; //line num int countNum = 0; String xmlPathAndFileName = null; String localName = null; String endLocalName = null; String rmUID = null; int index = -1; ArrayList<String> names = new ArrayList<String>();// colname LinkedHashMap<String, String> nameAndValue = new LinkedHashMap<String, String>(); FileInputStream fis = null; InputStreamReader isr = null; XMLStreamReader reader = null; try { fis = new FileInputStream(tempfile); isr = new InputStreamReader(fis, Constant.ENCODING_UTF8); XMLInputFactory fac = XMLInputFactory.newInstance(); reader = fac.createXMLStreamReader(isr); int event = -1; boolean setcolum = true; while (reader.hasNext()) { try { event = reader.next(); switch (event) { case XMLStreamConstants.START_ELEMENT: localName = reader.getLocalName(); if ("FieldName".equalsIgnoreCase(localName)) { FieldNameFlag = true; } if (FieldNameFlag) { if ("N".equalsIgnoreCase(localName)) { String colName = reader.getElementText().trim(); names.add(colName); } } if ("FieldValue".equalsIgnoreCase(localName)) { FieldValueFlag = true; } if (FieldValueFlag) { if (setcolum) { xmlPathAndFileName = this.setColumnNames(nename, names, type); setcolum = false; } if ("Object".equalsIgnoreCase(localName)) { int ac = reader.getAttributeCount(); for (int i = 0; i < ac; i++) { if ("rmUID".equalsIgnoreCase(reader.getAttributeLocalName(i))) { rmUID = reader.getAttributeValue(i).trim(); } } nameAndValue.put("rmUID", rmUID); } if ("V".equalsIgnoreCase(localName)) { index = Integer.parseInt(reader.getAttributeValue(0)) - 1; String currentName = names.get(index); String v = reader.getElementText().trim(); nameAndValue.put(currentName, v); } } break; case XMLStreamConstants.CHARACTERS: break; case XMLStreamConstants.END_ELEMENT: endLocalName = reader.getLocalName(); if ("FieldName".equalsIgnoreCase(endLocalName)) { FieldNameFlag = false; } if ("FieldValue".equalsIgnoreCase(endLocalName)) { FieldValueFlag = false; } if ("Object".equalsIgnoreCase(endLocalName)) { countNum++; this.appendLine(nameAndValue, bos); nameAndValue.clear(); } break; } } catch (Exception e) { log.error("" + StringUtil.getStackTrace(e)); event = reader.next(); } } if (bos != null) { bos.close(); bos = null; } if (fos != null) { fos.close(); fos = null; } String[] fileKeys = this.createZipFile(csvpathAndFileName, xmlPathAndFileName, nename); //ftp store Properties ftpPro = configurationInterface.getProperties(); String ip = ftpPro.getProperty("ftp_ip"); String port = ftpPro.getProperty("ftp_port"); String ftp_user = ftpPro.getProperty("ftp_user"); String ftp_password = ftpPro.getProperty("ftp_password"); String ftp_passive = ftpPro.getProperty("ftp_passive"); String ftp_type = ftpPro.getProperty("ftp_type"); String remoteFile = ftpPro.getProperty("ftp_remote_path"); this.ftpStore(fileKeys, ip, port, ftp_user, ftp_password, ftp_passive, ftp_type, remoteFile); //create Message String message = this.createMessage(fileKeys[1], ftp_user, ftp_password, ip, port, countNum, nename); //set message this.setMessage(message); } catch (Exception e) { log.error("" + StringUtil.getStackTrace(e)); return false; } finally { try { if (reader != null) { reader.close(); } if (isr != null) { isr.close(); } if (fis != null) { fis.close(); } if (bos != null) { bos.close(); } if (fos != null) { fos.close(); } } catch (Exception e) { log.error(e); } } return true; }
From source file:org.orbisgis.core.layerModel.mapcatalog.Workspace.java
private int parsePublishResponse(XMLStreamReader parser) throws XMLStreamException { List<String> hierarchy = new ArrayList<String>(); for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { // For each XML elements switch (event) { case XMLStreamConstants.START_ELEMENT: hierarchy.add(parser.getLocalName()); // Parse attributes if (RemoteCommons.endsWith(hierarchy, "context")) { for (int attributeId = 0; attributeId < parser.getAttributeCount(); attributeId++) { String attributeName = parser.getAttributeLocalName(attributeId); if (attributeName.equals("id")) { return Integer.parseInt(parser.getAttributeValue(attributeId)); }/*from w w w .j a v a 2 s.co m*/ } } break; case XMLStreamConstants.END_ELEMENT: hierarchy.remove(hierarchy.size() - 1); break; } } throw new XMLStreamException("Bad response on publishing a map context"); }
From source file:org.orbisgis.core.layerModel.mapcatalog.Workspace.java
/** * Read the parser and feed the provided list with workspaces * @param mapContextList Writable, empty list of RemoteMapContext * @param parser Opened parser//w ww . java2s . c o m * @throws XMLStreamException */ public void parseXML(List<RemoteMapContext> mapContextList, XMLStreamReader parser) throws XMLStreamException, UnsupportedEncodingException { List<String> hierarchy = new ArrayList<String>(); RemoteMapContext curMapContext = null; Locale curLocale = null; StringBuilder characters = new StringBuilder(); for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { // For each XML elements switch (event) { case XMLStreamConstants.START_ELEMENT: hierarchy.add(parser.getLocalName()); if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) { curMapContext = new RemoteOwsMapContext(cParams); curMapContext.setWorkspaceName(workspaceName); } // Parse attributes for (int attributeId = 0; attributeId < parser.getAttributeCount(); attributeId++) { String attributeName = parser.getAttributeLocalName(attributeId); if (attributeName.equals("id")) { curMapContext.setId(Integer.parseInt(parser.getAttributeValue(attributeId))); } else if (attributeName.equals("date")) { String attributeValue = parser.getAttributeValue(attributeId); try { curMapContext.setDate(parseDate(attributeValue)); } catch (ParseException ex) { LOGGER.warn(I18N.tr("Cannot parse the provided date {0}", attributeValue), ex); } } else if (attributeName.equals("lang")) { curLocale = LocalizedText.forLanguageTag(parser.getAttributeValue(attributeId)); } } break; case XMLStreamConstants.END_ELEMENT: if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) { mapContextList.add(curMapContext); curMapContext = null; } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "title")) { Locale descLocale = Locale.getDefault(); if (curLocale != null) { descLocale = curLocale; } curMapContext.getDescription().addTitle(descLocale, StringEscapeUtils.unescapeHtml(characters.toString().trim())); } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "abstract")) { Locale descLocale = Locale.getDefault(); if (curLocale != null) { descLocale = curLocale; } curMapContext.getDescription().addAbstract(descLocale, StringEscapeUtils.unescapeHtml(characters.toString().trim())); } characters = new StringBuilder(); curLocale = null; hierarchy.remove(hierarchy.size() - 1); break; case XMLStreamConstants.CHARACTERS: characters.append(StringEscapeUtils.unescapeHtml(parser.getText())); break; } } }
From source file:org.orbisgis.coremap.layerModel.mapcatalog.Workspace.java
/** * Read the parser and feed the provided list with workspaces * @param mapContextList Writable, empty list of RemoteMapContext * @param parser Opened parser/*from ww w. ja v a2s.c om*/ * @throws XMLStreamException */ public void parseXML(List<RemoteMapContext> mapContextList, XMLStreamReader parser) throws XMLStreamException, UnsupportedEncodingException { List<String> hierarchy = new ArrayList<String>(); RemoteMapContext curMapContext = null; Locale curLocale = null; StringBuilder characters = new StringBuilder(); for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { // For each XML elements switch (event) { case XMLStreamConstants.START_ELEMENT: hierarchy.add(parser.getLocalName()); if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) { curMapContext = new RemoteOwsMapContext(cParams); curMapContext.setWorkspaceName(workspaceName); } // Parse attributes for (int attributeId = 0; attributeId < parser.getAttributeCount(); attributeId++) { String attributeName = parser.getAttributeLocalName(attributeId); if (attributeName.equals("id")) { curMapContext.setId(Integer.parseInt(parser.getAttributeValue(attributeId))); } else if (attributeName.equals("date")) { String attributeValue = parser.getAttributeValue(attributeId); try { curMapContext.setDate(parseDate(attributeValue)); } catch (ParseException ex) { LOGGER.warn(I18N.tr("Cannot parse the provided date {0}", attributeValue), ex); } } else if (attributeName.equals("lang")) { curLocale = LocalizedText.forLanguageTag(parser.getAttributeValue(attributeId)); } } break; case XMLStreamConstants.END_ELEMENT: if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) { mapContextList.add(curMapContext); curMapContext = null; } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "title")) { Locale descLocale = Locale.getDefault(); if (curLocale != null) { descLocale = curLocale; } curMapContext.getDescription().addTitle(descLocale, StringEscapeUtils.unescapeHtml4(characters.toString().trim())); } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "abstract")) { Locale descLocale = Locale.getDefault(); if (curLocale != null) { descLocale = curLocale; } curMapContext.getDescription().addAbstract(descLocale, StringEscapeUtils.unescapeHtml4(characters.toString().trim())); } characters = new StringBuilder(); curLocale = null; hierarchy.remove(hierarchy.size() - 1); break; case XMLStreamConstants.CHARACTERS: characters.append(StringEscapeUtils.unescapeHtml4(parser.getText())); break; } } }
From source file:org.osaf.cosmo.xml.DomReader.java
private static Attr readAttribute(int i, Document d, XMLStreamReader reader) throws XMLStreamException { Attr a = null;//from w w w . ja va2 s .com String local = reader.getAttributeLocalName(i); String ns = reader.getAttributeNamespace(i); if (ns != null) { String prefix = reader.getAttributePrefix(i); String qualified = prefix != null ? prefix + ":" + local : local; a = d.createAttributeNS(ns, qualified); } else { a = d.createAttribute(reader.getAttributeLocalName(i)); } //if (log.isDebugEnabled()) //log.debug("Reading attribute " + a.getName()); a.setValue(reader.getAttributeValue(i)); return a; }
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); }/*from w w w . ja va 2 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 w w. j a v a 2 s.c o m*/ 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(); } } }