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: costa $'
7
 *     '$Date: 2007-03-29 16:03:41 -0700 (Thu, 29 Mar 2007) $'
8
 * '$Revision: 3217 $'
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
   * @param qformat         The qformat (skin) to use when displaying results.
51
   * @param xslPath         File path to the resultset.xsl stylesheet.
52
   * 
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
                                final String qformat,
59
                                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

    
80
      if (sessionId != null) {
81
        transformer.setParameter("sessid", sessionId);
82
      }
83

    
84
      transformer.setParameter("metacatURL", metacatURL);
85
      
86
      if ((qformat != null) && (!qformat.equals(""))) {
87
        transformer.setParameter("qformat", qformat);
88
      }
89
      
90
      transformer.transform(xmlSource, result);
91
      htmlString = stringWriter.toString();
92
    }
93
    catch (TransformerConfigurationException tce) {
94
      // Error generated by the parser
95
      Throwable x = tce;  // Use the contained exception, if any
96
        
97
      if (tce.getException() != null) {
98
        x = tce.getException();    
99
      }
100

    
101
      x.printStackTrace();   
102
    }
103
    catch (TransformerException te) {
104
      // Error generated by the parser
105
      Throwable x = te;  // Use the contained exception, if any
106
        
107
      if (te.getException() != null) {
108
        x = te.getException();    
109
      }
110

    
111
      x.printStackTrace(); 
112
    }
113
      
114
    return htmlString;
115
  }
116
  
117
}
(14-14/14)