Project

General

Profile

« Previous | Next » 

Revision 50

Added by Matt Jones over 24 years ago

changed database connection code, added utility class MetaCatUtil.java

View differences:

intro.html
1
<!--
2
  * contents.html
3
  *
4
  *      Authors: Matt Jones
5
  *    Copyright: 2000 Regents of the University of California and the
6
  *               National Center for Ecological Analysis and Synthesis
7
  *  For Details: http://www.nceas.ucsb.edu/
8
  *      Created: 2000 April 5
9
  *      Version: 0.01
10
  *    File Info: '$Id$'
11
  * 
12
  * This is an HTML document for displaying examples of the use of 
13
  * Oracle XML tools
14
-->
15
<html>
16
<head>
17
<title>MetaCat</title>
18
</head>
19
<body bgcolor="white">
20
<b>MetaCat</b>
21
<p>
22
A simple query against a flexible metadata catalog.
23
</p>
24
</body>
25
</html>
26 0

  
src/edu/ucsb/nceas/metacat/DBWriter.java
66 66
            dbstring = args[3];
67 67
          }
68 68

  
69
          new DBSAXWriter(filename, user, password, dbstring);
69
          // Open a connection to the database
70
          Connection dbconn = MetaCatUtil.openDBConnection(
71
                "oracle.jdbc.driver.OracleDriver",
72
                dbstring, user, password);
73

  
74
          new DBSAXWriter(filename, dbconn);
70 75
          System.out.println("Document processing finished for: " + filename);
71 76

  
72 77
        } catch (Exception e) {
......
81 86
   * construct a new instance of the class to write an XML file to the database
82 87
   *
83 88
   * @param filename the filename to be loaded into the database
84
   * @param user the username to use for the database connection
85
   * @param password the password to use for the database connection
86
   * @param dbstring the connection info to use for the database connection
89
   * @param conn the database connection to which to write the XML file
87 90
   */
88
  public DBSAXWriter( String filename, String user, 
89
                  String password, String dbstring) 
91
  public DBSAXWriter( String filename, Connection conn)
90 92
                  throws IOException, 
91 93
                         SQLException, 
92 94
                         ClassNotFoundException
93 95
   {
94
     // Open a connection to the database
95
     conn = openDBConnection(
96
                "oracle.jdbc.driver.OracleDriver",
97
                dbstring, user, password);
98

  
96
     this.conn = conn;
99 97
    
100 98
     //
101 99
     // Set up the SAX document handlers for parsing
......
143 141

  
144 142
   }
145 143
  
146
   /** Utility method to establish the JDBC connection */
147
   private Connection openDBConnection(String dbDriver, String connection,
148
                String user, String password)
149
                throws SQLException, ClassNotFoundException {
150
     // Load the Oracle JDBC driver
151
     Class.forName (dbDriver);
152

  
153
     // Connect to the database
154
     Connection conn = DriverManager.getConnection( connection, user, password);
155
     return conn;
156
   }
157

  
158 144
   /** Utility method to convert a file handle into a URL */
159 145
   static public URL fileToURL(File file) 
160 146
   {
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
1 1
import java.io.PrintWriter;
2 2
import java.io.IOException;
3
import java.io.Reader;
4
import java.io.StringReader;
3 5
import java.util.Enumeration;
4 6
import java.util.Hashtable;
7
import java.net.URL;
8
import java.net.MalformedURLException;
9
import java.sql.Connection;
5 10

  
6 11
import javax.servlet.ServletConfig;
7 12
import javax.servlet.ServletContext;
......
12 17
import javax.servlet.http.HttpServletResponse;
13 18
import javax.servlet.http.HttpUtils;
14 19

  
20
import oracle.xml.parser.v2.XSLStylesheet;
21
import oracle.xml.parser.v2.XSLException;
22
import oracle.xml.parser.v2.XMLDocumentFragment;
23
import oracle.xml.parser.v2.XSLProcessor;
24

  
15 25
/**
16 26
 * A metadata catalog server implemented as a Java Servlet
27
   *
28
   * <p>Valid parameters are:<br>
29
   * action=query -- query the values of all elements and attributes
30
   *                     and return a result set of nodes<br>
31
   * action=getdocument -- display an XML document in XML or HTML<br>
32
   * qformat=xml -- display resultset from query in XML<br>
33
   * qformat=html -- display resultset from query in HTML<br>
34
   * action=getdocument -- display an XML document in XML or HTML<br>
35
   * docid=34 -- display the document with the document ID number 34<br>
17 36
 */
18 37
public class MetaCatServlet extends HttpServlet {
19 38

  
......
21 40
  private ServletContext	context = null;
22 41
  DBSimpleQuery		queryobj = null;
23 42
  DBReader		docreader = null;
24
  static  String 	user = "jones";
25
  static  String 	password = "your-pw-goes-here";
26
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
43
  static  String 	user = MetaCatUtil.user;
44
  static  String 	password = MetaCatUtil.password;
45
  static  String 	defaultDB = MetaCatUtil.defaultDB;
46
  static  String 	resultStyleURL = "file:///home/httpd/html/xmltodb/resultset.xsl";
27 47

  
48
  /**
49
   * Initialize the servlet by creating appropriate database connections
50
   */
28 51
  public void init( ServletConfig config ) throws ServletException {
29 52
    try {
30 53
      super.init( config );
......
32 55
      this.context = config.getServletContext();
33 56
    
34 57
      try {
35
        queryobj = new DBSimpleQuery(user, password, defaultDB);
36
        docreader = new DBReader(user, password, defaultDB);
58
        // Open a connection to the database
59
        Connection dbconn = MetaCatUtil.openDBConnection(
60
                "oracle.jdbc.driver.OracleDriver",
61
                defaultDB, user, password);
62

  
63
        queryobj = new DBSimpleQuery(dbconn);
64
        docreader = new DBReader(dbconn);
37 65
      } catch (Exception e) {
38 66
      }
39 67
    } catch ( ServletException ex ) {
......
41 69
    }
42 70
  }
43 71

  
72
  /** Handle "GET" method requests from HTTP clients */
44 73
  public void doGet (HttpServletRequest request, HttpServletResponse response)
45 74
    throws ServletException, IOException {
46 75

  
......
52 81
    handleGetOrPost(response, params);
53 82
  }
54 83

  
84
  /** Handle "POST" method requests from HTTP clients */
55 85
  public void doPost( HttpServletRequest request, HttpServletResponse response)
56 86
    throws ServletException, IOException {
57 87

  
......
67 97
  }
68 98

  
69 99
  /**
70
   * routine to control servlet response depending on the action requested
71
   * Valid "action" parameters are:
72
   *     query -- query the text values of all elements and attributes
73
   *              and return a result set of nodes
74
   *  getdocument -- display an XML document in XML or HTML given a document ID
100
   * Control servlet response depending on the action parameter specified
75 101
   */
76 102
  private void handleGetOrPost(HttpServletResponse response, Hashtable params) 
77 103
    throws ServletException, IOException {
......
85 111
      handleQueryAction(out, params, response);
86 112
    } else if (action.equals("getdocument")) {
87 113
      handleGetDocumentAction(out, params, response);
114
    } else {
115
      out.println("Error: action not registered.  Please report this error.");
88 116
    }
89 117

  
90 118
    // Close the stream to the client
91 119
    out.close();
92 120
  }
93 121

  
122
  /** 
123
   * Handle the database query request and return a result set, possibly
124
   * transformed from XML into HTML
125
   */
94 126
  private void handleQueryAction(PrintWriter out, Hashtable params, 
95 127
               HttpServletResponse response) {
96 128
      // Run the query
97 129
      String query = ((String[])params.get("query"))[0]; 
98 130
      Hashtable nodelist = queryobj.findRootNodes(query);
99
  
100
      // set content type and other response header fields first
101
      response.setContentType("text/xml");
102
  
131
 
132
      // Create a buffer to hold the xml result
133
      StringBuffer resultset = new StringBuffer();
134
 
103 135
      // Print the resulting root nodes
104 136
      long nodeid;
105
      out.println("<?xml version=\"1.0\"?>\n");
106
      out.println("<resultset>\n");
107
      out.println("  <query>" + query + "</query>");
137
      resultset.append("<?xml version=\"1.0\"?>\n");
138
      resultset.append("<resultset>\n");
139
      resultset.append("  <query>" + query + "</query>");
108 140
      Enumeration rootlist = nodelist.keys(); 
109 141
      while (rootlist.hasMoreElements()) {
110 142
        nodeid = ((Long)rootlist.nextElement()).longValue();
111
        out.println("  <nodeid>" + nodeid + "</nodeid>");
143
        resultset.append("  <nodeid>" + nodeid + "</nodeid>");
112 144
      }
113
      out.println("</resultset>");
145
      resultset.append("</resultset>");
146

  
147
      String qformat = ((String[])params.get("qformat"))[0]; 
148
      if (qformat.equals("xml")) {
149
        // set content type and other response header fields first
150
        response.setContentType("text/xml");
151
        out.println(resultset.toString());
152
      } else if (qformat.equals("html")) {
153
        // set content type and other response header fields first
154
        response.setContentType("text/html");
155
        //out.println("Converting to HTML...");
156
        XMLDocumentFragment htmldoc = null;
157
        try {
158
          XSLStylesheet style = new XSLStylesheet(new URL(resultStyleURL), null);
159
          htmldoc = (new XSLProcessor()).processXSL(style, 
160
                     (Reader)(new StringReader(resultset.toString())),null);
161
          htmldoc.print(out);
162
        } catch (Exception e) {
163
          out.println("Error transforming document:\n" + e.getMessage());
164
        }
165
      }
114 166
  }
115 167

  
168
  /** 
169
   * Handle the database getdocument request and return a XML document, 
170
   * possibly transformed from XML into HTML
171
   */
116 172
  private void handleGetDocumentAction(PrintWriter out, Hashtable params, 
117 173
               HttpServletResponse response) {
118 174
      // Get the document indicated
......
123 179
      response.setContentType("text/xml");
124 180
  
125 181
      out.println(doc);
126

  
127
      // Print the resulting root nodes
128
      //out.println("<?xml version=\"1.0\"?>\n");
129
      //out.println("<docid>" + docid + "</docid>");
130 182
  }
131 183
}
src/edu/ucsb/nceas/metacat/DBSimpleQuery.java
61 61
            dbstring = args[3];
62 62
          }
