TransformMaster.java

/*
* TransformMaster.java
*
* Created on April 29, 2007, 9:59 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package com.arpu.opencds.datatransformation;

import java.io.*;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.text.MaskFormatter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;

/**
* Shows how to use the SqlXMLReader class. This is the TransformMaster command-line
* utility that takes an XSLT file as a command line parameter.
* A transformation is applied and the output is sent to XML.
*
* @author aabdelaziz
* http://www.aliabdelaziz.com/
*/
public class TransformMaster {

final String MS_URL = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=NewContent;";
//"jdbc:microsoft:sqlserver://10.10.10.72:1433;DatabaseName=NewContent";

final String MS_UserName = "Newcont";
final String MS_Password = "contpass";
final String MS_CMSDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

Driver MS_Driver;
Connection MS_Connection;

/** Creates a new instance of TransformMaster */
public TransformMaster() {
initDataBaseConnection();
}

/**
* Accept two command line arguments: the name of an XML file, and
* the name of an XSLT stylesheet. The result of the transformation
* is written to stdout.
*/
public static void main(String[] args) throws Exception {

TransformMaster transformMaster = new TransformMaster();
transformMaster.initDataBaseConnection();
ResultSet rs = transformMaster.getCountriesData();

String xsltFileName = "D:/NetBeans_Projects/DataTransformation/xslt/Echo.xsl";
//"D:/NetBeans_Projects/DataTransformation/xslt/Echo.xsl";
String outFile = "D:/NetBeans_Projects/DataTransformation/data/Countery.xml";
String rootTage = "Country";

OutputStream out = new FileOutputStream(outFile);
transformMaster.resultSetToXML(out, rs, xsltFileName, rootTage);

rs = transformMaster.getContentData();
outFile = "D:/NetBeans_Projects/DataTransformation/data/Content.xml";
out = new FileOutputStream(outFile);
rootTage = "Content";
transformMaster.resultSetToXML(out, rs, xsltFileName, rootTage);
}

protected void resultSetToXML(OutputStream out, ResultSet rs,
String stylesheet, String rootTage) throws IOException{

// Create reader and source objects
//AbstractXMLReader sxreader = new SqlXMLReader();
SqlXMLReader sxreader = new SqlXMLReader();
sxreader.setRootTage(rootTage);
SqlInputSource sis = new SqlInputSource(rs);

// Create SAX source and StreamResult for transform
SAXSource source = new SAXSource(sxreader, sis);
StreamResult result = new StreamResult(out);

// Perform XSLT transform to get results.
// If "stylesheet" is NULL, then use identity
// transform. Otherwise, parse stylesheet and
// build transformer for it.

// Create XSLT transformer
TransformerFactory tf =
TransformerFactory.newInstance();
Transformer t;
try {
if (stylesheet == null) {

t = tf.newTransformer();
} else {
// Read XSL stylesheet from app archive
// and wrap it as a StreamSource. Then use
// it to construct a transformer.
InputStream xslstream = new FileInputStream(stylesheet);
StreamSource xslsource = new StreamSource(xslstream);

t = tf.newTransformer(xslsource);

}
// Do transform
//source.
t.transform(source, result);
} catch (TransformerConfigurationException ex) {
ex.printStackTrace();
} catch (TransformerException ex) {
ex.printStackTrace();
}
}

private ResultSet getCountriesData() {
ResultSet resultSet = null;
try {
// SQL Queries
try {
// Create a statement
Statement stmt = MS_Connection.createStatement();

// Select the row if col_string contains the word pat
String sql = sql = "SELECT * FROM Countries";//Synchronize_view";

// Execute the query
resultSet = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
} finally {}
return resultSet;
}

private ResultSet getContentData() {
ResultSet resultSet = null;
try {
// SQL Queries
try {
// Create a statement
Statement stmt = MS_Connection.createStatement();

// Select the row if col_string contains the word pat
String sql = sql = "SELECT * FROM XMLContent_View";//Synchronize_view";

// Execute the query
resultSet = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
} finally {}
return resultSet;
}

private void initDataBaseConnection() {
// SQL Server Class Name.
try {
Class.forName(MS_CMSDriver);
} catch (ClassNotFoundException exc) {
System.out.println("SQL SERVER 2000 Class Definition in the class path.\n");
System.out.println(exc.getMessage());
return;
}
////////////////////////////////////////////////////////////////////
try {
MS_Driver = DriverManager.getDriver(MS_URL);
System.out.println("Sql Sever 2000 version " +
MS_Driver.getMajorVersion() + "." + MS_Driver.getMinorVersion() +
" Registeration connenction Driver Manager.");
boolean x = MS_Driver.acceptsURL(MS_URL);
} catch (SQLException exc) {
System.out.println("No NewCont has SQL SERVER 2000 access drivers registration.\n");
return;
}
////////////////////////////////////////////////////////////////////

try {
MS_Connection = DriverManager.getConnection(MS_URL, MS_UserName, MS_Password);
System.out.println("Connection established.\n");
} catch (SQLException exc) {
System.out.println("Error in establishing Connection.\n");
return;
}
}
}

There seems to be a lot of

There seems to be a lot of work that need to be done in this process. And it's good to have this kind of resource which can rally help a lot. - Marla Ahlgrimm

Powered by Drupal - Design by artinet