Project

General

Profile

« Previous | Next » 

Revision 55

Added by Matt Jones about 24 years ago

added xml doc loading to servlet actions

View differences:

src/edu/ucsb/nceas/metacat/DBWriter.java
90 90
   * @param filename the filename to be loaded into the database
91 91
   * @param conn the database connection to which to write the XML file
92 92
   */
93
  public DBSAXWriter( Reader xml, Connection conn)
94
                  throws IOException, 
95
                         SQLException, 
96
                         ClassNotFoundException
97
  {
98
    this.conn = conn;
99

  
100
    //try {
101

  
102
      try {
103
        SAXParser parser = initializeParser(conn);
104
        parser.parse(xml);
105
      } catch (SAXParseException e) {
106
        System.err.println(e.getMessage());
107
      } catch (SAXException e) {
108
        System.err.println(e.getMessage());
109
      }
110
    //} catch (Exception e) {
111
       //System.err.println(e.toString());
112
    //}
113
  }
114

  
93 115
  public DBSAXWriter( String filename, Connection conn)
94 116
                  throws IOException, 
95 117
                         SQLException, 
96 118
                         ClassNotFoundException
97
   {
119
  {
98 120
     this.conn = conn;
99
    
100
     //
101
     // Set up the SAX document handlers for parsing
102
     //
103

  
121
     FileReader xmlfile = new FileReader(new File(filename).toString());
104 122
     try {
123
       SAXParser parser = initializeParser(conn);
124
       parser.parse(xmlfile);
125
     } catch (SAXParseException e) {
126
       System.err.println(e.getMessage());
127
     } catch (SAXException e) {
128
       System.err.println(e.getMessage());
129
     }
130
  }
131
  
132
  private SAXParser initializeParser(Connection conn) {
133
    SAXParser parser = null;
134
    //
135
    // Set up the SAX document handlers for parsing
136
    //
137
    try {
138
      // Use the XMLDocumentHandler interface for namespace support
139
      // instead of org.xml.sax.DocumentHandler
140
      XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
105 141

  
106
        // Use the XMLDocumentHandler interface for namespace support
107
        // instead of org.xml.sax.DocumentHandler
108
        XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
142
      // For all the other interface use the default provided by
143
      // Handler base
144
      HandlerBase defHandler = new HandlerBase();
109 145

  
110
        // For all the other interface use the default provided by
111
        // Handler base
112
        HandlerBase defHandler = new HandlerBase();
146
      // Get an instance of the parser
147
      parser = new SAXParser();
113 148

  
114
        // Get an instance of the parser
115
        SAXParser parser = new SAXParser();
149
      // Set Handlers in the parser
150
      // Set the DocumentHandler to XMLDocumentHandler
151
      parser.setDocumentHandler(xmlDocHandler);
116 152

  
117
        // Set Handlers in the parser
118
        // Set the DocumentHandler to XMLDocumentHandler
119
        parser.setDocumentHandler(xmlDocHandler);
153
      // Set the other Handler to the defHandler
154
      parser.setErrorHandler(defHandler);
155
      parser.setEntityResolver(defHandler);
156
      parser.setDTDHandler(defHandler);
120 157

  
121
        // Set the other Handler to the defHandler
122
        parser.setErrorHandler(defHandler);
123
        parser.setEntityResolver(defHandler);
124
        parser.setDTDHandler(defHandler);
158
    } catch (Exception e) {
159
       System.err.println(e.toString());
160
    }
125 161

  
126
        try
127
        {
128
           parser.parse(fileToURL(new File(filename)).toString());
129
        }
130
        catch (SAXParseException e)
131
        {
132
           System.err.println(filename + ": " + e.getMessage());
133
        }
134
        catch (SAXException e)
135
        {
136
           System.err.println(filename + ": " + e.getMessage());
137
        }
138
     }
139
     catch (Exception e)
140
     {
141
        System.err.println(e.toString());
142
     }
162
    return parser;
163
  }
143 164

  
144
   }
145
  
146
   /** Utility method to convert a file handle into a URL */
147
   static public URL fileToURL(File file) 
148
   {
165
  /** Utility method to convert a file handle into a URL */
166
  static public URL fileToURL(File file) 
167
  {
149 168
     String path = file.getAbsolutePath();
150 169
     String fSep = System.getProperty("file.separator");
151 170
     if (fSep != null && fSep.length() == 1)
......
161 180
       throw new Error("unexpected MalformedURLException");
162 181
     }
163 182
  }