63 63

  
64
          // Open a connection to the database
65
          Connection dbconn = MetaCatUtil.openDBConnection( 
66
                              "oracle.jdbc.driver.OracleDriver",
67
                              dbstring, user, password);
64 68
          // Execute the simple query
65
          DBSimpleQuery rd = new DBSimpleQuery(user, password, dbstring);
69
          DBSimpleQuery rd = new DBSimpleQuery(dbconn);
66 70
          Hashtable nodelist = rd.findRootNodes(query);
67 71

  
68 72
          // Print the reulting root nodes
......
90 94
  /**
91 95
   * construct an instance of the DBSimpleQuery class 
92 96
   *
93
   * Generally, one would call the findRootNodes() routine after creating 
94
   * an instance to specify the query to search for
97
   * <p>Generally, one would call the findRootNodes() routine after creating 
98
   * an instance to specify the search query</p>
95 99
   *
96
   * @param user the username to use for the database connection
97
   * @param password the password to use for the database connection
98
   * @param dbstring the connection info to use for the database connection
100
   * @param conn the JDBC connection that we use for the query
99 101
   */
100
  public DBSimpleQuery( String user, String password, String dbstring) 
102
  public DBSimpleQuery( Connection conn ) 
101 103
                  throws IOException, 
102 104
                         SQLException, 
103 105
                         ClassNotFoundException
104 106
  {
105
     // Open a connection to the database
106
     conn = openDBConnection(
107
                "oracle.jdbc.driver.OracleDriver",
108
                dbstring, user, password);
109

  
107
    this.conn = conn;
110 108
  }
111 109
  
112
  /** Utility message to establish a JDBC database connection */
113
  private Connection openDBConnection(String dbDriver, String connection,
114
                String user, String password)
115
                throws SQLException, ClassNotFoundException {
116
     // Load the Oracle JDBC driver
117
     Class.forName (dbDriver);
118

  
119
     // Connect to the database
120
     Connection conn = DriverManager.getConnection( connection, user, password);
121
     return conn;
122
  }
