javanet.staxutils.IndentingXMLStreamWriter Java Examples

The following examples show how to use javanet.staxutils.IndentingXMLStreamWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NetworkXml.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private static XMLStreamWriter initializeWriter(Network n, OutputStream os, ExportOptions options) throws XMLStreamException {
    IidmXmlVersion version = options.getVersion() == null ? CURRENT_IIDM_XML_VERSION : IidmXmlVersion.of(options.getVersion(), ".");
    XMLStreamWriter writer;
    writer = XML_OUTPUT_FACTORY_SUPPLIER.get().createXMLStreamWriter(os, StandardCharsets.UTF_8.toString());
    if (options.isIndent()) {
        IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(writer);
        indentingWriter.setIndent(INDENT);
        writer = indentingWriter;
    }
    writer.writeStartDocument(StandardCharsets.UTF_8.toString(), "1.0");
    writer.setPrefix(IIDM_PREFIX, version.getNamespaceURI());
    writer.writeStartElement(version.getNamespaceURI(), NETWORK_ROOT_ELEMENT_NAME);
    writer.writeNamespace(IIDM_PREFIX, version.getNamespaceURI());
    if (!options.withNoExtension()) {
        writeExtensionNamespaces(n, options, writer);
    }
    writeMainAttributes(n, writer);
    return writer;
}
 
Example #2
Source File: WriteZoo.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void go() throws Exception {
    AegisContext context;

    context = new AegisContext();
    context.setWriteXsiTypes(true);
    Set<java.lang.reflect.Type> rootClasses = new HashSet<>();
    rootClasses.add(Zoo.class);
    context.setRootClasses(rootClasses);
    context.initialize();
    AegisWriter<XMLStreamWriter> writer = context.createXMLStreamWriter();
    FileOutputStream output = new FileOutputStream(outputPathname);
    XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(output);
    IndentingXMLStreamWriter indentWriter = new IndentingXMLStreamWriter(xmlWriter);

    Zoo zoo = populateZoo();
    AegisType aegisType = context.getTypeMapping().getType(zoo.getClass());
    writer.write(zoo, new QName("urn:aegis:demo", "zoo"), false, indentWriter, aegisType);
    xmlWriter.close();
    output.close();
}
 
Example #3
Source File: WsdlUtils.java    From iaf with Apache License 2.0 5 votes vote down vote up
public static XMLStreamWriter getWriter(OutputStream out, boolean indentWsdl) throws XMLStreamException {
    XMLStreamWriter w = XmlUtils.REPAIR_NAMESPACES_OUTPUT_FACTORY
            .createXMLStreamWriter(out, XmlUtils.STREAM_FACTORY_ENCODING);
    if (indentWsdl) {
        IndentingXMLStreamWriter iw = new IndentingXMLStreamWriter(w);
        iw.setIndent("\t");
        w = iw;
    }
    return w;
}
 
Example #4
Source File: JpaProcessor.java    From karaf-boot with Apache License 2.0 4 votes vote down vote up
public void process(Writer writer, Map<PersistentUnit, List<? extends AnnotationMirror>> units) throws Exception {
    Set<String> puNames = new HashSet<String>();
    XMLOutputFactory xof =  XMLOutputFactory.newInstance();
    XMLStreamWriter w = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(writer));
    w.setDefaultNamespace("http://java.sun.com/xml/ns/persistence");
    w.writeStartDocument();
    w.writeStartElement("persistence");
    w.writeAttribute("verson", "2.0");
    
    //w.println("<persistence version=\"2.0\" xmlns=\"http://java.sun.com/xml/ns/persistence\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd\">");
    for (PersistentUnit pu : units.keySet()) {
        if (pu.name() == null || pu.name().isEmpty()) {
            throw new IOException("Missing persistent unit name");
        }
        if (!puNames.add(pu.name())) {
            throw new IOException("Duplicate persistent unit name: " + pu.name());
        }
        w.writeStartElement("persistence-unit");
        w.writeAttribute("name", pu.name());
        w.writeAttribute("transaction-type", pu.transactionType().toString());
        writeElement(w, "description", pu.description());
        String providerName = getProvider(pu);
        writeElement(w, "provider", providerName);
        writeElement(w, "jta-data-source", pu.jtaDataSource());
        writeElement(w, "non-jta-data-source", pu.nonJtaDataSource());
        Map<String, String> props = new HashMap<>();
        addProperties(pu, props);
        addAnnProperties(units.get(pu), props);
        if (props.size() > 0) {
            w.writeStartElement("properties");
            for (String key : props.keySet()) {
                w.writeEmptyElement("property");
                w.writeAttribute("name", key);
                w.writeAttribute("value", props.get(key));
            }
            w.writeEndElement();
        }
        w.writeEndElement();
    }
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();
    w.close();
}
 
Example #5
Source File: WriteXMLResult.java    From nifi with Apache License 2.0 4 votes vote down vote up
public WriteXMLResult(final RecordSchema recordSchema, final SchemaAccessWriter schemaAccess, final OutputStream out, final boolean prettyPrint,
                      final NullSuppression nullSuppression, final ArrayWrapping arrayWrapping, final String arrayTagName, final String rootTagName, final String recordTagName,
                      final String charSet, final String dateFormat, final String timeFormat, final String timestampFormat) throws IOException {

    super(out);

    this.recordSchema = recordSchema;
    this.schemaAccess = schemaAccess;
    this.nullSuppression = nullSuppression;

    this.arrayWrapping = arrayWrapping;
    this.arrayTagName = arrayTagName;

    this.rootTagName = rootTagName;

    if (recordTagName != null) {
        this.recordTagName = recordTagName;
    } else {
        Optional<String> recordTagNameOptional = recordSchema.getSchemaName().isPresent()? recordSchema.getSchemaName() : recordSchema.getIdentifier().getName();
        if (recordTagNameOptional.isPresent()) {
            this.recordTagName = recordTagNameOptional.get();
        } else {
            final String message = "The property '" + RECORD_TAG_NAME.getDisplayName() +
                "' has not been set and the writer does not find a record name in the schema.";
            throw new IOException(message);
        }
    }

    this.allowWritingMultipleRecords = !(this.rootTagName == null);
    hasWrittenRecord = false;

    final DateFormat df = dateFormat == null ? null : DataTypeUtils.getDateFormat(dateFormat);
    final DateFormat tf = timeFormat == null ? null : DataTypeUtils.getDateFormat(timeFormat);
    final DateFormat tsf = timestampFormat == null ? null : DataTypeUtils.getDateFormat(timestampFormat);

    LAZY_DATE_FORMAT = () -> df;
    LAZY_TIME_FORMAT = () -> tf;
    LAZY_TIMESTAMP_FORMAT = () -> tsf;

    try {
        XMLOutputFactory factory = XMLOutputFactory.newInstance();

        if (prettyPrint) {
            writer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(out, charSet));
        } else {
            writer = factory.createXMLStreamWriter(out, charSet);
        }

    } catch (XMLStreamException e) {
        throw new IOException(e.getMessage());
    }
}