Annotation of FM2SQL/DomDataSource.java, revision 1.1
1.1 ! rogo 1: /*
! 2: * Created on 02.12.2004
! 3: *
! 4: *
! 5: */
! 6: import java.io.File;
! 7: import java.io.FilenameFilter;
! 8: import java.io.IOException;
! 9: import java.sql.PreparedStatement;
! 10: import java.sql.Statement;
! 11: import java.sql.Types;
! 12: import java.util.Hashtable;
! 13: import java.util.Iterator;
! 14: import java.util.Vector;
! 15:
! 16: import javax.xml.parsers.DocumentBuilder;
! 17: import javax.xml.parsers.DocumentBuilderFactory;
! 18: import javax.xml.parsers.ParserConfigurationException;
! 19:
! 20: import org.w3c.dom.Document;
! 21: import org.w3c.dom.Element;
! 22: import org.w3c.dom.NodeList;
! 23: import org.xml.sax.SAXException;
! 24:
! 25: /**
! 26: * @author rogo
! 27: *
! 28: *
! 29: */
! 30: public class DomDataSource
! 31: {
! 32:
! 33: Vector fieldNames = new Vector();
! 34: Vector rowData = new Vector();
! 35: Vector destFieldNames = new Vector();
! 36: String tableName = new String();
! 37: static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
! 38:
! 39: static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
! 40: static String schemaSource = "YourSchemaDefinition.xsd";
! 41: static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
! 42: private boolean first = true;
! 43:
! 44: Hashtable fieldNameToType = new Hashtable();
! 45:
! 46: public void readSingleXML(File fin)
! 47: {
! 48: // TODO rethrow any Exception
! 49: DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
! 50: // Document & its builder
! 51: DocumentBuilder domBuilder;
! 52: try
! 53: {
! 54: schemaSource = fin.getAbsolutePath();
! 55: schemaSource = schemaSource.substring(0, schemaSource.length() - 3) + "xsd";
! 56: System.out.println(schemaSource);
! 57: dbFactory.setValidating(true);
! 58: dbFactory.setNamespaceAware(true);
! 59:
! 60: dbFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
! 61: dbFactory.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource));
! 62:
! 63: domBuilder = dbFactory.newDocumentBuilder();
! 64:
! 65: Document document = domBuilder.parse(fin);
! 66: System.out.println("Schema ");
! 67: xmlReadFromElement(document.getDocumentElement());
! 68:
! 69: }
! 70: catch (ParserConfigurationException e)
! 71: {
! 72: e.printStackTrace();
! 73: }
! 74: catch (SAXException e)
! 75: {
! 76: e.printStackTrace();
! 77: }
! 78: catch (IOException e)
! 79: {
! 80: e.printStackTrace();
! 81: }
! 82:
! 83: }
! 84:
! 85: /**
! 86: * @param documentElement
! 87: */
! 88: private void xmlReadFromElement(Element documentElement)
! 89: {
! 90: NodeList list = documentElement.getChildNodes();
! 91:
! 92: for (int i = 0; i < list.getLength(); i++)
! 93: {
! 94: if (list.item(i) instanceof Element)
! 95: {
! 96: tableName = Convert.convertText(list.item(i).getNodeName());
! 97: NodeList list2 = list.item(i).getChildNodes();
! 98: Hashtable rowToField = new Hashtable();
! 99: for (int j = 0; j < list2.getLength(); j++)
! 100: {
! 101: if (list2.item(j) instanceof Element)
! 102: {
! 103: Element tempElement = (Element) list2.item(j);
! 104: String fieldName = tempElement.getNodeName();
! 105: String typeName = tempElement.getSchemaTypeInfo().getTypeName();
! 106: String fieldValue = tempElement.getTextContent();
! 107: if (!fieldNames.contains(fieldName))
! 108: {
! 109: fieldNames.add(tempElement.getNodeName());
! 110: fieldNameToType.put(fieldName, typeName);
! 111: }
! 112: rowToField.put(fieldName, fieldValue);
! 113:
! 114: // System.out.println(tempElement.getNodeName() + " " +
! 115: // tempElement.getTextContent() + " " +
! 116: // tempElement.getSchemaTypeInfo().getTypeName());
! 117: }
! 118: }
! 119: rowData.add(rowToField);
! 120: first = false;
! 121: // System.out.println(rowToField);
! 122: }
! 123: }
! 124: // System.out.println(fieldNameToType);
! 125: }
! 126: public static void main(String[] args)
! 127: {
! 128: File[] files = new File("./ragepxml").listFiles(new FilenameFilter()
! 129: {
! 130:
! 131: public boolean accept(File arg0, String arg1)
! 132: {
! 133:
! 134: if (arg1.endsWith(".xml"))
! 135: return true;
! 136: else
! 137: return false;
! 138: }
! 139: });
! 140: for (int i = 0; i < files.length; i++)
! 141: {
! 142: System.out.println(files[i]);
! 143:
! 144: }
! 145: for (int j = 0; j < files.length; j++)
! 146: {
! 147: DomDataSource dsrc = new DomDataSource();
! 148: dsrc.readSingleXML(files[j]);
! 149: String[] fieldNames = new String[dsrc.fieldNames.size()];
! 150: int count = 0;
! 151: for (Iterator iter = dsrc.fieldNames.iterator(); iter.hasNext();)
! 152: {
! 153: String element = (String) iter.next();
! 154: dsrc.destFieldNames.add(Convert.convertText(element));
! 155: fieldNames[count] = Convert.convertText(element);
! 156: ++count;
! 157: }
! 158: DBBean bean = new DBBean();
! 159: bean.setURL("jdbc:postgresql://xserve02/islamicms");
! 160: bean.setUserAndPasswd("dwinter", "3333");
! 161: try
! 162: {
! 163: bean.getConnection();
! 164: bean.makeQuery("select * from "+ bean.getQC()+ dsrc.tableName+bean.getQC(), 50);
! 165: DBBean beanDest = new DBBean();
! 166: beanDest.setURL("jdbc:postgresql://xserve02/islamicms2");
! 167: beanDest.setUserAndPasswd("dwinter", "3333");
! 168:
! 169: Statement stmDrop=beanDest.getConnection().createStatement();
! 170: Vector names=beanDest.getTableNames();
! 171: if(names.contains(dsrc.tableName))
! 172: {
! 173: stmDrop.execute("drop table "+dsrc.tableName);
! 174: }
! 175: // beanDest.makeQuery("select * from " + dsrc.tableName, 50);
! 176:
! 177: System.out.println("Warning empty or invalid create statement - creating one for you\n");
! 178:
! 179: StringBuffer command = new StringBuffer(50);
! 180: command.append("CREATE TABLE ");
! 181: command.append(bean.getQC());
! 182: command.append(dsrc.tableName);
! 183: command.append(bean.getQC());
! 184: command.append("(");
! 185: String type = null;
! 186: Vector columnNames = bean.getColumnNames();
! 187: for (int i = 0; i < columnNames.size() - 1; ++i)
! 188: {
! 189: type = bean.metaData.getColumnTypeName(i + 1);
! 190: // System.out.println(i+" "+result[1].get(i)+"
! 191: // "+type);
! 192: type = (type.equals("NUMBER")) ? "INT4" : type;
! 193: type = (type.equals("CONTAINER")) ? "TEXT" : type;
! 194:
! 195: command.append(bean.getQC() + columnNames.get(i) + bean.getQC() + " " + type + ", ");
! 196: }
! 197: type = bean.metaData.getColumnTypeName(columnNames.size());
! 198: type = (type.equals("NUMBER")) ? "INT4" : type;
! 199: type = (type.equals("CONTAINER")) ? "TEXT" : type;
! 200: command.append(bean.getQC() + (String) columnNames.get(columnNames.size() - 1) + bean.getQC() + " " + type);
! 201: command.append(" )");
! 202: System.out.println(command);
! 203: Statement stm = beanDest.getConnection().createStatement();
! 204: stm.execute(command.toString());
! 205: StringBuffer commandStringBuffer = Convert.createInsertCommand(dsrc.tableName, fieldNames);
! 206: PreparedStatement pstm = beanDest.getConnection().prepareStatement(commandStringBuffer.toString());
! 207: dsrc.writeDataInTable(pstm);
! 208: }
! 209: catch (Exception e)
! 210: {
! 211: // TODO Auto-generated catch block
! 212: e.printStackTrace();
! 213: }
! 214: // System.out.println(dsrc.destFieldNames);
! 215: }
! 216: }
! 217:
! 218: /**
! 219: * @param pstm
! 220: */
! 221: private void writeDataInTable(PreparedStatement pstm) throws Exception
! 222: {
! 223: for (Iterator iter = rowData.iterator(); iter.hasNext();)
! 224: {
! 225: Hashtable htTable = (Hashtable) iter.next();
! 226:
! 227: for (int k =0; k<fieldNames.size(); k++)
! 228: {
! 229: String element = (String) fieldNames.get(k);
! 230:
! 231: Object obj = htTable.get(element);
! 232: //System.out.println(obj);
! 233: String str = (obj == null) ? "NULL" : obj.toString();
! 234: if (obj instanceof Double)
! 235: {
! 236: pstm.setDouble(k + 1, ((Double) obj).doubleValue());
! 237: }
! 238: else if (!str.equals("NULL"))
! 239: pstm.setString(k + 1, str);
! 240: else
! 241: pstm.setNull(k + 1, Types.NULL);
! 242: }
! 243: pstm.execute();
! 244: }
! 245:
! 246: }
! 247: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>