123

  
124 110
  /** 
125 111
   * routine to search the elements and attributes looking to match query
126 112
   *
src/edu/ucsb/nceas/metacat/DBReader.java
57 57
            dbstring = args[3];
58 58
          }
59 59

  
60
          DBReader rd = new DBReader(user, password, dbstring);
60
          // Open a connection to the database
61
          Connection dbconn = MetaCatUtil.openDBConnection(
62
                "oracle.jdbc.driver.OracleDriver",
63
                dbstring, user, password);
64

  
65
          DBReader rd = new DBReader( dbconn );
61 66
          String xml = rd.readXMLDocument(nodeid);
62 67
          System.out.println(xml);
63 68

  
......
74 79
   *
75 80
   * Generally, one calls readXMLDocument() after constructing the instance
76 81
   *
77
   * @param user the username to use for the database connection
78
   * @param password the password to use for the database connection
79
   * @param dbstring the connection info to use for the database connection
82
   * @param conn the database connection from which to read the document
80 83
   */
81
  public DBReader( String user, String password, String dbstring) 
84
  public DBReader( Connection conn ) 
82 85
                  throws IOException, 
83 86
                         SQLException, 
84 87
                         ClassNotFoundException
85 88
  {
86
     // Open a connection to the database
87
     conn = openDBConnection(
88
                "oracle.jdbc.driver.OracleDriver",
89
                dbstring, user, password);
90

  
89
    this.conn = conn;
91 90
  }
92 91
  
93
  /** open a connection to the database with the supplied information */
94
  private Connection openDBConnection(String dbDriver, String connection,
95
                String user, String password)
96
                throws SQLException, ClassNotFoundException {
97
     // Load the Oracle JDBC driver
98
     Class.forName (dbDriver);
99

  
100
     // Connect to the database
101
     Connection conn = DriverManager.getConnection( connection, user, password);
102
     return conn;
103
  }
104

  
105 92
  /**
106 93
   * Create an XML document from the database starting with the element 
107 94
   * having element_id nodeid
src/edu/ucsb/nceas/metacat/MetaCatUtil.java
1
import java.sql.Connection;
2
import java.sql.DriverManager;
3
import java.sql.SQLException;
4

  
5
/**
6
 * A suite of utility classes for the metadata catalog server
7
 */
8
public class MetaCatUtil {
9

  
10
  static  String 	user = "jones";
11
  static  String 	password = "your-pw-goes-here";
12
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
13

  
14
  /** 
15
   * Utility method to establish a JDBC database connection 
16
   *
17
   * @param dbDriver the string representing the database driver
18
   * @param connection the string representing the database connectin parameters
19
   * @param user name of the user to use for database connection
20
   * @param password password for the user to use for database connection
21
   */
22
  public static Connection openDBConnection(String dbDriver, String connection,
23
                String user, String password)
24
                throws SQLException, ClassNotFoundException {
25

  
26
     // Load the Oracle JDBC driver
27
     Class.forName (dbDriver);
28

  
29
     // Connect to the database
30
     Connection conn = DriverManager.getConnection( connection, user, password);
31
     return conn;
32
  }
33
}
0 34

  
src/edu/ucsb/nceas/metacat/DBSAXWriter.java
66 66
            dbstring = args[3];
67 67
          }
68 68

  
69
          new DBSAXWriter(filename, user, password, dbstring);
69
          // Open a connection to the database
70
          Connection dbconn = MetaCatUtil.openDBConnection(
71
                "oracle.jdbc.driver.OracleDriver",
72
                dbstring, user, password);
73

  
74
          new DBSAXWriter(filename, dbconn);
70 75
          System.out.println("Document processing finished for: " + filename);
