001    //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/ogcwebservices/csw/XMLFactory.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    package org.deegree.ogcwebservices.csw;
037    
038    import java.io.IOException;
039    import java.util.HashSet;
040    import java.util.Set;
041    
042    import org.deegree.framework.xml.XMLParsingException;
043    import org.deegree.framework.xml.XMLTools;
044    import org.deegree.model.filterencoding.capabilities.FilterCapabilities;
045    import org.deegree.ogcwebservices.csw.capabilities.CatalogueCapabilities;
046    import org.deegree.ogcwebservices.csw.capabilities.CatalogueCapabilitiesDocument;
047    import org.deegree.ogcwebservices.csw.capabilities.CatalogueCapabilitiesDocument_2_0_2;
048    import org.deegree.ogcwebservices.csw.configuration.CatalogueOutputSchemaParameter;
049    import org.deegree.ogcwebservices.csw.configuration.CatalogueOutputSchemaValue;
050    import org.deegree.ogcwebservices.csw.configuration.CatalogueTypeNameSchemaParameter;
051    import org.deegree.ogcwebservices.csw.configuration.CatalogueTypeNameSchemaValue;
052    import org.deegree.ogcwebservices.getcapabilities.Contents;
053    import org.deegree.ogcwebservices.getcapabilities.DCPType;
054    import org.deegree.ogcwebservices.getcapabilities.Operation;
055    import org.deegree.ogcwebservices.getcapabilities.OperationsMetadata;
056    import org.deegree.ogcwebservices.getcapabilities.ServiceIdentification;
057    import org.deegree.ogcwebservices.getcapabilities.ServiceProvider;
058    import org.deegree.owscommon.OWSCommonCapabilitiesDocument;
059    import org.deegree.owscommon.OWSDomainType;
060    import org.w3c.dom.Element;
061    
062    /**
063     * factory class for creating OGC CSW 2.0.2 compliant XML representation of a CatalogueCapabilities
064     * instance
065     *
066     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
067     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
068     * @author last edited by: $Author: apoth $
069     *
070     * @version $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
071     *
072     */
073    public class XMLFactory_2_0_2 extends XMLFactory_2_0_0 {
074    
075        /**
076         * Exports a <code>CatalogCapabilities</code> instance as an <code>XmlDocument</code>.
077         *
078         * @param capabilities
079         * @param sections
080         *            names of sections to be exported, may contain 'All'
081         * @return DOM representation of the <code>CatalogCapabilities</code>
082         * @throws IOException
083         *             if XML template could not be loaded
084         */
085        public static CatalogueCapabilitiesDocument export( CatalogueCapabilities capabilities, String[] sections )
086                                throws IOException {
087    
088            // no sections specified? => return all sections
089            if ( sections == null || sections.length == 0 ) {
090                sections = new String[] { OWSCommonCapabilitiesDocument.ALL_NAME };
091            }
092    
093            // build HashSet with the names of the sections to be exported
094            Set<String> sectionSet = new HashSet<String>();
095            for ( int i = 0; i < sections.length; i++ ) {
096                sectionSet.add( sections[i] );
097            }
098    
099            CatalogueCapabilitiesDocument_2_0_2 doc = new CatalogueCapabilitiesDocument_2_0_2();
100            try {
101                doc.createEmptyDocument();
102                Element root = doc.getRootElement();
103    
104                // 'ServiceIdentification'-section
105                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
106                     || sectionSet.contains( OWSCommonCapabilitiesDocument.SERVICE_IDENTIFICATION_NAME ) ) {
107                    ServiceIdentification serviceIdentification = capabilities.getServiceIdentification();
108                    if ( serviceIdentification != null ) {
109                        appendServiceIdentification( root, serviceIdentification );
110                    }
111                }
112    
113                // 'ServiceProvider'-section
114                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
115                     || sectionSet.contains( OWSCommonCapabilitiesDocument.SERVICE_PROVIDER_NAME ) ) {
116                    ServiceProvider serviceProvider = capabilities.getServiceProvider();
117                    if ( serviceProvider != null ) {
118                        appendServiceProvider( root, capabilities.getServiceProvider() );
119                    }
120                }
121    
122                // 'OperationsMetadata'-section
123                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
124                     || sectionSet.contains( OWSCommonCapabilitiesDocument.OPERATIONS_METADATA_NAME ) ) {
125                    OperationsMetadata operationsMetadata = capabilities.getOperationsMetadata();
126                    if ( operationsMetadata != null ) {
127                        appendOperationsMetadata( root, operationsMetadata, true );
128                    }
129                }
130    
131                // 'Contents'-section
132                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
133                     || sectionSet.contains( OWSCommonCapabilitiesDocument.CONTENTS_NAME ) ) {
134                    Contents contents = capabilities.getContents();
135                    if ( contents != null ) {
136                        // appendContents(root, contents);
137                    }
138                }
139    
140                // 'Filter_Capabilities'-section
141                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
142                     || sectionSet.contains( CatalogueCapabilitiesDocument.FILTER_CAPABILITIES_NAME ) ) {
143                    FilterCapabilities filterCapabilities = capabilities.getFilterCapabilities();
144                    if ( filterCapabilities != null ) {
145                        org.deegree.model.filterencoding.XMLFactory.appendFilterCapabilities110( root, filterCapabilities );
146                    }
147                }
148    
149            } catch ( Exception e ) {
150                e.printStackTrace();
151                LOG.logError( e.getMessage(), e );
152            }
153            return doc;
154        }
155    
156        /**
157         * Appends the DOM representation of the <code>OperationsMetadata</code>- section to the
158         * passed <code>Element</code>.
159         *
160         * @param root
161         */
162        protected static void appendOperationsMetadata( Element root, OperationsMetadata operationsMetadata,
163                                                        boolean capabilities ) {
164    
165            // 'ows:OperationsMetadata'-element
166            Element operationsMetadataNode = XMLTools.appendElement( root, OWSNS, "ows:OperationsMetadata" );
167    
168            // append all Operations
169            Operation[] operations = operationsMetadata.getOperations();
170            for ( int i = 0; i < operations.length; i++ ) {
171                Operation operation = operations[i];
172    
173                // 'ows:Operation'-element
174                Element operationElement = XMLTools.appendElement( operationsMetadataNode, OWSNS, "ows:Operation" );
175    
176                operationElement.setAttribute( "name", operation.getName() );
177    
178                // 'ows:DCP'-elements
179                DCPType[] dcps = operation.getDCPs();
180                for ( int j = 0; j < dcps.length; j++ ) {
181                    appendDCP( operationElement, dcps[j] );
182                }
183    
184                // 'ows:Parameter'-elements
185                OWSDomainType[] parameters = operation.getParameters();
186                for ( int j = 0; j < parameters.length; j++ ) {
187                    if ( parameters[j] instanceof CatalogueOutputSchemaParameter ) {
188                        appendParameter( operationElement, (CatalogueOutputSchemaParameter) parameters[j], "ows:Parameter",
189                                         capabilities );
190                    } else if ( parameters[j] instanceof CatalogueTypeNameSchemaParameter ) {
191                        appendParameter( operationElement, (CatalogueTypeNameSchemaParameter) parameters[j],
192                                         "ows:Parameter", capabilities );
193                    } else {
194                        appendParameter( operationElement, parameters[j], "ows:Parameter" );
195                    }
196                }
197    
198                // 'ows:Metadata'-elements
199                Object[] metadata = operation.getMetadata();
200                if ( metadata != null ) {
201                    for ( int j = 0; j < metadata.length; j++ ) {
202                        appendMetadata( operationElement, metadata[j] );
203                    }
204                }
205            }
206    
207            // append general parameters
208            OWSDomainType[] parameters = operationsMetadata.getParameter();
209            for ( int i = 0; i < parameters.length; i++ ) {
210                appendParameter( operationsMetadataNode, parameters[i], "ows:Parameter" );
211            }
212    
213            // append constraints
214            OWSDomainType[] constraints = operationsMetadata.getConstraints();
215            for ( int i = 0; i < constraints.length; i++ ) {
216                appendParameter( operationsMetadataNode, constraints[i], "ows:Constraint" );
217            }
218        }
219    
220        /**
221         * Appends the DOM representation of a <code>OWSDomainType</code> instance to the passed
222         * <code>Element</code>.
223         *
224         * @param root
225         * @param parameter
226         */
227        protected static void appendParameter( Element root, CatalogueOutputSchemaParameter parameter, String elementName,
228                                               boolean capabilities ) {
229    
230            // 'ows:Parameter'-element
231            Element parameterNode = XMLTools.appendElement( root, OWSNS, elementName );
232            parameterNode.setAttribute( "name", parameter.getName() );
233    
234            // 'ows:Value'-elements
235            CatalogueOutputSchemaValue[] values = parameter.getSpecializedValues();
236            for ( int i = 0; i < values.length; i++ ) {
237                Element elem = XMLTools.appendElement( parameterNode, OWSNS, "ows:Value", values[i].getValue() );
238                if ( !capabilities ) {
239                    elem.setAttribute( "deegree:input", values[i].getInXsl() );
240                    elem.setAttribute( "deegree:ouput", values[i].getOutXsl() );
241                }
242            }
243        }
244    
245        /**
246         * Appends the DOM representation of a <code>OWSDomainType</code> instance to the passed
247         * <code>Element</code>.
248         *
249         * @param root
250         * @param parameter
251         */
252        protected static void appendParameter( Element root, CatalogueTypeNameSchemaParameter parameter,
253                                               String elementName, boolean capabilities ) {
254    
255            // 'ows:Parameter'-element
256            Element parameterNode = XMLTools.appendElement( root, OWSNS, elementName );
257            parameterNode.setAttribute( "name", parameter.getName() );
258    
259            // 'ows:Value'-elements
260            CatalogueTypeNameSchemaValue[] values = parameter.getSpecializedValues();
261            for ( int i = 0; i < values.length; i++ ) {
262                Element elem = XMLTools.appendElement( parameterNode, OWSNS, "ows:Value", values[i].getValue() );
263                if ( !capabilities ) {
264                    elem.setAttribute( "deegree:schema", values[i].getSchema() );
265                }
266            }
267        }
268    
269        /**
270         * Appends the DOM representation of the <code>ServiceIdentification</code>- section to the
271         * passed <code>Element</code>.
272         *
273         * @param root
274         * @param serviceIdentification
275         * @throws XMLParsingException
276         */
277        protected static void appendServiceIdentification( Element root, ServiceIdentification serviceIdentification ) {
278    
279            // 'ServiceIdentification'-element
280            Element serviceIdentificationNode = XMLTools.appendElement( root, OWSNS, "ows:ServiceIdentification" );
281    
282            // 'Title'-element
283            XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:Title", serviceIdentification.getTitle() );
284    
285            // 'Abstract'-element
286            if ( serviceIdentification.getAbstract() != null ) {
287                XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:Abstract",
288                                        serviceIdentification.getAbstract() );
289            }
290    
291            // 'Keywords'-element
292            appendOWSKeywords( serviceIdentificationNode, serviceIdentification.getKeywords() );
293    
294            // 'ServiceType'-element
295            XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:ServiceType",
296                                    serviceIdentification.getServiceType().getCode() );
297    
298            // 'ServiceTypeVersion'-elements
299            String[] versions = serviceIdentification.getServiceTypeVersions();
300            for ( int i = 0; i < versions.length; i++ ) {
301                XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:ServiceTypeVersion", versions[i] );
302            }
303    
304            // 'Fees'-element
305            XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:Fees", serviceIdentification.getFees() );
306    
307            // 'AccessConstraints'-element
308            String[] constraints = serviceIdentification.getAccessConstraints();
309            if ( constraints != null ) {
310                for ( int i = 0; i < constraints.length; i++ ) {
311                    XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:AccessConstraints", constraints[i] );
312                }
313            }
314        }
315    
316    }