    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_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.File;
    040    import java.io.FileNotFoundException;
    041    import java.io.IOException;
    042    import java.io.Reader;
    043    import java.util.HashMap;
    044    import java.util.Map;
    045    import java.util.Properties;
    046
    047    import javax.xml.transform.Transformer;
    048    import javax.xml.transform.TransformerException;
    049
    050    import org.deegree.framework.log.ILogger;
    051    import org.deegree.framework.log.LoggerFactory;
    052    import org.deegree.framework.xml.XMLFragment;
    053    import org.deegree.framework.xml.XSLTDocument;
    054    import org.deegree.portal.standard.context.control.ContextLoadListener;
    055    import org.xml.sax.SAXException;
    056
    057    /**
    058     * A <code>${type_name}</code> class.<br/>
    059     * TODO class description
    060     *
    061     * @author <a href="mailto:mays@lat-lon.de">Judit Mays</a>
    062     * @author last edited by: $Author: apoth $
    063     *
    064     * @version 2.0, $Revision: 23921 $, $Date: 2010-04-28 11:36:21 +0200 (Mi, 28. Apr 2010) $
    065     *
    066     * @since 2.0
    067     */
    068    public class MetadataTransformer {
    069
    070        private static final ILogger LOG = LoggerFactory.getLogger( ContextLoadListener.class );
    071
    072        /**
    073         * The <code>Transformer</code> object used in the transformation of a map context xml to html.
    074         */
    075        private Transformer transformer = null;
    076
    077        private XSLTDocument xslt;
    078
    079        /**
    080         * Creates a new MetadataTransformer and initializes it with the given <code>file</code> (path and name).
    081         *
    082         * @param filePathName
    083         * @throws FileNotFoundException
    084         *             , if filePathName does not point to an existing file.
    085         */
    086        public MetadataTransformer( String filePathName ) throws FileNotFoundException {
    087            init( filePathName );
    088        }
    089
    090        private void init( String filePathName ) {
    091            try {
    092                xslt = new XSLTDocument( new File( filePathName ).toURI().toURL() );
    093            } catch ( Exception e ) {
    094                LOG.logError( e );
    095                throw new RuntimeException( e );
    096            }
    097        }
    098
    099        /**
    100         * Transforms the context pointed to by <code>context</code> into html using <code>xsltURL</code>
    101         *
    102         * @param metadataXml
    103         *            The <code>Reader</code> containing the xml document to be transformed.
    104         * @param catalog
    105         *            The name of the catalog.
    106         * @param serviceCatalogs
    107         * @param hits
    108         *            The number of records matched for this catalog.
    109         * @param startPosition
    110         *            The position to start displaying the matched records from.
    111         * @param metaVersion
    112         *            The version of metadata to transform ( list, overview, detailed ).
    113         * @return Returns result of transformation.
    114         * @throws TransformerException
    115         * @throws IOException
    116         */
    117        public String transformMetadata( Reader metadataXml, String catalog, String[] serviceCatalogs, int hits,
    118                                         int startPosition, String metaVersion )
    119                                throws TransformerException, IOException {
    120
    121            // turn array of Strings into one comma-separated String
    122            StringBuffer sb = new StringBuffer();
    123            if ( serviceCatalogs != null ) {
    124                for ( int i = 0; i < serviceCatalogs.length; i++ ) {
    125                    sb.append( serviceCatalogs[i] );
    126                    if ( i < serviceCatalogs.length - 1 ) {
    127                        sb.append( "," );
    128                    }
    129                }
    130            }
    131
    132            Map<String, Object> params = new HashMap<String, Object>();
    133            params.put( "CATALOG", catalog );
    134            params.put( "SERVICECATALOGS", sb.toString() );
    135            params.put( "HITS", new Integer( hits ) );
    136            params.put( "STARTPOS", new Integer( startPosition ) );
    137            params.put( "METAVERSION", metaVersion );
    138
    139            XMLFragment xml = null;
    140            try {
    141                xml = new XMLFragment( metadataXml, XMLFragment.DEFAULT_URL );
    142            } catch ( SAXException e ) {
    143                throw new TransformerException( e );
    144            }
    145
    146            xml = xslt.transform( xml, XMLFragment.DEFAULT_URL, new Properties(), params );
    147
    148            return xml.getAsString();
    149        }
    150
    151        @Override
    152        public String toString() {
    153            return transformer.toString();
    154        }
    155
    156    }