001: /*
002: * XmlTableDefinitionParser.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.importer;
013:
014: import java.io.IOException;
015: import java.io.Reader;
016: import javax.xml.parsers.ParserConfigurationException;
017: import javax.xml.parsers.SAXParser;
018: import javax.xml.parsers.SAXParserFactory;
019: import org.xml.sax.Attributes;
020: import org.xml.sax.InputSource;
021: import org.xml.sax.SAXException;
022: import org.xml.sax.helpers.DefaultHandler;
023: import workbench.db.ColumnIdentifier;
024: import workbench.db.exporter.XmlRowDataConverter;
025: import workbench.log.LogMgr;
026: import workbench.util.StringUtil;
027:
028: /**
029: * A parser to read the table definition from a Workbench XML file.
030: * @author support@sql-workbench.net
031: */
032: public class XmlTableDefinitionParser extends DefaultHandler {
033: private int currentColIndex;
034: private ColumnIdentifier[] columnList;
035: private String tableName;
036: private ImportFileHandler fileHandler;
037: private StringBuilder chars;
038: private String tagFormat;
039:
040: public XmlTableDefinitionParser(ImportFileHandler handler)
041: throws IOException, SAXException {
042: this .fileHandler = handler;
043: this .parseTableStructure();
044: }
045:
046: public ColumnIdentifier[] getColumns() {
047: return this .columnList;
048: }
049:
050: public String getTagFormat() {
051: return this .tagFormat;
052: }
053:
054: public String getTableName() {
055: return this .tableName;
056: }
057:
058: private void parseTableStructure() throws IOException, SAXException {
059: SAXParserFactory factory = SAXParserFactory.newInstance();
060: factory.setValidating(false);
061: Reader in = null;
062: try {
063: SAXParser saxParser = factory.newSAXParser();
064: in = this .fileHandler.getMainFileReader();
065: InputSource source = new InputSource(in);
066: saxParser.parse(source, this );
067: } catch (ParserConfigurationException ce) {
068: // should not happen
069: } catch (ParsingEndedException e) {
070: // expected exception to stop parsing
071: } catch (IOException e) {
072: throw e;
073: } catch (SAXException e) {
074: LogMgr.logError("XmlDataTableParser.parseTableStructure()",
075: "Error reading table structure", e);
076: this .columnList = null;
077: this .tableName = null;
078: throw e;
079: } finally {
080: try {
081: fileHandler.done();
082: } catch (Throwable th) {
083: }
084: }
085: }
086:
087: public void startElement(String namespaceURI, String sName,
088: String qName, Attributes attrs) throws SAXException {
089: this .chars = new StringBuilder();
090: if (qName.equals(XmlRowDataConverter.COLUMN_DEF_TAG)) {
091: this .columnList[this .currentColIndex] = new ColumnIdentifier();
092: }
093: }
094:
095: public void endElement(String namespaceURI, String sName,
096: String qName) throws SAXException {
097: if (qName.equals(XmlRowDataConverter.TAG_TAG_FORMAT)) {
098: this .tagFormat = this .chars.toString();
099: } else if (qName.equals(XmlRowDataConverter.COLUMN_COUNT_TAG)) {
100: try {
101: int count = StringUtil.getIntValue(this .chars
102: .toString(), -1);
103: this .columnList = new ColumnIdentifier[count];
104: this .currentColIndex = 0;
105: } catch (Exception e) {
106: LogMgr.logError("XmlTableDefinitionParser.endElement",
107: "Incorrec value for "
108: + XmlRowDataConverter.COLUMN_COUNT_TAG
109: + ": " + this .chars, e);
110: throw new SAXException("Invalid column count");
111: }
112: } else if (qName.equals(XmlRowDataConverter.COLUMN_DEF_TAG)) {
113: currentColIndex++;
114: } else if (qName.equals(XmlRowDataConverter.TABLE_NAME_TAG)) {
115: this .tableName = this .chars.toString();
116: } else if (qName.equals(XmlRowDataConverter.COLUMN_NAME_TAG)) {
117: this .columnList[this .currentColIndex]
118: .setColumnName(this .chars.toString());
119: } else if (qName.equals(XmlRowDataConverter.JAVA_TYPE_TAG)) {
120: try {
121: int type = Integer.parseInt(this .chars.toString());
122: this .columnList[currentColIndex].setDataType(type);
123: } catch (Exception e) {
124: LogMgr.logError(
125: "XmlTableDefinitionParser.endElement()",
126: "Could not read columnn type!", e);
127: throw new SAXException("Could not read columnn type");
128: }
129: } else if (qName.equals("dbms-data-type")) {
130: try {
131: this .columnList[currentColIndex].setDbmsType(this .chars
132: .toString());
133: } catch (Exception e) {
134: LogMgr.logError("XmlDataFileParser.endElement()",
135: "Could not read dbms columnn type!", e);
136: throw new SAXException(
137: "Could not read dbms columnn type");
138: }
139: } else if (qName.equals(XmlRowDataConverter.JAVA_CLASS_TAG)) {
140: try {
141: this .columnList[currentColIndex]
142: .setColumnClassName(this .chars.toString());
143: } catch (Exception e) {
144: LogMgr.logError(
145: "XmlTableDefinitionParser.endElement()",
146: "Could not read columnn class name!", e);
147: throw new SAXException("Could not read columnn name");
148: }
149: } else if (qName.equals(XmlRowDataConverter.TABLE_DEF_TAG)) {
150: throw new ParsingEndedException();
151: }
152: }
153:
154: public void characters(char buf[], int offset, int len)
155: throws SAXException {
156: if (chars != null) {
157: this.chars.append(buf, offset, len);
158: }
159: }
160:
161: }
|