001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/wfs/operation/GmlResult.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.ogcwebservices.wfs.operation;
038    
039    import static org.deegree.framework.log.LoggerFactory.getLogger;
040    import static org.deegree.framework.xml.XMLTools.getStringFragmentAsElement;
041    
042    import java.io.IOException;
043    import java.io.OutputStream;
044    
045    import javax.xml.transform.TransformerException;
046    
047    import org.deegree.framework.log.ILogger;
048    import org.deegree.framework.xml.XMLException;
049    import org.deegree.framework.xml.XMLFragment;
050    import org.deegree.framework.xml.XSLTDocument;
051    import org.deegree.model.feature.Feature;
052    import org.deegree.model.feature.FeatureException;
053    import org.deegree.model.feature.GMLFeatureAdapter;
054    import org.deegree.model.spatialschema.GMLGeometryAdapter;
055    import org.deegree.model.spatialschema.Geometry;
056    import org.deegree.model.spatialschema.GeometryException;
057    import org.deegree.ogcwebservices.DefaultOGCWebServiceResponse;
058    import org.deegree.ogcwebservices.OGCWebServiceException;
059    import org.deegree.ogcwebservices.OGCWebServiceRequest;
060    import org.deegree.ogcwebservices.wfs.capabilities.FormatType;
061    import org.xml.sax.SAXException;
062    
063    /**
064     * <code>GmlResult</code>
065     *
066     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
067     * @author last edited by: $Author: mschneider $
068     *
069     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
070     */
071    public class GmlResult extends DefaultOGCWebServiceResponse {
072        private static final ILogger LOG = getLogger( GmlResult.class );
073    
074        private Feature feature;
075    
076        private Geometry geometry;
077    
078        private FormatType format;
079    
080        /**
081         * @param request
082         * @param exception
083         */
084        public GmlResult( OGCWebServiceRequest request, OGCWebServiceException exception ) {
085            super( request, exception );
086        }
087    
088        /**
089         * @param request
090         * @param feature
091         * @param format
092         */
093        public GmlResult( OGCWebServiceRequest request, Feature feature, FormatType format ) {
094            super( request );
095            this.feature = feature;
096            this.format = format;
097        }
098    
099        /**
100         * @param request
101         * @param geometry
102         * @param format
103         */
104        public GmlResult( OGCWebServiceRequest request, Geometry geometry, FormatType format ) {
105            super( request );
106            this.geometry = geometry;
107            this.format = format;
108        }
109    
110        /**
111         * @param out
112         * @throws FeatureException
113         * @throws IOException
114         * @throws GeometryException
115         */
116        public void writeResult( OutputStream out )
117                                throws IOException, FeatureException, GeometryException {
118            XMLFragment doc = null;
119            if ( feature != null ) {
120                try {
121                    doc = new GMLFeatureAdapter( ( (GetGmlObject) request ).getXLinkDepth() ).export( feature );
122                } catch ( XMLException e ) {
123                    LOG.logError( "Unknown error", e );
124                } catch ( SAXException e ) {
125                    LOG.logError( "Unknown error", e );
126                }
127            }
128            if ( geometry != null ) {
129                // the ugly hack is necessary to insert missing namespace bindings such as gml
130                StringBuffer sb = GMLGeometryAdapter.export( geometry, ( (GetGmlObject) request ).getObjectId() );
131                doc = new XMLFragment();
132                try {
133                    doc.setRootElement( getStringFragmentAsElement( sb.toString() ) );
134                } catch ( SAXException e ) {
135                    LOG.logError( "Unknown error", e );
136                }
137            }
138    
139            try {
140                XSLTDocument xslt = new XSLTDocument( format.getOutFilter().toURL() );
141                xslt.transform( doc, out );
142            } catch ( SAXException e ) {
143                LOG.logError( "Unknown error", e );
144            } catch ( TransformerException e ) {
145                LOG.logError( "Unknown error", e );
146            }
147        }
148    
149    }