Project

General

Profile

1
/**
2
 *  '$RCSfile$'
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
 *   '$Author: jones $'
10
 *     '$Date: 2000-06-26 03:35:05 -0700 (Mon, 26 Jun 2000) $'
11
 * '$Revision: 203 $'
12
 */
13

    
14
package edu.ucsb.nceas.metacat;
15

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

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

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

    
33
  private Connection	conn = null;
34

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

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

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

    
92
    // Look up the System ID of a particular object
93
    PreparedStatement pstmt;
94
    String the_system_id = null;
95
    try {
96
      pstmt =
97
        conn.prepareStatement("SELECT system_id " +
98
                "FROM xml_catalog " +
99
                "WHERE entry_type LIKE ? " +
100
                "AND source_doctype LIKE ? " +
101
                "AND target_doctype LIKE ? ");
102
      // Bind the values to the query
103
      pstmt.setString(1, objecttype);
104
      pstmt.setString(2, sourcetype);
105
      pstmt.setString(3, targettype);
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.err.println("Error with next: " + e.getMessage());
121
          return ("Error with next: " + e.getMessage());
122
        }
123
      } catch (SQLException e) {
124
        System.err.println("Error with getrset: " + e.getMessage());
125
        return ("Error with getrset: " + e.getMessage());
126
      }
127
      pstmt.close();
128
    } catch (SQLException e) {
129
      System.err.println("Error getting id: " + e.getMessage());
130
      return ("Error getting id: " + e.getMessage());
131
    }
132
    return the_system_id;
133
  }
134

    
135
  /**
136
   * the main routine used to test the transform utility.
137
   *
138
   * Usage: java DBTransform
139
   */
140
  static public void main(String[] args) {
141
     
142
     if (args.length > 0)
143
     {
144
        System.err.println("Wrong number of arguments!!!");
145
        System.err.println("USAGE: java DBTransform");
146
        return;
147
     } else {
148
        try {
149
                    
150
          // Open a connection to the database
151
          MetaCatUtil   util = new MetaCatUtil();
152
          Connection dbconn = util.openDBConnection();
153

    
154
          // Create a test document
155
          StringBuffer testdoc = new StringBuffer();
156
          testdoc.append("<?xml version=\"1.0\"?>");
157
          testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
158
          testdoc.append("<dataset_id>DS001</dataset_id>");
159
          testdoc.append("<title>My test doc</title></eml-dataset>");
160

    
161
          // Transform the document to the new doctype
162
          DBTransform dbt = new DBTransform(dbconn);
163
          dbt.transformXMLDocument(testdoc.toString(), 
164
                                   "-//NCEAS//eml-dataset//EN", 
165
                                   "-//W3C//HTML//EN", 
166
                                   new PrintWriter(System.out));
167

    
168
        } catch (Exception e) {
169
          System.err.println("EXCEPTION HANDLING REQUIRED");
170
          System.err.println(e.getMessage());
171
          e.printStackTrace(System.err);
172
        }
173
     }
174
  }
175
  
176
  private void dbg(int position) {
177
    System.err.println("Debug flag: " + position);
178
  }
179

    
180
}
181

    
182
/**
183
 * '$Log$
184
 * 'Revision 1.7.2.2  2000/06/25 23:38:16  jones
185
 * 'Added RCSfile keyword
186
 * '
187
 * 'Revision 1.7.2.1  2000/06/25 23:34:18  jones
188
 * 'Changed documentation formatting, added log entries at bottom of source files
189
 * ''
190
 */
(12-12/19)