Project

General

Profile

1 21 jones
/**
2 168 jones
 *      Name: DBReader.java
3
 *   Purpose: A Class that creates an XML text document
4
 *            from a query to a relational DB containing a DOM representation
5
 * Copyright: 2000 Regents of the University of California and the
6
 *            National Center for Ecological Analysis and Synthesis
7
 *   Authors: Matt Jones
8 21 jones
 *
9 168 jones
 *   Version: '$Id$'
10 21 jones
 */
11
12 51 jones
package edu.ucsb.nceas.metacat;
13
14 21 jones
import java.io.*;
15
import java.net.URL;
16
import java.net.MalformedURLException;
17
import java.sql.*;
18
import java.util.Stack;
19
20 31 jones
/**
21
 * A Class that creates an XML text document
22
 * from a query to a relational DB containing a DOM representation
23
 */
24 21 jones
public class DBReader {
25
26
  private Connection	conn = null;
27
28 31 jones
  /**
29
   * main routine used for testing.
30 184 jones
   * <p>
31
   * Usage: java DBReader <docid>
32 31 jones
   *
33
   * @param nodeid the id number of the root of the subtree to display
34
   */
35 21 jones
  static public void main(String[] args) {
36
37 184 jones
     if (args.length < 1)
38 21 jones
     {
39
        System.err.println("Wrong number of arguments!!!");
40 184 jones
        System.err.println("USAGE: java DBReader <docid>");
41 21 jones
        return;
42
     } else {
43
        try {
44
45 184 jones
          String docid = args[0];
46 21 jones
47 50 jones
          // Open a connection to the database
48 184 jones
          MetaCatUtil   util = new MetaCatUtil();
49
          Connection dbconn = util.openDBConnection();
50 50 jones
51
          DBReader rd = new DBReader( dbconn );
52 184 jones
          String xml = rd.readXMLDocument(docid);
53 21 jones
          System.out.println(xml);
54
55
        } catch (Exception e) {
56
          System.err.println("EXCEPTION HANDLING REQUIRED");
57
          System.err.println(e.getMessage());
58
          e.printStackTrace(System.err);
59
        }
60
     }
61
  }
62
63 31 jones
  /**
64
   * construct a DBReader instance.
65
   *
66
   * Generally, one calls readXMLDocument() after constructing the instance
67
   *
68 50 jones
   * @param conn the database connection from which to read the document
69 31 jones
   */
70 50 jones
  public DBReader( Connection conn )
71 21 jones
                  throws IOException,
72
                         SQLException,
73
                         ClassNotFoundException
74
  {
75 50 jones
    this.conn = conn;
76 21 jones
  }
77
78 31 jones
  /**
79 86 jones
   * Get the root node id for an XML document given a document id
80
   *
81
   * @param docid the document node contains the root of the document
82
   * @returns long the nodeid of the root node for this document
83
   */
84 162 bojilova
  public long getRootNode(String docid) {
85 86 jones
    // Now look up the root node id
86
    long rootnodeid = 0;
87
88
    try {
89
      PreparedStatement pstmt =
90
        conn.prepareStatement("SELECT rootnodeid " +
91
                "FROM xml_documents " +
92
                "WHERE docid = ?");
93
      // Bind the values to the query
94 162 bojilova
      pstmt.setString(1, docid);
95 86 jones
96
      pstmt.execute();
97
      try {
98
        ResultSet rs = pstmt.getResultSet();
99
        try {
100
          boolean tableHasRows = rs.next();
101
          if (tableHasRows) {
102
            try {
103
              rootnodeid = rs.getLong(1);
104
105
            } catch (SQLException e) {
106
              System.out.println("Error with getLong: " + e.getMessage());
107
            }
108
          }
109
        } catch (SQLException e) {
110
          System.out.println("Error with next: " + e.getMessage());
111
        }
112
      } catch (SQLException e) {
113
        System.out.println("Error with getrset: " + e.getMessage());
114
      }
115
      pstmt.close();
116
    } catch (SQLException e) {
117
      System.out.println("Error getting id: " + e.getMessage());
118
    }
119
120
    return rootnodeid;
121
  }
122
123
  /**
124 31 jones
   * Create an XML document from the database starting with the element
125
   * having element_id nodeid
126
   *
127 86 jones
   * @param docid the document that we want retrieved
128 31 jones
   */
129 162 bojilova
  public String readXMLDocument(String docid) {
130 21 jones
    StringBuffer doc = new StringBuffer();
131 103 jones
    DoctypeInfo dti = getDoctypeInfo(docid);
132 21 jones
133 132 jones
    if (dti != null) {
134
      String docname = dti.getDocname();
135
      String doctype = dti.getDoctype();
136
      String sysid = dti.getSystemID();
137
138
      ElementNode element = new ElementNode(conn, getRootNode(docid));
139
      doc.append("<?xml version=\"1.0\"?>\n");
140
141
      if (docname != null) {
142
        if ((doctype != null) && (sysid != null)) {
143
          doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype +
144
                     "\" \"" + sysid + "\">\n");
145
        } else {
146
          doc.append("<!DOCTYPE " + docname + ">\n");
147
        }
148 103 jones
      }
149 132 jones
      doc.append(element.toString());
150
    } else {
151
      doc.append("<error>Document " + docid + " not found.</error>");
152 103 jones
    }
153 21 jones
154
    return (doc.toString());
155
  }
