List of usage examples for javax.xml.stream XMLStreamReader getAttributeCount
public int getAttributeCount();
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;// w w w . j a v a2 s .c om } 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.spec.core.Definitions.java
static void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) { try {//from w w w. j a va 2 s . c o m // TransformerFactory tf = TransformerFactory.newInstance(); // Transformer t = tf.newTransformer(); // StAXSource source = new StAXSource(xmlStreamReader); // StAXResult result = new StAXResult(xmlStreamWriter); // t.transform(source, result); System.out.println("Defnition Id : " + xmlStreamReader.getAttributeValue(null, "id")); 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.netbeans.jbatch.modeler.specification.model.job.util.JobUtil.java
void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) { try {/*from w ww.j a v a 2 s.co m*/ // 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 ww . j a v a 2 s . co 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 w w w . j a va 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.openvpms.tools.data.loader.Data.java
/** * Creates a new <tt>Data</tt>. * * @param reader the stream to read from *//*from w ww . ja v a 2 s . c o m*/ public Data(XMLStreamReader reader) { shortName = reader.getAttributeValue(null, "archetype"); location = reader.getLocation(); if (StringUtils.isEmpty(shortName)) { throw new ArchetypeDataLoaderException(InvalidArchetype, location.getLineNumber(), location.getColumnNumber(), "<null>"); } if (!"data".equals(reader.getLocalName())) { throw new ArchetypeDataLoaderException(UnexpectedElement, reader.getLocalName(), location.getLineNumber(), location.getColumnNumber()); } id = reader.getAttributeValue(null, "id"); collection = reader.getAttributeValue(null, "collection"); for (int i = 0; i < reader.getAttributeCount(); i++) { String name = reader.getAttributeLocalName(i); String value = reader.getAttributeValue(i); if (!"archetype".equals(name) && !"id".equals(name) && !"collection".equals(name) && !StringUtils.isEmpty(value)) { attributes.put(name, value); } } }
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 ww.j a va2 s .c o 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/*from w ww. j a v a2 s . 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.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 w ww . j a va 2 s .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.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 Element readElement(Document d, XMLStreamReader reader) throws XMLStreamException { Element e = null;//from w w w. j a va2 s. c o m String local = reader.getLocalName(); String ns = reader.getNamespaceURI(); if (ns != null) { String prefix = reader.getPrefix(); String qualified = prefix != null ? 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; }