001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/standard/csw/MetadataTransformer.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 037 package org.deegree.portal.standard.csw; 038 039 import java.io.FileInputStream; 040 import java.io.FileNotFoundException; 041 import java.io.IOException; 042 import java.io.InputStream; 043 import java.io.Reader; 044 import java.io.StringWriter; 045 046 import javax.xml.transform.Transformer; 047 import javax.xml.transform.TransformerException; 048 import javax.xml.transform.TransformerFactory; 049 import javax.xml.transform.stream.StreamResult; 050 import javax.xml.transform.stream.StreamSource; 051 052 import org.deegree.framework.log.ILogger; 053 import org.deegree.framework.log.LoggerFactory; 054 import org.deegree.i18n.Messages; 055 import org.deegree.portal.standard.context.control.ContextLoadListener; 056 057 /** 058 * A <code>${type_name}</code> class.<br/> TODO class description 059 * 060 * @author <a href="mailto:mays@lat-lon.de">Judit Mays</a> 061 * @author last edited by: $Author: mschneider $ 062 * 063 * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $ 064 * 065 * @since 2.0 066 */ 067 public class MetadataTransformer { 068 069 private static final ILogger LOG = LoggerFactory.getLogger( ContextLoadListener.class ); 070 071 /** 072 * The <code>Transformer</code> object used in the transformation of a map context xml to html. 073 */ 074 private Transformer transformer = null; 075 076 /** 077 * Creates a new MetadataTransformer and initializes it with the given <code>file</code> (path and name). 078 * 079 * @param filePathName 080 * @throws FileNotFoundException, 081 * if filePathName does not point to an existing file. 082 */ 083 public MetadataTransformer( String filePathName ) throws FileNotFoundException { 084 initTransformer( filePathName ); 085 } 086 087 /** 088 * @param filePathName 089 * @throws FileNotFoundException 090 */ 091 private void initTransformer( String filePathName ) 092 throws FileNotFoundException { 093 094 InputStream xslInputStream = new FileInputStream( filePathName ); 095 096 try { 097 TransformerFactory tFactory = TransformerFactory.newInstance(); 098 transformer = tFactory.newTransformer( new StreamSource( xslInputStream ) ); 099 } catch ( Exception e ) { 100 LOG.logError( e.getMessage() ); 101 } 102 } 103 104 /** 105 * Transforms the context pointed to by <code>context</code> into html using <code>xsltURL</code> 106 * 107 * @param metadataXml 108 * The <code>Reader</code> containing the xml document to be transformed. 109 * @param catalog 110 * The name of the catalog. 111 * @param serviceCatalogs 112 * @param hits 113 * The number of records matched for this catalog. 114 * @param startPosition 115 * The position to start displaying the matched records from. 116 * @param metaVersion 117 * The version of metadata to transform ( list, overview, detailed ). 118 * @return Returns result of transformation. 119 * @throws TransformerException 120 * @throws IOException 121 */ 122 public String transformMetadata( Reader metadataXml, String catalog, String[] serviceCatalogs, int hits, 123 int startPosition, String metaVersion ) 124 throws TransformerException, IOException { 125 126 if ( transformer == null ) { 127 throw new IOException( Messages.getMessage( "IGEO_STD_CSW_TRANSFORMER_ERROR" ) ); 128 } 129 130 StringWriter sw = new StringWriter(); 131 StreamResult strmResult = new StreamResult( sw ); 132 StreamSource xmlSrc = new StreamSource( metadataXml ); 133 134 // turn array of Strings into one comma-separated String 135 StringBuffer sb = new StringBuffer(); 136 if ( serviceCatalogs != null ) { 137 for ( int i = 0; i < serviceCatalogs.length; i++ ) { 138 sb.append( serviceCatalogs[i] ); 139 if ( i < serviceCatalogs.length - 1 ) { 140 sb.append( "," ); 141 } 142 } 143 } 144 145 // setting global variables for xslt-scripts 146 transformer.setParameter( "CATALOG", catalog ); 147 transformer.setParameter( "SERVICECATALOGS", sb.toString() ); 148 transformer.setParameter( "HITS", new Integer( hits ) ); 149 transformer.setParameter( "STARTPOS", new Integer( startPosition ) ); 150 transformer.setParameter( "METAVERSION", metaVersion ); 151 152 transformer.transform( xmlSrc, strmResult ); 153 try { 154 sw.close(); 155 } catch ( IOException e ) { 156 LOG.logError( "Unable to close string writer.\n" ); 157 } 158 159 return sw.toString(); 160 } 161 162 @Override 163 public String toString() { 164 return transformer.toString(); 165 } 166 167 }