Source Code Cross Referenced for XmlTableDefinitionParser.java in  » Database-Client » SQL-Workbench » workbench » db » importer » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » SQL Workbench » workbench.db.importer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.