Project

General

Profile

1
/**
2
 *  '$RCSfile$'
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
 *    Release: @release@
9
 *
10
 *   '$Author: jones $'
11
 *     '$Date: 2000-08-14 13:53:34 -0700 (Mon, 14 Aug 2000) $'
12
 * '$Revision: 349 $'
13
 */
14

    
15
package edu.ucsb.nceas.metacat;
16

    
17
import java.io.*;
18
import java.sql.*;
19

    
20
/** 
21
 * A Class that creates an XML text document
22
 * from a query to a relational DB containing a DOM representation
23
 */
24
public class DBReader {
25

    
26
  private Connection	conn = null;
27

    
28
  /**
29
   * main routine used for testing.
30
   * <p>
31
   * Usage: java DBReader <docid>
32
   *
33
   * @param nodeid the id number of the root of the subtree to display
34
   */
35
  static public void main(String[] args) {
36
     
37
     if (args.length < 1)
38
     {
39
        System.err.println("Wrong number of arguments!!!");
40
        System.err.println("USAGE: java DBReader <docid>");
41
        return;
42
     } else {
43
        try {
44
                    
45
          String docid = args[0];
46

    
47
          // Open a connection to the database
48
          MetaCatUtil   util = new MetaCatUtil();
49
          Connection dbconn = util.openDBConnection();
50

    
51
          DBReader rd = new DBReader( dbconn );
52
          String xml = rd.readXMLDocument(docid);
53
          System.out.println(xml);
54

    
55
        } catch (McdbException me) {
56
          me.toXml(new PrintWriter(System.err));
57
        } catch (Exception e) {
58
          System.err.println("EXCEPTION HANDLING REQUIRED");
59
          System.err.println(e.getMessage());
60
          e.printStackTrace(System.err);
61
        }
62
     }
63
  }
64
  
65
  /**
66
   * construct a DBReader instance.
67
   *
68
   * Generally, one calls readXMLDocument() after constructing the instance
69
   *
70
   * @param conn the database connection from which to read the document
71
   */
72
  public DBReader( Connection conn ) 
73
  {
74
    this.conn = conn;
75
  }
76
  
77
  /**
78
   * Get the root node id for an XML document given a document id
79
   *
80
   * @param docid the document node contains the root of the document
81
   * @returns long the nodeid of the root node for this document
82
   */
83
  private long getRootNode(String docid) 
84
          throws McdbDocNotFoundException
85
  {
86
    // Now look up the root node id
87
    long rootnodeid = 0;
88

    
89
    try {
90
      PreparedStatement pstmt =
91
        conn.prepareStatement("SELECT rootnodeid " +
92
                "FROM xml_documents " +
93
                "WHERE docid = ?");
94
      // Bind the values to the query
95
      pstmt.setString(1, docid);
96

    
97
      pstmt.execute();
98
        ResultSet rs = pstmt.getResultSet();
99
          boolean tableHasRows = rs.next();
100
          if (tableHasRows) {
101
              rootnodeid = rs.getLong(1);
102

    
103
          }
104
      pstmt.close();
105
    } catch (SQLException e) {
106
      throw new McdbDocNotFoundException(
107
                "Root node not found for: " + docid, e);
108
    }
109

    
110
    return rootnodeid;
111
  }
112

    
113
  /**
114
   * Create an XML document from the database starting with the element 
115
   * having element_id nodeid
116
   *
117
   * @param docid the document that we want retrieved
118
   */
119
  public String readXMLDocument(String docid) 
120
         throws McdbException
121
  {
122
    try {
123
      StringBuffer doc = new StringBuffer();
124
      DoctypeInfo dti = getDoctypeInfo(docid);
125

    
126
      String docname = dti.getDocname();
127
      String doctype = dti.getDoctype();
128
      String sysid = dti.getSystemID();
129
    
130
      ElementNode element = new ElementNode(conn, getRootNode(docid));
131
      doc.append("<?xml version=\"1.0\"?>\n");
132
        
133
      if (docname != null) {
134
        if ((doctype != null) && (sysid != null)) {
135
          doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype + 
136
                     "\" \"" + sysid + "\">\n");
137
        } else {
138
          doc.append("<!DOCTYPE " + docname + ">\n");
139
        }
140
      }
141
      doc.append(element.toString());
142
  
143
      return (doc.toString());
144

    
145
    } catch (McdbException ex) {
146
      throw ex;
147
    } catch (Throwable t) {
148
      throw new McdbException("Error reading document " + docid + ".");
149
    }
150
  }