164

  
165 183
}
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
19 19
import java.net.URL;
20 20
import java.net.MalformedURLException;
21 21
import java.sql.Connection;
22
import java.sql.SQLException;
22 23

  
23 24
import javax.servlet.ServletConfig;
24 25
import javax.servlet.ServletContext;
......
50 51

  
51 52
  private ServletConfig		config = null;
52 53
  private ServletContext	context = null;
54
  Connection 		conn = null;
53 55
  DBSimpleQuery		queryobj = null;
54 56
  DBReader		docreader = null;
55 57
  static  String 	user = MetaCatUtil.user;
......
68 70
    
69 71
      try {
70 72
        // Open a connection to the database
71
        Connection dbconn = MetaCatUtil.openDBConnection(
73
        conn = MetaCatUtil.openDBConnection(
72 74
                "oracle.jdbc.driver.OracleDriver",
73 75
                defaultDB, user, password);
74 76

  
75
        queryobj = new DBSimpleQuery(dbconn);
76
        docreader = new DBReader(dbconn);
77
        queryobj = new DBSimpleQuery(conn);
78
        docreader = new DBReader(conn);
77 79
      } catch (Exception e) {
78 80
      }
79 81
    } catch ( ServletException ex ) {
......
123 125
      handleQueryAction(out, params, response);
124 126
    } else if (action.equals("getdocument")) {
125 127
      handleGetDocumentAction(out, params, response);
128
    } else if (action.equals("putdocument")) {
129
      handlePutDocumentAction(out, params, response);
126 130
    } else {
127 131
      out.println("Error: action not registered.  Please report this error.");
128 132
    }
......
192 196
  
193 197
      out.println(doc);
194 198
  }
199

  
200
  /** 
201
   * Handle the database putdocument request and write an XML document 
202
   * to the database connection
203
   */
204
  private void handlePutDocumentAction(PrintWriter out, Hashtable params, 
205
               HttpServletResponse response) {
206
      // Get the document indicated
207
      String doctext = ((String[])params.get("doctext"))[0]; 
208
      StringReader xml = new StringReader(doctext);
209
      try {
210
        DBSAXWriter dbw = new DBSAXWriter(doctext, conn);
211
      } catch (SQLException e1) {
212
          out.println("Error loading document:<p>\n" + e1.getMessage());
213
      }catch (IOException e2) {
214
          out.println("Error loading document:<p>\n" + e2.getMessage());
215
      }catch (ClassNotFoundException e3) {
216
          out.println("Error loading document:<p>\n" + e3.getMessage());
217
      }
218
      //String doc = docreader.readXMLDocument((new Long(docid)).longValue());
219

  
220
      // set content type and other response header fields first
221
      response.setContentType("text/html");
222
  
223
      out.println("Finished loading document.");
224
  }
195 225
}
src/edu/ucsb/nceas/metacat/DBSAXWriter.java
90 90
   * @param filename the filename to be loaded into the database
91 91
   * @param conn the database connection to which to write the XML file
92 92
   */
93
  public DBSAXWriter( Reader xml, Connection conn)
94
                  throws IOException, 
95
                         SQLException, 
96
                         ClassNotFoundException
97
  {
98
    this.conn = conn;
99

  
100
    //try {
101

  
102
      try {
103
        SAXParser parser = initializeParser(conn);
104
        parser.parse(xml);
105
      } catch (SAXParseException e) {
106
        System.err.println(e.getMessage());
107
      } catch (SAXException e) {
108
        System.err.println(e.getMessage());
109
      }
110
    //} catch (Exception e) {
111
       //System.err.println(e.toString());
112
    //}
113
  }
114

  
93 115
  public DBSAXWriter( String filename, Connection conn)
94 116
                  throws IOException, 
95 117
                         SQLException, 
96 118
                         ClassNotFoundException
