List of usage examples for javax.xml.stream XMLStreamReader getNamespaceURI
public String getNamespaceURI(int index);
From source file:com.evolveum.midpoint.common.validator.Validator.java
public void validate(InputStream inputStream, OperationResult validatorResult, String objectResultOperationName) { XMLStreamReader stream = null; try {/*from w w w .ja v a2s .c o m*/ Map<String, String> rootNamespaceDeclarations = new HashMap<String, String>(); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); stream = xmlInputFactory.createXMLStreamReader(inputStream); int eventType = stream.nextTag(); if (eventType == XMLStreamConstants.START_ELEMENT) { if (!stream.getName().equals(SchemaConstants.C_OBJECTS)) { // This has to be an import file with a single objects. Try // to process it. OperationResult objectResult = validatorResult.createSubresult(objectResultOperationName); progress++; objectResult.addContext(OperationResult.CONTEXT_PROGRESS, progress); EventResult cont = null; try { cont = readFromStreamAndValidate(stream, objectResult, rootNamespaceDeclarations, validatorResult); } catch (RuntimeException e) { // Make sure that unexpected error is recorded. objectResult.recordFatalError(e); throw e; } if (!cont.isCont()) { String message = null; if (cont.getReason() != null) { message = cont.getReason(); } else { message = "Object validation failed (no reason given)"; } if (objectResult.isUnknown()) { objectResult.recordFatalError(message); } validatorResult.recordFatalError(message); return; } // return to avoid processing objects in loop validatorResult.computeStatus("Validation failed", "Validation warnings"); return; } // Extract root namespace declarations for (int i = 0; i < stream.getNamespaceCount(); i++) { rootNamespaceDeclarations.put(stream.getNamespacePrefix(i), stream.getNamespaceURI(i)); } } else { throw new SystemException("StAX Malfunction?"); } while (stream.hasNext()) { eventType = stream.next(); if (eventType == XMLStreamConstants.START_ELEMENT) { OperationResult objectResult = validatorResult.createSubresult(objectResultOperationName); progress++; objectResult.addContext(OperationResult.CONTEXT_PROGRESS, progress); EventResult cont = null; try { // Read and validate individual object from the stream cont = readFromStreamAndValidate(stream, objectResult, rootNamespaceDeclarations, validatorResult); } catch (RuntimeException e) { if (objectResult.isUnknown()) { // Make sure that unexpected error is recorded. objectResult.recordFatalError(e); } throw e; } if (objectResult.isError()) { errors++; } objectResult.cleanupResult(); validatorResult.summarize(); if (cont.isStop()) { if (cont.getReason() != null) { validatorResult.recordFatalError("Processing has been stopped: " + cont.getReason()); } else { validatorResult.recordFatalError("Processing has been stopped"); } // This means total stop, no other objects will be // processed return; } if (!cont.isCont()) { if (stopAfterErrors > 0 && errors >= stopAfterErrors) { validatorResult.recordFatalError("Too many errors (" + errors + ")"); return; } } } } } catch (XMLStreamException ex) { // validatorResult.recordFatalError("XML parsing error: " + // ex.getMessage()+" on line "+stream.getLocation().getLineNumber(),ex); validatorResult.recordFatalError("XML parsing error: " + ex.getMessage(), ex); if (handler != null) { handler.handleGlobalError(validatorResult); } return; } // Error count is sufficient. Detailed messages are in subresults validatorResult.computeStatus(errors + " errors, " + (progress - errors) + " passed"); }
From source file:org.activiti.bpmn.converter.parser.DefinitionsParser.java
@SuppressWarnings("unchecked") public void parse(XMLStreamReader xtr, BpmnModel model) throws Exception { model.setTargetNamespace(xtr.getAttributeValue(null, TARGET_NAMESPACE_ATTRIBUTE)); for (int i = 0; i < xtr.getNamespaceCount(); i++) { String prefix = xtr.getNamespacePrefix(i); if (StringUtils.isNotEmpty(prefix)) { model.addNamespace(prefix, xtr.getNamespaceURI(i)); }/*from w ww . j a va 2s . co m*/ } for (int i = 0; i < xtr.getAttributeCount(); i++) { ExtensionAttribute extensionAttribute = new ExtensionAttribute(); extensionAttribute.setName(xtr.getAttributeLocalName(i)); extensionAttribute.setValue(xtr.getAttributeValue(i)); if (StringUtils.isNotEmpty(xtr.getAttributeNamespace(i))) { extensionAttribute.setNamespace(xtr.getAttributeNamespace(i)); } if (StringUtils.isNotEmpty(xtr.getAttributePrefix(i))) { extensionAttribute.setNamespacePrefix(xtr.getAttributePrefix(i)); } if (!BpmnXMLUtil.isBlacklisted(extensionAttribute, defaultAttributes)) { model.addDefinitionsAttribute(extensionAttribute); } } }
From source file:org.apache.axis2.jaxws.context.listener.ParserInputStreamCustomBuilder.java
private HashMap<String, String> getElementNamespaceDeclarations(XMLStreamReader reader) { HashMap<String, String> nsElementDecls = new HashMap<String, String>(); int count = reader.getNamespaceCount(); for (int i = 0; i < count; i++) { String prefix = reader.getNamespacePrefix(i); String namespace = reader.getNamespaceURI(i); if (namespace != null && namespace.length() > 0) { nsElementDecls.put(prefix == null ? "" : prefix, namespace); }//from ww w. j av a 2s . c o m } return nsElementDecls; }
From source file:org.deegree.client.core.renderer.OutputXMLRenderer.java
private void encodeXML(ResponseWriter writer, String value) throws IOException { int depth = 0; try {//from w ww. ja v a2s. c om XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(value)); boolean lastWasEndElement = false; boolean lastWasComment = false; while (reader.hasNext()) { switch (reader.getEventType()) { case XMLStreamConstants.START_ELEMENT: if (!lastWasComment) { writer.startElement("br", null); writer.endElement("br"); } writer.write(getSpaces(depth)); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("<"); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "tag", null); String prefix = reader.getPrefix(); writer.write( (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName()); writer.endElement("span"); for (int i = 0; i < reader.getAttributeCount(); i++) { writer.startElement("span", null); writer.writeAttribute("class", "attributeName", null); writer.write(" "); String attributePrefix = reader.getAttributePrefix(i); writer.write( (attributePrefix != null && attributePrefix.length() > 0 ? attributePrefix + ":" : "") + reader.getAttributeLocalName(i)); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("=\""); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "text", null); writer.write(encodeString(reader.getAttributeValue(i))); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("\""); writer.endElement("span"); } for (int i = 0; i < reader.getNamespaceCount(); i++) { writer.startElement("span", null); writer.writeAttribute("class", "attributeName", null); writer.write(" "); String nsPrefix = reader.getNamespacePrefix(i); writer.write("xmlns"); if (nsPrefix != null && !nsPrefix.isEmpty()) { writer.write(":" + nsPrefix); } writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("=\""); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "text", null); writer.write(reader.getNamespaceURI(i)); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("\""); writer.endElement("span"); } writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write(">"); writer.endElement("span"); depth++; lastWasEndElement = false; lastWasComment = false; break; case XMLStreamConstants.CHARACTERS: String text = reader.getText(); if (text.trim().length() > 0) { writer.startElement("span", null); writer.writeAttribute("class", "text", null); writer.write(encodeString(text)); writer.endElement("span"); lastWasEndElement = false; lastWasComment = false; } break; case XMLStreamConstants.END_ELEMENT: depth--; if (lastWasEndElement) { writer.startElement("br", null); writer.endElement("br"); writer.write(getSpaces(depth)); } writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("</"); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "tag", null); prefix = reader.getPrefix(); writer.write( (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName()); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write(">"); writer.endElement("span"); lastWasEndElement = true; lastWasComment = false; break; case XMLStreamConstants.COMMENT: writer.startElement("div", null); writer.writeAttribute("class", "comment", null); writer.write("</!--" + reader.getText() + "-->"); writer.endElement("div"); lastWasEndElement = false; lastWasComment = true; break; default: break; } reader.next(); } reader.close(); } catch (Throwable e) { if (depth == 0) { writer.writeText("Response could not parsed as XML.", null); } else { writer.writeText("... (if you want the complete document, please click the download button)", null); } } }
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.ja va2 s . com 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.slc.sli.modeling.wadl.reader.WadlReader.java
private static final QName toQName(final String lexicalName, final XMLStreamReader reader) { final int colonIndex = lexicalName.indexOf(":"); if (colonIndex >= 0) { final String localName = lexicalName.substring(colonIndex + 1); final String prefix = lexicalName.substring(0, colonIndex); final String namespaceURI = reader.getNamespaceURI(prefix); return new QName(namespaceURI, localName, prefix); } else {/* w ww . ja v a2s .co m*/ final String namespaceURI = reader.getNamespaceURI(""); return new QName(namespaceURI, lexicalName); } }