151

    
152
  /**
153
   * Look up the document type information from the database
154
   *
155
   * @param docid the id of the document to look up
156
   */
157
  public DoctypeInfo getDoctypeInfo(String docid) 
158
    throws McdbException 
159
  {
160
    PreparedStatement pstmt;
161
    String doctype = null;
162
    String docname = null;
163
    String sysid   = null;
164
    DoctypeInfo dti = null;
165

    
166
    try {
167
      pstmt =
168
        conn.prepareStatement("SELECT docname,doctype " +
169
                                "FROM xml_documents " +
170
                               "WHERE docid = ?");
171
      // Bind the values to the query
172
      pstmt.setString(1, docid);
173

    
174
      pstmt.execute();
175
      ResultSet rs = pstmt.getResultSet();
176
      boolean tableHasRows = rs.next();
177
      if (tableHasRows) {
178
        docname  = rs.getString(1);
179
        doctype  = rs.getString(2);
180
      } 
181
      pstmt.close();
182

    
183
      if (doctype != null) {
184
        pstmt =
185
          conn.prepareStatement("SELECT system_id " +
186
                                  "FROM xml_catalog " +
187
                                 "WHERE public_id = ?");
188
        // Bind the values to the query
189
        pstmt.setString(1, doctype);
190
  
191
        pstmt.execute();
192
        rs = pstmt.getResultSet();
193
        tableHasRows = rs.next();
194
        if (tableHasRows) {
195
          sysid  = rs.getString(1);
196
        } 
197
        pstmt.close();
198
      }
199
    } catch (SQLException e) {
200
      throw new McdbException("Error accessing database connection.", e);
201
    }
202

    
203
    if (docname != null) {
204
      dti = new DoctypeInfo(docname, doctype, sysid);
205
      return dti;
206
    } else {
207
      throw new McdbDocNotFoundException("Document not found: " + docid);
208
    }
209
  }
210

    
211
  /**
212
   * A utility class that encapsulates document type information
213
   */
214
  public class DoctypeInfo {
215
    private String docname = null;
216
    private String doctype = null;
217
    private String system_id = null;
218

    
219
    /**
220
     * Constructor
221
     */
222
    public DoctypeInfo(String docname, String doctype, String system_id) {
223
      this.docname = docname;
224
      this.doctype = doctype;
225
      this.system_id = system_id;
226
    }
227

    
228
    /**
229
     * get the document name
230
     */
231
    public String getDocname() {
232
      return docname;
233
    }
234

    
235
    /**
236
     * get the document type
237
     */
238
    public String getDoctype() {
239
      return doctype;
240
    }
241

    
242
    /**
243
     * get the system identifier
244
     */
245
    public String getSystemID() {
246
      return system_id;
247
    }
248
  }
249

    
250
}
251

    
252
/**
253
 * '$Log$
254
 * 'Revision 1.19  2000/08/11 22:20:04  jones
255
 * 'Changed exception handling mechanisms for DBReader
256
 * '
257
 * 'Revision 1.18  2000/08/10 22:39:04  jones
258
 * 'changed getRootNode method from public to private
259
 * '
260
 * 'Revision 1.17  2000/06/26 10:35:05  jones
261
 * 'Merged in substantial changes to DBWriter and associated classes and to
262
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
263
 * 'functions.  The command line tools and the parameters for the
264
 * 'servlet have changed substantially.
265
 * '
266
 * 'Revision 1.16.2.2  2000/06/25 23:38:16  jones
267
 * 'Added RCSfile keyword
268
 * '
269
 * 'Revision 1.16.2.1  2000/06/25 23:34:17  jones
270
 * 'Changed documentation formatting, added log entries at bottom of source files
271
 * ''
272
 */
(9-9/27)