97
   {
119
  {
98 120
     this.conn = conn;
99
    
100
     //
101
     // Set up the SAX document handlers for parsing
102
     //
103

  
121
     FileReader xmlfile = new FileReader(new File(filename).toString());
104 122
     try {
123
       SAXParser parser = initializeParser(conn);
124
       parser.parse(xmlfile);
125
     } catch (SAXParseException e) {
126
       System.err.println(e.getMessage());
127
     } catch (SAXException e) {
128
       System.err.println(e.getMessage());
129
     }
130
  }
131
  
132
  private SAXParser initializeParser(Connection conn) {
133
    SAXParser parser = null;
134
    //
135
    // Set up the SAX document handlers for parsing
136
    //
137
    try {
138
      // Use the XMLDocumentHandler interface for namespace support
139
      // instead of org.xml.sax.DocumentHandler
140
      XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
105 141

  
106
        // Use the XMLDocumentHandler interface for namespace support
107
        // instead of org.xml.sax.DocumentHandler
108
        XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
142
      // For all the other interface use the default provided by
143
      // Handler base
144
      HandlerBase defHandler = new HandlerBase();
109 145

  
110
        // For all the other interface use the default provided by
111
        // Handler base
112
        HandlerBase defHandler = new HandlerBase();
146
      // Get an instance of the parser
147
      parser = new SAXParser();
113 148

  
114
        // Get an instance of the parser
115
        SAXParser parser = new SAXParser();
149
      // Set Handlers in the parser
150
      // Set the DocumentHandler to XMLDocumentHandler
151
      parser.setDocumentHandler(xmlDocHandler);
116 152

  
117
        // Set Handlers in the parser
118
        // Set the DocumentHandler to XMLDocumentHandler
119
        parser.setDocumentHandler(xmlDocHandler);
153
      // Set the other Handler to the defHandler
154
      parser.setErrorHandler(defHandler);
155
      parser.setEntityResolver(defHandler);
156
      parser.setDTDHandler(defHandler);
120 157

  
121
        // Set the other Handler to the defHandler
122
        parser.setErrorHandler(defHandler);
123
        parser.setEntityResolver(defHandler);
124
        parser.setDTDHandler(defHandler);
158
    } catch (Exception e) {
159
       System.err.println(e.toString());
160
    }
125 161

  
126
        try
127
        {
128
           parser.parse(fileToURL(new File(filename)).toString());
129
        }
130
        catch (SAXParseException e)
131
        {
132
           System.err.println(filename + ": " + e.getMessage());
133
        }
134
        catch (SAXException e)
135
        {
136
           System.err.println(filename + ": " + e.getMessage());
137
        }
138
     }
139
     catch (Exception e)
140
     {
141
        System.err.println(e.toString());
142
     }
162
    return parser;
163
  }
143 164

  
144
   }
145
  
146
   /** Utility method to convert a file handle into a URL */
147
   static public URL fileToURL(File file) 
148
   {
165
  /** Utility method to convert a file handle into a URL */
166
  static public URL fileToURL(File file) 
167
  {
149 168
     String path = file.getAbsolutePath();
150 169
     String fSep = System.getProperty("file.separator");
151 170
     if (fSep != null && fSep.length() == 1)
......
161 180
       throw new Error("unexpected MalformedURLException");
162 181
     }
163 182
  }
164

  
165 183
}
contents.html
33 33
For the test database, some valid values for the search that produce document
34 34
results include "Value1" and "lakename"
35 35
</p>
36
<p><a href="loadxml.html" target="right">Load an XML Document</a>
37
</p>
36 38
<p>
37 39
To view results that are XML formatted in this demo, it is easiest to use 
38 40
Internet Explorer 5. Although Netscape browsers will allow you to 
MetaCatServlet.java
19 19
import java.net.URL;
20 20
import java.net.MalformedURLException;
21 21
import java.sql.Connection;
22
import java.sql.SQLException;
22 23

  
23 24
import javax.servlet.ServletConfig;
24 25
import javax.servlet.ServletContext;
......
50 51

  
51 52
  private ServletConfig		config = null;
52 53
  private ServletContext	context = null;
54
  Connection 		conn = null;
53 55
  DBSimpleQuery		queryobj = null;
54 56
  DBReader		docreader = null;
55 57
  static  String 	user = MetaCatUtil.user;
......
68 70
    
