List of usage examples for javax.xml.stream XMLStreamReader getAttributePrefix
public String getAttributePrefix(int index);
From source file:org.mule.module.xml.util.XMLUtils.java
private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { String local = reader.getLocalName(); String uri = reader.getNamespaceURI(); String prefix = reader.getPrefix(); if (prefix == null) { prefix = ""; }/* www. j a v a 2 s . co m*/ // System.out.println("STAXUTILS:writeStartElement : node name : " + local + " namespace URI" + uri); boolean writeElementNS = false; if (uri != null) { String boundPrefix = writer.getPrefix(uri); if (boundPrefix == null || !prefix.equals(boundPrefix)) { writeElementNS = true; } } // Write out the element name if (uri != null) { if (prefix.length() == 0 && StringUtils.isEmpty(uri)) { writer.writeStartElement(local); writer.setDefaultNamespace(uri); } else { writer.writeStartElement(prefix, local, uri); writer.setPrefix(prefix, uri); } } else { writer.writeStartElement(local); } // Write out the namespaces for (int i = 0; i < reader.getNamespaceCount(); i++) { String nsURI = reader.getNamespaceURI(i); String nsPrefix = reader.getNamespacePrefix(i); if (nsPrefix == null) { nsPrefix = ""; } if (nsPrefix.length() == 0) { writer.writeDefaultNamespace(nsURI); } else { writer.writeNamespace(nsPrefix, nsURI); } if (nsURI.equals(uri) && nsPrefix.equals(prefix)) { writeElementNS = false; } } // Check if the namespace still needs to be written. // We need this check because namespace writing works // different on Woodstox and the RI. if (writeElementNS) { if (prefix.length() == 0) { writer.writeDefaultNamespace(uri); } else { writer.writeNamespace(prefix, uri); } } // Write out attributes for (int i = 0; i < reader.getAttributeCount(); i++) { String ns = reader.getAttributeNamespace(i); String nsPrefix = reader.getAttributePrefix(i); if (ns == null || ns.length() == 0) { writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } else if (nsPrefix == null || nsPrefix.length() == 0) { writer.writeAttribute(reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } else { writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } } }
From source file:org.neo4j.gis.spatial.osm.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);/*ww w .j av a2 s .c o m*/ 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(); }
From source file:org.netbeans.jbatch.modeler.spec.core.Definitions.java
static void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) { try {/*from w ww. ja v a2s.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 www.ja v a 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); 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.osaf.cosmo.xml.DomReader.java
private static Attr readAttribute(int i, Document d, XMLStreamReader reader) throws XMLStreamException { Attr a = null;//from w ww . ja v a 2 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.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java
private static void updateFile(File configFile, Map<String, PropertySimple> allProps) throws IOException, InterruptedException, XMLStreamException { InputStream in = null;/* ww w .j av a2 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(); } } }
From source file:org.unitedinternet.cosmo.util.DomReader.java
private static Attr readAttribute(int i, Document d, XMLStreamReader reader) throws XMLStreamException { Attr a = null;/*from w ww . jav a2 s. c o m*/ String local = reader.getAttributeLocalName(i); String ns = reader.getAttributeNamespace(i); if (ns != null && !ns.equals("")) { 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; }