List of usage examples for javax.xml.stream XMLStreamWriter setDefaultNamespace
public void setDefaultNamespace(String uri) throws XMLStreamException;
From source file:org.eclipse.smila.datamodel.xml.StaxRecordWriter.java
/** * write a record to an XML stream./* w w w. j av a 2s. com*/ * * @param staxWriter * target XML stream * @param record * the record to write * @param addNamespace * add xmlns attribute * @throws XMLStreamException * StAX error */ private void writeRecord(final XMLStreamWriter staxWriter, final Record record, final boolean addNamespace) throws XMLStreamException { staxWriter.setDefaultNamespace(XmlConstants.NAMESPACE_RECORD); newline(staxWriter); writeStartElement(staxWriter, XmlConstants.TAG_RECORD); if (addNamespace) { staxWriter.writeDefaultNamespace(XmlConstants.NAMESPACE_RECORD); } staxWriter.writeAttribute(XmlConstants.ATTRIBUTE_VERSION, XmlConstants.SCHEMA_VERSION_RECORD); newline(staxWriter); if (record.getId() != null) { staxWriter.setDefaultNamespace(XmlConstants.NAMESPACE_RECORD); } if (record.getMetadata() != null) { writeMetadata(staxWriter, record.getMetadata()); } if (record.hasAttachments()) { final Iterator<String> attachmentNames = record.getAttachmentNames(); while (attachmentNames.hasNext()) { final String attachmentName = attachmentNames.next(); writeTextElement(staxWriter, XmlConstants.TAG_ATTACHMENT, attachmentName); } } writeEndElement(staxWriter); }
From source file:org.flowable.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(FLOWABLE_EXTENSIONS_PREFIX, FLOWABLE_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)); }/* w w w . java 2 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.flowable.cmmn.converter.export.DefinitionsRootExport.java
public static void writeRootElement(CmmnModel model, XMLStreamWriter xtw, String encoding) throws Exception { xtw.writeStartDocument(encoding, "1.0"); // start definitions root element xtw.writeStartElement(ELEMENT_DEFINITIONS); xtw.setDefaultNamespace(CMMN_NAMESPACE); xtw.writeDefaultNamespace(CMMN_NAMESPACE); xtw.writeNamespace(XSI_PREFIX, XSI_NAMESPACE); xtw.writeNamespace(FLOWABLE_EXTENSIONS_PREFIX, FLOWABLE_EXTENSIONS_NAMESPACE); xtw.writeNamespace(CMMNDI_PREFIX, CMMNDI_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 ww w. j av a 2 s. co m*/ if (StringUtils.isNotEmpty(model.getTargetNamespace())) { xtw.writeAttribute(ATTRIBUTE_TARGET_NAMESPACE, model.getTargetNamespace()); } else { xtw.writeAttribute(ATTRIBUTE_TARGET_NAMESPACE, CASE_NAMESPACE); } }
From source file:org.mule.module.xml.util.XMLUtils.java
private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { String local = reader.getLocalName(); String uri = reader.getNamespaceURI(); String prefix = reader.getPrefix(); if (prefix == null) { prefix = ""; }/* w w w . j av a 2s . c o m*/ // System.out.println("STAXUTILS:writeStartElement : node name : " + local + " namespace URI" + uri); boolean writeElementNS = false; if (uri != null) { String boundPrefix = writer.getPrefix(uri); if (boundPrefix == null || !prefix.equals(boundPrefix)) { writeElementNS = true; } } // Write out the element name if (uri != null) { if (prefix.length() == 0 && StringUtils.isEmpty(uri)) { writer.writeStartElement(local); writer.setDefaultNamespace(uri); } else { writer.writeStartElement(prefix, local, uri); writer.setPrefix(prefix, uri); } } else { writer.writeStartElement(local); } // Write out the namespaces for (int i = 0; i < reader.getNamespaceCount(); i++) { String nsURI = reader.getNamespaceURI(i); String nsPrefix = reader.getNamespacePrefix(i); if (nsPrefix == null) { nsPrefix = ""; } if (nsPrefix.length() == 0) { writer.writeDefaultNamespace(nsURI); } else { writer.writeNamespace(nsPrefix, nsURI); } if (nsURI.equals(uri) && nsPrefix.equals(prefix)) { writeElementNS = false; } } // Check if the namespace still needs to be written. // We need this check because namespace writing works // different on Woodstox and the RI. if (writeElementNS) { if (prefix.length() == 0) { writer.writeDefaultNamespace(uri); } else { writer.writeNamespace(prefix, uri); } } // Write out attributes for (int i = 0; i < reader.getAttributeCount(); i++) { String ns = reader.getAttributeNamespace(i); String nsPrefix = reader.getAttributePrefix(i); if (ns == null || ns.length() == 0) { writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } else if (nsPrefix == null || nsPrefix.length() == 0) { writer.writeAttribute(reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } else { writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } } }
From source file:org.netbeans.jbatch.modeler.spec.core.Definitions.java
public static void unload(ModelerFile file, List<String> definitionIdList) { File savedFile = file.getFile(); if (definitionIdList.isEmpty()) { return;//from w ww . j a v a2s. com } try { File cloneSavedFile = File.createTempFile("TMP", "job"); FileUtils.copyFile(savedFile, cloneSavedFile); BufferedReader br = new BufferedReader(new FileReader(savedFile)); String line = null; while ((line = br.readLine()) != null) { System.out.println("pre savedFile : " + line); } XMLOutputFactory xof = XMLOutputFactory.newFactory(); XMLStreamWriter xsw = xof.createXMLStreamWriter(new FileWriter(savedFile)); xsw.setDefaultNamespace("http://jbatchsuite.java.net"); xsw.writeStartDocument(); xsw.writeStartElement("jbatchnb", "root", "http://jbatchsuite.java.net"); xsw.writeNamespace("jbatch", "http://xmlns.jcp.org/xml/ns/javaee"); xsw.writeNamespace("jbatchnb", "http://jbatchsuite.java.net"); xsw.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); xsw.writeNamespace("java", "http://jcp.org/en/jsr/detail?id=270"); xsw.writeNamespace("nbm", "http://nbmodeler.java.net"); if (cloneSavedFile.length() != 0) { try { XMLInputFactory xif = XMLInputFactory.newFactory(); StreamSource xml = new StreamSource(cloneSavedFile); XMLStreamReader xsr = xif.createXMLStreamReader(xml); xsr.nextTag(); while (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) { // Def Y N // Tag N(D) Y(D) // ________________ // T T // ---------------- // // Def Y N // Tag Y(S) N(S) // ________________ // S S // ---------------- // // Def Y N // Tag Y(D) N(S) // ________________ // T S // ---------------- // // (D) => Different // (S) => Same // Y => Id Exist // N => Def Id is null // T => Transform // S => Skip if (xsr.getLocalName().equals("definitions")) { // if (definitionId == null) { // if (xsr.getAttributeValue(null, "id") != null) { // transformXMLStream(xsr, xsw); // } // } else { if (xsr.getAttributeValue(null, "id") == null) { System.out.println("transformXMLStream " + null); transformXMLStream(xsr, xsw); } else { if (!definitionIdList.contains(xsr.getAttributeValue(null, "id"))) { System.out.println("transformXMLStream " + xsr.getAttributeValue(null, "id")); transformXMLStream(xsr, xsw); } else { System.out.println("skipXMLStream " + xsr.getAttributeValue(null, "id")); skipXMLStream(xsr); } } // } } System.out.println( "pre xsr.getEventType() : " + xsr.getEventType() + " " + xsr.getLocalName()); xsr.nextTag(); System.out.println( "post xsr.getEventType() : " + xsr.getEventType() + " " + xsr.getLocalName()); } } catch (XMLStreamException ex) { Exceptions.printStackTrace(ex); } } xsw.writeEndDocument(); xsw.close(); br = new BufferedReader(new FileReader(savedFile)); line = null; while ((line = br.readLine()) != null) { System.out.println("post savedFile : " + line); } } catch (IOException ex) { Exceptions.printStackTrace(ex); } catch (XMLStreamException ex) { Exceptions.printStackTrace(ex); } }
From source file:org.netbeans.jbatch.modeler.specification.model.job.util.JobUtil.java
public void saveModelerFile(ModelerFile modelerFile) { Definitions definitions = (Definitions) modelerFile.getDefinitionElement(); try {// w ww. j av a 2s . c om updateBatchDiagram(modelerFile); List<String> closeDefinitionIdList = closeDiagram(modelerFile, definitions.getGarbageDefinitions()); List<String> definitionIdList = new ArrayList<String>(closeDefinitionIdList); // definitionIdList.addAll(definitions.getGarbageDefinitions()); definitionIdList.add(definitions.getId()); File savedFile = modelerFile.getFile(); BufferedReader br = new BufferedReader(new FileReader(savedFile)); String line = null; while ((line = br.readLine()) != null) { System.out.println("savedFile : " + line); } File cloneSavedFile = File.createTempFile("TMP", "job"); FileUtils.copyFile(savedFile, cloneSavedFile); // br = new BufferedReader(new FileReader(cloneSavedFile)); // line = null; // while ((line = br.readLine()) != null) { // System.out.println("line2 : " + line); // } XMLOutputFactory xof = XMLOutputFactory.newFactory(); XMLStreamWriter xsw = xof.createXMLStreamWriter(new FileWriter(savedFile)); xsw.setDefaultNamespace("http://jbatchsuite.java.net"); xsw.writeStartDocument(); xsw.writeStartElement("jbatchnb", "root", "http://jbatchsuite.java.net"); xsw.writeNamespace("jbatch", "http://xmlns.jcp.org/xml/ns/javaee"); xsw.writeNamespace("jbatchnb", "http://jbatchsuite.java.net"); xsw.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); xsw.writeNamespace("java", "http://jcp.org/en/jsr/detail?id=270"); xsw.writeNamespace("nbm", "http://nbmodeler.java.net"); // br = new BufferedReader(new FileReader(savedFile)); // line = null; // while ((line = br.readLine()) != null) { // System.out.println("line3 : " + line); // } if (cloneSavedFile.length() != 0) { try { XMLInputFactory xif = XMLInputFactory.newFactory(); StreamSource xml = new StreamSource(cloneSavedFile); XMLStreamReader xsr = xif.createXMLStreamReader(xml); xsr.nextTag(); while (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) { // Def Y N // Tag N(D) Y(D) // ________________ // T T // ---------------- // // Def Y N // Tag Y(S) N(S) // ________________ // S S // ---------------- // // Def Y N // Tag Y(D) N(S) // ________________ // T S // ---------------- // // (D) => Different // (S) => Same // Y => Id Exist // N => Id is null // T => Transform // S => Skip if (xsr.getLocalName().equals("definitions")) { // if (definitions.getId() == null) { // if (xsr.getAttributeValue(null, "id") != null) { // transformXMLStream(xsr, xsw); // } else { // skipXMLStream(xsr); // } // } else { if (xsr.getAttributeValue(null, "id") == null) { if (definitions.getId() == null) { skipXMLStream(xsr); } else { transformXMLStream(xsr, xsw); } } else { if (!definitionIdList.contains(xsr.getAttributeValue(null, "id"))) { transformXMLStream(xsr, xsw); } else { skipXMLStream(xsr); } } // } } xsr.nextTag(); } } catch (XMLStreamException ex) { Exceptions.printStackTrace(ex); } } JAXBElement<Definitions> je = new JAXBElement<Definitions>( new QName("http://jbatchsuite.java.net", "definitions", "jbatchnb"), Definitions.class, definitions); if (jobContext == null) { jobContext = JAXBContext.newInstance(new Class<?>[] { ShapeDesign.class, Definitions.class }); } if (jobMarshaller == null) { jobMarshaller = jobContext.createMarshaller(); } // output pretty printed jobMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jobMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); // jobMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.omg.org/spec/Batch/20100524/MODEL http://www.omg.org/spec/Batch/2.0/20100501/Batch20.xsd"); jobMarshaller.setEventHandler(new ValidateJAXB()); jobMarshaller.marshal(je, System.out); jobMarshaller.marshal(je, xsw); // xsw.writeEndElement(); xsw.writeEndDocument(); xsw.close(); // StringWriter sw = new StringWriter(); // jobMarshaller.marshal(file.getDefinitionElement(), sw); // FileUtils.writeStringToFile(savedFile, sw.toString().replaceFirst("xmlns:ns[A-Za-z\\d]{0,3}=\"http://www.omg.org/spec/Batch/20100524/MODEL\"", // "xmlns=\"http://www.omg.org/spec/Batch/20100524/MODEL\"")); } catch (JAXBException ex) { Exceptions.printStackTrace(ex); } catch (IOException ex) { Exceptions.printStackTrace(ex); } catch (XMLStreamException ex) { Exceptions.printStackTrace(ex); } }
From source file:org.ops4j.pax.exam.karaf.container.internal.DependenciesDeployer.java
/** * Write a feature xml structure for test dependencies specified as ProvisionOption * in system to the given writer/*from ww w . j a v a2s. com*/ * * @param writer where to write the feature xml * @param provisionOptions dependencies */ static void writeDependenciesFeature(Writer writer, ProvisionOption<?>... provisionOptions) { XMLOutputFactory xof = XMLOutputFactory.newInstance(); xof.setProperty("javax.xml.stream.isRepairingNamespaces", true); XMLStreamWriter sw = null; try { sw = xof.createXMLStreamWriter(writer); sw.writeStartDocument("UTF-8", "1.0"); sw.setDefaultNamespace(KARAF_FEATURE_NS); sw.writeCharacters("\n"); sw.writeStartElement("features"); sw.writeAttribute("name", "test-dependencies"); sw.writeCharacters("\n"); sw.writeStartElement("feature"); sw.writeAttribute("name", "test-dependencies"); sw.writeCharacters("\n"); for (ProvisionOption<?> provisionOption : provisionOptions) { if (provisionOption.getURL().startsWith("link") || provisionOption.getURL().startsWith("scan-features")) { // well those we've already handled at another location... continue; } sw.writeStartElement("bundle"); if (provisionOption.getStartLevel() != null) { sw.writeAttribute("start-level", provisionOption.getStartLevel().toString()); } sw.writeCharacters(provisionOption.getURL()); endElement(sw); } endElement(sw); endElement(sw); sw.writeEndDocument(); } catch (XMLStreamException e) { throw new RuntimeException("Error writing feature " + e.getMessage(), e); } finally { close(sw); } }
From source file:org.osaf.cosmo.mc.CollectionService.java
public void writeTo(OutputStream out) throws IOException, XMLStreamException { XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); writer.setPrefix(PRE_XML, NS_XML);/* w w w . j a v a2 s .c o m*/ writer.setDefaultNamespace(NS_MC); try { writer.writeStartDocument(); writer.writeStartElement(EL_MC_SERVICE); writer.writeDefaultNamespace(NS_MC); writer.writeAttribute(NS_XML, EL_XML_BASE, locator.getMorseCodeBase()); for (CollectionItem collection : collections) { writer.writeStartElement(EL_MC_COLLECTION); writer.writeAttribute(ATTR_MC_UUID, collection.getUid()); writer.writeAttribute(ATTR_MC_HREF, href(collection)); writer.writeStartElement(EL_MC_NAME); writer.writeCharacters(collection.getDisplayName()); writer.writeEndElement(); for (Ticket ticket : visibleTickets(collection)) { writer.writeStartElement(EL_MC_TICKET); writer.writeAttribute(ATTR_MC_TYPE, ticket.getType().toString()); writer.writeCharacters(ticket.getKey()); writer.writeEndElement(); } writer.writeEndElement(); } writer.writeEndElement(); writer.writeEndDocument(); } finally { writer.close(); } }
From source file:org.osaf.cosmo.xml.DomWriter.java
private static void writeElement(Element e, XMLStreamWriter writer) throws XMLStreamException { //if (log.isDebugEnabled()) //log.debug("Writing element " + e.getNodeName()); String local = e.getLocalName(); if (local == null) local = e.getNodeName();//from w w w . j a v a 2 s .co m String ns = e.getNamespaceURI(); if (ns != null) { String prefix = e.getPrefix(); if (prefix != null) { writer.writeStartElement(prefix, local, ns); writer.writeNamespace(prefix, ns); } else { writer.setDefaultNamespace(ns); writer.writeStartElement(ns, local); writer.writeDefaultNamespace(ns); } } else { writer.writeStartElement(local); } NamedNodeMap attributes = e.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) writeAttribute((Attr) attributes.item(i), writer); NodeList children = e.getChildNodes(); for (int i = 0; i < children.getLength(); i++) writeNode(children.item(i), writer); writer.writeEndElement(); }
From source file:org.rhq.enterprise.server.sync.ExportingInputStream.java
/** * @param wrt//from w w w . j a v a 2 s . c o m * @throws XMLStreamException */ private void exportPrologue(XMLStreamWriter wrt) throws XMLStreamException { wrt.setDefaultNamespace(SynchronizationConstants.EXPORT_NAMESPACE); wrt.setPrefix(SynchronizationConstants.EXPORT_NAMESPACE_PREFIX, SynchronizationConstants.EXPORT_NAMESPACE); wrt.setPrefix(SynchronizationConstants.CONFIGURATION_INSTANCE_NAMESPACE_PREFIX, SynchronizationConstants.CONFIGURATION_INSTANCE_NAMESPACE); wrt.setPrefix(SynchronizationConstants.CONFIGURATION_NAMESPACE_PREFIX, SynchronizationConstants.CONFIGURATION_NAMESPACE); NamespaceContext nsContext = SynchronizationConstants.createConfigurationExportNamespaceContext(); wrt.setNamespaceContext(nsContext); wrt.writeStartDocument(); wrt.writeStartElement(SynchronizationConstants.EXPORT_NAMESPACE, SynchronizationConstants.CONFIGURATION_EXPORT_ELEMENT); wrt.writeNamespace(SynchronizationConstants.CONFIGURATION_INSTANCE_NAMESPACE_PREFIX, ConfigurationInstanceDescriptorUtil.NS_CONFIGURATION_INSTANCE); wrt.writeNamespace(SynchronizationConstants.CONFIGURATION_NAMESPACE_PREFIX, SynchronizationConstants.CONFIGURATION_NAMESPACE); writeValidators(wrt); }