69 71
      try {
70 72
        // Open a connection to the database
71
        Connection dbconn = MetaCatUtil.openDBConnection(
73
        conn = MetaCatUtil.openDBConnection(
72 74
                "oracle.jdbc.driver.OracleDriver",
73 75
                defaultDB, user, password);
74 76

  
75
        queryobj = new DBSimpleQuery(dbconn);
76
        docreader = new DBReader(dbconn);
77
        queryobj = new DBSimpleQuery(conn);
78
        docreader = new DBReader(conn);
77 79
      } catch (Exception e) {
78 80
      }
79 81
    } catch ( ServletException ex ) {
......
123 125
      handleQueryAction(out, params, response);
124 126
    } else if (action.equals("getdocument")) {
125 127
      handleGetDocumentAction(out, params, response);
128
    } else if (action.equals("putdocument")) {
129
      handlePutDocumentAction(out, params, response);
126 130
    } else {
127 131
      out.println("Error: action not registered.  Please report this error.");
128 132
    }
......
192 196
  
193 197
      out.println(doc);
194 198
  }
199

  
200
  /** 
201
   * Handle the database putdocument request and write an XML document 
202
   * to the database connection
203
   */
204
  private void handlePutDocumentAction(PrintWriter out, Hashtable params, 
205
               HttpServletResponse response) {
206
      // Get the document indicated
207
      String doctext = ((String[])params.get("doctext"))[0]; 
208
      StringReader xml = new StringReader(doctext);
209
      try {
210
        DBSAXWriter dbw = new DBSAXWriter(doctext, conn);
211
      } catch (SQLException e1) {
212
          out.println("Error loading document:<p>\n" + e1.getMessage());
213
      }catch (IOException e2) {
214
          out.println("Error loading document:<p>\n" + e2.getMessage());
215
      }catch (ClassNotFoundException e3) {
216
          out.println("Error loading document:<p>\n" + e3.getMessage());
217
      }
218
      //String doc = docreader.readXMLDocument((new Long(docid)).longValue());
219

  
220
      // set content type and other response header fields first
221
      response.setContentType("text/html");
222
  
223
      out.println("Finished loading document.");
224
  }
195 225
}
loadxml.html
1
<!--
2
  * loadxml.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
  *    File Info: '$Id$'
10
  * 
11
  * This is an HTML document for loading an xml document into Oracle
12
-->
13
<html>
14
<head>
15
<title>MetaCat</title>
16
</head>
17
<body bgcolor="white">
18
<b>MetaCat XML Loader</b>
19
<p>
20
Load an XML document into MetaCat<br>
21
by pasting the XML into this box.<br>
22
<form action="/servlets/MetaCatServlet" target="right" method="POST">
23
  <input type="hidden" name="action" value="putdocument">
24
  <textarea name="doctext" size="50"></textarea>
25
  <input type="submit" value="Load XML">
26
</form>
27
<p>
28
To view results that are XML formatted in this demo, it is easiest to use 
29
Internet Explorer 5. Although Netscape browsers will allow you to 
30
download XML documents, only IE5 will display XML at this time.
31
<p>
32
</body>
33
</html>
0 34

  
lib/contents.html
33 33
For the test database, some valid values for the search that produce document
34 34
results include "Value1" and "lakename"
35 35
</p>
36
<p><a href="loadxml.html" target="right">Load an XML Document</a>
37
</p>
36 38
<p>
37 39
To view results that are XML formatted in this demo, it is easiest to use 
38 40
Internet Explorer 5. Although Netscape browsers will allow you to 
lib/loadxml.html
1
<!--
2
  * loadxml.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
  *    File Info: '$Id$'
10
  * 
11
  * This is an HTML document for loading an xml document into Oracle
12
-->
13
<html>
14
<head>
15
<title>MetaCat</title>
16
</head>
17
<body bgcolor="white">
18
<b>MetaCat XML Loader</b>
19
<p>
20
Load an XML document into MetaCat<br>
21
by pasting the XML into this box.<br>
22
<form action="/servlets/MetaCatServlet" target="right" method="POST">
23
  <input type="hidden" name="action" value="putdocument">
24
  <textarea name="doctext" size="50"></textarea>
25
  <input type="submit" value="Load XML">
26
</form>
27
<p>
28
To view results that are XML formatted in this demo, it is easiest to use 
29
Internet Explorer 5. Although Netscape browsers will allow you to 
30
download XML documents, only IE5 will display XML at this time.
31
<p>
32
</body>
33
</html>
0 34

  
DBSAXWriter.java
90 90
   * @param filename the filename to be loaded into the database
