Project

General

Profile

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: DBTransform.java 99 2000-05-15 05:11:02Z jones $'
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
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
32
  private Connection	conn = null;
33

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

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

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

    
94
    // Look up the System ID of a particular object
95
    PreparedStatement pstmt;
96
    String the_system_id = null;
97
    try {
98
      pstmt =
99
        conn.prepareStatement("SELECT system_id " +
100
                "FROM xml_catalog " +
101
                "WHERE entity_type LIKE ? " +
102
                "AND source_doctype LIKE ? " +
103
                "AND target_doctype LIKE ? ");
104
      // Bind the values to the query
105
      pstmt.setString(1, objecttype);
106
      pstmt.setString(2, sourcetype);
107
      pstmt.setString(3, targettype);
108
      pstmt.execute();
109
      try {
110
        ResultSet rs = pstmt.getResultSet();
111
        try {
112
          boolean tableHasRows = rs.next();
113
          if (tableHasRows) {
114
            try {
115
              the_system_id = rs.getString(1);
116
            } catch (SQLException e) {
117
              System.out.println("Error with getString: " + e.getMessage());                }
118
          } else {
119
            the_system_id = null; 
120
          }
121
        } catch (SQLException e) {
122
          System.err.println("Error with next: " + e.getMessage());
123
          return ("Error with next: " + e.getMessage());
124
        }
125
      } catch (SQLException e) {
126
        System.err.println("Error with getrset: " + e.getMessage());
127
        return ("Error with getrset: " + e.getMessage());
128
      }
129
      pstmt.close();
130
    } catch (SQLException e) {
131
      System.err.println("Error getting id: " + e.getMessage());
132
      return ("Error getting id: " + e.getMessage());
133
    }
134
    return the_system_id;
135
  }
136

    
137
  /**
138
   * the main routine used to test the transform utility.
139
   *
140
   * Usage: java transform <user> <password> [dbstring]
141
   *
142
   * @param user the username to use for the database connection
143
   * @param password the password to use for the database connection
144
   * @param dbstring the connection info to use for the database connection
145
   */
146
  static public void main(String[] args) {
147
     
148
     if (args.length < 2)
149
     {
150
        System.err.println("Wrong number of arguments!!!");
151
        System.err.println("USAGE: java transform " +
152
                           "<user> <password> [dbstring]");
153
        return;
154
     } else {
155
        try {
156
                    
157
          String user     = args[0];
158
          String password = args[1];
159
          String dbstring = null;
160

    
161
          if (args.length <= 2) {
162
            dbstring = defaultDB;
163
          } else {
164
            dbstring = args[2];
165
          }
166

    
167
          // Open a connection to the database
168
          Connection dbconn = MetaCatUtil.openDBConnection( 
169
                              "oracle.jdbc.driver.OracleDriver",
170
                              dbstring, user, password);
171

    
172
          // Create a test document
173
          StringBuffer testdoc = new StringBuffer();
174
          testdoc.append("<?xml version=\"1.0\"?>");
175
          testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
176
          testdoc.append("<dataset_id>DS001</dataset_id>");
177
          testdoc.append("<title>My test doc</title></eml-dataset>");
178

    
179
          // Transform the document to the new doctype
180
          DBTransform dbt = new DBTransform(dbconn);
181
          dbt.transformXMLDocument(testdoc.toString(), 
182
                                   "-//NCEAS//eml-dataset//EN", 
183
                                   "-//W3C//HTML//EN", 
184
                                   new PrintWriter(System.out));
185

    
186
        } catch (Exception e) {
187
          System.err.println("EXCEPTION HANDLING REQUIRED");
188
          System.err.println(e.getMessage());
189
          e.printStackTrace(System.err);
190
        }
191
     }
192
  }
193
  
194
  public void dbg(int position) {
195
    System.err.println("Debug flag: " + position);
196
  }
197

    
198
}
(12-12/20)