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);
}
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;
}
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();
}
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;
}
Aggregations