71 76

  
72 77
        } catch (Exception e) {
......
81 86
   * construct a new instance of the class to write an XML file to the database
82 87
   *
83 88
   * @param filename the filename to be loaded into the database
84
   * @param user the username to use for the database connection
85
   * @param password the password to use for the database connection
86
   * @param dbstring the connection info to use for the database connection
89
   * @param conn the database connection to which to write the XML file
87 90
   */
88
  public DBSAXWriter( String filename, String user, 
89
                  String password, String dbstring) 
91
  public DBSAXWriter( String filename, Connection conn)
90 92
                  throws IOException, 
91 93
                         SQLException, 
92 94
                         ClassNotFoundException
93 95
   {
94
     // Open a connection to the database
95
     conn = openDBConnection(
96
                "oracle.jdbc.driver.OracleDriver",
97
                dbstring, user, password);
98

  
96
     this.conn = conn;
99 97
    
100 98
     //
101 99
     // Set up the SAX document handlers for parsing
......
143 141

  
144 142
   }
145 143
  
146
   /** Utility method to establish the JDBC connection */
147
   private Connection openDBConnection(String dbDriver, String connection,
148
                String user, String password)
149
                throws SQLException, ClassNotFoundException {
150
     // Load the Oracle JDBC driver
151
     Class.forName (dbDriver);
152

  
153
     // Connect to the database
154
     Connection conn = DriverManager.getConnection( connection, user, password);
155
     return conn;
156
   }
157

  
158 144
   /** Utility method to convert a file handle into a URL */
159 145
   static public URL fileToURL(File file) 
160 146
   {
DBReader.java
57 57
            dbstring = args[3];
58 58
          }
59 59

  
60
          DBReader rd = new DBReader(user, password, dbstring);
60
          // Open a connection to the database
61
          Connection dbconn = MetaCatUtil.openDBConnection(
62
                "oracle.jdbc.driver.OracleDriver",
63
                dbstring, user, password);
64

  
65
          DBReader rd = new DBReader( dbconn );
61 66
          String xml = rd.readXMLDocument(nodeid);
62 67
          System.out.println(xml);
63 68

  
......
74 79
   *
75 80
   * Generally, one calls readXMLDocument() after constructing the instance
76 81
   *
77
   * @param user the username to use for the database connection
78
   * @param password the password to use for the database connection
79
   * @param dbstring the connection info to use for the database connection
82
   * @param conn the database connection from which to read the document
80 83
   */
81
  public DBReader( String user, String password, String dbstring) 
84
  public DBReader( Connection conn ) 
82 85
                  throws IOException, 
83 86
                         SQLException, 
84 87
                         ClassNotFoundException
85 88
  {
86
     // Open a connection to the database
87
     conn = openDBConnection(
88
                "oracle.jdbc.driver.OracleDriver",
89
                dbstring, user, password);
90

  
89
    this.conn = conn;
91 90
  }
92 91
  
93
  /** open a connection to the database with the supplied information */
94
  private Connection openDBConnection(String dbDriver, String connection,
95
                String user, String password)
96
                throws SQLException, ClassNotFoundException {
97
     // Load the Oracle JDBC driver
98
     Class.forName (dbDriver);
99

  
100
     // Connect to the database
101
     Connection conn = DriverManager.getConnection( connection, user, password);
102
     return conn;
103
  }
104

  
105 92
  /**
106 93
   * Create an XML document from the database starting with the element 
107 94
   * having element_id nodeid
docs/ReaderElement.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: Class  ReaderElement
8 8
</TITLE>
......
33 33

  
34 34
<TR>
35 35
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
36
&nbsp;<A HREF="DBSimpleQuery.html"><B>PREV CLASS</B></A>&nbsp;
36
&nbsp;<A HREF="MetaCatServlet.html"><B>PREV CLASS</B></A>&nbsp;
37 37
&nbsp;NEXT CLASS</FONT></TD>
38 38
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
39 39
  <A HREF="index.html" TARGET="_top"><B>FRAMES</B></A>  &nbsp;
......
264 264

  
265 265
<TR>
266 266
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
267
&nbsp;<A HREF="DBSimpleQuery.html"><B>PREV CLASS</B></A>&nbsp;
267
&nbsp;<A HREF="MetaCatServlet.html"><B>PREV CLASS</B></A>&nbsp;
268 268
&nbsp;NEXT CLASS</FONT></TD>
269 269
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
270 270
  <A HREF="index.html" TARGET="_top"><B>FRAMES</B></A>  &nbsp;
docs/index-all.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: Index
8 8
</TITLE>
......
96 96
Constructor for class <A HREF="DBSAXWriter.html">DBSAXWriter</A>
97 97
<DD>construct a new instance of the class to write an XML file to the database
98 98
<DT><A HREF="DBSimpleQuery.html"><B>DBSimpleQuery</B></A> - class <A HREF="DBSimpleQuery.html">DBSimpleQuery</A>.<DD>A Class that searches a relational DB for elements and attributes that
99
 have free text matches to the query string.</DL>
99
 have free text matches to the query string.<DT><A HREF="DBSimpleQuery.html#DBSimpleQuery(java.lang.String, java.lang.String, java.lang.String)"><B>DBSimpleQuery(String, String, String)</B></A> - 
100
Constructor for class <A HREF="DBSimpleQuery.html">DBSimpleQuery</A>
101
<DD>construct an instance of the DBSimpleQuery class 
102

  
103
 Generally, one would call the findRootNodes() routine after creating 
104
 an instance to specify the query to search for
105
<DT><A HREF="MetaCatServlet.html#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)"><B>doGet(HttpServletRequest, HttpServletResponse)</B></A> - 
106
Method in class <A HREF="MetaCatServlet.html">MetaCatServlet</A>
107
<DD>Handle "GET" method requests from HTTP clients
108
<DT><A HREF="MetaCatServlet.html#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)"><B>doPost(HttpServletRequest, HttpServletResponse)</B></A> - 
109
Method in class <A HREF="MetaCatServlet.html">MetaCatServlet</A>
110
<DD>Handle "POST" method requests from HTTP clients
111
</DL>
100 112
<HR>
101 113
<A NAME="_E_"><!-- --></A><H2>
102 114
<B>E</B></H2>
......
147 159
Method in class <A HREF="DBSAXHandler.html">DBSAXHandler</A>
148 160
<DD>SAX Handler that is called for each XML text node that is Ignorable
149 161
 white space
162
<DT><A HREF="MetaCatServlet.html#init(javax.servlet.ServletConfig)"><B>init(ServletConfig)</B></A> - 
163
Method in class <A HREF="MetaCatServlet.html">MetaCatServlet</A>
164
<DD>Initialize the servlet by creating appropriate database connections
150 165
</DL>
151 166
<HR>
152 167
<A NAME="_M_"><!-- --></A><H2>
......
161 176
<DT><A HREF="DBSimpleQuery.html#main(java.lang.String[])"><B>main(String[])</B></A> - 
162 177
Static method in class <A HREF="DBSimpleQuery.html">DBSimpleQuery</A>
163 178
<DD>the main routine used to test the DBSimpleQuery utility.
179
<DT><A HREF="MetaCatServlet.html"><B>MetaCatServlet</B></A> - class <A HREF="MetaCatServlet.html">MetaCatServlet</A>.<DD>A metadata catalog server implemented as a Java Servlet
180

  
181
 <DT><A HREF="MetaCatServlet.html#MetaCatServlet()"><B>MetaCatServlet()</B></A> - 
182
Constructor for class <A HREF="MetaCatServlet.html">MetaCatServlet</A>
183
<DD>&nbsp;
164 184
</DL>
165 185
<HR>
166 186
<A NAME="_R_"><!-- --></A><H2>
docs/allclasses-frame.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
All Classes
8 8
</TITLE>
......
27 27
<BR>
28 28
<A HREF="DBSimpleQuery.html" TARGET="classFrame">DBSimpleQuery</A>
29 29
<BR>
30
<A HREF="MetaCatServlet.html" TARGET="classFrame">MetaCatServlet</A>
31
<BR>
30 32
<A HREF="ReaderElement.html" TARGET="classFrame">ReaderElement</A>
31 33
<BR>
32 34
</FONT></TD>
docs/packages.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7

  
8 8
</TITLE>
docs/DBSimpleQuery.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: Class  DBSimpleQuery
8 8
</TITLE>
......
34 34
<TR>
35 35
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
36 36
&nbsp;<A HREF="DBSAXWriter.html"><B>PREV CLASS</B></A>&nbsp;
37
&nbsp;<A HREF="ReaderElement.html"><B>NEXT CLASS</B></A></FONT></TD>
37
&nbsp;<A HREF="MetaCatServlet.html"><B>NEXT CLASS</B></A></FONT></TD>
38 38
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
39 39
  <A HREF="index.html" TARGET="_top"><B>FRAMES</B></A>  &nbsp;
40 40
&nbsp;<A HREF="DBSimpleQuery.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD>
41 41
</TR>
42 42
<TR>
43 43
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
44
  SUMMARY: &nbsp;INNER&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
44
  SUMMARY: &nbsp;INNER&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
45 45
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
46
DETAIL: &nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
46
DETAIL: &nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
47 47
</TR>
48 48
</TABLE>
49 49
<!-- =========== END OF NAVBAR =========== -->
......
77 77

  
78 78
<!-- ======== CONSTRUCTOR SUMMARY ======== -->
79 79

  
80
<A NAME="constructor_summary"><!-- --></A>
81
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
82
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
83
<TD COLSPAN=2><FONT SIZE="+2">
84
<B>Constructor Summary</B></FONT></TD>
85
</TR>
86
<TR BGCOLOR="white" CLASS="TableRowColor">
87
<TD><CODE><B><A HREF="DBSimpleQuery.html#DBSimpleQuery(java.lang.String, java.lang.String, java.lang.String)">DBSimpleQuery</A></B>(java.lang.String&nbsp;user,
88
              java.lang.String&nbsp;password,
89
              java.lang.String&nbsp;dbstring)</CODE>
80 90

  
91
<BR>
92
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;construct an instance of the DBSimpleQuery class 
93

  
94
 Generally, one would call the findRootNodes() routine after creating 
95
 an instance to specify the query to search for</TD>
96
</TR>
97
</TABLE>
98
&nbsp;
81 99
<!-- ========== METHOD SUMMARY =========== -->
82 100

  
83 101
<A NAME="method_summary"><!-- --></A>
......
130 148

  
131 149
<!-- ========= CONSTRUCTOR DETAIL ======== -->
132 150

  
151
<A NAME="constructor_detail"><!-- --></A>
152
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
153
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
154
<TD COLSPAN=1><FONT SIZE="+2">
155
<B>Constructor Detail</B></FONT></TD>
156
</TR>
157
</TABLE>
133 158

  
159
<A NAME="DBSimpleQuery(java.lang.String, java.lang.String, java.lang.String)"><!-- --></A><H3>
160
DBSimpleQuery</H3>
161
<PRE>
162
public <B>DBSimpleQuery</B>(java.lang.String&nbsp;user,
163
                     java.lang.String&nbsp;password,
164
                     java.lang.String&nbsp;dbstring)
165
              throws java.io.IOException,
166
                     java.sql.SQLException,
167
                     java.lang.ClassNotFoundException</PRE>
168
<DL>
169
<DD>construct an instance of the DBSimpleQuery class 
170

  
171
 Generally, one would call the findRootNodes() routine after creating 
172
 an instance to specify the query to search for<DD><DL>
173
<DT><B>Parameters:</B><DD><CODE>user</CODE> - the username to use for the database connection<DD><CODE>password</CODE> - the password to use for the database connection<DD><CODE>dbstring</CODE> - the connection info to use for the database connection</DL>
174
</DD>
175
</DL>
176

  
134 177
<!-- ============ METHOD DETAIL ========== -->
135 178

  
136 179
<A NAME="method_detail"><!-- --></A>
......
190 233
<TR>
191 234
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
192 235
&nbsp;<A HREF="DBSAXWriter.html"><B>PREV CLASS</B></A>&nbsp;
193
&nbsp;<A HREF="ReaderElement.html"><B>NEXT CLASS</B></A></FONT></TD>
236
&nbsp;<A HREF="MetaCatServlet.html"><B>NEXT CLASS</B></A></FONT></TD>
194 237
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
195 238
  <A HREF="index.html" TARGET="_top"><B>FRAMES</B></A>  &nbsp;
196 239
&nbsp;<A HREF="DBSimpleQuery.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD>
197 240
</TR>
198 241
<TR>
199 242
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
200
  SUMMARY: &nbsp;INNER&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
243
  SUMMARY: &nbsp;INNER&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
201 244
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
202
DETAIL: &nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
245
DETAIL: &nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
203 246
</TR>
204 247
</TABLE>
205 248
<!-- =========== END OF NAVBAR =========== -->
docs/BasicElement.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: Class  BasicElement
8 8
</TITLE>
docs/help-doc.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: API Help
8 8
</TITLE>
docs/serialized-form.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
Serialized Form
8 8
</TITLE>
......
47 47
<H1>
48 48
Serialized Form</H1>
49 49
</CENTER>
50
<A NAME="MetaCatServlet"><!-- --></A>
51
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
52
<TR BGCOLOR="#CCCCFF" CLASS="TableSubHeadingColor">
53
<TD COLSPAN=2><FONT SIZE="+2">
54
<B>Class <A HREF="MetaCatServlet.html">MetaCatServlet</A> implements Serializable</B></FONT></TD>
55
</TR>
56
</TABLE>
57

  
58
<P>
59
<A NAME="serializedForm"><!-- --></A>
60
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
61
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
62
<TD COLSPAN=1><FONT SIZE="+2">
63
<B>Serialized Fields</B></FONT></TD>
64
</TR>
65
</TABLE>
66

  
67
<H3>
68
config</H3>
69
<PRE>
70
javax.servlet.ServletConfig <B>config</B></PRE>
71
<DL>
72
</DL>
50 73
<HR>
51 74

  
75
<H3>
76
context</H3>
77
<PRE>
78
javax.servlet.ServletContext <B>context</B></PRE>
79
<DL>
80
</DL>
81
<HR>
82

  
83
<H3>
84
docreader</H3>
85
<PRE>
86
<A HREF="DBReader.html">DBReader</A> <B>docreader</B></PRE>
87
<DL>
88
</DL>
89
<HR>
90

  
91
<H3>
92
queryobj</H3>
93
<PRE>
94
<A HREF="DBSimpleQuery.html">DBSimpleQuery</A> <B>queryobj</B></PRE>
95
<DL>
96
</DL>
97

  
98
<P>
99
<HR>
100

  
52 101
<!-- ========== START OF NAVBAR ========== -->
53 102
<A NAME="navbar_bottom"><!-- --></A>
54 103
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0">
docs/DBSAXWriter.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: Class  DBSAXWriter
8 8
</TITLE>
docs/DBSAXHandler.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: Class  DBSAXHandler
8 8
</TITLE>
docs/overview-tree.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:14 AKDT 2000 -->
6 6
<TITLE>
7 7
: Class Hierarchy
8 8
</TITLE>
......
54 54
<LI TYPE="circle">class java.lang.Object<UL>
55 55
<LI TYPE="circle">class <A HREF="BasicElement.html"><B>BasicElement</B></A><UL>
56 56
<LI TYPE="circle">class <A HREF="DBSAXElement.html"><B>DBSAXElement</B></A><LI TYPE="circle">class <A HREF="ReaderElement.html"><B>ReaderElement</B></A></UL>
57
<LI TYPE="circle">class <A HREF="DBReader.html"><B>DBReader</B></A><LI TYPE="circle">class <A HREF="DBSAXWriter.html"><B>DBSAXWriter</B></A><LI TYPE="circle">class <A HREF="DBSimpleQuery.html"><B>DBSimpleQuery</B></A><LI TYPE="circle">class org.xml.sax.HandlerBase (implements org.xml.sax.DocumentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler)
57
<LI TYPE="circle">class <A HREF="DBReader.html"><B>DBReader</B></A><LI TYPE="circle">class <A HREF="DBSAXWriter.html"><B>DBSAXWriter</B></A><LI TYPE="circle">class <A HREF="DBSimpleQuery.html"><B>DBSimpleQuery</B></A><LI TYPE="circle">class javax.servlet.GenericServlet (implements java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig)
58 58
<UL>
59
<LI TYPE="circle">class javax.servlet.http.HttpServlet (implements java.io.Serializable)
60
<UL>
61
<LI TYPE="circle">class <A HREF="MetaCatServlet.html"><B>MetaCatServlet</B></A></UL>
62
</UL>
63
<LI TYPE="circle">class org.xml.sax.HandlerBase (implements org.xml.sax.DocumentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler)
64
<UL>
59 65
<LI TYPE="circle">class oracle.xml.parser.v2.DefaultXMLDocumentHandler (implements oracle.xml.parser.v2.XMLDocumentHandler)
60 66
<UL>
61 67
<LI TYPE="circle">class <A HREF="DBSAXHandler.html"><B>DBSAXHandler</B></A></UL>
docs/index.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000-->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000-->
6 6
<TITLE>
7 7
Generated Documentation (Untitled)
8 8
</TITLE>
docs/DBReader.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: Class  DBReader
8 8
</TITLE>
docs/deprecated-list.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: Deprecated List
8 8
</TITLE>
docs/DBSAXElement.html
2 2
<!--NewPage-->
3 3
<HTML>
4 4
<HEAD>
5
<!-- Generated by javadoc on Tue Apr 11 19:05:41 AKDT 2000 -->
5
<!-- Generated by javadoc on Mon Apr 17 13:49:15 AKDT 2000 -->
6 6
<TITLE>
7 7
: Class  DBSAXElement
8 8
</TITLE>
rowcol.css
1
 .page    {font-family: Tahoma, sans-serif; background-color:white}
2
 .emlbody    {font-family: Tahoma, sans-serif; background-color:white}
3
 .rowodd  {background-color: #CCCCCC; color: black; vertical-align: top}
4
 .roweven {background-color: #F8F8F8; color: black; vertical-align: top}
5
 .rowlight {background-color: #CCCCFF; color: black; vertical-align: top}
6
 .rowwhite {background-color: #FFFFFF; color: black; vertical-align: top}
7
 .coleven {}
8
 .colodd  {}
9
 .shaded  {background-color: #DDDDDD; color: black}
0 10

  
MetaCatUtil.java
1
import java.sql.Connection;
2
import java.sql.DriverManager;
3
import java.sql.SQLException;
4

  
5
/**
6
 * A suite of utility classes for the metadata catalog server
7
 */
8
public class MetaCatUtil {
9

  
10
  static  String 	user = "jones";
11
  static  String 	password = "your-pw-goes-here";
12
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
13

  
14
  /** 
15
   * Utility method to establish a JDBC database connection 
16
   *
17
   * @param dbDriver the string representing the database driver
18
   * @param connection the string representing the database connectin parameters
19
   * @param user name of the user to use for database connection
20
   * @param password password for the user to use for database connection
21
   */
22
  public static Connection openDBConnection(String dbDriver, String connection,
23
                String user, String password)
24
                throws SQLException, ClassNotFoundException {
25

  
26
     // Load the Oracle JDBC driver
27
     Class.forName (dbDriver);
28

  
29
     // Connect to the database
30
     Connection conn = DriverManager.getConnection( connection, user, password);
31
     return conn;
32
  }
33
}
0 34

  
contents.html
22 22
Simple database query against MetaCat<br>
23 23
<form action="/servlets/MetaCatServlet" target="right" method="POST">
24 24
  <input type="hidden" name="action" value="query">
25
  <input type="text" name="query" size="15">
25
  <input type="text" name="query" value="Value1" size="15">
26 26
  <input type="submit" value="Run query">
27 27
  <br />
28
  <input type="checkbox" name="convert" value="true">Results in HTML
28
  Format: 
29
  <input type="radio" name="qformat" value="xml">XML
30
  <input type="radio" name="qformat" value="html" checked>HTML
29 31
</form>
30 32
<p>
31
<a href="/servlets/MetaCatServlet?action=getdocument&docid=1" 
32
   target="right">Test Document Display (1)</a><br>
33
<a href="/servlets/MetaCatServlet?action=getdocument&docid=31" 
34
   target="right">Test Document Display (31)</a>
33
For the test database, some valid values for the search that produce document
34
results include "Value1" and "lakename"
35
</p>
36
<p>
37
To view results that are XML formatted in this demo, it is easiest to use 
38
Internet Explorer 5. Although Netscape browsers will allow you to 
39
download XML documents, only IE5 will display XML at this time.
35 40
</body>
36 41
</html>
DBSimpleQuery.java
61 61
            dbstring = args[3];
62 62
          }
