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 |
3217
|
costa
|
* mERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
2741
|
costa
|
* 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 |
3768
|
daigle
|
import org.apache.log4j.Logger;
|
36 |
|
|
|
37 |
4080
|
daigle
|
import edu.ucsb.nceas.metacat.util.SystemUtil;
|
38 |
|
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
39 |
3768
|
daigle
|
|
40 |
2741
|
costa
|
/**
|
41 |
|
|
* @author dcosta
|
42 |
|
|
*
|
43 |
|
|
* Stylizer class applies the resultset.xsl stylesheet to the pathquery
|
44 |
|
|
* results returned by Metacat.
|
45 |
|
|
*/
|
46 |
|
|
public class Stylizer {
|
47 |
|
|
|
48 |
4080
|
daigle
|
private Logger logMetacat = Logger.getLogger(Stylizer.class);
|
49 |
2741
|
costa
|
|
50 |
4080
|
daigle
|
/**
|
51 |
|
|
* Applies the resultset.xsl stylesheet to the pathquery result string
|
52 |
|
|
* returned by Metacat.
|
53 |
|
|
*
|
54 |
|
|
* @param resultset the pathquery result string from Metacat
|
55 |
|
|
* @param sessionId the user's session id
|
56 |
|
|
* @param metacatURL the URL to the Metacat server
|
57 |
|
|
* @param qformat The qformat (skin) to use when displaying results.
|
58 |
|
|
* @param xslPath File path to the resultset.xsl stylesheet.
|
59 |
|
|
*
|
60 |
|
|
* @return htmlString the result of the transformation from XML to HTML
|
61 |
|
|
*/
|
62 |
|
|
public String resultsetToHTML(final String resultset, final String sessionId,
|
63 |
|
|
final String metacatURL, final String qformat, final String xslPath) {
|
64 |
|
|
String htmlString = "";
|
65 |
|
|
Result result;
|
66 |
|
|
StringWriter stringWriter = new StringWriter();
|
67 |
|
|
Transformer transformer;
|
68 |
|
|
TransformerFactory transformerFactory;
|
69 |
|
|
Source xmlSource;
|
70 |
|
|
File xsltFile = new File(xslPath);
|
71 |
|
|
Source xsltSource;
|
72 |
|
|
StringReader stringReader = new StringReader(resultset);
|
73 |
2741
|
costa
|
|
74 |
4080
|
daigle
|
xmlSource = new javax.xml.transform.stream.StreamSource(stringReader);
|
75 |
|
|
xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
|
76 |
|
|
result = new javax.xml.transform.stream.StreamResult(stringWriter);
|
77 |
3217
|
costa
|
|
78 |
4080
|
daigle
|
// create an instance of TransformerFactory
|
79 |
|
|
transformerFactory = TransformerFactory.newInstance();
|
80 |
3217
|
costa
|
|
81 |
4080
|
daigle
|
try {
|
82 |
|
|
transformer = transformerFactory.newTransformer(xsltSource);
|
83 |
2741
|
costa
|
|
84 |
4080
|
daigle
|
String contextURL = SystemUtil.getContextURL();
|
85 |
|
|
transformer.setParameter("contextURL", contextURL);
|
86 |
2741
|
costa
|
|
87 |
4080
|
daigle
|
if (sessionId != null) {
|
88 |
|
|
transformer.setParameter("sessid", sessionId);
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
transformer.setParameter("metacatURL", metacatURL);
|
92 |
|
|
|
93 |
|
|
if ((qformat != null) && (!qformat.equals(""))) {
|
94 |
|
|
transformer.setParameter("qformat", qformat);
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
transformer.transform(xmlSource, result);
|
98 |
|
|
htmlString = stringWriter.toString();
|
99 |
|
|
} catch (TransformerConfigurationException tce) {
|
100 |
|
|
// Error generated by the parser
|
101 |
|
|
Throwable x = tce; // Use the contained exception, if any
|
102 |
|
|
|
103 |
|
|
if (tce.getException() != null) {
|
104 |
|
|
x = tce.getException();
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
x.printStackTrace();
|
108 |
|
|
} catch (TransformerException te) {
|
109 |
|
|
// Error generated by the parser
|
110 |
|
|
Throwable x = te; // Use the contained exception, if any
|
111 |
|
|
|
112 |
|
|
if (te.getException() != null) {
|
113 |
|
|
x = te.getException();
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
x.printStackTrace();
|
117 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
118 |
|
|
pnfe.printStackTrace();
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
return htmlString;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
2741
|
costa
|
}
|