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-03-18 16:31:08 -0700 (Tue, 18 Mar 2008) $'
8
 * '$Revision: 3768 $'
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.MetaCatUtil;
38

    
39
/**
40
 * @author dcosta
41
 * 
42
 * Stylizer class applies the resultset.xsl stylesheet to the pathquery
43
 * results returned by Metacat.
44
 */
45
public class Stylizer {
46

    
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);
79

    
80
    // create an instance of TransformerFactory
81
    transformerFactory = TransformerFactory.newInstance();
82

    
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
      }
101

    
102
      if (sessionId != null) {
103
        transformer.setParameter("sessid", sessionId);
104
      }
105

    
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
      }
122

    
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
      }
132

    
133
      x.printStackTrace(); 
134
    }
135
      
136
    return htmlString;
137
  }
138
  
139
}
(14-14/14)