63 63

  
64
          // Open a connection to the database
65
          Connection dbconn = MetaCatUtil.openDBConnection( 
66
                              "oracle.jdbc.driver.OracleDriver",
67
                              dbstring, user, password);
64 68
          // Execute the simple query
65
          DBSimpleQuery rd = new DBSimpleQuery(user, password, dbstring);
69
          DBSimpleQuery rd = new DBSimpleQuery(dbconn);
66 70
          Hashtable nodelist = rd.findRootNodes(query);
67 71

  
68 72
          // Print the reulting root nodes
......
90 94
  /**
91 95
   * construct an instance of the DBSimpleQuery class 
92 96
   *
93
   * Generally, one would call the findRootNodes() routine after creating 
94
   * an instance to specify the query to search for
97
   * <p>Generally, one would call the findRootNodes() routine after creating 
98
   * an instance to specify the search query</p>
95 99
   *
96
   * @param user the username to use for the database connection
97
   * @param password the password to use for the database connection
98
   * @param dbstring the connection info to use for the database connection
100
   * @param conn the JDBC connection that we use for the query
99 101
   */
100
  public DBSimpleQuery( String user, String password, String dbstring) 
102
  public DBSimpleQuery( Connection conn ) 
101 103
                  throws IOException, 
102 104
                         SQLException, 
