Project

General

Profile

1
/**
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: daigle $'
7
 *     '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
8
 * '$Revision: 4080 $'
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
import org.apache.log4j.Logger;
36

    
37
import edu.ucsb.nceas.metacat.util.SystemUtil;
38
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39

    
40
/**
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
	private Logger logMetacat = Logger.getLogger(Stylizer.class);
49

    
50
	/**
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

    
74
		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

    
78
		// create an instance of TransformerFactory
79
		transformerFactory = TransformerFactory.newInstance();
80

    
81
		try {
82
			transformer = transformerFactory.newTransformer(xsltSource);
83

    
84
			String contextURL = SystemUtil.getContextURL();
85
			transformer.setParameter("contextURL", contextURL);
86

    
87
			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
}
(14-14/14)