001 /** 002 * Copyright (c) 2003 Daffodil Software Ltd all rights reserved, 003 * Modifications Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved. 004 * This program is free software; you can redistribute it and/or modify 005 * it under the terms of version 2 of the GNU General Public License as 006 * published by the Free Software Foundation. 007 * There are special exceptions to the terms and conditions of the GPL 008 * as it is applied to this software. See the GNU General Public License for more details. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 package org.dbreplicator.replication.xml; 021 022 import java.sql.*; 023 024 import org.xml.sax.*; 025 import org.xml.sax.helpers.*; 026 import org.dbreplicator.replication.*; 027 028 public class ServerHandler extends DefaultHandler 029 { 030 XMLElement currentElement; 031 Connection subConnection; 032 Statement statement; 033 034 public ServerHandler(Connection connection) 035 { 036 try 037 { 038 currentElement = new XMLElement("root"); 039 subConnection = connection; 040 statement = subConnection.createStatement(); 041 } 042 catch (SQLException ex) 043 { 044 RepConstants.writeERROR_FILE(ex); 045 } 046 047 } 048 049 public void startElement(String namespace, String localname, String qname, 050 Attributes atts) throws SAXException 051 { 052 XMLElement childElement = new XMLElement(qname); 053 currentElement.addChild(childElement); 054 childElement.setParentElement(currentElement); 055 currentElement = childElement; 056 } 057 058 public void endElement(String namespace, String localname, String qname) throws 059 SAXException 060 { 061 if (qname.equalsIgnoreCase("row")) 062 { 063 createQuery(); 064 } 065 if (qname.equalsIgnoreCase("tablename")) 066 { 067 currentElement.elementList.clear(); 068 } 069 XMLElement parentElement = currentElement.getParentElement(); 070 currentElement = parentElement; 071 } 072 073 public void characters(char[] ch, int start, int len) 074 { 075 String elementValue = new String(ch, start, len); 076 if (elementValue.equalsIgnoreCase("") || elementValue.equalsIgnoreCase("\n")) 077 { 078 return; 079 } 080 currentElement.setElementValue(elementValue); 081 } 082 083 /** @todo 084 * Implement it */ 085 public void createQuery() 086 { 087 // ALL code to be written 088 // try { 089 // String tableName = currentElement.getParentElement().elementValue; 090 // System.out.println("Query for Table > "+ tableName); 091 // 092 // statement.execute("delete from "+tableName); 093 // StringBuffer query = new StringBuffer("Insert into " 094 // +tableName); 095 // query.append(" values "); 096 // ArrayList elements = currentElement.getChildElements(); 097 // StringBuffer columns = new StringBuffer(" ( "); 098 // StringBuffer values = new StringBuffer(" ( "); 099 // for (int i = 0; i < elements.size(); i++) { 100 // columns.append(((XMLElement)elements.get(i)).elementName); 101 // values.append(((XMLElement)elements.get(i)).elementValue); 102 // } 103 // query.append(columns.toString()+" )"); 104 // query.append(values.toString()+" )"); 105 // System.out.println("SNAPSHOT HANDELER Insert Query "+ query.toString()); 106 // 107 // statement.execute(query.toString()); 108 // 109 // } 110 // catch (Exception ex) { 111 // System.out.println(" EXCPTION IN CREATE QUERY "); 112 // throw ex; 113 // } 114 115 } 116 117 }