91 91
   * @param conn the database connection to which to write the XML file
92 92
   */
93
  public DBSAXWriter( Reader xml, Connection conn)
94
                  throws IOException, 
95
                         SQLException, 
96
                         ClassNotFoundException
97
  {
98
    this.conn = conn;
99

  
100
    //try {
101

  
102
      try {
103
        SAXParser parser = initializeParser(conn);
104
        parser.parse(xml);
105
      } catch (SAXParseException e) {
106
        System.err.println(e.getMessage());
107
      } catch (SAXException e) {
108
        System.err.println(e.getMessage());
109
      }
110
    //} catch (Exception e) {
111
       //System.err.println(e.toString());
112
    //}
113
  }
114

  
93 115
  public DBSAXWriter( String filename, Connection conn)
94 116
                  throws IOException, 
95 117
                         SQLException, 
96 118
                         ClassNotFoundException
97
   {
119
  {
98 120
     this.conn = conn;
99
    
100
     //
101
     // Set up the SAX document handlers for parsing
102
     //
103

  
121
     FileReader xmlfile = new FileReader(new File(filename).toString());
104 122
     try {
123
       SAXParser parser = initializeParser(conn);
124
       parser.parse(xmlfile);
125
     } catch (SAXParseException e) {
126
       System.err.println(e.getMessage());
127
     } catch (SAXException e) {
128
       System.err.println(e.getMessage());
129
     }
130
  }
131
  
132
  private SAXParser initializeParser(Connection conn) {
133
    SAXParser parser = null;
134
    //
135
    // Set up the SAX document handlers for parsing
136
    //
137
    try {
138
      // Use the XMLDocumentHandler interface for namespace support
139
      // instead of org.xml.sax.DocumentHandler
140
      XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
105 141

  
106
        // Use the XMLDocumentHandler interface for namespace support
107
        // instead of org.xml.sax.DocumentHandler
108
        XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
142
      // For all the other interface use the default provided by
143
      // Handler base
144
      HandlerBase defHandler = new HandlerBase();
109 145

  
110
        // For all the other interface use the default provided by
111
        // Handler base
112
        HandlerBase defHandler = new HandlerBase();
146
      // Get an instance of the parser
147
      parser = new SAXParser();
113 148

  
114
        // Get an instance of the parser
115
        SAXParser parser = new SAXParser();
149
      // Set Handlers in the parser
150
      // Set the DocumentHandler to XMLDocumentHandler
151
      parser.setDocumentHandler(xmlDocHandler);
116 152

  
117
        // Set Handlers in the parser
118
        // Set the DocumentHandler to XMLDocumentHandler
119
        parser.setDocumentHandler(xmlDocHandler);
153
      // Set the other Handler to the defHandler
154
      parser.setErrorHandler(defHandler);
155
      parser.setEntityResolver(defHandler);
156
      parser.setDTDHandler(defHandler);
120 157

  
121
        // Set the other Handler to the defHandler
122
        parser.setErrorHandler(defHandler);
123
        parser.setEntityResolver(defHandler);
124
        parser.setDTDHandler(defHandler);
158
    } catch (Exception e) {
159
       System.err.println(e.toString());
160
    }
125 161

  
126
        try
127
        {
128
           parser.parse(fileToURL(new File(filename)).toString());
129
        }
130
        catch (SAXParseException e)
131
        {
132
           System.err.println(filename + ": " + e.getMessage());
133
        }
134
        catch (SAXException e)
135
        {
136
           System.err.println(filename + ": " + e.getMessage());
137
        }
138
     }
139
     catch (Exception e)
140
     {
141
        System.err.println(e.toString());
142
     }
162
    return parser;
163
  }
143 164

  
144
   }
145
  
146
   /** Utility method to convert a file handle into a URL */
147
   static public URL fileToURL(File file) 
148
   {
165
  /** Utility method to convert a file handle into a URL */
166
  static public URL fileToURL(File file) 
167
  {
149 168
     String path = file.getAbsolutePath();
150 169
     String fSep = System.getProperty("file.separator");
151 170
     if (fSep != null && fSep.length() == 1)
......
161 180
       throw new Error("unexpected MalformedURLException");
162 181
     }
163 182
  }
164

  
165 183
}

Also available in: Unified diff