All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.data.dataset.io.schema.XMLDataSetSchemaWriter Maven / Gradle / Ivy


package com.datastax.data.dataset.io.schema;


import com.datastax.data.dataset.*;

import java.io.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.Set;

/**
 * Writes the schema for a {@link com.datastax.data.dataset.DataSet} out as an XML Schema.
 *
 * 

Canonical use: *

 * Writer writer = new PrintWriter(System.out);
 * XMLDataSetSchemaWriter xmlWriter = new XMLDataSetSchemaWriter(writer);
 * xmlWriter.writeDataSet(ds);
 * 
*

You can also use the static {@link com.datastax.data.dataset.util.DataSetUtility} methods, like * {@link com.datastax.data.dataset.util.DataSetUtility#writeDataSetAsXml(OutputStream, DataSet)} for a one-line operation. */ public class XMLDataSetSchemaWriter implements DataSetSchemaWriter { /** * The PrintWriter used internally for the export, instantiated in the constructor. */ private PrintWriter outputPrintWriter; /** * Creates a new instance of XMLDataSetSchemaWriter for a given {@link OutputStream}. * @param os The {@link java.io.OutputStream} to write to; must be open. Will not be closed by this class. The * OutputStream will be wrapped in a {@link java.io.PrintWriter} so line separation * will be environment-specific. */ public XMLDataSetSchemaWriter(OutputStream os) { if ( ! ( os instanceof BufferedOutputStream)) { os = new BufferedOutputStream(os); } this.outputPrintWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os))); } /** * Creates a new instance of XMLDataSetSchemaWriter for a given {@link Writer}. * @param writer The {@link java.io.Writer} to write to; must be open. Will not be closed by this class. The * writer will be wrapped in a {@link java.io.PrintWriter} so line separation will be * environment-specific. */ public XMLDataSetSchemaWriter(Writer writer) { if ( writer instanceof PrintWriter) { this.outputPrintWriter = (PrintWriter)writer; } else { if ( writer instanceof BufferedWriter) { writer = new BufferedWriter(writer); } this.outputPrintWriter = new PrintWriter(writer); } } public void writeDataSet(DataSet ds) throws SchemaWriterException { writeXmlSchema(ds, ""); outputPrintWriter.flush(); } public void writeDataSet(DataSet ds, String... tableNames) throws SchemaWriterException { writeXmlSchema(ds, tableNames); outputPrintWriter.flush(); } /** * Writes the entire schema to the current {@link PrintWriter}. * @param ds The DataSource to write. * @param tableNames List of table names to write, will include their relations as well. */ private void writeXmlSchema(DataSet ds, String... tableNames) { PrintWriter pw = this.outputPrintWriter; boolean allTables = ( tableNames.length == 1 && tableNames[0].length() == 0 ); pw.println(""); pw.print(""); pw.print("\t"); pw.println("\t\t"); pw.println("\t\t\t"); Map> tables = DataSetIOUtility.extractTableList(tableNames); for (DataTable table : ds.getTables()) { if (!(table instanceof DataRelationTable)) { String tableName = table.getName(); if ( ! allTables && ! tables.containsKey(tableName)) continue; pw.print("\t\t\t\t"); pw.println("\t\t\t\t\t"); pw.println("\t\t\t\t\t\t"); for (DataColumn col : table.getColumns()) { String columnName = col.getName(); Set cols = tables.get(tableName); if ( ! allTables && ! cols.contains(columnName)) continue; pw.print("\t\t\t\t\t\t\t"); } pw.println("\t\t\t\t\t\t"); pw.println("\t\t\t\t\t"); pw.println("\t\t\t\t"); } } pw.println("\t\t\t"); pw.println("\t\t"); pw.println("\t\t"); pw.println("\t\t\t"); //write the relations out for (DataRelation r : ds.getRelations()) { DataColumn parentCol = r.getParentColumn(); DataColumn childCol = r.getChildColumn(); String parentTableName = parentCol.getTable().getName(); String childTableName = childCol.getTable().getName(); if ( ! allTables && ! ( tables.containsKey(parentTableName) && tables.containsKey(childTableName))) continue; Set cols = null; cols = tables.get(parentTableName); if ( ! allTables && ! cols.contains(parentCol.getName())) continue; cols = tables.get(childTableName); if ( ! allTables && ! cols.contains(childCol.getName())) continue; pw.print("\t\t\t\t"); } //write the data relation tables out for (DataTable table : ds.getTables()) { if (table instanceof DataRelationTable) { DataRelationTable drt = (DataRelationTable)table; if ( ! allTables && ! tables.containsKey(drt.getParentTable().getName())) continue; pw.print("\t\t\t\t"); } } //write the data values out for (DataValue value : ds.getValues()) { pw.print("\t\t\t\t"); } //close the annotation section pw.println("\t\t\t"); pw.println("\t\t"); //close the document down pw.println("\t"); pw.println(""); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy