Project

General

Profile

« Previous | Next » 

Revision 85

Added by Matt Jones about 24 years ago

creating automatic transformation for XML docs retreived from catalog

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
21 21
import java.util.PropertyResourceBundle;
22 22
import java.net.URL;
23 23
import java.net.MalformedURLException;
24
import java.sql.PreparedStatement;
25
import java.sql.ResultSet;
24 26
import java.sql.Connection;
25 27
import java.sql.SQLException;
26 28

  
......
225 227
      String docid = ((String[])params.get("docid"))[0]; 
226 228
      String doc = docreader.readXMLDocument((new Long(docid)).longValue());
227 229

  
228
      // set content type and other response header fields first
229
      response.setContentType("text/xml");
230

  
231
      String qformat = ((String[])params.get("qformat"))[0]; 
232
      if (qformat.equals("xml")) {
233
        // set content type and other response header fields first
234
        response.setContentType("text/xml");
235
        out.println(doc);
236
      } else if (qformat.equals("html")) {
237
        // set content type and other response header fields first
238
        response.setContentType("text/html");
239
        //out.println("Converting to HTML...");
240
        XMLDocumentFragment htmldoc = null;
241

  
242
        // Look up the System ID of the XSL sheet
243
        PreparedStatement pstmt;
244
        String xsl_system_id = null;
230 245
  
231
      out.println(doc);
246
        try {
247
          pstmt =
248
            conn.prepareStatement("SELECT system_id " +
249
                    "FROM xml_catalog_entities " +
250
                    "WHERE source_doctype LIKE ? " +
251
                    "AND target_doctype LIKE ?");
252
          // Bind the values to the query
253
          pstmt.setString(1, "-//NCEAS//eml-dataset//EN");
254
          pstmt.setString(2, "-//W3C//HTML//EN");
255
  
256
          pstmt.execute();
257
          try {
258
            ResultSet rs = pstmt.getResultSet();
259
            try {
260
              boolean tableHasRows = rs.next();
261
              if (tableHasRows) {
262
                try {
263
                  xsl_system_id = rs.getString(1);
264
                } catch (SQLException e) {
265
                  System.out.println("Error with getString: " + e.getMessage());
266
                }
267
              }
268
            } catch (SQLException e) {
269
              System.out.println("Error with next: " + e.getMessage());
270
            }
271
          } catch (SQLException e) {
272
            System.out.println("Error with getrset: " + e.getMessage());
273
          }
274
          pstmt.close();
275
        } catch (SQLException e) {
276
          System.out.println("Error getting id: " + e.getMessage());
277
        }
278
 
279
        // Try to apply the style
280
        try {
281
          XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
282
          htmldoc = (new XSLProcessor()).processXSL(style, 
283
                     (Reader)(new StringReader(doc)),null);
284
          htmldoc.print(out);
285
        } catch (Exception e) {
286
          out.println("Error transforming document:\n" + e.getMessage());
287
        }
288
      }
232 289
  }
