001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/portal/context/ContextTransformer.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004    This file is part of deegree.
005    Copyright (C) 2001-2008 by:
006    EXSE, Department of Geography, University of Bonn
007    http://www.giub.uni-bonn.de/deegree/
008    lat/lon GmbH
009    http://www.lat-lon.de
010    
011    This library is free software; you can redistribute it and/or
012    modify it under the terms of the GNU Lesser General Public
013    License as published by the Free Software Foundation; either
014    version 2.1 of the License, or (at your option) any later version.
015    
016    This library is distributed in the hope that it will be useful,
017    but WITHOUT ANY WARRANTY; without even the implied warranty of
018    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019    Lesser General Public License for more details.
020    
021    You should have received a copy of the GNU Lesser General Public
022    License along with this library; if not, write to the Free Software
023    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025    Contact:
026    
027    Andreas Poth
028    lat/lon GmbH
029    Aennchenstr. 19
030    53115 Bonn
031    Germany
032    E-Mail: poth@lat-lon.de
033    
034    Prof. Dr. Klaus Greve
035    Department of Geography
036    University of Bonn
037    Meckenheimer Allee 166
038    53115 Bonn
039    Germany
040    E-Mail: greve@giub.uni-bonn.de
041    
042                     
043     ---------------------------------------------------------------------------*/
044    
045    package org.deegree.portal.context;
046    
047    import java.io.IOException;
048    import java.io.InputStream;
049    import java.io.StringWriter;
050    
051    import javax.xml.transform.Transformer;
052    import javax.xml.transform.TransformerConfigurationException;
053    import javax.xml.transform.TransformerException;
054    import javax.xml.transform.TransformerFactory;
055    import javax.xml.transform.TransformerFactoryConfigurationError;
056    import javax.xml.transform.stream.StreamResult;
057    import javax.xml.transform.stream.StreamSource;
058    
059    import org.deegree.framework.log.ILogger;
060    import org.deegree.framework.log.LoggerFactory;
061    
062    
063    /**
064     * Singleton class capsulating a <code>javax.xml.transform.Transformer</code>. 
065     * This class is used to transform a map context xml document in a html 
066     * document used the transformation (xslt) provided.
067     * 
068     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
069     * 
070     */
071    public class ContextTransformer {
072        
073        private static final ILogger LOG = LoggerFactory.getLogger( ContextTransformer.class );
074    
075        /**
076         * The <code>Transformer</code> object used in the transformation of 
077         * a map context xml to html.*/
078        protected static Transformer transformer = null;   
079        private static final ContextTransformer INSTANCE = new ContextTransformer();
080        
081        // Forbid instanstiation
082        private ContextTransformer() { }
083            
084        private static void initTransformer(InputStream is){
085            
086                try {
087                TransformerFactory tFactory = TransformerFactory.newInstance();
088                transformer = tFactory.newTransformer( 
089                            new StreamSource( is ) );
090            } catch (TransformerConfigurationException e) {
091                e.printStackTrace();
092            } catch (TransformerFactoryConfigurationError e) {
093                e.printStackTrace();
094            }   
095        }
096        
097        public static ContextTransformer getInstance(){
098            
099            return INSTANCE;        
100        }
101    
102        /**
103         * Transforms the context pointed to by <code>context</code> into html
104         * using <code>xsltURL</code> (though this is currently fixed; there's 
105         * really no need to define one's wn xsl).
106         * @param xsl the <code>InputStream</code> containing the xls
107         * @param contxt the <code>InputStream</code> containing the context to be transformed
108         */
109        public String transformContext( InputStream xsl, InputStream contxt) 
110            throws TransformerException {
111    
112            StringWriter sw = new StringWriter();        
113    
114            if( transformer == null ){ // needs to instatiate transformer            
115                initTransformer(xsl);
116            }
117            if( transformer == null ){ 
118                LOG.logInfo( "transformer is still null" ); 
119            }
120            StreamResult sr = new StreamResult(sw);
121            StreamSource sSrc = new StreamSource( contxt );
122            transformer.transform( sSrc , sr );
123            
124            try {
125                sw.close();
126            } catch (IOException e) {
127                LOG.logError( "Unable to close string writer.", e );
128            }
129                            
130            return sw.toString();
131        }    
132    
133        /**
134         * 
135         */
136        public String toString(){
137            return transformer.toString();
138        }
139    }