List of usage examples for javax.xml.stream XMLStreamReader getAttributePrefix
public String getAttributePrefix(int index);
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)); }//w w w .ja v a 2 s .c om } 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.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 www . j av a 2s. c o m 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/*from w ww . j av a2s.c o 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.axiom.om.impl.serialize.StreamingOMSerializer.java
/** * @param reader//ww w .ja v a2s . c o m * @param writer * @throws XMLStreamException */ protected void serializeAttributes(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { int count = reader.getAttributeCount(); String prefix = null; String namespaceName = null; String writerPrefix = null; for (int i = 0; i < count; i++) { prefix = reader.getAttributePrefix(i); namespaceName = reader.getAttributeNamespace(i); /* Some parser implementations return null for the unqualified namespace. But getPrefix(null) will throw an exception (according to the XMLStreamWriter javadoc. We guard against this by using "" for the unqualified namespace. */ namespaceName = (namespaceName == null) ? "" : namespaceName; // Using getNamespaceContext should be avoided when not necessary. // Some parser implementations construct a new NamespaceContext each time it is invoked. // writerPrefix = writer.getNamespaceContext().getPrefix(namespaceName); writerPrefix = writer.getPrefix(namespaceName); if (!"".equals(namespaceName)) { //prefix has already being declared but this particular attrib has a //no prefix attached. So use the prefix provided by the writer if (writerPrefix != null && (prefix == null || prefix.equals(""))) { writer.writeAttribute(writerPrefix, namespaceName, reader.getAttributeLocalName(i), reader.getAttributeValue(i)); //writer prefix is available but different from the current //prefix of the attrib. We should be decalring the new prefix //as a namespace declaration } else if (prefix != null && !"".equals(prefix) && !prefix.equals(writerPrefix)) { writer.writeNamespace(prefix, namespaceName); writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader.getAttributeValue(i)); //prefix is null (or empty), but the namespace name is valid! it has not //being written previously also. So we need to generate a prefix //here } else { prefix = generateUniquePrefix(writer.getNamespaceContext()); writer.writeNamespace(prefix, namespaceName); writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } } else { //empty namespace is equal to no namespace! writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } } }
From source file:org.apache.axis2.jaxws.context.listener.ParserInputStreamCustomBuilder.java
private HashMap<String, String> getElementAttributeDeclarations(XMLStreamReader reader) { HashMap<String, String> attrElementDecls = new HashMap<String, String>(); int count = reader.getAttributeCount(); for (int i = 0; i < count; i++) { String prefix = reader.getAttributePrefix(i); String name = reader.getAttributeLocalName(i); String value = convertEntityReferences(reader.getAttributeValue(i)); String compoundName;// w w w. j a va 2 s.co m if (prefix != null && prefix.length() > 0) { compoundName = prefix + ":" + name; } else { compoundName = name; } attrElementDecls.put(compoundName, value); } return attrElementDecls; }
From source file:org.apache.axis2.jaxws.message.util.impl.SAAJConverterImpl.java
/** * add attributes/*www .ja va 2 s . c om*/ * * @param NameCreator nc * @param element SOAPElement which is the target of the new attributes * @param reader XMLStreamReader whose cursor is at START_ELEMENT * @throws SOAPException */ protected void addAttributes(NameCreator nc, SOAPElement element, XMLStreamReader reader) throws SOAPException { if (log.isDebugEnabled()) { log.debug("addAttributes: Entry"); } // Add the attributes from the reader int size = reader.getAttributeCount(); for (int i = 0; i < size; i++) { QName qName = reader.getAttributeName(i); String prefix = reader.getAttributePrefix(i); String value = reader.getAttributeValue(i); Name name = nc.createName(qName.getLocalPart(), prefix, qName.getNamespaceURI()); element.addAttribute(name, value); try { if (log.isDebugEnabled()) { log.debug("Setting attrType"); } String namespace = qName.getNamespaceURI(); Attr attr = null; // This is an org.w3c.dom.Attr if (namespace == null || namespace.length() == 0) { attr = element.getAttributeNode(qName.getLocalPart()); } else { attr = element.getAttributeNodeNS(namespace, qName.getLocalPart()); } if (attr != null) { String attrType = reader.getAttributeType(i); attr.setUserData(SAAJConverter.OM_ATTRIBUTE_KEY, attrType, null); if (log.isDebugEnabled()) { log.debug("Storing attrType in UserData: " + attrType); } } } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("An error occured while processing attrType: " + e.getMessage()); } } } if (log.isDebugEnabled()) { log.debug("addAttributes: Exit"); } }
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 w w w . j av a 2s.c o m*/ 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();/*from w ww . j a va 2 s . com*/ 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.deegree.client.core.renderer.OutputXMLRenderer.java
private void encodeXML(ResponseWriter writer, String value) throws IOException { int depth = 0; try {/* w w w. j a v a 2 s.c o m*/ 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.deegree.services.csw.exporthandling.GetCapabilitiesHelper.java
private void writeTemplateElement(XMLStreamWriter writer, XMLStreamReader inStream, Map<String, String> varToValue) throws XMLStreamException { if (inStream.getEventType() != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Input stream does not point to a START_ELEMENT event."); }/*from w w w . j av a 2 s .c o m*/ int openElements = 0; boolean firstRun = true; while (firstRun || openElements > 0) { firstRun = false; int eventType = inStream.getEventType(); switch (eventType) { case CDATA: { writer.writeCData(inStream.getText()); break; } case CHARACTERS: { String s = new String(inStream.getTextCharacters(), inStream.getTextStart(), inStream.getTextLength()); // TODO optimize for (String param : varToValue.keySet()) { String value = varToValue.get(param); s = s.replace(param, value); } writer.writeCharacters(s); break; } case END_ELEMENT: { writer.writeEndElement(); openElements--; break; } case START_ELEMENT: { if (inStream.getNamespaceURI() == "" || inStream.getPrefix() == DEFAULT_NS_PREFIX || inStream.getPrefix() == null) { writer.writeStartElement(inStream.getLocalName()); } else { if (writer.getNamespaceContext().getPrefix(inStream.getPrefix()) == "") { // TODO handle special cases for prefix binding, see // http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/xml/namespace/NamespaceContext.html#getNamespaceURI(java.lang.String) writer.setPrefix(inStream.getPrefix(), inStream.getNamespaceURI()); } writer.writeStartElement(inStream.getPrefix(), inStream.getLocalName(), inStream.getNamespaceURI()); } // copy all namespace bindings for (int i = 0; i < inStream.getNamespaceCount(); i++) { String nsPrefix = inStream.getNamespacePrefix(i); String nsURI = inStream.getNamespaceURI(i); writer.writeNamespace(nsPrefix, nsURI); } // copy all attributes for (int i = 0; i < inStream.getAttributeCount(); i++) { String localName = inStream.getAttributeLocalName(i); String nsPrefix = inStream.getAttributePrefix(i); String value = inStream.getAttributeValue(i); String nsURI = inStream.getAttributeNamespace(i); if (nsURI == null) { writer.writeAttribute(localName, value); } else { writer.writeAttribute(nsPrefix, nsURI, localName, value); } } openElements++; break; } default: { break; } } if (openElements > 0) { inStream.next(); } } }