Project

General

Profile

1 87 jones
/**
2 203 jones
 *  '$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 349 jones
 *    Release: @release@
9 87 jones
 *
10 203 jones
 *   '$Author$'
11
 *     '$Date$'
12
 * '$Revision$'
13 87 jones
 */
14
15
package edu.ucsb.nceas.metacat;
16
17
import java.io.*;
18
import java.net.URL;
19
import java.net.MalformedURLException;
20
import java.sql.*;
21
import java.util.Stack;
22
23
import oracle.xml.parser.v2.XSLStylesheet;
24
import oracle.xml.parser.v2.XSLException;
25
import oracle.xml.parser.v2.XSLProcessor;
26
import oracle.xml.parser.v2.XMLDocument;
27
import oracle.xml.parser.v2.DOMParser;
28
29
/**
30
 * A Class that transforms XML documents utitlizing XSL style sheets
31
 */
32
public class DBTransform {
33
34
  private Connection	conn = null;
35
36
  /**
37
   * construct a DBTransform instance.
38
   *
39
   * Generally, one calls transformXMLDocument() after constructing the instance
40
   *
41
   * @param conn the database connection from which to lookup the public ids
42
   */
43
  public DBTransform( Connection conn )
44
                  throws IOException,
45
                         SQLException,
46
                         ClassNotFoundException
47
  {
48
    this.conn = conn;
49
  }
50
51
  /**
52
   * Transform an XML document using the stylesheet reference from the db
53
   *
54
   * @param doc the document to be transformed
55
   * @param sourcetype the document type of the source
56
   * @param targettype the target document type
57
   */
58
  public void transformXMLDocument(String doc, String sourcetype,
59
                String targettype, PrintWriter pw) {
60 99 jones
61 87 jones
    // Look up the stylesheet for this type combination
62
    String xsl_system_id = getSystemId("XSL", sourcetype, targettype);
63
64
    if (xsl_system_id != null) {
65
      // Create a stylesheet from the system id that was found
66
      try {
67
        XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
68
        DOMParser dp = new DOMParser();
69
        dp.setValidationMode(false);
70
        dp.parse((Reader)(new StringReader(doc)));
71
        new XSLProcessor().processXSL(style, dp.getDocument(), pw);
72
      } catch (Exception e) {
73
        pw.println(xsl_system_id + "Error transforming document:\n" +
74
                   e.getMessage());
75
      }
76
    } else {
77
      // No stylesheet registered form this document type, so just return the
78
      // XML stream we were passed
79 100 jones
      pw.print(doc);
80 87 jones
    }
81
  }
82
83
  /**
84
   * Lookup a stylesheet reference from the db catalog
85
   *
86
   * @param objecttype the type of the object we want to retrieve
87
   * @param sourcetype the document type of the source
88
   * @param targettype the document type of the target
89
   */
90
  public String getSystemId(String objecttype, String sourcetype,
91
                String targettype) {
92
93
    // Look up the System ID of a particular object
94
    PreparedStatement pstmt;
95
    String the_system_id = null;
96
    try {
97
      pstmt =
98
        conn.prepareStatement("SELECT system_id " +
99 94 jones
                "FROM xml_catalog " +
100 122 jones
                "WHERE entry_type LIKE ? " +
101 94 jones
                "AND source_doctype LIKE ? " +
102
                "AND target_doctype LIKE ? ");
103 87 jones
      // Bind the values to the query
104 94 jones
      pstmt.setString(1, objecttype);
105
      pstmt.setString(2, sourcetype);
106
      pstmt.setString(3, targettype);
107 87 jones
      pstmt.execute();
108
      try {
109
        ResultSet rs = pstmt.getResultSet();
110
        try {
111
          boolean tableHasRows = rs.next();
112
          if (tableHasRows) {
113
            try {
114
              the_system_id = rs.getString(1);
115
            } catch (SQLException e) {
116
              System.out.println("Error with getString: " + e.getMessage());                }
117
          } else {
118
            the_system_id = null;
119
          }
120
        } catch (SQLException e) {
121 99 jones
          System.err.println("Error with next: " + e.getMessage());
122 87 jones
          return ("Error with next: " + e.getMessage());
123
        }
124
      } catch (SQLException e) {
125 99 jones
        System.err.println("Error with getrset: " + e.getMessage());
126 87 jones
        return ("Error with getrset: " + e.getMessage());
127
      }
128
      pstmt.close();
129
    } catch (SQLException e) {
130 99 jones
      System.err.println("Error getting id: " + e.getMessage());
131 87 jones
      return ("Error getting id: " + e.getMessage());
132
    }
133
    return the_system_id;
134
  }
135 99 jones
136
  /**
137
   * the main routine used to test the transform utility.
138
   *
139 184 jones
   * Usage: java DBTransform
140 99 jones
   */
141
  static public void main(String[] args) {
142
143 184 jones
     if (args.length > 0)
144 99 jones
     {
145
        System.err.println("Wrong number of arguments!!!");
146 184 jones
        System.err.println("USAGE: java DBTransform");
147 99 jones
        return;
148
     } else {
149
        try {
150
151
          // Open a connection to the database
152 184 jones
          MetaCatUtil   util = new MetaCatUtil();
153
          Connection dbconn = util.openDBConnection();
154 99 jones
155
          // Create a test document
156
          StringBuffer testdoc = new StringBuffer();
157
          testdoc.append("<?xml version=\"1.0\"?>");
158
          testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
159
          testdoc.append("<dataset_id>DS001</dataset_id>");
160
          testdoc.append("<title>My test doc</title></eml-dataset>");
161
162
          // Transform the document to the new doctype
163
          DBTransform dbt = new DBTransform(dbconn);
164
          dbt.transformXMLDocument(testdoc.toString(),
165
                                   "-//NCEAS//eml-dataset//EN",
166
                                   "-//W3C//HTML//EN",
167
                                   new PrintWriter(System.out));
168
169
        } catch (Exception e) {
170
          System.err.println("EXCEPTION HANDLING REQUIRED");
171
          System.err.println(e.getMessage());
172
          e.printStackTrace(System.err);
173
        }
174
     }
175
  }
176
177 184 jones
  private void dbg(int position) {
178 99 jones
    System.err.println("Debug flag: " + position);
179
  }
180
181 87 jones
}
182 203 jones
183
/**
184
 * '$Log$
185 349 jones
 * 'Revision 1.8  2000/06/26 10:35:05  jones
186
 * 'Merged in substantial changes to DBWriter and associated classes and to
187
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
188
 * 'functions.  The command line tools and the parameters for the
189
 * 'servlet have changed substantially.
190
 * '
191 203 jones
 * 'Revision 1.7.2.2  2000/06/25 23:38:16  jones
192
 * 'Added RCSfile keyword
193
 * '
194
 * 'Revision 1.7.2.1  2000/06/25 23:34:18  jones
195
 * 'Changed documentation formatting, added log entries at bottom of source files
196
 * ''
197
 */