1
2 package org.webdocwf.util.xml;
3
4 import java.sql.*;
5 import java.util.Properties;
6 import java.io.File;
7 import java.io.RandomAccessFile;
8
9 /***
10 * This class implements the Driver interface for the XmlJdbc driver.
11 */
12
13 public class XmlDriver implements Driver
14 {
15
16 public static final String DEFAULT_EXTENSION = ".xml";
17 public static final String FILE_EXTENSION="fileExtension";
18 private final static String URL_PREFIX = "jdbc:webdocwf:xml:";
19 private Properties info = null;
20 private String filePath;
21
22
23 /***
24 *Gets the propertyInfo attribute of the XmlDriver object
25 *
26 * @param url Description of Parameter
27 * @param info Description of Parameter
28 * @return The propertyInfo value
29 * @exception SQLException Description of Exception
30 * @since
31 */
32 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
33 throws SQLException
34 {
35 return new DriverPropertyInfo[0];
36 }
37
38
39 /***
40 *Gets the majorVersion attribute of the XmlDriver object
41 *
42 * @return The majorVersion value
43 * @since
44 */
45 public int getMajorVersion()
46 {
47 return 1;
48 }
49
50
51 /***
52 *Gets the minorVersion attribute of the XmlDriver object
53 *
54 * @return The minorVersion value
55 * @since
56 */
57 public int getMinorVersion()
58 {
59 return 0;
60 }
61
62
63 /***
64 *Description of the Method
65 *
66 * @param url Description of Parameter
67 * @param info Description of Parameter
68 * @return Description of the Returned Value
69 * @exception SQLException Description of Exception
70 * @since
71 */
72 public Connection connect(String url, Properties info) throws SQLException
73 {
74 DriverManager.println("XmlJdbc - XmlDriver:connect() - url=" + url);
75 // check for correct url
76 if (!url.startsWith(URL_PREFIX))
77 {
78 return null;
79 }
80 // get filepath from url
81 String filePath = url.substring(URL_PREFIX.length());
82 this.filePath = filePath;
83 // if (!filePath.endsWith(File.separator))
84 // {
85 // filePath += File.separator;
86 // }
87 DriverManager.println("XmlJdbc - XmlDriver:connect() - filePath=" + filePath);
88 // check if filepath is a correct path.
89 File checkPath = new File(filePath + this.DEFAULT_EXTENSION);
90 if (!checkPath.exists())
91 {
92 this.createDatabase();
93 }
94
95 return new XmlConnection(filePath, info);
96 }
97
98
99 /***
100 *Description of the Method
101 *
102 * @param url Description of Parameter
103 * @return Description of the Returned Value
104 * @exception SQLException Description of Exception
105 * @since
106 */
107 public boolean acceptsURL(String url) throws SQLException
108 {
109 DriverManager.println("XmlJdbc - XmlDriver:accept() - url=" + url);
110 return url.startsWith(URL_PREFIX);
111 }
112
113
114 /***
115 *Description of the Method
116 *
117 * @return Description of the Returned Value
118 * @since
119 */
120 public boolean jdbcCompliant()
121 {
122 return false;
123 }
124 // This static block inits the driver when the class is loaded by the JVM.
125 static
126 {
127 try
128 {
129 java.sql.DriverManager.registerDriver(new XmlDriver());
130 }
131 catch (SQLException e)
132 {
133 throw new RuntimeException(
134 "FATAL ERROR: Could not initialise Xml driver ! Message was: "
135 + e.getMessage());
136 }
137 }
138
139 private void createDatabase() throws SQLException {
140 String path = this.filePath + this.DEFAULT_EXTENSION;
141 File file = new File( path );
142 try {
143 if(!file.exists())
144 file.createNewFile();
145 RandomAccessFile fileLogr = new RandomAccessFile( path , "rw");
146 fileLogr.seek(fileLogr.length());
147 fileLogr.writeBytes( "<?xml version=\"1.0\"?>" + "\r\n");
148 fileLogr.close();
149 } catch (Exception e) { throw new SQLException("Error in creating new database file : "+e.getMessage()); }
150 }
151 }
152
This page automatically generated by Maven