103 105
                         ClassNotFoundException
104 106
  {
105
     // Open a connection to the database
106
     conn = openDBConnection(
107
                "oracle.jdbc.driver.OracleDriver",
108
                dbstring, user, password);
109

  
107
    this.conn = conn;
110 108
  }
111 109
  
112
  /** Utility message to establish a JDBC database connection */
113
  private Connection openDBConnection(String dbDriver, String connection,
114
                String user, String password)
115
                throws SQLException, ClassNotFoundException {
116
     // Load the Oracle JDBC driver
117
     Class.forName (dbDriver);
118

  
119
     // Connect to the database
120
     Connection conn = DriverManager.getConnection( connection, user, password);
121
     return conn;
122
  }
123

  
124 110
  /** 
125 111
   * routine to search the elements and attributes looking to match query
126 112
   *
lib/style/rowcol.css
1
 .page    {font-family: Tahoma, sans-serif; background-color:white}
2
 .emlbody    {font-family: Tahoma, sans-serif; background-color:white}
3
 .rowodd  {background-color: #CCCCCC; color: black; vertical-align: top}
4
 .roweven {background-color: #F8F8F8; color: black; vertical-align: top}
5
 .rowlight {background-color: #CCCCFF; color: black; vertical-align: top}
6
 .rowwhite {background-color: #FFFFFF; color: black; vertical-align: top}
7
 .coleven {}
8
 .colodd  {}
9
 .shaded  {background-color: #DDDDDD; color: black}
0 10

  
lib/rowcol.css
1
 .page    {font-family: Tahoma, sans-serif; background-color:white}
2
 .emlbody    {font-family: Tahoma, sans-serif; background-color:white}
3
 .rowodd  {background-color: #CCCCCC; color: black; vertical-align: top}
4
 .roweven {background-color: #F8F8F8; color: black; vertical-align: top}
5
 .rowlight {background-color: #CCCCFF; color: black; vertical-align: top}
6
 .rowwhite {background-color: #FFFFFF; color: black; vertical-align: top}
7
 .coleven {}
8
 .colodd  {}
9
 .shaded  {background-color: #DDDDDD; color: black}
0 10

  
Makefile
11 11

  
12 12
all: orasax reader query servlet
13 13

  
14
orasax:
14
orasax: util
15 15
	javac -classpath "$(CPATH)" \
16 16
		DBSAXWriter.java \
17 17
		BasicElement.java \
......
23 23
		DBWriter.java \
24 24
		DBElement.java
25 25
		
26
reader:
26
reader: util
27 27
	javac -classpath "$(CPATH)" \
28 28
		DBReader.java \
29 29
		BasicElement.java \
......
33 33
		BasicElement.class \
34 34
		/home/httpd/servlets/
35 35

  
36
query:
36
query: util
37 37
	javac -classpath "$(CPATH)" \
38 38
		DBSimpleQuery.java
39 39
	cp DBSimpleQuery.class /home/httpd/servlets/
......
42 42
	javac -classpath "$(CPATH)" MetaCatServlet.java
43 43
	cp MetaCatServlet.class /home/httpd/servlets/
44 44

  
45
util: 
46
	javac -classpath "$(CPATH)" \
47
		MetaCatUtil.java
48
	cp MetaCatUtil.class /home/httpd/servlets/
49

  
45 50
test:
46 51
	java -cp $(CPATH) DBSAXWriter test.xml $(USER) $(PW)
47 52

  
MetaCatServlet.java
1 1
import java.io.PrintWriter;
2 2
import java.io.IOException;
3
import java.io.Reader;
4
import java.io.StringReader;
3 5
import java.util.Enumeration;
4 6
import java.util.Hashtable;
7
import java.net.URL;
8
import java.net.MalformedURLException;
9
import java.sql.Connection;
5 10

  
6 11
import javax.servlet.ServletConfig;
7 12
import javax.servlet.ServletContext;
......
12 17
import javax.servlet.http.HttpServletResponse;
13 18
import javax.servlet.http.HttpUtils;
14 19

  
20
import oracle.xml.parser.v2.XSLStylesheet;
21
import oracle.xml.parser.v2.XSLException;
22
import oracle.xml.parser.v2.XMLDocumentFragment;
23
import oracle.xml.parser.v2.XSLProcessor;
24

  
15 25
/**
16 26
 * A metadata catalog server implemented as a Java Servlet
27
   *
28
   * <p>Valid parameters are:<br>
29
   * action=query -- query the values of all elements and attributes
30
   *                     and return a result set of nodes<br>
31
   * action=getdocument -- display an XML document in XML or HTML<br>
32
   * qformat=xml -- display resultset from query in XML<br>
33
   * qformat=html -- display resultset from query in HTML<br>
34
   * action=getdocument -- display an XML document in XML or HTML<br>
35
   * docid=34 -- display the document with the document ID number 34<br>
17 36
 */
