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.util.*; 023 024 import org.xml.sax.*; 025 import org.xml.sax.helpers.*; 026 import org.dbreplicator.replication.*; 027 import org.dbreplicator.replication.MetaDataInfo; 028 029 public class DDLHandler extends DefaultHandler 030 { 031 032 XMLElement currentElement = new XMLElement(""); ; 033 private Subscription subscription; 034 private ArrayList tables; 035 private String tableName; 036 private String filterClause; 037 private ArrayList repTables; 038 private HashMap primColMap; 039 private ArrayList primCols; 040 private ArrayList foreignKeyCols; 041 private ArrayList schemas; 042 private String conflictResolver; 043 private MetaDataInfo mdi; 044 private String createShadowTable; 045 private String cyclicDependency; 046 private ArrayList columnsToBeIgnored; 047 048 private ArrayList alterTableAddForeignKeyQueries; 049 /** 050 * This class is the implementation class for ContentHandler for parsing the 051 * structure XML file. This class implements differnt event methods which automatically 052 * are called by the parser. It implementsthese methods to get and use the different 053 * values stored on the XML file. The methods are implemented for getting the shemas, 054 * queries, other stored information needed for subscribing. 055 */ 056 057 public DDLHandler(Subscription subscription0, ArrayList schemas0, 058 ArrayList tables0, String conflictResolver0, 059 HashMap primColMap0,MetaDataInfo mdi0) 060 { 061 subscription = subscription0; 062 schemas = schemas0; 063 tables = tables0; 064 conflictResolver = conflictResolver0; 065 primColMap = primColMap0; 066 mdi =mdi0; 067 repTables = new ArrayList(); 068 alterTableAddForeignKeyQueries = new ArrayList(); 069 } 070 071 public void startElement(String namespace, String localname, 072 String qname, Attributes atts) throws SAXException 073 { 074 XMLElement childElement = new XMLElement(qname); 075 currentElement.addChild(childElement); 076 childElement.setParentElement(currentElement); 077 currentElement = childElement; 078 if (qname.equalsIgnoreCase("PrimaryColumns")) 079 { 080 primCols = new ArrayList(); 081 } 082 else if (qname.equalsIgnoreCase("ForeignKeyColumns")) { 083 foreignKeyCols = new ArrayList(); 084 } 085 else if (qname.equalsIgnoreCase("IgnoredColumns")) { 086 columnsToBeIgnored = new ArrayList(); 087 } 088 089 } 090 091 public void endElement(String namespace, String localname, String qname) throws 092 SAXException 093 { 094 if (qname.equalsIgnoreCase("Database")) 095 { 096 subscription.setSubscriptionTables(repTables); 097 subscription.setAlterTableAddFKStatements(alterTableAddForeignKeyQueries); 098 } 099 if (qname.equalsIgnoreCase("SchemaName")) 100 { 101 schemas.add(currentElement.elementValue); 102 } 103 if (qname.equalsIgnoreCase("Table")) 104 { 105 createRepTable(); 106 primColMap.put(tableName.toLowerCase(), primCols); 107 primCols = null; 108 } 109 if (qname.equalsIgnoreCase("ColumnName")) 110 { 111 primCols.add(currentElement.elementValue); 112 } 113 if (qname.equalsIgnoreCase("FKColumnName")) { 114 foreignKeyCols.add(currentElement.elementValue); 115 } 116 if (qname.equalsIgnoreCase("Query")) { 117 tables.add(currentElement.elementValue); 118 } 119 if (currentElement.elementName.equalsIgnoreCase( 120 "AlterTableForeignKeyStatement")) { 121 alterTableAddForeignKeyQueries.add(currentElement.elementValue); 122 } 123 if (qname.equalsIgnoreCase("IgnoredColumnName")) { 124 columnsToBeIgnored.add(currentElement.elementValue); 125 } 126 127 XMLElement parentElement = currentElement.getParentElement(); 128 currentElement = parentElement; 129 } 130 131 public void characters(char[] ch, int start, int len) 132 { 133 String elementValue = new String(ch, start, len); 134 if (elementValue.equalsIgnoreCase("") || elementValue.equalsIgnoreCase("\n")) 135 { 136 return; 137 } 138 currentElement.setElementValue(elementValue); 139 if (currentElement.elementName.equalsIgnoreCase("TableName")) 140 { 141 tableName = currentElement.elementValue; 142 } 143 if (currentElement.elementName.equalsIgnoreCase("CreateShadowTable")) { 144 createShadowTable = currentElement.elementValue; 145 } 146 if (currentElement.elementName.equalsIgnoreCase("CyclicDependency")) { 147 cyclicDependency = currentElement.elementValue; 148 } 149 if (currentElement.elementName.equalsIgnoreCase("FilterClause")) { 150 if (currentElement.elementValue.equalsIgnoreCase("NO_DATA")) { 151 filterClause = null; 152 } 153 else 154 { 155 filterClause = currentElement.elementValue; 156 } 157 } 158 } 159 160 private void createRepTable() throws SAXException 161 { 162 SchemaQualifiedName sname = new SchemaQualifiedName(mdi,tableName); 163 RepTable repTable = new RepTable(sname, RepConstants.subscriber); 164 repTable.setFilterClause(filterClause); 165 repTable.setCreateShadowTable(createShadowTable); 166 repTable.setCyclicDependency(cyclicDependency); 167 repTable.setConflictResolver(conflictResolver); 168 //MetaDataInfo mdi = new MetaDataInfo(con); 169 //mdi.setPrimaryColumns(repTable,sname.getSchemaName(),sname.getTableName()); 170 repTable.setPrimaryColumns( (String[]) primCols.toArray(new String[0])); 171 if (foreignKeyCols.size() > 0) 172 //System.out.println("DDHandler foreignKeyCols : "+foreignKeyCols); 173 repTable.setForeignKeyCols( (String[]) foreignKeyCols.toArray(new String[0])); 174 if (columnsToBeIgnored.size() > 0) { 175 repTable.setColumnsToBeIgnored( (String[]) columnsToBeIgnored.toArray(new 176 String[0])); 177 } 178 repTables.add(repTable); 179 } 180 }

