List of usage examples for javax.xml.stream XMLStreamWriter writeDefaultNamespace
public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException;
From source file:org.gaul.s3proxy.S3ProxyHandler.java
private void handleContainerLocation(HttpServletResponse response, BlobStore blobStore, String containerName) throws IOException { try (Writer writer = response.getWriter()) { XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer); xml.writeStartDocument();/* w ww .j av a2s . c om*/ // TODO: using us-standard semantics but could emit actual location xml.writeStartElement("LocationConstraint"); xml.writeDefaultNamespace(AWS_XMLNS); xml.writeEndElement(); xml.flush(); } catch (XMLStreamException xse) { throw new IOException(xse); } }
From source file:ca.uhn.fhir.parser.XmlParser.java
private void encodeXhtml(XhtmlDt theDt, XMLStreamWriter theEventWriter) throws XMLStreamException { if (theDt == null || theDt.getValue() == null) { return;//w ww . j a va 2 s.c om } boolean firstElement = true; for (XMLEvent event : theDt.getValue()) { switch (event.getEventType()) { case XMLStreamConstants.ATTRIBUTE: Attribute attr = (Attribute) event; if (isBlank(attr.getName().getPrefix())) { if (isBlank(attr.getName().getNamespaceURI())) { theEventWriter.writeAttribute(attr.getName().getLocalPart(), attr.getValue()); } else { theEventWriter.writeAttribute(attr.getName().getNamespaceURI(), attr.getName().getLocalPart(), attr.getValue()); } } else { theEventWriter.writeAttribute(attr.getName().getPrefix(), attr.getName().getNamespaceURI(), attr.getName().getLocalPart(), attr.getValue()); } break; case XMLStreamConstants.CDATA: theEventWriter.writeCData(((Characters) event).getData()); break; case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: String data = ((Characters) event).getData(); theEventWriter.writeCharacters(data); break; case XMLStreamConstants.COMMENT: theEventWriter.writeComment(((Comment) event).getText()); break; case XMLStreamConstants.END_ELEMENT: theEventWriter.writeEndElement(); break; case XMLStreamConstants.ENTITY_REFERENCE: EntityReference er = (EntityReference) event; theEventWriter.writeEntityRef(er.getName()); break; case XMLStreamConstants.NAMESPACE: Namespace ns = (Namespace) event; theEventWriter.writeNamespace(ns.getPrefix(), ns.getNamespaceURI()); break; case XMLStreamConstants.START_ELEMENT: StartElement se = event.asStartElement(); if (firstElement) { if (StringUtils.isBlank(se.getName().getPrefix())) { String namespaceURI = se.getName().getNamespaceURI(); if (StringUtils.isBlank(namespaceURI)) { namespaceURI = "http://www.w3.org/1999/xhtml"; } theEventWriter.writeStartElement(se.getName().getLocalPart()); theEventWriter.writeDefaultNamespace(namespaceURI); } else { String prefix = se.getName().getPrefix(); String namespaceURI = se.getName().getNamespaceURI(); theEventWriter.writeStartElement(prefix, se.getName().getLocalPart(), namespaceURI); theEventWriter.writeNamespace(prefix, namespaceURI); } firstElement = false; } else { if (isBlank(se.getName().getPrefix())) { if (isBlank(se.getName().getNamespaceURI())) { theEventWriter.writeStartElement(se.getName().getLocalPart()); } else { if (StringUtils.isBlank(se.getName().getPrefix())) { theEventWriter.writeStartElement(se.getName().getLocalPart()); // theEventWriter.writeDefaultNamespace(se.getName().getNamespaceURI()); } else { theEventWriter.writeStartElement(se.getName().getNamespaceURI(), se.getName().getLocalPart()); } } } else { theEventWriter.writeStartElement(se.getName().getPrefix(), se.getName().getLocalPart(), se.getName().getNamespaceURI()); } for (Iterator<?> attrIter = se.getAttributes(); attrIter.hasNext();) { Attribute next = (Attribute) attrIter.next(); theEventWriter.writeAttribute(next.getName().getLocalPart(), next.getValue()); } } break; case XMLStreamConstants.DTD: case XMLStreamConstants.END_DOCUMENT: case XMLStreamConstants.ENTITY_DECLARATION: case XMLStreamConstants.NOTATION_DECLARATION: case XMLStreamConstants.PROCESSING_INSTRUCTION: case XMLStreamConstants.START_DOCUMENT: break; } } }
From source file:org.activiti.bpmn.converter.export.DefinitionsRootExport.java
@SuppressWarnings("unchecked") public static void writeRootElement(BpmnModel model, XMLStreamWriter xtw, String encoding) throws Exception { xtw.writeStartDocument(encoding, "1.0"); // start definitions root element xtw.writeStartElement(ELEMENT_DEFINITIONS); xtw.setDefaultNamespace(BPMN2_NAMESPACE); xtw.writeDefaultNamespace(BPMN2_NAMESPACE); xtw.writeNamespace(XSI_PREFIX, XSI_NAMESPACE); xtw.writeNamespace(XSD_PREFIX, SCHEMA_NAMESPACE); xtw.writeNamespace(ACTIVITI_EXTENSIONS_PREFIX, ACTIVITI_EXTENSIONS_NAMESPACE); xtw.writeNamespace(BPMNDI_PREFIX, BPMNDI_NAMESPACE); xtw.writeNamespace(OMGDC_PREFIX, OMGDC_NAMESPACE); xtw.writeNamespace(OMGDI_PREFIX, OMGDI_NAMESPACE); for (String prefix : model.getNamespaces().keySet()) { if (!defaultNamespaces.contains(prefix) && StringUtils.isNotEmpty(prefix)) xtw.writeNamespace(prefix, model.getNamespaces().get(prefix)); }/*from w w w . j av a2 s . c o m*/ xtw.writeAttribute(TYPE_LANGUAGE_ATTRIBUTE, SCHEMA_NAMESPACE); xtw.writeAttribute(EXPRESSION_LANGUAGE_ATTRIBUTE, XPATH_NAMESPACE); if (StringUtils.isNotEmpty(model.getTargetNamespace())) { xtw.writeAttribute(TARGET_NAMESPACE_ATTRIBUTE, model.getTargetNamespace()); } else { xtw.writeAttribute(TARGET_NAMESPACE_ATTRIBUTE, PROCESS_NAMESPACE); } BpmnXMLUtil.writeCustomAttributes(model.getDefinitionsAttributes().values(), xtw, model.getNamespaces(), defaultAttributes); }
From source file:org.activiti.dmn.xml.converter.DmnXMLConverter.java
public byte[] convertToXML(DmnDefinition model, String encoding) { try {// w w w .ja v a 2 s. co m ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); XMLOutputFactory xof = XMLOutputFactory.newInstance(); OutputStreamWriter out = new OutputStreamWriter(outputStream, encoding); XMLStreamWriter writer = xof.createXMLStreamWriter(out); XMLStreamWriter xtw = new IndentingXMLStreamWriter(writer); xtw.writeStartElement(ELEMENT_DEFINITIONS); xtw.writeDefaultNamespace(DMN_NAMESPACE); xtw.writeAttribute(ATTRIBUTE_ID, model.getId()); if (StringUtils.isNotEmpty(model.getName())) { xtw.writeAttribute(ATTRIBUTE_NAME, model.getName()); } xtw.writeAttribute(ATTRIBUTE_NAMESPACE, MODEL_NAMESPACE); DmnXMLUtil.writeElementDescription(model, xtw); DmnXMLUtil.writeExtensionElements(model, xtw); for (ItemDefinition itemDefinition : model.getItemDefinitions()) { xtw.writeStartElement(ELEMENT_ITEM_DEFINITION); xtw.writeAttribute(ATTRIBUTE_ID, itemDefinition.getId()); if (StringUtils.isNotEmpty(itemDefinition.getName())) { xtw.writeAttribute(ATTRIBUTE_NAME, itemDefinition.getName()); } DmnXMLUtil.writeElementDescription(itemDefinition, xtw); DmnXMLUtil.writeExtensionElements(itemDefinition, xtw); xtw.writeStartElement(ELEMENT_TYPE_DEFINITION); xtw.writeCharacters(itemDefinition.getTypeDefinition()); xtw.writeEndElement(); xtw.writeEndElement(); } for (Decision decision : model.getDrgElements()) { xtw.writeStartElement(ELEMENT_DECISION); xtw.writeAttribute(ATTRIBUTE_ID, decision.getId()); if (StringUtils.isNotEmpty(decision.getName())) { xtw.writeAttribute(ATTRIBUTE_NAME, decision.getName()); } DmnXMLUtil.writeElementDescription(decision, xtw); DmnXMLUtil.writeExtensionElements(decision, xtw); DecisionTable decisionTable = decision.getDecisionTable(); xtw.writeStartElement(ELEMENT_DECISION_TABLE); xtw.writeAttribute(ATTRIBUTE_ID, decisionTable.getId()); if (decisionTable.getHitPolicy() != null) { xtw.writeAttribute(ATTRIBUTE_HIT_POLICY, decisionTable.getHitPolicy().toString()); } DmnXMLUtil.writeElementDescription(decisionTable, xtw); DmnXMLUtil.writeExtensionElements(decisionTable, xtw); for (InputClause clause : decisionTable.getInputs()) { xtw.writeStartElement(ELEMENT_INPUT_CLAUSE); if (StringUtils.isNotEmpty(clause.getId())) { xtw.writeAttribute(ATTRIBUTE_ID, clause.getId()); } if (StringUtils.isNotEmpty(clause.getLabel())) { xtw.writeAttribute(ATTRIBUTE_LABEL, clause.getLabel()); } DmnXMLUtil.writeElementDescription(clause, xtw); DmnXMLUtil.writeExtensionElements(clause, xtw); if (clause.getInputExpression() != null) { xtw.writeStartElement(ELEMENT_INPUT_EXPRESSION); xtw.writeAttribute(ATTRIBUTE_ID, clause.getInputExpression().getId()); if (StringUtils.isNotEmpty(clause.getInputExpression().getTypeRef())) { xtw.writeAttribute(ATTRIBUTE_TYPE_REF, clause.getInputExpression().getTypeRef()); } if (StringUtils.isNotEmpty(clause.getInputExpression().getText())) { xtw.writeStartElement(ELEMENT_TEXT); xtw.writeCharacters(clause.getInputExpression().getText()); xtw.writeEndElement(); } xtw.writeEndElement(); } xtw.writeEndElement(); } for (OutputClause clause : decisionTable.getOutputs()) { xtw.writeStartElement(ELEMENT_OUTPUT_CLAUSE); if (StringUtils.isNotEmpty(clause.getId())) { xtw.writeAttribute(ATTRIBUTE_ID, clause.getId()); } if (StringUtils.isNotEmpty(clause.getLabel())) { xtw.writeAttribute(ATTRIBUTE_LABEL, clause.getLabel()); } if (StringUtils.isNotEmpty(clause.getName())) { xtw.writeAttribute(ATTRIBUTE_NAME, clause.getName()); } if (StringUtils.isNotEmpty(clause.getTypeRef())) { xtw.writeAttribute(ATTRIBUTE_TYPE_REF, clause.getTypeRef()); } DmnXMLUtil.writeElementDescription(clause, xtw); DmnXMLUtil.writeExtensionElements(clause, xtw); xtw.writeEndElement(); } for (DecisionRule rule : decisionTable.getRules()) { xtw.writeStartElement(ELEMENT_RULE); if (StringUtils.isNotEmpty(rule.getId())) { xtw.writeAttribute(ATTRIBUTE_ID, rule.getId()); } DmnXMLUtil.writeElementDescription(rule, xtw); DmnXMLUtil.writeExtensionElements(rule, xtw); for (RuleInputClauseContainer container : rule.getInputEntries()) { xtw.writeStartElement(ELEMENT_INPUT_ENTRY); xtw.writeAttribute(ATTRIBUTE_ID, container.getInputEntry().getId()); xtw.writeStartElement(ELEMENT_TEXT); xtw.writeCharacters(container.getInputEntry().getText()); xtw.writeEndElement(); xtw.writeEndElement(); } for (RuleOutputClauseContainer container : rule.getOutputEntries()) { xtw.writeStartElement(ELEMENT_OUTPUT_ENTRY); xtw.writeAttribute(ATTRIBUTE_ID, container.getOutputEntry().getId()); xtw.writeStartElement(ELEMENT_TEXT); xtw.writeCharacters(container.getOutputEntry().getText()); xtw.writeEndElement(); xtw.writeEndElement(); } xtw.writeEndElement(); } xtw.writeEndElement(); xtw.writeEndElement(); } // end definitions root element xtw.writeEndElement(); xtw.writeEndDocument(); xtw.flush(); outputStream.close(); xtw.close(); return outputStream.toByteArray(); } catch (Exception e) { LOGGER.error("Error writing BPMN XML", e); throw new DmnXMLException("Error writing BPMN XML", e); } }
From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java
/** * @param reader//from ww w .jav a 2s .c om * @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.util.OMSerializerUtil.java
/** * Method serializeNamespace./*ww w .j a v a 2s . 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// www . j av a2 s . c o m * @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.olingo.client.core.serialization.AtomSerializer.java
private void entityRef(final XMLStreamWriter writer, final Entity entity) throws XMLStreamException { writer.writeStartElement(Constants.ATOM_ELEM_ENTRY_REF); writer.writeDefaultNamespace(namespaceMetadata); writer.writeAttribute(Constants.ATOM_ATTR_ID, entity.getId().toASCIIString()); writer.writeEndElement();/* www. j a v a 2 s . c o m*/ }
From source file:org.apache.olingo.client.core.serialization.AtomSerializer.java
private void entityRef(final XMLStreamWriter writer, final ResWrap<Entity> container) throws XMLStreamException { writer.writeStartElement(Constants.ATOM_ELEM_ENTRY_REF); writer.writeDefaultNamespace(namespaceMetadata); addContextInfo(writer, container);/*w w w . j a va2 s . c o m*/ writer.writeAttribute(Constants.ATOM_ATTR_ID, container.getPayload().getId().toASCIIString()); }