List of usage examples for javax.xml.stream XMLStreamWriter writeAttribute
public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException;
From source file:org.netbeans.jbatch.modeler.specification.model.job.util.JobUtil.java
void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) { try {/* w w w . jav a2 s . c o m*/ // TransformerFactory tf = TransformerFactory.newInstance(); // Transformer t = tf.newTransformer(); // StAXSource source = new StAXSource(xmlStreamReader); // StAXResult result = new StAXResult(xmlStreamWriter); // t.transform(source, result); boolean finish = false; while (xmlStreamReader.hasNext() && !finish) { switch (xmlStreamReader.getEventType()) { case XMLEvent.START_ELEMENT: String prefix = xmlStreamReader.getPrefix(); String namespaceURI = xmlStreamReader.getNamespaceURI(); if (namespaceURI != null) { if (prefix != null) { xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(), xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI()); } else { xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(), xmlStreamReader.getLocalName()); } } else { xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName()); } for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) { xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i), xmlStreamReader.getNamespaceURI(i)); } int count = xmlStreamReader.getAttributeCount(); for (int i = 0; i < count; i++) { // xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i), // xmlStreamReader.getAttributeNamespace(i), // xmlStreamReader.getAttributeLocalName(i), // xmlStreamReader.getAttributeValue(i)); String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i), attrPrefix = xmlStreamReader.getAttributePrefix(i); if (attrNamespaceURI != null) { if (attrPrefix != null) { xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI, xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } else { xmlStreamWriter.writeAttribute(attrNamespaceURI, xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } } else { xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } } break; case XMLEvent.END_ELEMENT: xmlStreamWriter.writeEndElement(); if (xmlStreamReader.getLocalName().equals("definitions")) { finish = true; } break; case XMLEvent.SPACE: case XMLEvent.CHARACTERS: xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(), xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength()); break; case XMLEvent.PROCESSING_INSTRUCTION: xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(), xmlStreamReader.getPIData()); break; case XMLEvent.CDATA: xmlStreamWriter.writeCData(xmlStreamReader.getText()); break; case XMLEvent.COMMENT: xmlStreamWriter.writeComment(xmlStreamReader.getText()); break; case XMLEvent.ENTITY_REFERENCE: xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName()); break; case XMLEvent.START_DOCUMENT: String encoding = xmlStreamReader.getCharacterEncodingScheme(); String version = xmlStreamReader.getVersion(); if (encoding != null && version != null) { xmlStreamWriter.writeStartDocument(encoding, version); } else if (version != null) { xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion()); } break; case XMLEvent.END_DOCUMENT: xmlStreamWriter.writeEndDocument(); break; case XMLEvent.DTD: xmlStreamWriter.writeDTD(xmlStreamReader.getText()); break; } if (!finish) { xmlStreamReader.next(); } } } catch (XMLStreamException ex) { Exceptions.printStackTrace(ex); } }
From source file:org.osaf.cosmo.xml.DomWriter.java
private static void writeAttribute(Attr a, XMLStreamWriter writer) throws XMLStreamException { //if (log.isDebugEnabled()) //log.debug("Writing attribute " + a.getNodeName()); String local = a.getLocalName(); if (local == null) local = a.getNodeName();/* w w w .j ava 2s .c o m*/ String ns = a.getNamespaceURI(); String value = a.getValue(); // was handled by writing the default namespace in writeElement if (local.equals("xmlns")) return; if (ns != null) { String prefix = a.getPrefix(); if (prefix != null) writer.writeAttribute(prefix, ns, local, value); else writer.writeAttribute(ns, local, value); } else { writer.writeAttribute(local, value); } }
From source file:org.plasma.sdo.xml.StreamMarshaller.java
private void writeRootAttributes(XMLStreamWriter writer) throws XMLStreamException { writer.writeAttribute("xmlns", this.document.getRootElementURI(), namespacePrefix, this.document.getRootElementURI()); writer.writeAttribute("xmlns", SchemaConstants.XMLSCHEMA_NAMESPACE_URI, "xs", SchemaConstants.XMLSCHEMA_NAMESPACE_URI); writer.writeAttribute("xmlns", XMLConstants.XMLSCHEMA_INSTANCE_NAMESPACE_URI, "xsi", XMLConstants.XMLSCHEMA_INSTANCE_NAMESPACE_URI); if (this.document.getSchemaLocation() != null) writer.writeAttribute("xsi", XMLConstants.XMLSCHEMA_INSTANCE_NAMESPACE_URI, "schemaLocation", this.document.getSchemaLocation()); }
From source file:org.plasma.sdo.xml.StreamMarshaller.java
private void writeNonContainmentReferenceContent(XMLStreamWriter writer, DataObject dataObject, DataObject source, Property sourceProperty, PlasmaType targetType, PlasmaType sourceType, int level) throws IOException, XMLStreamException { // create XSI type for all non-containment refs writer.writeAttribute("xsi", XMLConstants.XMLSCHEMA_INSTANCE_NAMESPACE_URI, "type", namespacePrefix + ":" + SchemaUtil.getNonContainmentReferenceName(targetType)); writer.writeAttribute(SchemaUtil.getSerializationAttributeName(), ((PlasmaDataObject) dataObject).getUUIDAsString()); }
From source file:org.plasma.sdo.xml.StreamMarshaller.java
private void writeContent(XMLStreamWriter writer, DataObject dataObject, DataObject source, Property sourceProperty, PlasmaType targetType, PlasmaType sourceType, int level) throws IOException, XMLStreamException { int externKeyCount = 0; for (Property property : targetType.getProperties()) { PlasmaProperty prop = (PlasmaProperty) property; if (prop.isKey(KeyType.external)) { externKeyCount++;/*from w w w .j a v a 2 s . c o m*/ } } // create XSI type on demand for containment refs // FIXME: SDO namespaces are necessary in some cases // to determine exact XSI type to unmarshal. Can't determine // this from the property type on unmarshalling. if (externKeyCount > 0) writer.writeAttribute("xsi", XMLConstants.XMLSCHEMA_INSTANCE_NAMESPACE_URI, "type", namespacePrefix + ":" + SchemaUtil.getContainmentReferenceName(targetType)); writer.writeAttribute(SchemaUtil.getSerializationAttributeName(), ((PlasmaDataObject) dataObject).getUUIDAsString()); for (Property property : targetType.getProperties()) { PlasmaProperty prop = (PlasmaProperty) property; if (!prop.getType().isDataType() || !prop.isXMLAttribute()) { continue; } // FIXME - what about pri-keys which are not sequences //VisibilityKind visibility = (VisibilityKind)prop.get(PlasmaProperty.INSTANCE_PROPERTY_OBJECT_VISIBILITY); //if (visibility != null && visibility.ordinal() == VisibilityKind.private_.ordinal()) // continue; // for properties defined as private no XML or XML Schema property generated Object value = dataObject.get(prop); if (value == null) continue; writer.writeAttribute(helper.getLocalName(prop), fromObject(prop.getType(), value)); } // add element properties List<Property> list = targetType.getProperties(); PlasmaProperty[] properties = new PlasmaProperty[list.size()]; list.toArray(properties); Arrays.sort(properties, this.comparator); for (Property property : properties) { PlasmaProperty prop = (PlasmaProperty) property; if (!prop.getType().isDataType() || prop.isXMLAttribute()) continue; // FIXME - what about pri-keys which are not sequences //VisibilityKind visibility = (VisibilityKind)prop.get(PlasmaProperty.INSTANCE_PROPERTY_OBJECT_VISIBILITY); //if (visibility != null && visibility.ordinal() == VisibilityKind.private_.ordinal()) // continue; // for properties defined as private no XML or XML Schema property generated Object value = dataObject.get(prop); if (value == null) continue; writer.writeStartElement(helper.getLocalName(prop)); writer.writeCharacters(this.fromObject(prop.getType(), value)); writer.writeEndElement(); } }
From source file:org.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java
private static void updateFile(File configFile, Map<String, PropertySimple> allProps) throws IOException, InterruptedException, XMLStreamException { InputStream in = null;/*from w w w. j a v a2s . c o m*/ XMLStreamReader rdr = null; OutputStream out = null; XMLStreamWriter outWrt = null; try { Set<String> processedPropertyNames = new HashSet<String>(); in = new BufferedInputStream(new FileInputStream(configFile)); rdr = XML_INPUT_FACTORY.createXMLStreamReader(in); File tmpFile = File.createTempFile("hadoop-plugin", null); out = new FileOutputStream(tmpFile); outWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); ByteArrayOutputStream stash = new ByteArrayOutputStream(); XMLStreamWriter stashWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(stash); boolean outputActive = true; outWrt.writeStartDocument(); while (rdr.hasNext()) { int event = rdr.next(); XMLStreamWriter wrt = outputActive ? outWrt : stashWrt; switch (event) { case XMLStreamConstants.ATTRIBUTE: break; case XMLStreamConstants.CDATA: wrt.writeCData(rdr.getText()); break; case XMLStreamConstants.CHARACTERS: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.COMMENT: wrt.writeComment(rdr.getText()); break; case XMLStreamConstants.DTD: wrt.writeDTD(rdr.getText()); break; case XMLStreamConstants.END_DOCUMENT: wrt.writeEndDocument(); break; case XMLStreamConstants.END_ELEMENT: if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { String encoding = rdr.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } String propertyTagSoFar = Charset.forName(encoding) .decode(ByteBuffer.wrap(stash.toByteArray())).toString(); DetectedPropertyNameAndUpdatedTag propAndTag = updateProperty(propertyTagSoFar, allProps); //yes, we're intentionally circumventing the xml stream writer, because we already have the XML data we want to write. outWrt.flush(); out.write(propAndTag.updatedTag.getBytes("UTF-8")); processedPropertyNames.add(propAndTag.propertyName); //reset stuff stash.reset(); wrt = outWrt; outputActive = true; } else if (CONFIGURATION_TAG_NAME.equals(rdr.getName().getLocalPart())) { //now add the new props for (String prop : processedPropertyNames) { allProps.remove(prop); } for (Map.Entry<String, PropertySimple> e : allProps.entrySet()) { outWrt.writeStartElement(PROPERTY_TAG_NAME); outWrt.writeStartElement(NAME_TAG_NAME); outWrt.writeCharacters(e.getKey()); outWrt.writeEndElement(); outWrt.writeStartElement(VALUE_TAG_NAME); outWrt.writeCharacters(e.getValue().getStringValue()); outWrt.writeEndElement(); outWrt.writeEndElement(); } } wrt.writeEndElement(); break; case XMLStreamConstants.ENTITY_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.ENTITY_REFERENCE: wrt.writeEntityRef(rdr.getText()); break; case XMLStreamConstants.NAMESPACE: for (int i = 0; i < rdr.getNamespaceCount(); ++i) { wrt.writeNamespace(rdr.getNamespacePrefix(i), rdr.getNamespaceURI(i)); } break; case XMLStreamConstants.NOTATION_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.PROCESSING_INSTRUCTION: wrt.writeProcessingInstruction(rdr.getPITarget(), rdr.getPIData()); break; case XMLStreamConstants.SPACE: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.START_DOCUMENT: //this seems to be never called for some strange reason //wrt.writeStartDocument(); break; case XMLStreamConstants.START_ELEMENT: wrt.writeStartElement(rdr.getName().getPrefix(), rdr.getName().getLocalPart(), rdr.getName().getNamespaceURI()); for (int i = 0; i < rdr.getAttributeCount(); ++i) { wrt.writeAttribute(rdr.getAttributePrefix(i), rdr.getAttributeNamespace(i), rdr.getAttributeLocalName(i), rdr.getAttributeValue(i)); } if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { wrt.writeCharacters(""); outputActive = false; } break; } } outWrt.flush(); out.flush(); out.close(); in.close(); //now copy the temp file in the place of the original one FileUtil.copyFile(tmpFile, configFile); } finally { rdr.close(); outWrt.flush(); outWrt.close(); try { in.close(); } finally { out.flush(); out.close(); } } }
From source file:org.slc.sli.modeling.wadl.writer.WadlWriter.java
private static final void writeApplication(final Application app, final Map<String, String> prefixMappings, final XMLStreamWriter xsw) throws XMLStreamException { xsw.writeStartElement(WADL_PREFIX, WadlElementName.APPLICATION.getLocalName(), WadlSyntax.NAMESPACE); try {//from w w w .j av a 2 s . com xsw.writeNamespace(WADL_PREFIX, WadlSyntax.NAMESPACE); xsw.writeNamespace("xsi", SCHEMA_INSTANCE_NS); xsw.writeAttribute("xsi", SCHEMA_INSTANCE_NS, "schemaLocation", SCHEMA_LOCATION_VALUE); xsw.writeNamespace("xs", SCHEMA_NS); for (final Map.Entry<String, String> prefix : prefixMappings.entrySet()) { xsw.writeNamespace(prefix.getKey(), prefix.getValue()); } writeDocumentation(app, xsw); writeGrammars(app.getGrammars(), xsw); writeResources(app.getResources(), xsw); for (final ResourceType resourceType : app.getResourceTypes()) { writeResourceType(resourceType, xsw); } for (final Method method : app.getMethods()) { writeMethod(method, xsw); } for (final Representation representation : app.getRepresentations()) { writeRepresentation(representation, xsw); } for (final Representation representation : app.getFaults()) { writeFault(representation, xsw); } } finally { xsw.writeEndElement(); } }
From source file:org.slc.sli.modeling.wadl.writer.WadlWriter.java
private static final void writeDocumentation(final Documentation doc, final XMLStreamWriter xsw) throws XMLStreamException { xsw.writeStartElement(WADL_PREFIX, WadlElementName.DOCUMENTATION.getLocalName(), WadlSyntax.NAMESPACE); try {// ww w . j ava 2 s . com if (doc.getLanguage() != null) { xsw.writeAttribute("xml", "http://www.w3.org/XML/1998/namespace", "lang", doc.getLanguage()); } if (doc.getTitle() != null) { xsw.writeAttribute(WadlAttributeName.TITLE.getLocalName(), doc.getTitle()); } writeContents(doc.getContents(), xsw); } finally { xsw.writeEndElement(); } }
From source file:org.slc.sli.modeling.xmi.comp.XmiMappingWriter.java
private static final void writeMappingDocument(final XmiComparison documentElement, final XMLStreamWriter xsw) throws XMLStreamException { xsw.writeProcessingInstruction("xml-stylesheet", "type='text/xsl' href='xmi-mapping.xsl'"); xsw.setPrefix("", XmiMappingConstants.NAMESPACE_URI); xsw.writeStartElement(XmiMappingConstants.DOCUMENT_ELEMENT.getLocalPart()); xsw.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); xsw.writeAttribute("xsi", "http://www.w3.org/2001/XMLSchema-instance", "noNamespaceSchemaLocation", "xmi-mapping.xsd"); try {// w ww .j a va 2 s. c o m writeReference(documentElement.getLhsDef(), XmiMappingConstants.LHS_MODEL, xsw); writeReference(documentElement.getRhsDef(), XmiMappingConstants.RHS_MODEL, xsw); for (final XmiMapping mapping : documentElement.getMappings()) { writeMapping(mapping, xsw); } } finally { xsw.writeEndElement(); } }