233 290

  
234 291
  /** 
xsqltest/eml-dataset-display.xsl
22 22
  <xsl:template match="/">
23 23
    <html>
24 24
      <head>
25
        <link rel="stylesheet" type="text/css" href="./rowcol.css" />
25
        <link rel="stylesheet" type="text/css" href="/xmltodb/xsqltest/rowcol.css" />
26 26
      </head>
27 27
      <body class="emlbody">
28 28
        <center>
......
164 164
  <xsl:template match="keyword_info"/>
165 165
  <xsl:template match="relations"/>
166 166

  
167
</xsl:stylesheet>
167
</xsl:stylesheet>
MetaCatServlet.java
21 21
import java.util.PropertyResourceBundle;
22 22
import java.net.URL;
23 23
import java.net.MalformedURLException;
24
import java.sql.PreparedStatement;
25
import java.sql.ResultSet;
24 26
import java.sql.Connection;
25 27
import java.sql.SQLException;
26 28

  
......
225 227
      String docid = ((String[])params.get("docid"))[0]; 
226 228
      String doc = docreader.readXMLDocument((new Long(docid)).longValue());
227 229

  
228
      // set content type and other response header fields first
229
      response.setContentType("text/xml");
230

  
231
      String qformat = ((String[])params.get("qformat"))[0]; 
232
      if (qformat.equals("xml")) {
233
        // set content type and other response header fields first
234
        response.setContentType("text/xml");
235
        out.println(doc);
236
      } else if (qformat.equals("html")) {
237
        // set content type and other response header fields first
238
        response.setContentType("text/html");
239
        //out.println("Converting to HTML...");
240
        XMLDocumentFragment htmldoc = null;
241

  
242
        // Look up the System ID of the XSL sheet
243
        PreparedStatement pstmt;
244
        String xsl_system_id = null;
230 245
  
231
      out.println(doc);
246
        try {
247
          pstmt =
248
            conn.prepareStatement("SELECT system_id " +
249
                    "FROM xml_catalog_entities " +
250
                    "WHERE source_doctype LIKE ? " +
251
                    "AND target_doctype LIKE ?");
252
          // Bind the values to the query
253
          pstmt.setString(1, "-//NCEAS//eml-dataset//EN");
254
          pstmt.setString(2, "-//W3C//HTML//EN");
255
  
256
          pstmt.execute();
257
          try {
258
            ResultSet rs = pstmt.getResultSet();
259
            try {
260
              boolean tableHasRows = rs.next();
261
              if (tableHasRows) {
262
                try {
263
                  xsl_system_id = rs.getString(1);
264
                } catch (SQLException e) {
265
                  System.out.println("Error with getString: " + e.getMessage());
266
                }
267
              }
268
            } catch (SQLException e) {
269
              System.out.println("Error with next: " + e.getMessage());
270
            }
271
          } catch (SQLException e) {
272
            System.out.println("Error with getrset: " + e.getMessage());
273
          }
274
          pstmt.close();
275
        } catch (SQLException e) {
276
          System.out.println("Error getting id: " + e.getMessage());
277
        }
278
 
279
        // Try to apply the style
280
        try {
281
          XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
282
          htmldoc = (new XSLProcessor()).processXSL(style, 
283
                     (Reader)(new StringReader(doc)),null);
284
          htmldoc.print(out);
285
        } catch (Exception e) {
286
          out.println("Error transforming document:\n" + e.getMessage());
287
        }
288
      }
232 289
  }
233 290

  
234 291
  /** 
lib/resultset.xsl
29 29
          </h3>
30 30
        </center>
31 31
        
32
        <form action="/servlets/MetaCatServlet" method="POST">
33
        <input type="hidden" name="action" value="getdocument"/>
34

  
35
        <xsl:text>Output Format: </xsl:text>
36
        <select name="qformat">
37
          <option value="html">HTML</option>
38
          <option value="xml">XML</option>
39
        </select>
40

  
32 41
        <table width="100%">
33 42
        <tr class="rowodd">
43
        <th>&nbsp;</th>
34 44
        <th><xsl:text>Document ID</xsl:text></th>
35 45
        <th><xsl:text>Title</xsl:text></th>
36 46
        </tr>
......
44 54
              </xsl:choose>
45 55
            </xsl:attribute>
46 56

  
47
          <td><a>
57
          <td>
58
              <input type="radio" name="docid">
59
                <xsl:attribute name="value">
60
                  <xsl:value-of select="."/>
61
                </xsl:attribute>
62
              </input>
63
              <input type="submit" value="Display"/>
64
       <!--
65
              <a>
48 66
              <xsl:attribute name="href">
49 67
              /servlets/MetaCatServlet?action=getdocument&amp;docid=<xsl:value-of select="."/>
50 68
              </xsl:attribute>
51 69
              <xsl:text>Document </xsl:text><xsl:value-of select="."/>
52 70
              </a>
71
        -->
53 72
          </td>
73
          <td>
74
              <xsl:text>Document </xsl:text><xsl:value-of select="."/>
75
          </td>
54 76
          <td>Fake title</td>
55 77
          </tr>
56 78
        </xsl:for-each>
57 79
        </table>
80
        </form>
58 81

  
59 82
      </body>
60 83
    </html>
lib/style/resultset.xsl
29 29
          </h3>
30 30
        </center>
31 31
        
32
        <form action="/servlets/MetaCatServlet" method="POST">
33
        <input type="hidden" name="action" value="getdocument"/>
34

  
35
        <xsl:text>Output Format: </xsl:text>
36
        <select name="qformat">
37
          <option value="html">HTML</option>
38
          <option value="xml">XML</option>
39
        </select>
40

  
32 41
        <table width="100%">
33 42
        <tr class="rowodd">
43
        <th>&nbsp;</th>
34 44
        <th><xsl:text>Document ID</xsl:text></th>
35 45
        <th><xsl:text>Title</xsl:text></th>
36 46
        </tr>
......
44 54
              </xsl:choose>
45 55
            </xsl:attribute>
46 56

  
47
          <td><a>
57
          <td>
58
              <input type="radio" name="docid">
59
                <xsl:attribute name="value">
60
                  <xsl:value-of select="."/>
61
                </xsl:attribute>
62
              </input>
63
              <input type="submit" value="Display"/>
64
       <!--
65
              <a>
48 66
              <xsl:attribute name="href">
49 67
              /servlets/MetaCatServlet?action=getdocument&amp;docid=<xsl:value-of select="."/>
50 68
              </xsl:attribute>
51 69
              <xsl:text>Document </xsl:text><xsl:value-of select="."/>
52 70
              </a>
71
        -->
53 72
          </td>
73
          <td>
74
              <xsl:text>Document </xsl:text><xsl:value-of select="."/>
75
          </td>
54 76
          <td>Fake title</td>
55 77
          </tr>
56 78
        </xsl:for-each>
57 79
        </table>
80
        </form>
58 81

  
59 82
      </body>
60 83
    </html>
lib/style/eml-dataset-display.xsl
22 22
  <xsl:template match="/">
23 23
    <html>
24 24
      <head>
25
        <link rel="stylesheet" type="text/css" href="./rowcol.css" />
25
        <link rel="stylesheet" type="text/css" href="/xmltodb/xsqltest/rowcol.css" />
26 26
      </head>
27 27
      <body class="emlbody">
28 28
        <center>
......
164 164
  <xsl:template match="keyword_info"/>
165 165
  <xsl:template match="relations"/>
166 166

  
167
</xsl:stylesheet>
167
</xsl:stylesheet>
resultset.xsl
29 29
          </h3>
30 30
        </center>
31 31
        
32
        <form action="/servlets/MetaCatServlet" method="POST">
33
        <input type="hidden" name="action" value="getdocument"/>
34

  
35
        <xsl:text>Output Format: </xsl:text>
36
        <select name="qformat">
37
          <option value="html">HTML</option>
38
          <option value="xml">XML</option>
39
        </select>
40

  
32 41
        <table width="100%">
33 42
        <tr class="rowodd">
43
        <th>&nbsp;</th>
34 44
        <th><xsl:text>Document ID</xsl:text></th>
35 45
        <th><xsl:text>Title</xsl:text></th>
36 46
        </tr>
......
44 54
              </xsl:choose>
45 55
            </xsl:attribute>
46 56

  
47
          <td><a>
57
          <td>
58
              <input type="radio" name="docid">
59
                <xsl:attribute name="value">
60
                  <xsl:value-of select="."/>
61
                </xsl:attribute>
62
              </input>
63
              <input type="submit" value="Display"/>
64
       <!--
65
              <a>
48 66
              <xsl:attribute name="href">
49 67
              /servlets/MetaCatServlet?action=getdocument&amp;docid=<xsl:value-of select="."/>
50 68
              </xsl:attribute>
51 69
              <xsl:text>Document </xsl:text><xsl:value-of select="."/>
52 70
              </a>
71
        -->
53 72
          </td>
73
          <td>
74
              <xsl:text>Document </xsl:text><xsl:value-of select="."/>
75
          </td>
54 76
          <td>Fake title</td>
55 77
          </tr>
56 78
        </xsl:for-each>
57 79
        </table>
80
        </form>
58 81

  
59 82
      </body>
60 83
    </html>

Also available in: Unified diff