001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/context/ContextTransformer.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.context;
038    
039    import java.io.IOException;
040    import java.io.InputStream;
041    import java.io.StringWriter;
042    
043    import javax.xml.transform.Transformer;
044    import javax.xml.transform.TransformerConfigurationException;
045    import javax.xml.transform.TransformerException;
046    import javax.xml.transform.TransformerFactory;
047    import javax.xml.transform.TransformerFactoryConfigurationError;
048    import javax.xml.transform.stream.StreamResult;
049    import javax.xml.transform.stream.StreamSource;
050    
051    import org.deegree.framework.log.ILogger;
052    import org.deegree.framework.log.LoggerFactory;
053    
054    /**
055     * Singleton class capsulating a <code>javax.xml.transform.Transformer</code>. This class is used
056     * to transform a map context xml document in a html document used the transformation (xslt)
057     * provided.
058     *
059     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
060     *
061     */
062    public class ContextTransformer {
063    
064        private static final ILogger LOG = LoggerFactory.getLogger( ContextTransformer.class );
065    
066        /**
067         * The <code>Transformer</code> object used in the transformation of a map context xml to
068         * html.
069         */
070        protected static Transformer transformer = null;
071    
072        private static final ContextTransformer INSTANCE = new ContextTransformer();
073    
074    
075        private ContextTransformer() {
076            // Forbid instantiation
077        }
078    
079        private static void initTransformer( InputStream is ) {
080    
081            try {
082                TransformerFactory tFactory = TransformerFactory.newInstance();
083                transformer = tFactory.newTransformer( new StreamSource( is ) );
084            } catch ( TransformerConfigurationException e ) {
085                e.printStackTrace();
086            } catch ( TransformerFactoryConfigurationError e ) {
087                e.printStackTrace();
088            }
089        }
090    
091        /**
092         * @return the singleton.
093         */
094        public static ContextTransformer getInstance() {
095            return INSTANCE;
096        }
097    
098        /**
099         * Transforms the context pointed to by <code>context</code> into html using
100         * <code>xsltURL</code> (though this is currently fixed; there's really no need to define
101         * one's wn xsl).
102         *
103         * @param xsl
104         *            the <code>InputStream</code> containing the xls
105         * @param contxt
106         *            the <code>InputStream</code> containing the context to be transformed
107         * @return the converted string.
108         * @throws TransformerException
109         */
110        public String transformContext( InputStream xsl, InputStream contxt )
111                                throws TransformerException {
112    
113            StringWriter sw = new StringWriter();
114    
115            if ( transformer == null ) { // needs to instantiate transformer
116                initTransformer( xsl );
117            }
118            if ( transformer == null ) {
119                LOG.logInfo( "transformer is still null" );
120            }
121            StreamResult sr = new StreamResult( sw );
122            StreamSource sSrc = new StreamSource( contxt );
123            transformer.transform( sSrc, sr );
124    
125            try {
126                sw.close();
127            } catch ( IOException e ) {
128                LOG.logError( "Unable to close string writer.", e );
129            }
130    
131            return sw.toString();
132        }
133    
134        @Override
135        public String toString() {
136            return transformer.toString();
137        }
138    }