Search in sources :

Example 16 with OutputFormat

use of org.apache.xml.serialize.OutputFormat in project mondrian by pentaho.

the class XmlUtil method validate.

public static void validate(Document doc, String schemaLocationPropertyValue, EntityResolver resolver) throws IOException, SAXException {
    OutputFormat format = new OutputFormat(doc, null, true);
    StringWriter writer = new StringWriter(1000);
    XMLSerializer serial = new XMLSerializer(writer, format);
    serial.asDOMSerializer();
    serial.serialize(doc);
    String docString = writer.toString();
    validate(docString, schemaLocationPropertyValue, resolver);
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) OutputFormat(org.apache.xml.serialize.OutputFormat)

Example 17 with OutputFormat

use of org.apache.xml.serialize.OutputFormat in project ats-framework by Axway.

the class DatabaseSnapshotBackupUtils method saveToFile.

/**
 * Save a snapshot into a file
 * @param snapshot the snapshot to save
 * @param backupFile the backup file name
 * @return the XML document
 */
public Document saveToFile(DatabaseSnapshot snapshot, String backupFile) {
    log.info("Save database snapshot into file " + backupFile + " - START");
    // create the directory if does not exist
    File dirPath = new File(IoUtils.getFilePath(backupFile));
    if (!dirPath.exists()) {
        dirPath.mkdirs();
    }
    Document doc;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (Exception e) {
        throw new DatabaseSnapshotException("Error creating DOM parser for " + backupFile, e);
    }
    // TODO - add DTD or schema for manual creation and easy validation
    Element dbNode = doc.createElement(DatabaseSnapshotUtils.NODE_DB_SNAPSHOT);
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_SNAPSHOT_NAME, snapshot.name);
    // the timestamp comes when user takes a snapshot from database
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_METADATA_TIME, DatabaseSnapshotUtils.dateToString(snapshot.metadataTimestamp));
    // the timestamp now
    snapshot.contentTimestamp = System.currentTimeMillis();
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_CONTENT_TIME, DatabaseSnapshotUtils.dateToString(snapshot.contentTimestamp));
    doc.appendChild(dbNode);
    // append all table data
    for (TableDescription tableDescription : snapshot.tables) {
        Element tableNode = doc.createElement(DatabaseSnapshotUtils.NODE_TABLE);
        // append table meta data
        dbNode.appendChild(tableNode);
        tableDescription.toXmlNode(doc, tableNode);
        // check if table content is to be skipped
        SkipContent skipTableContentOption = snapshot.skipContentPerTable.get(tableDescription.getName().toLowerCase());
        if (skipTableContentOption != null) {
            // we skip the table content
            if (skipTableContentOption.isRememberNumberOfRows()) {
                // ... but we want to persist the number of rows
                int numberRows = snapshot.loadTableLength(snapshot.name, tableDescription, null, null);
                tableNode.setAttribute(DatabaseSnapshotUtils.ATTR_TABLE_NUMBER_ROWS, String.valueOf(numberRows));
            }
            continue;
        }
        // append table content
        List<String> valuesList = snapshot.loadTableData(snapshot.name, tableDescription, snapshot.skipColumnsPerTable, snapshot.skipRowsPerTable, null, null);
        for (String values : valuesList) {
            Element rowNode = doc.createElement(DatabaseSnapshotUtils.NODE_ROW);
            rowNode.setTextContent(StringUtils.escapeNonPrintableAsciiCharacters(values));
            tableNode.appendChild(rowNode);
        }
    }
    // append any skip table content rules
    for (SkipContent skipContent : snapshot.skipContentPerTable.values()) {
        skipContent.toXmlNode(doc, dbNode);
    }
    // append any skip table column rules
    for (SkipColumns skipColumns : snapshot.skipColumnsPerTable.values()) {
        skipColumns.toXmlNode(doc, dbNode);
    }
    // append any skip index attribute rules
    for (SkipIndexAttributes skipIndexAttributes : snapshot.skipIndexAttributesPerTable.values()) {
        skipIndexAttributes.toXmlNode(doc, dbNode);
    }
    // append any skip table row rules
    for (SkipRows skipRows : snapshot.skipRowsPerTable.values()) {
        skipRows.toXmlNode(doc, dbNode);
    }
    // save the XML file
    OutputStream fos = null;
    try {
        OutputFormat format = new OutputFormat(doc);
        format.setIndenting(true);
        format.setIndent(4);
        format.setLineWidth(1000);
        fos = new FileOutputStream(new File(backupFile));
        XMLSerializer serializer = new XMLSerializer(fos, format);
        serializer.serialize(doc);
    } catch (Exception e) {
        throw new DatabaseSnapshotException("Error saving " + backupFile, e);
    } finally {
        IoUtils.closeStream(fos, "Error closing IO stream to file used for database snapshot backup " + backupFile);
    }
    log.info("Save database snapshot into file " + backupFile + " - END");
    return doc;
}
Also used : SkipIndexAttributes(com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes) SkipRows(com.axway.ats.action.dbaccess.snapshot.rules.SkipRows) XMLSerializer(org.apache.xml.serialize.XMLSerializer) Element(org.w3c.dom.Element) SkipContent(com.axway.ats.action.dbaccess.snapshot.rules.SkipContent) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.apache.xml.serialize.OutputFormat) Document(org.w3c.dom.Document) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) FileOutputStream(java.io.FileOutputStream) File(java.io.File) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) SkipColumns(com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)

