List of usage examples for javax.xml.stream XMLStreamWriter writeNamespace
public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException;
From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java
/** * @param reader/*from w ww . jav a 2s .co 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.axiom.om.impl.serialize.StreamingOMSerializer.java
/** * Method serializeNamespace./*from w w w .ja v a 2 s .c o m*/ * * @param prefix * @param URI * @param writer * @throws XMLStreamException */ private void serializeNamespace(String prefix, String URI, XMLStreamWriter writer) throws XMLStreamException { String prefix1 = writer.getPrefix(URI); if (prefix1 == null) { writer.writeNamespace(prefix, URI); writer.setPrefix(prefix, URI); } }
From source file:org.apache.axiom.om.impl.util.OMSerializerUtil.java
/** * Method serializeNamespace.//from w w w .java2 s . c o m * * @param namespace * @param writer * @throws XMLStreamException * @deprecated Use serializeStartpart instead */ public static void serializeNamespace(OMNamespace namespace, XMLStreamWriter writer) throws XMLStreamException { if (namespace == null) { return; } String uri = namespace.getNamespaceURI(); String prefix = namespace.getPrefix(); if (uri != null && !"".equals(uri)) { String prefixFromWriter = writer.getPrefix(uri); // Handling Default Namespaces First // Case 1 : // here we are trying define a default namespace. But has this been defined in the current context. // yes, there can be a default namespace, but it may have a different URI. If its a different URI // then explicitly define the default namespace here. // Case 2 : // The passed in namespace is a default ns, but there is a non-default ns declared // in the current scope. if (("".equals(prefix) && "".equals(prefixFromWriter) && !uri.equals(writer.getNamespaceContext().getNamespaceURI(""))) || (prefix != null && "".equals(prefix) && (prefixFromWriter == null || !prefix.equals(prefixFromWriter)))) { // this has not been declared earlier writer.writeDefaultNamespace(uri); writer.setDefaultNamespace(uri); } else { prefix = prefix == null ? getNextNSPrefix(writer) : prefix; if (prefix != null && !prefix.equals(prefixFromWriter) && !checkForPrefixInTheCurrentContext(writer, uri, prefix)) { writer.writeNamespace(prefix, uri); writer.setPrefix(prefix, uri); } } } else { // now the nsURI passed is "" or null. Meaning we gonna work with defaultNS. // check whether there is a defaultNS already declared. If yes, is it the same as this ? String currentDefaultNSURI = writer.getNamespaceContext().getNamespaceURI(""); if ((currentDefaultNSURI != null && !currentDefaultNSURI.equals(uri)) || uri != null && !uri.equals(currentDefaultNSURI)) { // this has not been declared earlier writer.writeDefaultNamespace(uri); writer.setDefaultNamespace(uri); } } }
From source file:org.apache.axiom.om.impl.util.OMSerializerUtil.java
/** * Method serializeStartpart. Serialize the start tag of an element. * * @param element//from w ww. j a v a 2 s . com * @param localName (in some cases, the caller wants to force a different localName) * @param writer * @throws XMLStreamException */ public static void serializeStartpart(OMElement element, String localName, 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 StreamingOMSerializer.serializeElement // 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 namespace and prefix of the element OMNamespace eOMNamespace = element.getNamespace(); String ePrefix = null; String eNamespace = null; if (eOMNamespace != null) { ePrefix = eOMNamespace.getPrefix(); eNamespace = eOMNamespace.getNamespaceURI(); } ePrefix = (ePrefix != null && ePrefix.length() == 0) ? null : ePrefix; eNamespace = (eNamespace != null && eNamespace.length() == 0) ? null : eNamespace; if (eNamespace != null) { if (ePrefix == null) { if (!isAssociated("", eNamespace, writer)) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains("")) { writePrefixList.add(""); writeNSList.add(eNamespace); } } writer.writeStartElement("", localName, eNamespace); } else { /* * If XMLStreamWriter.writeStartElement(prefix,localName,namespaceURI) associates * the prefix with the namespace .. */ if (!isAssociated(ePrefix, eNamespace, writer)) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains(ePrefix)) { writePrefixList.add(ePrefix); writeNSList.add(eNamespace); } } writer.writeStartElement(ePrefix, localName, eNamespace); } } else { writer.writeStartElement(localName); } // Generate setPrefix for the namespace declarations Iterator it = element.getAllDeclaredNamespaces(); while (it != null && it.hasNext()) { OMNamespace omNamespace = (OMNamespace) it.next(); String prefix = null; String namespace = null; if (omNamespace != null) { prefix = omNamespace.getPrefix(); namespace = omNamespace.getNamespaceURI(); } prefix = (prefix != null && prefix.length() == 0) ? null : prefix; namespace = (namespace != null && namespace.length() == 0) ? null : namespace; String newPrefix = 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 // Get the prefix and namespace of the element. "" and null are identical. String newPrefix = 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 Iterator attrs = element.getAllAttributes(); while (attrs != null && attrs.hasNext()) { OMAttribute attr = (OMAttribute) attrs.next(); OMNamespace omNamespace = attr.getNamespace(); String prefix = null; String namespace = null; if (omNamespace != null) { prefix = omNamespace.getPrefix(); namespace = omNamespace.getNamespaceURI(); } prefix = (prefix != null && prefix.length() == 0) ? null : prefix; 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 : getNextNSPrefix(); } newPrefix = 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". attrs = element.getAllAttributes(); while (attrs != null && attrs.hasNext()) { OMAttribute attr = (OMAttribute) attrs.next(); OMNamespace omNamespace = attr.getNamespace(); String prefix = null; String namespace = null; if (omNamespace != null) { prefix = omNamespace.getPrefix(); namespace = omNamespace.getNamespaceURI(); } prefix = (prefix != null && prefix.length() == 0) ? null : prefix; namespace = (namespace != null && namespace.length() == 0) ? null : namespace; String local = attr.getLocalName(); if (XSI_URI.equals(namespace) && XSI_LOCAL_NAME.equals(local)) { String value = attr.getAttributeValue(); 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(":")); OMNamespace omNS = element.findNamespaceURI(refPrefix); String refNamespace = (omNS == null) ? null : omNS.getNamespaceURI(); if (refNamespace != null && refNamespace.length() > 0) { newPrefix = 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 attrs = element.getAllAttributes(); while (attrs != null && attrs.hasNext()) { OMAttribute attr = (OMAttribute) attrs.next(); OMNamespace omNamespace = attr.getNamespace(); String prefix = null; String namespace = null; if (omNamespace != null) { prefix = omNamespace.getPrefix(); namespace = omNamespace.getNamespaceURI(); } prefix = (prefix != null && prefix.length() == 0) ? null : prefix; 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 i = 0; i < writePrefixList.size(); i++) { if (namespace.equals((String) writeNSList.get(i))) { prefix = (String) writePrefixList.get(i); } } } } else if (namespace != null) { // 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) && writerPrefix != null && !"".equals(writerPrefix)) { prefix = writerPrefix; } } if (namespace != null) { if (prefix == null && OMConstants.XMLNS_URI.equals(namespace)) { prefix = OMConstants.XMLNS_PREFIX; } // Qualified attribute writer.writeAttribute(prefix, namespace, attr.getLocalName(), attr.getAttributeValue()); } else { // Unqualified attribute writer.writeAttribute(attr.getLocalName(), attr.getAttributeValue()); } } }
From source file:org.apache.axis2.databinding.utils.ConverterUtil.java
public static void serializeAnyType(Object value, XMLStreamWriter xmlStreamWriter) throws XMLStreamException { if (value instanceof String) { serializeAnyType("string", value.toString(), xmlStreamWriter); } else if (value instanceof Integer) { serializeAnyType("int", value.toString(), xmlStreamWriter); } else if (value instanceof Boolean) { serializeAnyType("boolean", value.toString(), xmlStreamWriter); } else if (value instanceof URI) { serializeAnyType("anyURI", value.toString(), xmlStreamWriter); } else if (value instanceof Byte) { serializeAnyType("byte", value.toString(), xmlStreamWriter); } else if (value instanceof Date) { serializeAnyType("date", convertToString((Date) value), xmlStreamWriter); } else if (value instanceof Calendar) { serializeAnyType("dateTime", convertToString((Calendar) value), xmlStreamWriter); } else if (value instanceof Time) { serializeAnyType("time", convertToString((Time) value), xmlStreamWriter); } else if (value instanceof Float) { serializeAnyType("float", value.toString(), xmlStreamWriter); } else if (value instanceof Long) { serializeAnyType("long", value.toString(), xmlStreamWriter); } else if (value instanceof Double) { serializeAnyType("double", value.toString(), xmlStreamWriter); } else if (value instanceof Short) { serializeAnyType("short", value.toString(), xmlStreamWriter); } else if (value instanceof BigDecimal) { serializeAnyType("decimal", ((BigDecimal) value).toPlainString(), xmlStreamWriter); } else if (value instanceof DataHandler) { addTypeAttribute(xmlStreamWriter, "base64Binary"); try {/*from ww w . ja v a 2s.c o m*/ XMLStreamWriterUtils.writeDataHandler(xmlStreamWriter, (DataHandler) value, null, true); } catch (IOException ex) { throw new XMLStreamException("Unable to read data handler", ex); } } else if (value instanceof QName) { QName qNameValue = (QName) value; String prefix = xmlStreamWriter.getPrefix(qNameValue.getNamespaceURI()); if (prefix == null) { prefix = BeanUtil.getUniquePrefix(); xmlStreamWriter.writeNamespace(prefix, qNameValue.getNamespaceURI()); xmlStreamWriter.setPrefix(prefix, qNameValue.getNamespaceURI()); } String attributeValue = qNameValue.getLocalPart(); if (!prefix.equals("")) { attributeValue = prefix + ":" + attributeValue; } serializeAnyType("QName", attributeValue, xmlStreamWriter); } else if (value instanceof UnsignedByte) { serializeAnyType("unsignedByte", convertToString((UnsignedByte) value), xmlStreamWriter); } else if (value instanceof UnsignedLong) { serializeAnyType("unsignedLong", convertToString((UnsignedLong) value), xmlStreamWriter); } else if (value instanceof UnsignedShort) { serializeAnyType("unsignedShort", convertToString((UnsignedShort) value), xmlStreamWriter); } else if (value instanceof UnsignedInt) { serializeAnyType("unsignedInt", convertToString((UnsignedInt) value), xmlStreamWriter); } else if (value instanceof PositiveInteger) { serializeAnyType("positiveInteger", convertToString((PositiveInteger) value), xmlStreamWriter); } else if (value instanceof NegativeInteger) { serializeAnyType("negativeInteger", convertToString((NegativeInteger) value), xmlStreamWriter); } else if (value instanceof NonNegativeInteger) { serializeAnyType("nonNegativeInteger", convertToString((NonNegativeInteger) value), xmlStreamWriter); } else if (value instanceof NonPositiveInteger) { serializeAnyType("nonPositiveInteger", convertToString((NonPositiveInteger) value), xmlStreamWriter); } else { throw new XMLStreamException("Unknow type can not serialize"); } }
From source file:org.apache.axis2.databinding.utils.ConverterUtil.java
private static void addTypeAttribute(XMLStreamWriter xmlStreamWriter, String type) throws XMLStreamException { String prefix = xmlStreamWriter.getPrefix(Constants.XSI_NAMESPACE); if (prefix == null) { prefix = BeanUtil.getUniquePrefix(); xmlStreamWriter.writeNamespace(prefix, Constants.XSI_NAMESPACE); xmlStreamWriter.setPrefix(prefix, Constants.XSI_NAMESPACE); }/*from w w w . ja va 2 s.c om*/ prefix = xmlStreamWriter.getPrefix(Constants.XSD_NAMESPACE); if (prefix == null) { prefix = BeanUtil.getUniquePrefix(); xmlStreamWriter.writeNamespace(prefix, Constants.XSD_NAMESPACE); xmlStreamWriter.setPrefix(prefix, Constants.XSD_NAMESPACE); } String attributeValue = null; if (prefix.equals("")) { attributeValue = type; } else { attributeValue = prefix + ":" + type; } xmlStreamWriter.writeAttribute(Constants.XSI_NAMESPACE, "type", attributeValue); }
From source file:org.apache.axis2.policy.model.MTOM10Assertion.java
public void serialize(XMLStreamWriter writer) throws XMLStreamException { String prefix = writer.getPrefix(NS); if (prefix == null) { prefix = PREFIX;/* w ww . j a v a2 s.c om*/ writer.setPrefix(PREFIX, NS); } writer.writeStartElement(PREFIX, MTOM_SERIALIZATION_CONFIG_LN, NS); if (optional) { writer.writeAttribute(Constants.ATTR_WSP, null, Constants.Q_ELEM_OPTIONAL_ATTR.getLocalPart(), "true"); } writer.writeNamespace(PREFIX, NS); writer.writeEndElement(); }
From source file:org.apache.axis2.policy.model.MTOM11Assertion.java
public void serialize(XMLStreamWriter writer) throws XMLStreamException { String prefix = writer.getPrefix(NS); if (prefix == null) { prefix = PREFIX;//from w ww. j a v a2 s .c om writer.setPrefix(PREFIX, NS); } writer.writeStartElement(PREFIX, MTOM_LN, NS); if (optional) { writer.writeAttribute(Constants.ATTR_WSP, null, Constants.Q_ELEM_OPTIONAL_ATTR.getLocalPart(), "true"); } writer.writeNamespace(PREFIX, NS); writer.writeEndElement(); }
From source file:org.apache.flex.compiler.config.Configuration.java
/** * @return Metadata XML string./*w ww .jav a 2 s . co m*/ */ private final String generateMetadata() { final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); assert xmlOutputFactory != null : "Expect XMLOutputFactory implementation."; final StringWriter stringWriter = new StringWriter(); XMLStreamWriter xmlWriter = null; try { xmlWriter = new XMLFormatter(xmlOutputFactory.createXMLStreamWriter(stringWriter)); xmlWriter.writeStartDocument(); xmlWriter.writeStartElement("rdf", "RDF", RDF_URI); xmlWriter.setPrefix("rdf", RDF_URI); xmlWriter.writeNamespace("rdf", RDF_URI); // write rdf:Description xmlWriter.writeStartElement(RDF_URI, "Description"); xmlWriter.setPrefix("dc", DC_URI); xmlWriter.setPrefix(VersionInfo.COMPILER_NAMESPACE_PREFIX, VersionInfo.COMPILER_NAMESPACE_URI); xmlWriter.writeNamespace("dc", DC_URI); xmlWriter.writeNamespace(VersionInfo.COMPILER_NAMESPACE_PREFIX, VersionInfo.COMPILER_NAMESPACE_URI); // write dc:format xmlWriter.writeStartElement(DC_URI, "format"); xmlWriter.writeCharacters("application/x-shockwave-flash"); xmlWriter.writeEndElement(); if (isFlex()) { // write localizedTitles writeMap(xmlWriter, DC_URI, "description", localizedDescriptions); // write localizedDescription writeMap(xmlWriter, DC_URI, "title", localizedTitles); // write publisher writeCollection(xmlWriter, DC_URI, "publisher", publishers); // write creators writeCollection(xmlWriter, DC_URI, "creator", creators); // write contributor writeCollection(xmlWriter, DC_URI, "contributor", contributors); // write language writeCollection(xmlWriter, DC_URI, "language", langs); // write date writeDate(xmlWriter); } // write compiledBy writeCompiledBy(xmlWriter); // write xmlWriter.writeEndElement(); // Description xmlWriter.writeEndDocument(); } catch (XMLStreamException e) { return ""; } return stringWriter.toString(); }
From source file:org.apache.olingo.client.core.serialization.AbstractAtomDealer.java
protected void namespaces(final XMLStreamWriter writer) throws XMLStreamException { writer.writeNamespace(StringUtils.EMPTY, Constants.NS_ATOM); writer.writeNamespace(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI); writer.writeNamespace(Constants.PREFIX_METADATA, Constants.NS_METADATA); writer.writeNamespace(Constants.PREFIX_DATASERVICES, Constants.NS_DATASERVICES); writer.writeNamespace(Constants.PREFIX_GML, Constants.NS_GML); writer.writeNamespace(Constants.PREFIX_GEORSS, Constants.NS_GEORSS); }