Project

General

Profile

1 2741 costa
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24
25
package edu.ucsb.nceas.metacat.advancedsearch;
26
27
import java.io.*;
28
import javax.xml.transform.Result;
29
import javax.xml.transform.Source;
30
import javax.xml.transform.Transformer;
31
import javax.xml.transform.TransformerConfigurationException;
32
import javax.xml.transform.TransformerException;
33
import javax.xml.transform.TransformerFactory;
34
35
/**
36
 * @author dcosta
37
 *
38
 * Stylizer class applies the resultset.xsl stylesheet to the pathquery
39
 * results returned by Metacat.
40
 */
41
public class Stylizer {
42
43
  /**
44
   * Applies the resultset.xsl stylesheet to the pathquery result string
45
   * returned by Metacat.
46
   *
47
   * @param resultset       the pathquery result string from Metacat
48
   * @param sessionId       the user's session id
49
   * @param metacatURL      the URL to the Metacat server
50 3007 costa
   * @param qformat         The qformat (skin) to use when displaying results.
51
   * @param xslPath         File path to the resultset.xsl stylesheet.
52 2741 costa
   *
53
   * @return htmlString     the result of the transformation from XML to HTML
54
   */
55
  public String resultsetToHTML(final String resultset,
56
                                final String sessionId,
57
                                final String metacatURL,
58 3007 costa
                                final String qformat,
59 2741 costa
                                final String xslPath) {
60
    String htmlString = "";
61
    Result result;
62
    StringWriter stringWriter = new StringWriter();
63
    Transformer transformer;
64
    TransformerFactory transformerFactory;
65
    Source xmlSource;
66
    File xsltFile = new File(xslPath);
67
    Source xsltSource;
68
    StringReader stringReader = new StringReader(resultset);
69
70
    xmlSource = new javax.xml.transform.stream.StreamSource(stringReader);
71
    xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
72
    result = new javax.xml.transform.stream.StreamResult(stringWriter);
73
74
    // create an instance of TransformerFactory
75
    transformerFactory = TransformerFactory.newInstance();
76
77
    try {
78
      transformer = transformerFactory.newTransformer(xsltSource);
79
      transformer.setParameter("sessid", sessionId);
80
      transformer.setParameter("metacatURL", metacatURL);
81 3007 costa
82
      if ((qformat != null) && (!qformat.equals(""))) {
83
        transformer.setParameter("qformat", qformat);
84
      }
85
86 2741 costa
      transformer.transform(xmlSource, result);
87
      htmlString = stringWriter.toString();
88
    }
89
    catch (TransformerConfigurationException tce) {
90
      // Error generated by the parser
91
      Throwable x = tce;  // Use the contained exception, if any
92
93
      if (tce.getException() != null) {
94
        x = tce.getException();
95
      }
96
97
      x.printStackTrace();
98
    }
99
    catch (TransformerException te) {
100
      // Error generated by the parser
101
      Throwable x = te;  // Use the contained exception, if any
102
103
      if (te.getException() != null) {
104
        x = te.getException();
105
      }
106
107
      x.printStackTrace();
108
    }
109
110
    return htmlString;
111
  }
112
113
}