18 37
public class MetaCatServlet extends HttpServlet {
19 38

  
......
21 40
  private ServletContext	context = null;
22 41
  DBSimpleQuery		queryobj = null;
23 42
  DBReader		docreader = null;
24
  static  String 	user = "jones";
25
  static  String 	password = "your-pw-goes-here";
26
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
43
  static  String 	user = MetaCatUtil.user;
44
  static  String 	password = MetaCatUtil.password;
45
  static  String 	defaultDB = MetaCatUtil.defaultDB;
46
  static  String 	resultStyleURL = "file:///home/httpd/html/xmltodb/resultset.xsl";
27 47

  
48
  /**
49
   * Initialize the servlet by creating appropriate database connections
50
   */
28 51
  public void init( ServletConfig config ) throws ServletException {
29 52
    try {
30 53
      super.init( config );
......
32 55
      this.context = config.getServletContext();
33 56
    
34 57
      try {
35
        queryobj = new DBSimpleQuery(user, password, defaultDB);
36
        docreader = new DBReader(user, password, defaultDB);
58
        // Open a connection to the database
59
        Connection dbconn = MetaCatUtil.openDBConnection(
60
                "oracle.jdbc.driver.OracleDriver",
61
                defaultDB, user, password);
62

  
63
        queryobj = new DBSimpleQuery(dbconn);
64
        docreader = new DBReader(dbconn);
37 65
      } catch (Exception e) {
38 66
      }
39 67
    } catch ( ServletException ex ) {
......
41 69
    }
42 70
  }
43 71

  
72
  /** Handle "GET" method requests from HTTP clients */
44 73
  public void doGet (HttpServletRequest request, HttpServletResponse response)
45 74
    throws ServletException, IOException {
46 75

  
......
52 81
    handleGetOrPost(response, params);
53 82
  }
54 83

  
84
  /** Handle "POST" method requests from HTTP clients */
55 85
  public void doPost( HttpServletRequest request, HttpServletResponse response)
56 86
    throws ServletException, IOException {
57 87

  
......
67 97
  }
68 98

  
69 99
  /**
70
   * routine to control servlet response depending on the action requested
71
   * Valid "action" parameters are:
72
   *     query -- query the text values of all elements and attributes
73
   *              and return a result set of nodes
74
   *  getdocument -- display an XML document in XML or HTML given a document ID
100
   * Control servlet response depending on the action parameter specified
75 101
   */
76 102
  private void handleGetOrPost(HttpServletResponse response, Hashtable params) 
77 103
    throws ServletException, IOException {
......
85 111
      handleQueryAction(out, params, response);
86 112
    } else if (action.equals("getdocument")) {
87 113
      handleGetDocumentAction(out, params, response);
114
    } else {
115
      out.println("Error: action not registered.  Please report this error.");
88 116
    }
89 117

  
90 118
    // Close the stream to the client
91 119
    out.close();
92 120
  }
