Revision 4080
Added by daigle about 16 years ago
Stylizer.java | ||
---|---|---|
34 | 34 |
|
35 | 35 |
import org.apache.log4j.Logger; |
36 | 36 |
|
37 |
import edu.ucsb.nceas.metacat.MetaCatUtil; |
|
37 |
import edu.ucsb.nceas.metacat.util.SystemUtil; |
|
38 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
|
38 | 39 |
|
39 | 40 |
/** |
40 | 41 |
* @author dcosta |
... | ... | |
44 | 45 |
*/ |
45 | 46 |
public class Stylizer { |
46 | 47 |
|
47 |
private Logger logMetacat = Logger.getLogger(Stylizer.class); |
|
48 |
|
|
49 |
/** |
|
50 |
* Applies the resultset.xsl stylesheet to the pathquery result string |
|
51 |
* returned by Metacat. |
|
52 |
* |
|
53 |
* @param resultset the pathquery result string from Metacat |
|
54 |
* @param sessionId the user's session id |
|
55 |
* @param metacatURL the URL to the Metacat server |
|
56 |
* @param qformat The qformat (skin) to use when displaying results. |
|
57 |
* @param xslPath File path to the resultset.xsl stylesheet. |
|
58 |
* |
|
59 |
* @return htmlString the result of the transformation from XML to HTML |
|
60 |
*/ |
|
61 |
public String resultsetToHTML(final String resultset, |
|
62 |
final String sessionId, |
|
63 |
final String metacatURL, |
|
64 |
final String qformat, |
|
65 |
final String xslPath) { |
|
66 |
String htmlString = ""; |
|
67 |
Result result; |
|
68 |
StringWriter stringWriter = new StringWriter(); |
|
69 |
Transformer transformer; |
|
70 |
TransformerFactory transformerFactory; |
|
71 |
Source xmlSource; |
|
72 |
File xsltFile = new File(xslPath); |
|
73 |
Source xsltSource; |
|
74 |
StringReader stringReader = new StringReader(resultset); |
|
75 |
|
|
76 |
xmlSource = new javax.xml.transform.stream.StreamSource(stringReader); |
|
77 |
xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile); |
|
78 |
result = new javax.xml.transform.stream.StreamResult(stringWriter); |
|
48 |
private Logger logMetacat = Logger.getLogger(Stylizer.class); |
|
79 | 49 |
|
80 |
// create an instance of TransformerFactory |
|
81 |
transformerFactory = TransformerFactory.newInstance(); |
|
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); |
|
82 | 73 |
|
83 |
try { |
|
84 |
transformer = transformerFactory.newTransformer(xsltSource); |
|
85 |
|
|
86 |
MetaCatUtil metaCatUtil = new MetaCatUtil(); |
|
87 |
|
|
88 |
String httpServer = MetaCatUtil.getOption("httpserver"); |
|
89 |
String context = MetaCatUtil.getOption("context"); |
|
90 |
|
|
91 |
// Log error if httpserver or context are not set in metacat |
|
92 |
// configuration, but allow transform to proceed. User will |
|
93 |
// see data, albeit without pretty formatting. |
|
94 |
if (httpServer == null || context == null) { |
|
95 |
logMetacat.error("httpserver and context values must be set in " + |
|
96 |
"metacat configuration for advanced search formatting to " + |
|
97 |
"work properly"); |
|
98 |
} else { |
|
99 |
transformer.setParameter("contextURL", httpServer+"/"+context+"/"); |
|
100 |
} |
|
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); |
|
101 | 77 |
|
102 |
if (sessionId != null) { |
|
103 |
transformer.setParameter("sessid", sessionId); |
|
104 |
} |
|
78 |
// create an instance of TransformerFactory |
|
79 |
transformerFactory = TransformerFactory.newInstance(); |
|
105 | 80 |
|
106 |
transformer.setParameter("metacatURL", metacatURL); |
|
107 |
|
|
108 |
if ((qformat != null) && (!qformat.equals(""))) { |
|
109 |
transformer.setParameter("qformat", qformat); |
|
110 |
} |
|
111 |
|
|
112 |
transformer.transform(xmlSource, result); |
|
113 |
htmlString = stringWriter.toString(); |
|
114 |
} |
|
115 |
catch (TransformerConfigurationException tce) { |
|
116 |
// Error generated by the parser |
|
117 |
Throwable x = tce; // Use the contained exception, if any |
|
118 |
|
|
119 |
if (tce.getException() != null) { |
|
120 |
x = tce.getException(); |
|
121 |
} |
|
81 |
try { |
|
82 |
transformer = transformerFactory.newTransformer(xsltSource); |
|
122 | 83 |
|
123 |
x.printStackTrace(); |
|
124 |
} |
|
125 |
catch (TransformerException te) { |
|
126 |
// Error generated by the parser |
|
127 |
Throwable x = te; // Use the contained exception, if any |
|
128 |
|
|
129 |
if (te.getException() != null) { |
|
130 |
x = te.getException(); |
|
131 |
} |
|
84 |
String contextURL = SystemUtil.getContextURL(); |
|
85 |
transformer.setParameter("contextURL", contextURL); |
|
132 | 86 |
|
133 |
x.printStackTrace(); |
|
134 |
} |
|
135 |
|
|
136 |
return htmlString; |
|
137 |
} |
|
138 |
|
|
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 |
|
|
139 | 124 |
} |
Also available in: Unified diff
Merge 1.9 changes into Head