Source Code Cross Referenced for XmlReader.java in  » Database-JDBC-Connection-Pool » octopus » org » webdocwf » util » xml » 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 JDBC Connection Pool » octopus » org.webdocwf.util.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:            Copyright (C) 2003  Together
003:
004:            This library is free software; you can redistribute it and/or
005:            modify it under the terms of the GNU Lesser General Public
006:            License as published by the Free Software Foundation; either
007:            version 2.1 of the License, or (at your option) any later version.
008:
009:            This library is distributed in the hope that it will be useful,
010:            but WITHOUT ANY WARRANTY; without even the implied warranty of
011:            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012:            Lesser General Public License for more details.
013:
014:            You should have received a copy of the GNU Lesser General Public
015:            License along with this library; if not, write to the Free Software
016:            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
017:         */
018:
019:        package org.webdocwf.util.xml;
020:
021:        //xml imports
022:        import org.w3c.dom.Document;
023:        import org.w3c.dom.NodeList;
024:        import org.w3c.dom.Node;
025:        import org.w3c.dom.Element;
026:        import org.enhydra.xml.*;
027:        import javax.xml.parsers.DocumentBuilder;
028:        import javax.xml.parsers.DocumentBuilderFactory;
029:
030:        import java.sql.*;
031:        import java.io.File;
032:        import java.util.ArrayList;
033:
034:        /**
035:         * Class load existing XML file, creating DOM from file or creating
036:         * new DOM.Class has methods for reading data from XML file.
037:         *
038:         * @author     Zoran Milakovic
039:         */
040:        public class XmlReader {
041:            private String[] columnNames;
042:            private String[] columnValues;
043:            private String tableName;
044:
045:            /**
046:             * Document made from XML file, and in which will
047:             * be made changes.Document will be saved in XML file.
048:             */
049:            private SearchElement searchDocument;
050:            private Document document;
051:            /**
052:             * Full path of the XML file.
053:             */
054:            private String fileName;
055:
056:            /**
057:             * Constructor will build Document from the specified file
058:             *  if file exist, or will create new Document if file not exist.
059:             *
060:             * @param fileName full pathname of the XML file
061:             * @throws SQLException
062:             */
063:            public XmlReader(String fileName) throws SQLException {
064:                DocumentBuilderFactory factory = DocumentBuilderFactory
065:                        .newInstance();
066:                try {
067:                    this .fileName = fileName;
068:                    File file = new File(fileName);
069:                    DocumentBuilder builder = factory.newDocumentBuilder();
070:                    try {
071:                        this .document = builder.parse(file);
072:                    } catch (Exception e) {
073:                        throw new SQLException(
074:                                "Error while parsing XML file ! : "
075:                                        + e.getMessage());
076:                    }
077:                    this .searchDocument = (SearchElement) SearchElement
078:                            .newInstance(document);
079:                } catch (Exception e) {
080:                    throw new SQLException("Error in creating DOM : "
081:                            + e.getMessage());
082:                }
083:            }
084:
085:            private ArrayList rset = new ArrayList();
086:
087:            /**
088:             * Gets data from database.Method will fill array list which will be result set.
089:             * ArrayList will contain arrays of strings.Every array of string will present
090:             * one row in database.
091:             *
092:             * @param tableName Name of table.
093:             * @param columnNames Names of columns from which will be select data.
094:             * @param whereColumnNames Names of columns in where conditions.
095:             * @param whereColumnValues Values of conditions.
096:             * @throws SQLException
097:             */
098:            public void select(String tableName, String[] columnNames,
099:                    String[] whereColumnNames, String[] whereColumnValues)
100:                    throws SQLException {
101:                try {
102:                    NodeList tableRows = searchDocument
103:                            .getSubElementsByTagName("dml/" + tableName);
104:                    for (int i = 0; i < tableRows.getLength(); i++) {
105:                        boolean isMatch = true;
106:                        if (whereColumnNames != null
107:                                && whereColumnValues != null) {
108:                            for (int k = 0; k < whereColumnNames.length; k++) {
109:                                NodeList columns = ((SearchElement) tableRows
110:                                        .item(i))
111:                                        .getSubElementsByCondition(whereColumnNames[k]
112:                                                + "=" + whereColumnValues[k]);
113:                                if (columns.getLength() == 0)
114:                                    isMatch = false;
115:                            }
116:                        }
117:                        if (isMatch) {
118:                            ArrayList colValuesList = new ArrayList();
119:                            colValuesList.clear();
120:                            //columnNames has names of ALL columns, even if some of them are not have tag in xml file.
121:                            //This is posible because all column names are stored in CREATE TABLE statement
122:                            for (int k = 0; k < columnNames.length; k++) {
123:                                NodeList columns = ((SearchElement) tableRows
124:                                        .item(i))
125:                                        .getSubElementsByTagName(columnNames[k]);
126:                                //it is posible that variable columns has zero length,if some column tags are missing
127:                                //in that case, column has null value
128:                                if (columns.getLength() != 0) {
129:                                    Node column = columns.item(0);
130:                                    Node textNode = column.getFirstChild();
131:                                    if (textNode == null)
132:                                        colValuesList.add("null");
133:                                    else
134:                                        colValuesList.add(formatString(textNode
135:                                                .getNodeValue()));
136:                                } else {
137:                                    colValuesList.add("null");
138:                                }
139:                            }
140:                            rset.add(colValuesList.toArray(new String[0]));
141:                            int y = 0;
142:                        }
143:                    }
144:                } catch (Exception e) {
145:                    throw new SQLException("Error in select : "
146:                            + e.getMessage());
147:                }
148:            }
149:
150:            /**
151:             * Gets table names from database.
152:             *
153:             * @throws SQLException
154:             */
155:            public void selectTableNames() throws SQLException {
156:                try {
157:                    ArrayList tableNames = new ArrayList();
158:                    ArrayList tableNamesAll = new ArrayList();
159:
160:                    NodeList sqlStatements = searchDocument
161:                            .getSubElementsByTagName("ddl");
162:                    XmlSqlParser parser = new XmlSqlParser();
163:                    for (int i = 0; i < sqlStatements.getLength(); i++) {
164:                        Node node = sqlStatements.item(i);
165:                        parser.parse(node.getFirstChild().toString());
166:                        String tableName = parser.getTableName();
167:                        if (!tableNamesAll.contains(tableName)) {
168:                            tableNamesAll.add(tableName);
169:                            tableNames.clear();
170:                            tableNames.add(tableName);
171:                            rset.add(tableNames.toArray(new String[0]));
172:                        }
173:                    }
174:
175:                    NodeList allRowTableNames = ((Element) (searchDocument
176:                            .getSubElementsByTagName("dml").item(0)))
177:                            .getChildNodes();
178:                    for (int i = 0; i < allRowTableNames.getLength(); i++) {
179:                        if (allRowTableNames.item(i).getNodeType() != 3) {
180:                            String tableName = allRowTableNames.item(i)
181:                                    .getNodeName();
182:                            if (!tableNamesAll.contains(tableName)) {
183:                                tableNamesAll.add(tableName);
184:                                tableNames.clear();
185:                                tableNames.add(tableName);
186:                                rset.add(tableNames.toArray(new String[0]));
187:                            }
188:                        }
189:                    }
190:                } catch (Exception e) {
191:                    throw new SQLException("Error in selectTableNames : "
192:                            + e.getMessage());
193:                }
194:            }
195:
196:            private String formatString(String str) {
197:                String retVal = str;
198:                retVal = Utils
199:                        .replaceAll(retVal, XmlSqlParser.equalEscape, "=");
200:                retVal = Utils.replaceAll(retVal, XmlSqlParser.atEscape, "@");
201:                retVal = Utils
202:                        .replaceAll(retVal, XmlSqlParser.slashEscape, "/");
203:                return retVal;
204:            }
205:
206:            /**
207:             *
208:             * @return list with results
209:             */
210:            public ArrayList getResultSet() {
211:                return this.rset;
212:            }
213:
214:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.