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: 2005-11-16 09:57:33 -0800 (Wed, 16 Nov 2005) $'
8
 * '$Revision: 2741 $'
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
   * 
51
   * @return htmlString     the result of the transformation from XML to HTML
52
   */
53
  public String resultsetToHTML(final String resultset, 
54
                                final String sessionId,
55
                                final String metacatURL,
56
                                final String xslPath) {
57
    String htmlString = "";
58
    Result result;
59
    StringWriter stringWriter = new StringWriter();
60
    Transformer transformer;
61
    TransformerFactory transformerFactory;
62
    Source xmlSource;
63
    File xsltFile = new File(xslPath);            
64
    Source xsltSource;
65
    StringReader stringReader = new StringReader(resultset);
66
    
67
    xmlSource = new javax.xml.transform.stream.StreamSource(stringReader);
68
    xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
69
    result = new javax.xml.transform.stream.StreamResult(stringWriter);
70

    
71
    // create an instance of TransformerFactory
72
    transformerFactory = TransformerFactory.newInstance();
73

    
74
    try {
75
      transformer = transformerFactory.newTransformer(xsltSource);
76
      transformer.setParameter("sessid", sessionId);
77
      transformer.setParameter("metacatURL", metacatURL);
78
      transformer.transform(xmlSource, result);
79
      htmlString = stringWriter.toString();
80
    }
81
    catch (TransformerConfigurationException tce) {
82
      // Error generated by the parser
83
      Throwable x = tce;  // Use the contained exception, if any
84
        
85
      if (tce.getException() != null) {
86
        x = tce.getException();    
87
      }
88

    
89
      x.printStackTrace();   
90
    }
91
    catch (TransformerException te) {
92
      // Error generated by the parser
93
      Throwable x = te;  // Use the contained exception, if any
94
        
95
      if (te.getException() != null) {
96
        x = te.getException();    
97
      }
98

    
99
      x.printStackTrace(); 
100
    }
101
      
102
    return htmlString;
103
  }
104
  
105
}
(14-14/14)