List of usage examples for javax.xml.stream XMLStreamReader getNamespaceURI
public String getNamespaceURI();
From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java
public static DmnExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception { DmnExtensionElement extensionElement = new DmnExtensionElement(); extensionElement.setName(xtr.getLocalName()); if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) { extensionElement.setNamespace(xtr.getNamespaceURI()); }/*from w w w.jav a 2 s.c om*/ if (StringUtils.isNotEmpty(xtr.getPrefix())) { extensionElement.setNamespacePrefix(xtr.getPrefix()); } for (int i = 0; i < xtr.getAttributeCount(); i++) { DmnExtensionAttribute extensionAttribute = new DmnExtensionAttribute(); 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)); } extensionElement.addAttribute(extensionAttribute); } boolean readyWithExtensionElement = false; while (readyWithExtensionElement == false && xtr.hasNext()) { xtr.next(); if (xtr.isCharacters() || XMLStreamReader.CDATA == xtr.getEventType()) { if (StringUtils.isNotEmpty(xtr.getText().trim())) { extensionElement.setElementText(xtr.getText().trim()); } } else if (xtr.isStartElement()) { DmnExtensionElement childExtensionElement = parseExtensionElement(xtr); extensionElement.addChildElement(childExtensionElement); } else if (xtr.isEndElement() && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) { readyWithExtensionElement = true; } } return extensionElement; }
From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java
/** * @param reader/*ww w . j a v a 2 s .co m*/ * @param writer * @throws XMLStreamException */ protected void serializeElement(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { // Note: To serialize the start tag, we must follow the order dictated by the JSR-173 (StAX) specification. // Please keep this code in sync with the code in OMSerializerUtil.serializeStartpart // The algorithm is: // ... generate writeStartElement // // ... generate setPrefix/setDefaultNamespace for each namespace declaration if the prefix is unassociated. // ... generate setPrefix/setDefaultNamespace if the prefix of the element is unassociated // ... generate setPrefix/setDefaultNamespace for each unassociated prefix of the attributes. // // ... generate writeNamespace/writerDefaultNamespace for the new namespace declarations determine during the "set" processing // ... generate writeAttribute for each attribute ArrayList writePrefixList = null; ArrayList writeNSList = null; // Get the prefix and namespace of the element. "" and null are identical. String ePrefix = reader.getPrefix(); ePrefix = (ePrefix != null && ePrefix.length() == 0) ? null : ePrefix; String eNamespace = reader.getNamespaceURI(); eNamespace = (eNamespace != null && eNamespace.length() == 0) ? null : eNamespace; // Write the startElement if required if (eNamespace != null) { if (ePrefix == null) { if (!OMSerializerUtil.isAssociated("", eNamespace, writer)) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } writePrefixList.add(""); writeNSList.add(eNamespace); } writer.writeStartElement("", reader.getLocalName(), eNamespace); } else { if (!OMSerializerUtil.isAssociated(ePrefix, eNamespace, writer)) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } writePrefixList.add(ePrefix); writeNSList.add(eNamespace); } writer.writeStartElement(ePrefix, reader.getLocalName(), eNamespace); } } else { writer.writeStartElement(reader.getLocalName()); } // Generate setPrefix for the namespace declarations int count = reader.getNamespaceCount(); for (int i = 0; i < count; i++) { String prefix = reader.getNamespacePrefix(i); prefix = (prefix != null && prefix.length() == 0) ? null : prefix; String namespace = reader.getNamespaceURI(i); namespace = (namespace != null && namespace.length() == 0) ? null : namespace; String newPrefix = OMSerializerUtil.generateSetPrefix(prefix, namespace, writer, false); // If this is a new association, remember it so that it can written out later if (newPrefix != null) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains(newPrefix)) { writePrefixList.add(newPrefix); writeNSList.add(namespace); } } } // Generate setPrefix for the element // If the prefix is not associated with a namespace yet, remember it so that we can // write out a namespace declaration String newPrefix = OMSerializerUtil.generateSetPrefix(ePrefix, eNamespace, writer, false); // If this is a new association, remember it so that it can written out later if (newPrefix != null) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains(newPrefix)) { writePrefixList.add(newPrefix); writeNSList.add(eNamespace); } } // Now Generate setPrefix for each attribute count = reader.getAttributeCount(); for (int i = 0; i < count; i++) { String prefix = reader.getAttributePrefix(i); prefix = (prefix != null && prefix.length() == 0) ? null : prefix; String namespace = reader.getAttributeNamespace(i); namespace = (namespace != null && namespace.length() == 0) ? null : namespace; // Default prefix referencing is not allowed on an attribute if (prefix == null && namespace != null) { String writerPrefix = writer.getPrefix(namespace); writerPrefix = (writerPrefix != null && writerPrefix.length() == 0) ? null : writerPrefix; prefix = (writerPrefix != null) ? writerPrefix : generateUniquePrefix(writer.getNamespaceContext()); } newPrefix = OMSerializerUtil.generateSetPrefix(prefix, namespace, writer, true); // If the prefix is not associated with a namespace yet, remember it so that we can // write out a namespace declaration if (newPrefix != null) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains(newPrefix)) { writePrefixList.add(newPrefix); writeNSList.add(namespace); } } } // Now Generate setPrefix for each prefix referenced in an xsi:type // For example xsi:type="p:dataType" // The following code will make sure that setPrefix is called for "p". count = reader.getAttributeCount(); for (int i = 0; i < count; i++) { String prefix = reader.getAttributePrefix(i); prefix = (prefix != null && prefix.length() == 0) ? null : prefix; String namespace = reader.getAttributeNamespace(i); namespace = (namespace != null && namespace.length() == 0) ? null : namespace; String localName = reader.getAttributeLocalName(i); if (XSI_URI.equals(namespace) && XSI_LOCAL_NAME.equals(localName)) { String value = reader.getAttributeValue(i); if (DEBUG_ENABLED) { log.debug("The value of xsi:type is " + value); } if (value != null) { value = value.trim(); if (value.indexOf(":") > 0) { String refPrefix = value.substring(0, value.indexOf(":")); String refNamespace = reader.getNamespaceURI(refPrefix); if (refNamespace != null && refNamespace.length() > 0) { newPrefix = OMSerializerUtil.generateSetPrefix(refPrefix, refNamespace, writer, true); // If the prefix is not associated with a namespace yet, remember it so that we can // write out a namespace declaration if (newPrefix != null) { if (DEBUG_ENABLED) { log.debug( "An xmlns:" + newPrefix + "=\"" + refNamespace + "\" will be written"); } if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains(newPrefix)) { writePrefixList.add(newPrefix); writeNSList.add(refNamespace); } } } } } } } // Now write out the list of namespace declarations in this list that we constructed // while doing the "set" processing. if (writePrefixList != null) { for (int i = 0; i < writePrefixList.size(); i++) { String prefix = (String) writePrefixList.get(i); String namespace = (String) writeNSList.get(i); if (prefix != null) { if (namespace == null) { writer.writeNamespace(prefix, ""); } else { writer.writeNamespace(prefix, namespace); } } else { writer.writeDefaultNamespace(namespace); } } } // Now write the attributes count = reader.getAttributeCount(); for (int i = 0; i < count; i++) { String prefix = reader.getAttributePrefix(i); prefix = (prefix != null && prefix.length() == 0) ? null : prefix; String namespace = reader.getAttributeNamespace(i); namespace = (namespace != null && namespace.length() == 0) ? null : namespace; if (prefix == null && namespace != null) { // Default namespaces are not allowed on an attribute reference. // Earlier in this code, a unique prefix was added for this case...now obtain and use it prefix = writer.getPrefix(namespace); //XMLStreamWriter doesn't allow for getPrefix to know whether you're asking for the prefix //for an attribute or an element. So if the namespace matches the default namespace getPrefix will return //the empty string, as if it were an element, in all cases (even for attributes, and even if //there was a prefix specifically set up for this), which is not the desired behavior. //Since the interface is base java, we can't fix it where we need to (by adding an attr boolean to //XMLStreamWriter.getPrefix), so we hack it in here... if (prefix == null || "".equals(prefix)) { for (int j = 0; j < writePrefixList.size(); j++) { if (namespace.equals((String) writeNSList.get(j))) { prefix = (String) writePrefixList.get(j); } } } } else if (namespace != null && !prefix.equals("xml")) { // Use the writer's prefix if it is different, but if the writers // prefix is empty then do not replace because attributes do not // default to the default namespace like elements do. String writerPrefix = writer.getPrefix(namespace); if (!prefix.equals(writerPrefix) && !"".equals(writerPrefix)) { prefix = writerPrefix; } } if (namespace != null) { // Qualified attribute writer.writeAttribute(prefix, namespace, reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } else { // Unqualified attribute writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } } }
From source file:org.apache.axis2.jaxws.context.listener.ParserInputStreamCustomBuilder.java
public OMElement create(String namespace, String localPart, OMContainer parent, XMLStreamReader reader, OMFactory factory) throws OMException { if (log.isDebugEnabled()) { log.debug("create namespace = " + namespace); log.debug(" localPart = " + localPart); log.debug(" reader = " + reader.getClass()); }// w w w. j ava2 s.c o m if (!shouldUnmarshal(namespace, localPart)) { if (log.isDebugEnabled()) { log.debug("This element won't be unmarshalled with the custom builder"); } return null; } /* * 1) Use the the parser to fetch the inputStream * 2) Use the inputStream to create a DataSource, delay reading of content as much as you can. * 3) Use the OMFactory to create OMSourcedElement, OMSourcedElement is backed by ParsedEntityDataSource. */ try { ParsedEntityReaderFactory perf = (ParsedEntityReaderFactory) FactoryRegistry .getFactory(ParsedEntityReaderFactory.class); ParsedEntityReader entityReader = perf.getParsedEntityReader(); if (log.isDebugEnabled()) { log.debug("ParsedEntityReader = " + entityReader); } //Do not user custom builder if Parser does not have ability to read sub content. if (!entityReader.isParsedEntityStreamAvailable()) { if (log.isDebugEnabled()) { log.debug("ParsedEntityStream is not available, defaulting to normal build"); } return null; } // Create an OMSourcedElement backed by the ParsedData InputStream parsedStream = getPayloadContent(reader, entityReader); if (parsedStream == null) { //cant read content from EntityReader, returning null. if (log.isDebugEnabled()) { log.debug("Unable to read content from the entity reader, defaulting to normal build"); } return null; } HashMap<String, String> nsElementDecls = getElementNamespaceDeclarations(reader); HashMap<String, String> attrElementDecls = getElementAttributeDeclarations(reader); //read the payload. Lets move the parser forward. if (reader.hasNext()) { reader.next(); } if (namespace == null) { //lets look for ns in reader namespace = reader.getNamespaceURI(); if (namespace == null) { //still cant find the namespace, just set it to ""; namespace = ""; } } OMNamespace ns = factory.createOMNamespace(namespace, reader.getPrefix()); InputStream payload = ContextListenerUtils.createPayloadElement(parsedStream, ns, localPart, parent, nsElementDecls, attrElementDecls); ParserInputStreamDataSource ds = new ParserInputStreamDataSource(payload, encoding); OMSourcedElement om = null; if (parent instanceof SOAPHeader && factory instanceof SOAPFactory) { om = ((SOAPFactory) factory).createSOAPHeaderBlock(localPart, ns, ds); } else { om = factory.createOMElement(ds, localPart, ns); } //Add the new OMSourcedElement ot the parent parent.addChild(om); /* //Lets Mark the body as complete so Serialize calls dont fetch data from parser for body content. if(parent instanceof SOAPBodyImpl){ ((SOAPBodyImpl)parent).setComplete(true); } */ return om; } catch (OMException e) { throw e; } catch (Throwable t) { throw new OMException(t); } }
From source file:org.apache.axis2.jaxws.context.listener.ParserInputStreamCustomBuilder.java
public OMElement create(String namespace, String localPart, OMContainer parent, XMLStreamReader reader, OMFactory factory, InputStream payload) throws OMException { if (log.isDebugEnabled()) { log.debug("create namespace = " + namespace); log.debug(" localPart = " + localPart); log.debug(" reader = " + reader.getClass()); }// www. j a va 2 s. c om /* * 1) Use the the parser to fetch the inputStream * 2) Use the inputStream to create a DataSource, delay reading of content as much as you can. * 3) Use the OMFactory to create OMSourcedElement, OMSourcedElement is backed by ParsedEntityDataSource. */ try { if (namespace == null) { //lets look for ns in reader namespace = reader.getNamespaceURI(); if (namespace == null) { //still cant find the namespace, just set it to ""; namespace = ""; } } if (!shouldUnmarshal(namespace, localPart)) { if (log.isDebugEnabled()) { log.debug("This element won't be unmarshalled with the custom builder"); } return null; } OMNamespace ns = factory.createOMNamespace(namespace, reader.getPrefix()); ParserInputStreamDataSource ds = new ParserInputStreamDataSource(payload, encoding); OMSourcedElement om = null; if (parent instanceof SOAPHeader && factory instanceof SOAPFactory) { om = ((SOAPFactory) factory).createSOAPHeaderBlock(localPart, ns, ds); } else { om = factory.createOMElement(ds, localPart, ns); } //Add the new OMSourcedElement ot the parent parent.addChild(om); return om; } catch (OMException e) { throw e; } catch (Throwable t) { throw new OMException(t); } }
From source file:org.apache.axis2.jaxws.message.util.impl.SAAJConverterImpl.java
/** * update the tag data of the SOAPElement * * @param NameCreator nc/*from w w w . j a v a 2 s .co m*/ * @param element SOAPElement * @param reader XMLStreamReader whose cursor is at START_ELEMENT */ protected void updateTagData(NameCreator nc, SOAPElement element, XMLStreamReader reader, boolean newElement) throws SOAPException { String prefix = reader.getPrefix(); prefix = (prefix == null) ? "" : prefix; // Make sure the prefix is correct if (prefix.length() > 0 && !element.getPrefix().equals(prefix)) { // Due to a bug in Axiom DOM or in the reader...not sure where yet, // there may be a non-null prefix and no namespace String ns = reader.getNamespaceURI(); if (ns != null && ns.length() != 0) { element.setPrefix(prefix); } } if (!newElement) { // Add the namespace declarations from the reader for the missing namespaces int size = reader.getNamespaceCount(); for (int i = 0; i < size; i++) { String pre = reader.getNamespacePrefix(i); String ns = reader.getNamespaceURI(i); if ((pre != null && pre.length() > 0) && (ns == null || ns.length() == 0)) { if (log.isDebugEnabled()) { log.debug("The prefix is (" + pre + ") but there is no namespace. " + "This erroneous declaration is skipped."); } } else { String existingNS = element.getNamespaceURI(pre); if (!ns.equals(existingNS)) { element.removeNamespaceDeclaration(pre); // Is it necessary to remove the existing prefix/ns element.addNamespaceDeclaration(pre, ns); } } } } else { // Add the namespace declarations from the reader int size = reader.getNamespaceCount(); for (int i = 0; i < size; i++) { String newPrefix = reader.getNamespacePrefix(i); String newNS = reader.getNamespaceURI(i); if ((newPrefix != null && newPrefix.length() > 0) && (newNS == null || newNS.length() == 0)) { // Due to a bug in Axiom DOM or the reader, I have // seen cases where the prefix is non-null but there is not // namespace. Example: prefix is axis2ns3 and namespace is null. // This is an error..log, tolerate and continue if (log.isDebugEnabled()) { log.debug("The prefix is (" + newPrefix + ") but there is no namespace. " + "This erroneous declaration is skipped."); } } else { element.addNamespaceDeclaration(newPrefix, newNS); } } } addAttributes(nc, element, reader); return; }
From source file:org.apache.ode.utils.DOMUtils.java
private static void parse(XMLStreamReader reader, Document doc, Node parent) throws XMLStreamException { int event = reader.getEventType(); while (reader.hasNext()) { switch (event) { case XMLStreamConstants.START_ELEMENT: // create element Element e = doc.createElementNS(reader.getNamespaceURI(), reader.getLocalName()); if (reader.getPrefix() != null && reader.getPrefix() != "") { e.setPrefix(reader.getPrefix()); }//from ww w . j a va 2s. c om parent.appendChild(e); // copy namespaces for (int ns = 0; ns < reader.getNamespaceCount(); ns++) { String uri = reader.getNamespaceURI(ns); String prefix = reader.getNamespacePrefix(ns); declare(e, uri, prefix); } // copy attributes for (int att = 0; att < reader.getAttributeCount(); att++) { String name = reader.getAttributeLocalName(att); String prefix = reader.getAttributePrefix(att); if (prefix != null && prefix.length() > 0) { name = prefix + ":" + name; } Attr attr = doc.createAttributeNS(reader.getAttributeNamespace(att), name); attr.setValue(reader.getAttributeValue(att)); e.setAttributeNode(attr); } // sub-nodes if (reader.hasNext()) { reader.next(); parse(reader, doc, e); } if (parent instanceof Document) { while (reader.hasNext()) reader.next(); return; } break; case XMLStreamConstants.END_ELEMENT: return; case XMLStreamConstants.CHARACTERS: if (parent != null) { parent.appendChild(doc.createTextNode(reader.getText())); } break; case XMLStreamConstants.COMMENT: if (parent != null) { parent.appendChild(doc.createComment(reader.getText())); } break; case XMLStreamConstants.CDATA: parent.appendChild(doc.createCDATASection(reader.getText())); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData())); break; case XMLStreamConstants.ENTITY_REFERENCE: parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData())); break; case XMLStreamConstants.NAMESPACE: case XMLStreamConstants.ATTRIBUTE: break; default: break; } if (reader.hasNext()) { event = reader.next(); } } }
From source file:org.apache.synapse.commons.json.JsonDataSource.java
public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException { XMLStreamReader reader = getReader(); xmlWriter.writeStartDocument();// w ww .ja v a 2s. c om while (reader.hasNext()) { int x = reader.next(); switch (x) { case XMLStreamConstants.START_ELEMENT: xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI()); int namespaceCount = reader.getNamespaceCount(); for (int i = namespaceCount - 1; i >= 0; i--) { xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); } int attributeCount = reader.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } break; case XMLStreamConstants.START_DOCUMENT: break; case XMLStreamConstants.CHARACTERS: xmlWriter.writeCharacters(reader.getText()); break; case XMLStreamConstants.CDATA: xmlWriter.writeCData(reader.getText()); break; case XMLStreamConstants.END_ELEMENT: xmlWriter.writeEndElement(); break; case XMLStreamConstants.END_DOCUMENT: xmlWriter.writeEndDocument(); break; case XMLStreamConstants.SPACE: break; case XMLStreamConstants.COMMENT: xmlWriter.writeComment(reader.getText()); break; case XMLStreamConstants.DTD: xmlWriter.writeDTD(reader.getText()); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData()); break; case XMLStreamConstants.ENTITY_REFERENCE: xmlWriter.writeEntityRef(reader.getLocalName()); break; default: throw new OMException(); } } xmlWriter.writeEndDocument(); xmlWriter.flush(); xmlWriter.close(); }
From source file:org.codice.ddf.opensearch.source.OpenSearchSource.java
private InputTransformer getInputTransformer(InputStream inputStream) throws UnsupportedQueryException { XMLStreamReader xmlStreamReader = null; try {//from w w w. j a v a2s.c o m xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream); while (xmlStreamReader.hasNext()) { int next = xmlStreamReader.next(); if (next == XMLStreamConstants.START_ELEMENT) { String namespaceUri = xmlStreamReader.getNamespaceURI(); InputTransformer transformerReference = lookupTransformerReference(namespaceUri); if (transformerReference != null) { return transformerReference; } } } } catch (XMLStreamException | InvalidSyntaxException e) { LOGGER.debug("Failed to parse transformer namespace", e); } finally { try { if (xmlStreamReader != null) { xmlStreamReader.close(); } } catch (XMLStreamException e) { LOGGER.debug("Failed to close namespace reader", e); } } throw new UnsupportedQueryException( "Unable to find applicable InputTransformer for metacard content from Atom feed."); }
From source file:org.deegree.commons.config.AbstractResourceManager.java
@Override protected ExtendedResourceProvider<T> getProvider(URL file) { String namespace = null;/*from www. j av a 2s . c o m*/ InputStream is = null; try { is = new DURL(file.toExternalForm()).openStream(); XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(is); XMLStreamUtils.nextElement(xmlReader); namespace = xmlReader.getNamespaceURI(); LOG.debug("Config namespace: '" + namespace + "'"); xmlReader.close(); return nsToProvider.get(namespace); } catch (Throwable e) { String msg = "Error determining configuration namespace for file '" + file + "'"; LOG.error(msg); } finally { IOUtils.closeQuietly(is); } return null; }
From source file:org.deegree.cs.persistence.CRSManager.java
/** * Returns an uninitialized {@link CRSStore} instance that's created from the specified CRSStore configuration * document./*from w ww .j a v a 2s . co m*/ * * @param configURL * URL of the configuration document, must not be <code>null</code> * @return corresponding {@link CRSStore} instance, not yet initialized, never <code>null</code> * @throws CRSStoreException * if the creation fails, e.g. due to a configuration error */ public synchronized CRSStore create(URL configURL) throws CRSStoreException { String namespace = null; XMLStreamReader xmlReader = null; InputStream urlStream = null; try { urlStream = configURL.openStream(); xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(urlStream); XMLStreamUtils.nextElement(xmlReader); namespace = xmlReader.getNamespaceURI(); } catch (Exception e) { String msg = Messages.get("CRSManager.CREATING_STORE_FAILED", configURL); LOG.error(msg); throw new CRSStoreException(msg); } finally { closeQuietly(xmlReader); IOUtils.closeQuietly(urlStream); } LOG.debug("Config namespace: '" + namespace + "'"); CRSStoreProvider provider = getProviders().get(namespace); if (provider == null) { String msg = Messages.get("CRSManager.MISSING_PROVIDER", namespace, configURL); LOG.error(msg); throw new CRSStoreException(msg); } return provider.getCRSStore(configURL, workspace); }