Example 18 with OutputFormat

use of org.apache.xml.serialize.OutputFormat in project dbflute-core by dbflute.

the class DfSchemaXmlSerializer method serialize.

// ===================================================================================
// Execute
// =======
public void serialize() {
    _log.info("");
    _log.info("...Starting to process JDBC to SchemaXML");
    loadPreviousSchema();
    _doc = createDocumentImpl();
    _doc.appendChild(_doc.createComment(" Auto-generated by JDBC task! "));
    final String filePath = _schemaXml;
    final String encoding = getSchemaXmlEncoding();
    OutputStreamWriter writer = null;
    try {
        initializeIdentityMapIfNeeds();
        generateXML();
        _log.info("...Serializing XML:");
        _log.info("  filePath = " + filePath);
        _log.info("  encoding = " + encoding);
        final XMLSerializer xmlSerializer;
        {
            mkdirIfNotExists(filePath);
            writer = new OutputStreamWriter(new FileOutputStream(filePath), encoding);
            final OutputFormat outputFormar = new OutputFormat(Method.XML, encoding, true);
            xmlSerializer = new XMLSerializer(writer, outputFormar);
        }
        xmlSerializer.serialize(_doc);
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported encoding: " + encoding;
        throw new IllegalStateException(msg, e);
    } catch (FileNotFoundException e) {
        String msg = "Not found file: " + filePath;
        throw new IllegalStateException(msg, e);
    } catch (IOException e) {
        String msg = "IO exception when serializing SchemaXml: " + filePath;
        throw new IllegalStateException(msg, e);
    } catch (SQLException e) {
        String msg = "SQL exception when serializing SchemaXml: " + filePath;
        throw new IllegalStateException(msg, DfJDBCException.voice(e));
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException ignored) {
            }
        }
    }
    loadNextSchema();
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) SQLException(java.sql.SQLException) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.apache.xml.serialize.OutputFormat) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 19 with OutputFormat

use of org.apache.xml.serialize.OutputFormat in project pentaho-platform by pentaho.

the class PentahoResourceDoclet method getXMLSerializer.

private static XMLSerializer getXMLSerializer(OutputStream os) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    OutputFormat of = new OutputFormat();
    of.setCDataElements(new String[] { "ns1^commentText", "ns2^commentText", "^commentText" });
    XMLSerializer serializer = new XMLSerializer(of);
    serializer.setOutputByteStream(os);
    return serializer;
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) OutputFormat(org.apache.xml.serialize.OutputFormat)

Aggregations

OutputFormat (org.apache.xml.serialize.OutputFormat)19 XMLSerializer (org.apache.xml.serialize.XMLSerializer)19 FileOutputStream (java.io.FileOutputStream)5 Document (org.w3c.dom.Document)5 File (java.io.File)4 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)3 StringWriter (java.io.StringWriter)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DOMSerializer (org.apache.xml.serialize.DOMSerializer)2 Element (org.w3c.dom.Element)2 SAXException (org.xml.sax.SAXException)2 SkipColumns (com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)1 SkipContent (com.axway.ats.action.dbaccess.snapshot.rules.SkipContent)1 SkipIndexAttributes (com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes)1 SkipRows (com.axway.ats.action.dbaccess.snapshot.rules.SkipRows)1 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)1 TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)1 FileSystemSnapshotException (com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)1 AtsConfigurationException (com.axway.ats.core.atsconfig.exceptions.AtsConfigurationException)1