156 103 jones
157
  /**
158
   * Look up the document type information from the database
159
   *
160
   * @param docid the id of the document to look up
161
   */
162 162 bojilova
  public DoctypeInfo getDoctypeInfo(String docid) {
163 103 jones
    PreparedStatement pstmt;
164
    String doctype = null;
165
    String docname = null;
166
    String sysid   = null;
167
    DoctypeInfo dti = null;
168
169
    try {
170
      pstmt =
171
        conn.prepareStatement("SELECT docname,doctype " +
172
                                "FROM xml_documents " +
173
                               "WHERE docid = ?");
174
      // Bind the values to the query
175 165 jones
      pstmt.setString(1, docid);
176 103 jones
177
      pstmt.execute();
178
      ResultSet rs = pstmt.getResultSet();
179
      boolean tableHasRows = rs.next();
180
      if (tableHasRows) {
181
        docname  = rs.getString(1);
182
        doctype  = rs.getString(2);
183
      }
184
      pstmt.close();
185
186 132 jones
      if (doctype != null) {
187
        pstmt =
188
          conn.prepareStatement("SELECT system_id " +
189
                                  "FROM xml_catalog " +
190
                                 "WHERE public_id = ?");
191
        // Bind the values to the query
192
        pstmt.setString(1, doctype);
193
194
        pstmt.execute();
195
        rs = pstmt.getResultSet();
196
        tableHasRows = rs.next();
197
        if (tableHasRows) {
198
          sysid  = rs.getString(1);
199
        }
200
        pstmt.close();
201
      }
202 103 jones
    } catch (SQLException e) {
203
      System.out.println("Error getting id: " + e.getMessage());
204
    }
205
206 132 jones
    if (docname != null) {
207
      dti = new DoctypeInfo(docname, doctype, sysid);
208
    } else {
209
      dti = null;
210
    }
211
212 103 jones
    return dti;
213
  }
214
215
  /**
216
   * A utility class that encapsulates document type information
217
   */
218
  public class DoctypeInfo {
219
    private String docname = null;
220
    private String doctype = null;
221
    private String system_id = null;
222
223
    /**
224
     * Constructor
225
     */
226
    public DoctypeInfo(String docname, String doctype, String system_id) {
227
      this.docname = docname;
228
      this.doctype = doctype;
229
      this.system_id = system_id;
230
    }
231
232
    /**
233
     * get the document name
234
     */
235
    public String getDocname() {
236
      return docname;
237
    }
238
239
    /**
240
     * get the document type
241
     */
242
    public String getDoctype() {
243
      return doctype;
244
    }
245
246
    /**
247
     * get the system identifier
248
     */
249
    public String getSystemID() {
250
      return system_id;
251
    }
252
  }
253
254 21 jones
}