93 121

  
122
  /** 
123
   * Handle the database query request and return a result set, possibly
124
   * transformed from XML into HTML
125
   */
94 126
  private void handleQueryAction(PrintWriter out, Hashtable params, 
95 127
               HttpServletResponse response) {
96 128
      // Run the query
97 129
      String query = ((String[])params.get("query"))[0]; 
98 130
      Hashtable nodelist = queryobj.findRootNodes(query);
99
  
100
      // set content type and other response header fields first
101
      response.setContentType("text/xml");
102
  
131
 
132
      // Create a buffer to hold the xml result
133
      StringBuffer resultset = new StringBuffer();
134
 
103 135
      // Print the resulting root nodes
104 136
      long nodeid;
105
      out.println("<?xml version=\"1.0\"?>\n");
106
      out.println("<resultset>\n");
107
      out.println("  <query>" + query + "</query>");
137
      resultset.append("<?xml version=\"1.0\"?>\n");
138
      resultset.append("<resultset>\n");
139
      resultset.append("  <query>" + query + "</query>");
108 140
      Enumeration rootlist = nodelist.keys(); 
109 141
      while (rootlist.hasMoreElements()) {
110 142
        nodeid = ((Long)rootlist.nextElement()).longValue();
111
        out.println("  <nodeid>" + nodeid + "</nodeid>");
143
        resultset.append("  <nodeid>" + nodeid + "</nodeid>");
112 144
      }
113
      out.println("</resultset>");
145
      resultset.append("</resultset>");
146

  
147
      String qformat = ((String[])params.get("qformat"))[0]; 
148
      if (qformat.equals("xml")) {
149
        // set content type and other response header fields first
150
        response.setContentType("text/xml");
151
        out.println(resultset.toString());
152
      } else if (qformat.equals("html")) {
153
        // set content type and other response header fields first
154
        response.setContentType("text/html");
155
        //out.println("Converting to HTML...");
156
        XMLDocumentFragment htmldoc = null;
157
        try {
158
          XSLStylesheet style = new XSLStylesheet(new URL(resultStyleURL), null);
159
          htmldoc = (new XSLProcessor()).processXSL(style, 
160
                     (Reader)(new StringReader(resultset.toString())),null);
161
          htmldoc.print(out);
162
        } catch (Exception e) {
163
          out.println("Error transforming document:\n" + e.getMessage());
164
        }
165
      }
114 166
  }
115 167

  
168
  /** 
169
   * Handle the database getdocument request and return a XML document, 
170
   * possibly transformed from XML into HTML
171
   */
116 172
  private void handleGetDocumentAction(PrintWriter out, Hashtable params, 
117 173
               HttpServletResponse response) {
118 174
      // Get the document indicated
......
123 179
      response.setContentType("text/xml");
124 180
  
125 181
      out.println(doc);
126

  
127
      // Print the resulting root nodes
128
      //out.println("<?xml version=\"1.0\"?>\n");
129
      //out.println("<docid>" + docid + "</docid>");
130 182
  }
131 183
}
lib/contents.html
22 22
Simple database query against MetaCat<br>
23 23
<form action="/servlets/MetaCatServlet" target="right" method="POST">
24 24
  <input type="hidden" name="action" value="query">
25
  <input type="text" name="query" size="15">
25
  <input type="text" name="query" value="Value1" size="15">
26 26
  <input type="submit" value="Run query">
27 27
  <br />
28
  <input type="checkbox" name="convert" value="true">Results in HTML
28
  Format: 
29
  <input type="radio" name="qformat" value="xml">XML
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff