Project

General

Profile

« Previous | Next » 

Revision 87

Added by Matt Jones about 24 years ago

created XSL transformation module based on the internal database catalog

View differences:

src/edu/ucsb/nceas/metacat/DBTransform.java
1
/**
2
 *        Name: DBTransform.java
3
 *     Purpose: A Class that transforms an XML text document
4
 *              into a another type using XSL
5
 *   Copyright: 2000 Regents of the University of California and the
6
 *              National Center for Ecological Analysis and Synthesis
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id$'
10
 */
11

  
12
package edu.ucsb.nceas.metacat;
13

  
14
import java.io.*;
15
import java.net.URL;
16
import java.net.MalformedURLException;
17
import java.sql.*;
18
import java.util.Stack;
19

  
20
import oracle.xml.parser.v2.XSLStylesheet;
21
import oracle.xml.parser.v2.XSLException;
22
import oracle.xml.parser.v2.XSLProcessor;
23
import oracle.xml.parser.v2.XMLDocument;
24
import oracle.xml.parser.v2.DOMParser;
25

  
26
/** 
27
 * A Class that transforms XML documents utitlizing XSL style sheets
28
 */
29
public class DBTransform {
30

  
31
  private Connection	conn = null;
32

  
33
  /**
34
   * construct a DBTransform instance.
35
   *
36
   * Generally, one calls transformXMLDocument() after constructing the instance
37
   *
38
   * @param conn the database connection from which to lookup the public ids
39
   */
40
  public DBTransform( Connection conn ) 
41
                  throws IOException, 
42
                         SQLException, 
43
                         ClassNotFoundException
44
  {
45
    this.conn = conn;
46
  }
47
  
48
  /**
49
   * Transform an XML document using the stylesheet reference from the db
50
   *
51
   * @param doc the document to be transformed
52
   * @param sourcetype the document type of the source
53
   * @param targettype the target document type
54
   */
55
  public void transformXMLDocument(String doc, String sourcetype, 
56
                String targettype, PrintWriter pw) {
57

  
58
    // Look up the stylesheet for this type combination
59
    String xsl_system_id = getSystemId("XSL", sourcetype, targettype);
60

  
61
    if (xsl_system_id != null) {
62
      // Create a stylesheet from the system id that was found
63
      try {
64
        XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
65
        DOMParser dp = new DOMParser();
66
        dp.setValidationMode(false);
67
        dp.parse((Reader)(new StringReader(doc)));
68
        new XSLProcessor().processXSL(style, dp.getDocument(), pw);
69
      } catch (Exception e) {
70
        pw.println(xsl_system_id + "Error transforming document:\n" + 
71
                   e.getMessage());
72
      }
73
    } else {
74
      // No stylesheet registered form this document type, so just return the 
75
      // XML stream we were passed
76
      pw.print(doc);
77
    }
78
  }
79

  
80
  /**
81
   * Lookup a stylesheet reference from the db catalog
82
   *
83
   * @param objecttype the type of the object we want to retrieve
84
   * @param sourcetype the document type of the source
85
   * @param targettype the document type of the target
86
   */
87
  public String getSystemId(String objecttype, String sourcetype, 
88
                String targettype) {
89

  
90
    // Look up the System ID of a particular object
91
    PreparedStatement pstmt;
92
    String the_system_id = null;
93

  
94
    try {
95
      pstmt =
96
        conn.prepareStatement("SELECT system_id " +
97
                "FROM xml_catalog_entities " +
98
                "WHERE source_doctype LIKE ? " +
99
                "AND target_doctype LIKE ? " +
100
                "AND entity_type LIKE ?");
101
      // Bind the values to the query
102
      pstmt.setString(1, sourcetype);
103
      pstmt.setString(2, targettype);
104
      pstmt.setString(3, objecttype);
105

  
106
      pstmt.execute();
107
      try {
108
        ResultSet rs = pstmt.getResultSet();
109
        try {
110
          boolean tableHasRows = rs.next();
111
          if (tableHasRows) {
112
            try {
113
              the_system_id = rs.getString(1);
114
            } catch (SQLException e) {
115
              System.out.println("Error with getString: " + e.getMessage());                }
116
          } else {
117
            the_system_id = null; 
118
          }
119
        } catch (SQLException e) {
120
          //System.out.println("Error with next: " + e.getMessage());
121
          return ("Error with next: " + e.getMessage());
122
        }
123
      } catch (SQLException e) {
124
        //System.out.println("Error with getrset: " + e.getMessage());
125
        return ("Error with getrset: " + e.getMessage());
126
      }
127
      pstmt.close();
128
    } catch (SQLException e) {
129
      //System.out.println("Error getting id: " + e.getMessage());
130
      return ("Error getting id: " + e.getMessage());
131
    }
132
    return the_system_id;
133
  }
134
}
0 135

  
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
65 65
  Connection 		conn = null;
66 66
  DBSimpleQuery		queryobj = null;
67 67
  DBReader		docreader = null;
68
  DBTransform		dbt = null;
68 69
  String 	user = null;
69 70
  String 	password = null;
70 71
  String 	defaultDB = null;
......
98 99

  
99 100
        queryobj = new DBSimpleQuery(conn);
100 101
        docreader = new DBReader(conn);
102
        dbt = new DBTransform(conn);
101 103

  
102 104
      } catch (Exception e) {
103 105
      }
......
147 149
    if (action.equals("query")) {
148 150
      handleQueryAction(out, params, response);
149 151
    } else if (action.equals("getdocument")) {
150
      handleGetDocumentAction(out, params, response);
152
      try {
153
        handleGetDocumentAction(out, params, response);
154
      } catch (ClassNotFoundException e) {
155
        System.out.println(e.getMessage());
156
      } catch (SQLException se) {
157
        System.out.println(se.getMessage());
158
      }
151 159
    } else if (action.equals("putdocument")) {
152 160
      handlePutDocumentAction(out, params, response);
153 161
    } else if (action.equals("validate")) {
......
187 195
      // Print the resulting root nodes
188 196
      long nodeid;
189 197
      resultset.append("<?xml version=\"1.0\"?>\n");
198
      //resultset.append("<!DOCTYPE resultset PUBLIC " +
199
      //               "\"-//NCEAS//resultset//EN\" \"resultset.dtd\">\n");
190 200
      resultset.append("<resultset>\n");
191 201
      resultset.append("  <query>" + query + "</query>");
192 202
      Enumeration rootlist = nodelist.keys(); 
......
222 232
   * possibly transformed from XML into HTML
223 233
   */
224 234
  private void handleGetDocumentAction(PrintWriter out, Hashtable params, 
225
               HttpServletResponse response) {
226
      // Get the document indicated
227
      String docid = ((String[])params.get("docid"))[0]; 
228
      String doc = docreader.readXMLDocument((new Long(docid)).longValue());
235
               HttpServletResponse response) 
236
               throws ClassNotFoundException, IOException, SQLException {
237
      // Find the document id number
238
      String docidstr = ((String[])params.get("docid"))[0]; 
239
      long docid = (new Long(docidstr)).longValue();
229 240

  
241
      // Get the document indicated fromthe db
242
      String doc = docreader.readXMLDocument(docid);
230 243

  
244

  
245
      // Return the document in XML or HTML format
231 246
      String qformat = ((String[])params.get("qformat"))[0]; 
232 247
      if (qformat.equals("xml")) {
233 248
        // set content type and other response header fields first
......
236 251
      } else if (qformat.equals("html")) {
237 252
        // set content type and other response header fields first
238 253
        response.setContentType("text/html");
239
        //out.println("Converting to HTML...");
240
        XMLDocumentFragment htmldoc = null;
241 254

  
242
        // Look up the System ID of the XSL sheet
243
        PreparedStatement pstmt;
244
        String xsl_system_id = null;
245
  
246
        try {
247
          pstmt =
248
            conn.prepareStatement("SELECT system_id " +
249
                    "FROM xml_catalog_entities " +
250
                    "WHERE source_doctype LIKE " +
251
                    "  (SELECT doctype from xml_documents " +
252
                    "    WHERE docid = ? ) " +
253
                    "AND target_doctype LIKE ?");
254
          // Bind the values to the query
255
          //pstmt.setString(1, "-//NCEAS//eml-dataset//EN");
256
          pstmt.setLong(1, new Long(docid).longValue());
257
          pstmt.setString(2, "-//W3C//HTML//EN");
258
  
259
          pstmt.execute();
260
          try {
261
            ResultSet rs = pstmt.getResultSet();
262
            try {
263
              boolean tableHasRows = rs.next();
264
              if (tableHasRows) {
265
                try {
266
                  xsl_system_id = rs.getString(1);
267
                } catch (SQLException e) {
268
                  System.out.println("Error with getString: " + e.getMessage());
269
                }
270
              }
271
            } catch (SQLException e) {
272
              System.out.println("Error with next: " + e.getMessage());
273
            }
274
          } catch (SQLException e) {
275
            System.out.println("Error with getrset: " + e.getMessage());
276
          }
277
          pstmt.close();
278
        } catch (SQLException e) {
279
          System.out.println("Error getting id: " + e.getMessage());
280
        }
281
 
282
        //out.println(xsl_system_id);
255
        // Look up the document type
256
        String sourcetype = getDoctype(docid);
283 257

  
284
        // Try to apply the style
285
        try {
286
          XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
287
          htmldoc = (new XSLProcessor()).processXSL(style, 
288
                     (Reader)(new StringReader(doc)),null);
289
          htmldoc.print(out);
290
        } catch (Exception e) {
291
          //out.println("Error transforming document:\n" + e.getMessage());
292
          // set content type and other response header fields first
293
          response.setContentType("text/xml");
294
          out.println(doc);
295
        }
258
        // Transform the document to the new doctype
259
        dbt.transformXMLDocument(doc, sourcetype, "-//W3C//HTML//EN", out);
296 260
      }
297 261
  }
298 262

  
......
349 313
        out.println("The input XML is NOT VALID\n" + gxv.returnErrors());
350 314
      } 
351 315
    }
316

  
317
  /** 
318
   * Look up the document type from the database
319
   *
320
   */
321
  private String getDoctype(long docid) {
322
    // Look up the System ID of the XSL sheet
323
    PreparedStatement pstmt;
324
    String doctype = null;
325
 
326
    try {
327
      pstmt =
328
        conn.prepareStatement("SELECT doctype " + 
329
                                "FROM xml_documents " +
330
                               "WHERE docid = ?");
331
      // Bind the values to the query
332
      pstmt.setLong(1, new Long(docid).longValue());
333

  
334
      pstmt.execute();
335
      try {
336
        ResultSet rs = pstmt.getResultSet();
337
        try {
338
          boolean tableHasRows = rs.next();
339
          if (tableHasRows) {
340
            try {
341
              doctype  = rs.getString(1);
342
            } catch (SQLException e) {
343
              System.out.println("Error with getString: " + e.getMessage());
344
            }
345
          }
346
        } catch (SQLException e) {
347
          System.out.println("Error with next: " + e.getMessage());
348
        }
349
      } catch (SQLException e) {
350
        System.out.println("Error with getrset: " + e.getMessage());
351
      }
352
      pstmt.close();
353
    } catch (SQLException e) {
354
      System.out.println("Error getting id: " + e.getMessage());
355
    }
356

  
357
    return doctype;
358
  }
352 359
}
DBTransform.java
1
/**
2
 *        Name: DBTransform.java
3
 *     Purpose: A Class that transforms an XML text document
4
 *              into a another type using XSL
5
 *   Copyright: 2000 Regents of the University of California and the
6
 *              National Center for Ecological Analysis and Synthesis
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id$'
10
 */
11

  
12
package edu.ucsb.nceas.metacat;
13

  
14
import java.io.*;
15
import java.net.URL;
16
import java.net.MalformedURLException;
17
import java.sql.*;
18
import java.util.Stack;
19

  
20
import oracle.xml.parser.v2.XSLStylesheet;
21
import oracle.xml.parser.v2.XSLException;
22
import oracle.xml.parser.v2.XSLProcessor;
23
import oracle.xml.parser.v2.XMLDocument;
24
import oracle.xml.parser.v2.DOMParser;
25

  
26
/** 
27
 * A Class that transforms XML documents utitlizing XSL style sheets
28
 */
29
public class DBTransform {
30

  
31
  private Connection	conn = null;
32

  
33
  /**
34
   * construct a DBTransform instance.
35
   *
36
   * Generally, one calls transformXMLDocument() after constructing the instance
37
   *
38
   * @param conn the database connection from which to lookup the public ids
39
   */
40
  public DBTransform( Connection conn ) 
41
                  throws IOException, 
42
                         SQLException, 
43
                         ClassNotFoundException
44
  {
45
    this.conn = conn;
46
  }
47
  
48
  /**
49
   * Transform an XML document using the stylesheet reference from the db
50
   *
51
   * @param doc the document to be transformed
52
   * @param sourcetype the document type of the source
53
   * @param targettype the target document type
54
   */
55
  public void transformXMLDocument(String doc, String sourcetype, 
56
                String targettype, PrintWriter pw) {
57

  
58
    // Look up the stylesheet for this type combination
59
    String xsl_system_id = getSystemId("XSL", sourcetype, targettype);
60

  
61
    if (xsl_system_id != null) {
62
      // Create a stylesheet from the system id that was found
63
      try {
64
        XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
65
        DOMParser dp = new DOMParser();
66
        dp.setValidationMode(false);
67
        dp.parse((Reader)(new StringReader(doc)));
68
        new XSLProcessor().processXSL(style, dp.getDocument(), pw);
69
      } catch (Exception e) {
70
        pw.println(xsl_system_id + "Error transforming document:\n" + 
71
                   e.getMessage());
72
      }
73
    } else {
74
      // No stylesheet registered form this document type, so just return the 
75
      // XML stream we were passed
76
      pw.print(doc);
77
    }
78
  }
79

  
80
  /**
81
   * Lookup a stylesheet reference from the db catalog
82
   *
83
   * @param objecttype the type of the object we want to retrieve
84
   * @param sourcetype the document type of the source
85
   * @param targettype the document type of the target
86
   */
87
  public String getSystemId(String objecttype, String sourcetype, 
88
                String targettype) {
89

  
90
    // Look up the System ID of a particular object
91
    PreparedStatement pstmt;
92
    String the_system_id = null;
93

  
94
    try {
95
      pstmt =
96
        conn.prepareStatement("SELECT system_id " +
97
                "FROM xml_catalog_entities " +
98
                "WHERE source_doctype LIKE ? " +
99
                "AND target_doctype LIKE ? " +
100
                "AND entity_type LIKE ?");
101
      // Bind the values to the query
102
      pstmt.setString(1, sourcetype);
103
      pstmt.setString(2, targettype);
104
      pstmt.setString(3, objecttype);
105

  
106
      pstmt.execute();
107
      try {
108
        ResultSet rs = pstmt.getResultSet();
109
        try {
110
          boolean tableHasRows = rs.next();
111
          if (tableHasRows) {
112
            try {
113
              the_system_id = rs.getString(1);
114
            } catch (SQLException e) {
115
              System.out.println("Error with getString: " + e.getMessage());                }
116
          } else {
117
            the_system_id = null; 
118
          }
119
        } catch (SQLException e) {
120
          //System.out.println("Error with next: " + e.getMessage());
121
          return ("Error with next: " + e.getMessage());
122
        }
123
      } catch (SQLException e) {
124
        //System.out.println("Error with getrset: " + e.getMessage());
125
        return ("Error with getrset: " + e.getMessage());
126
      }
127
      pstmt.close();
128
    } catch (SQLException e) {
129
      //System.out.println("Error getting id: " + e.getMessage());
130
      return ("Error getting id: " + e.getMessage());
131
    }
132
    return the_system_id;
133
  }
134
}
0 135

  
MetaCatServlet.java
65 65
  Connection 		conn = null;
66 66
  DBSimpleQuery		queryobj = null;
67 67
  DBReader		docreader = null;
68
  DBTransform		dbt = null;
68 69
  String 	user = null;
69 70
  String 	password = null;
70 71
  String 	defaultDB = null;
......
98 99

  
99 100
        queryobj = new DBSimpleQuery(conn);
100 101
        docreader = new DBReader(conn);
102
        dbt = new DBTransform(conn);
101 103

  
102 104
      } catch (Exception e) {
103 105
      }
......
147 149
    if (action.equals("query")) {
148 150
      handleQueryAction(out, params, response);
149 151
    } else if (action.equals("getdocument")) {
150
      handleGetDocumentAction(out, params, response);
152
      try {
153
        handleGetDocumentAction(out, params, response);
154
      } catch (ClassNotFoundException e) {
155
        System.out.println(e.getMessage());
156
      } catch (SQLException se) {
157
        System.out.println(se.getMessage());
158
      }
151 159
    } else if (action.equals("putdocument")) {
152 160
      handlePutDocumentAction(out, params, response);
153 161
    } else if (action.equals("validate")) {
......
187 195
      // Print the resulting root nodes
188 196
      long nodeid;
189 197
      resultset.append("<?xml version=\"1.0\"?>\n");
198
      //resultset.append("<!DOCTYPE resultset PUBLIC " +
199
      //               "\"-//NCEAS//resultset//EN\" \"resultset.dtd\">\n");
190 200
      resultset.append("<resultset>\n");
191 201
      resultset.append("  <query>" + query + "</query>");
192 202
      Enumeration rootlist = nodelist.keys(); 
......
222 232
   * possibly transformed from XML into HTML
223 233
   */
224 234
  private void handleGetDocumentAction(PrintWriter out, Hashtable params, 
225
               HttpServletResponse response) {
226
      // Get the document indicated
227
      String docid = ((String[])params.get("docid"))[0]; 
228
      String doc = docreader.readXMLDocument((new Long(docid)).longValue());
235
               HttpServletResponse response) 
236
               throws ClassNotFoundException, IOException, SQLException {
237
      // Find the document id number
238
      String docidstr = ((String[])params.get("docid"))[0]; 
239
      long docid = (new Long(docidstr)).longValue();
229 240

  
241
      // Get the document indicated fromthe db
242
      String doc = docreader.readXMLDocument(docid);
230 243

  
244

  
245
      // Return the document in XML or HTML format
231 246
      String qformat = ((String[])params.get("qformat"))[0]; 
232 247
      if (qformat.equals("xml")) {
233 248
        // set content type and other response header fields first
......
236 251
      } else if (qformat.equals("html")) {
237 252
        // set content type and other response header fields first
238 253
        response.setContentType("text/html");
239
        //out.println("Converting to HTML...");
240
        XMLDocumentFragment htmldoc = null;
241 254

  
242
        // Look up the System ID of the XSL sheet
243
        PreparedStatement pstmt;
244
        String xsl_system_id = null;
245
  
246
        try {
247
          pstmt =
248
            conn.prepareStatement("SELECT system_id " +
249
                    "FROM xml_catalog_entities " +
250
                    "WHERE source_doctype LIKE " +
251
                    "  (SELECT doctype from xml_documents " +
252
                    "    WHERE docid = ? ) " +
253
                    "AND target_doctype LIKE ?");
254
          // Bind the values to the query
255
          //pstmt.setString(1, "-//NCEAS//eml-dataset//EN");
256
          pstmt.setLong(1, new Long(docid).longValue());
257
          pstmt.setString(2, "-//W3C//HTML//EN");
258
  
259
          pstmt.execute();
260
          try {
261
            ResultSet rs = pstmt.getResultSet();
262
            try {
263
              boolean tableHasRows = rs.next();
264
              if (tableHasRows) {
265
                try {
266
                  xsl_system_id = rs.getString(1);
267
                } catch (SQLException e) {
268
                  System.out.println("Error with getString: " + e.getMessage());
269
                }
270
              }
271
            } catch (SQLException e) {
272
              System.out.println("Error with next: " + e.getMessage());
273
            }
274
          } catch (SQLException e) {
275
            System.out.println("Error with getrset: " + e.getMessage());
276
          }
277
          pstmt.close();
278
        } catch (SQLException e) {
279
          System.out.println("Error getting id: " + e.getMessage());
280
        }
281
 
282
        //out.println(xsl_system_id);
255
        // Look up the document type
256
        String sourcetype = getDoctype(docid);
283 257

  
284
        // Try to apply the style
285
        try {
286
          XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
287
          htmldoc = (new XSLProcessor()).processXSL(style, 
288
                     (Reader)(new StringReader(doc)),null);
289
          htmldoc.print(out);
290
        } catch (Exception e) {
291
          //out.println("Error transforming document:\n" + e.getMessage());
292
          // set content type and other response header fields first
293
          response.setContentType("text/xml");
294
          out.println(doc);
295
        }
258
        // Transform the document to the new doctype
259
        dbt.transformXMLDocument(doc, sourcetype, "-//W3C//HTML//EN", out);
296 260
      }
297 261
  }
298 262

  
......
349 313
        out.println("The input XML is NOT VALID\n" + gxv.returnErrors());
350 314
      } 
351 315
    }
316

  
317
  /** 
318
   * Look up the document type from the database
319
   *
320
   */
321
  private String getDoctype(long docid) {
322
    // Look up the System ID of the XSL sheet
323
    PreparedStatement pstmt;
324
    String doctype = null;
325
 
326
    try {
327
      pstmt =
328
        conn.prepareStatement("SELECT doctype " + 
329
                                "FROM xml_documents " +
330
                               "WHERE docid = ?");
331
      // Bind the values to the query
332
      pstmt.setLong(1, new Long(docid).longValue());
333

  
334
      pstmt.execute();
335
      try {
336
        ResultSet rs = pstmt.getResultSet();
337
        try {
338
          boolean tableHasRows = rs.next();
339
          if (tableHasRows) {
340
            try {
341
              doctype  = rs.getString(1);
342
            } catch (SQLException e) {
343
              System.out.println("Error with getString: " + e.getMessage());
344
            }
345
          }
346
        } catch (SQLException e) {
347
          System.out.println("Error with next: " + e.getMessage());
348
        }
349
      } catch (SQLException e) {
350
        System.out.println("Error with getrset: " + e.getMessage());
351
      }
352
      pstmt.close();
353
    } catch (SQLException e) {
354
      System.out.println("Error getting id: " + e.getMessage());
355
    }
356

  
357
    return doctype;
358
  }
352 359
}
Makefile
10 10

  
11 11
default: all
12 12

  
13
all: orasax reader query servlet xvalid
13
all: orasax reader query servlet xvalid transform
14 14

  
15
transform:
16
	$(JAVAC) -classpath "$(CPATH)" \
17
		DBTransform.java
18

  
15 19
xvalid:
16 20
	$(JAVAC) -classpath "$(CPATH)" \
17 21
		XMLValidate.java \
......
42 46
	$(JAVAC) -classpath "$(CPATH)" \
43 47
		DBSimpleQuery.java
44 48

  
45
servlet: query reader orasax xvalid
49
servlet: query reader orasax xvalid transform
46 50
	$(JAVAC) -classpath "$(CPATH)" MetaCatServlet.java
47 51
	cp -r classes/edu /home/httpd/servlets/
48 52
	cp metacat.properties /home/httpd/servlets/edu/ucsb/nceas/metacat/

Also available in: Unified diff