001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/csw/XMLFactory_2_0_0.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.net.URI;
040    import java.util.HashSet;
041    import java.util.List;
042    import java.util.Map;
043    import java.util.Set;
044    
045    import org.deegree.framework.xml.XMLParsingException;
046    import org.deegree.framework.xml.XMLTools;
047    import org.deegree.io.JDBCConnection;
048    import org.deegree.model.filterencoding.capabilities.FilterCapabilities;
049    import org.deegree.model.metadata.iso19115.OnlineResource;
050    import org.deegree.ogcbase.CommonNamespaces;
051    import org.deegree.ogcwebservices.csw.capabilities.CSWFeature;
052    import org.deegree.ogcwebservices.csw.capabilities.CatalogueCapabilities;
053    import org.deegree.ogcwebservices.csw.capabilities.CatalogueCapabilitiesDocument;
054    import org.deegree.ogcwebservices.csw.capabilities.EBRIMCapabilities;
055    import org.deegree.ogcwebservices.csw.configuration.CatalogueConfiguration;
056    import org.deegree.ogcwebservices.csw.configuration.CatalogueConfigurationDocument;
057    import org.deegree.ogcwebservices.csw.configuration.CatalogueDeegreeParams;
058    import org.deegree.ogcwebservices.csw.configuration.CatalogueOutputSchemaParameter;
059    import org.deegree.ogcwebservices.csw.configuration.CatalogueOutputSchemaValue;
060    import org.deegree.ogcwebservices.csw.configuration.CatalogueTypeNameSchemaParameter;
061    import org.deegree.ogcwebservices.csw.configuration.CatalogueTypeNameSchemaValue;
062    import org.deegree.ogcwebservices.getcapabilities.Contents;
063    import org.deegree.ogcwebservices.getcapabilities.DCPType;
064    import org.deegree.ogcwebservices.getcapabilities.Operation;
065    import org.deegree.ogcwebservices.getcapabilities.OperationsMetadata;
066    import org.deegree.ogcwebservices.getcapabilities.ServiceIdentification;
067    import org.deegree.ogcwebservices.getcapabilities.ServiceProvider;
068    import org.deegree.owscommon.OWSCommonCapabilitiesDocument;
069    import org.deegree.owscommon.OWSDomainType;
070    import org.w3c.dom.Element;
071    import org.xml.sax.SAXException;
072    
073    /**
074     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
075     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
076     * @author last edited by: $Author: mschneider $
077     *
078     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
079     *
080     */
081    public class XMLFactory_2_0_0 extends org.deegree.owscommon.XMLFactory {
082    
083        protected static final URI DEEGREECSW = CommonNamespaces.DEEGREECSW;
084    
085        /**
086         * Exports a <code>CatalogConfiguration</code> instance to an <code>XmlDocument</code>.
087         *
088         * @param configuration
089         * @return DOM representation of the <code>CatalogConfiguration</code>
090         */
091        public static CatalogueConfigurationDocument export( CatalogueConfiguration configuration ) {
092            CatalogueConfigurationDocument configurationDocument = new CatalogueConfigurationDocument();
093            try {
094                configurationDocument.createEmptyDocument();
095                Element root = configurationDocument.getRootElement();
096    
097                // 'deegreeParams'-section
098                CatalogueDeegreeParams deegreeParams = configuration.getDeegreeParams();
099                if ( deegreeParams != null ) {
100                    appendDeegreeParams( root, configuration.getDeegreeParams() );
101                }
102    
103                // 'ServiceIdentification'-section
104                ServiceIdentification serviceIdentification = configuration.getServiceIdentification();
105                if ( serviceIdentification != null ) {
106                    appendServiceIdentification( root, serviceIdentification );
107                }
108    
109                // 'ServiceProvider'-section
110                ServiceProvider serviceProvider = configuration.getServiceProvider();
111                if ( serviceProvider != null ) {
112                    appendServiceProvider( root, configuration.getServiceProvider() );
113                }
114    
115                // 'OperationsMetadata'-section
116                OperationsMetadata operationsMetadata = configuration.getOperationsMetadata();
117                if ( operationsMetadata != null ) {
118                    appendOperationsMetadata( root, operationsMetadata );
119                }
120    
121                // 'Contents'-section
122                Contents contents = configuration.getContents();
123                if ( contents != null ) {
124                    // appendContents(root, contents);
125                }
126    
127                // 'Filter_Capabilities'-section
128                FilterCapabilities filterCapabilities = configuration.getFilterCapabilities();
129                if ( filterCapabilities != null ) {
130                    org.deegree.model.filterencoding.XMLFactory.appendFilterCapabilities100( root, filterCapabilities );
131                }
132    
133                EBRIMCapabilities ebrimming = configuration.getEbrimCaps();
134                if ( ebrimming != null ) {
135                    appendEBRimCapabilities( root, ebrimming );
136                }
137            } catch ( SAXException e ) {
138                LOG.logError( e.getMessage(), e );
139            } catch ( XMLParsingException e ) {
140                LOG.logError( e.getMessage(), e );
141            } catch ( Exception e ) {
142                LOG.logError( e.getMessage(), e );
143            }
144            return configurationDocument;
145        }
146    
147        /**
148         * @param root
149         * @param ebrimming
150         */
151        private static void appendEBRimCapabilities( Element root, EBRIMCapabilities ebrimming ) {
152            Element serviceFeatures = XMLTools.appendElement( root, CommonNamespaces.WRS_EBRIMNS,
153                                                              CommonNamespaces.WRS_EBRIM_PREFIX + ":ServiceFeatures" );
154            Map<URI, CSWFeature> features = ebrimming.getServiceFeatures();
155            Set<URI> keys = features.keySet();
156            for ( URI key : keys ) {
157                Element feature = XMLTools.appendElement( serviceFeatures, CommonNamespaces.WRS_EBRIMNS,
158                                                          CommonNamespaces.WRS_EBRIM_PREFIX + ":feature" );
159                feature.setAttribute( "name", key.toString() );
160                CSWFeature featureBean = features.get( key );
161                appendProperties( feature, featureBean.getAllProperties() );
162            }
163            Element serviceProperties = XMLTools.appendElement( root, CommonNamespaces.WRS_EBRIMNS,
164                                                                CommonNamespaces.WRS_EBRIM_PREFIX + ":ServiceProperties" );
165            appendProperties( serviceProperties, ebrimming.getServiceProperties() );
166            Element wsdl_services = XMLTools.appendElement( root, CommonNamespaces.WRS_EBRIMNS,
167                                                            CommonNamespaces.WRS_EBRIM_PREFIX + ":WSDL-services" );
168            appendSimpleLinkAttributes( wsdl_services, ebrimming.getWsdl_services() );
169    
170        }
171    
172        private static void appendProperties( Element theElement, Map<URI, List<String>> properties ) {
173            Set<URI> keys = properties.keySet();
174            for ( URI key : keys ) {
175                Element property = XMLTools.appendElement( theElement, CommonNamespaces.WRS_EBRIMNS,
176                                                           CommonNamespaces.WRS_EBRIM_PREFIX + ":property" );
177                property.setAttribute( "name", key.toString() );
178                List<String> values = properties.get( key );
179                for ( String value : values ) {
180                    XMLTools.appendElement( property, CommonNamespaces.WRS_EBRIMNS, CommonNamespaces.WRS_EBRIM_PREFIX
181                                                                                    + ":value", value );
182                }
183            }
184        }
185    
186        /**
187         * Exports a <code>CatalogCapabilities</code> instance as an <code>XmlDocument</code>.
188         *
189         * @param capabilities
190         * @param sections
191         *            names of sections to be exported, may contain 'All'
192         * @return DOM representation of the <code>CatalogCapabilities</code>
193         * @throws IOException
194         *             if XML template could not be loaded
195         */
196        public static CatalogueCapabilitiesDocument export( CatalogueCapabilities capabilities, String[] sections )
197                                throws IOException {
198    
199            // no sections specified? => return all sections
200            if ( sections == null || sections.length == 0 ) {
201                sections = new String[] { OWSCommonCapabilitiesDocument.ALL_NAME };
202            }
203    
204            // build HashSet with the names of the sections to be exported
205            Set<String> sectionSet = new HashSet<String>();
206            for ( int i = 0; i < sections.length; i++ ) {
207                sectionSet.add( sections[i] );
208            }
209    
210            CatalogueCapabilitiesDocument doc = new CatalogueCapabilitiesDocument();
211            try {
212                doc.createEmptyDocument();
213                Element root = doc.getRootElement();
214    
215                // 'ServiceIdentification'-section
216                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
217                     || sectionSet.contains( OWSCommonCapabilitiesDocument.SERVICE_IDENTIFICATION_NAME ) ) {
218                    ServiceIdentification serviceIdentification = capabilities.getServiceIdentification();
219                    if ( serviceIdentification != null ) {
220                        appendServiceIdentification( root, serviceIdentification );
221                    }
222                }
223    
224                // 'ServiceProvider'-section
225                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
226                     || sectionSet.contains( OWSCommonCapabilitiesDocument.SERVICE_PROVIDER_NAME ) ) {
227                    ServiceProvider serviceProvider = capabilities.getServiceProvider();
228                    if ( serviceProvider != null ) {
229                        appendServiceProvider( root, capabilities.getServiceProvider() );
230                    }
231                }
232    
233                // 'OperationsMetadata'-section
234                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
235                     || sectionSet.contains( OWSCommonCapabilitiesDocument.OPERATIONS_METADATA_NAME ) ) {
236                    OperationsMetadata operationsMetadata = capabilities.getOperationsMetadata();
237                    if ( operationsMetadata != null ) {
238                        appendOperationsMetadata( root, operationsMetadata, true );
239                    }
240                }
241    
242                // 'Contents'-section
243                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
244                     || sectionSet.contains( OWSCommonCapabilitiesDocument.CONTENTS_NAME ) ) {
245                    Contents contents = capabilities.getContents();
246                    if ( contents != null ) {
247                        // appendContents(root, contents);
248                    }
249                }
250    
251                // 'Filter_Capabilities'-section
252                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
253                     || sectionSet.contains( CatalogueCapabilitiesDocument.FILTER_CAPABILITIES_NAME ) ) {
254                    FilterCapabilities filterCapabilities = capabilities.getFilterCapabilities();
255                    if ( filterCapabilities != null ) {
256                        org.deegree.model.filterencoding.XMLFactory.appendFilterCapabilities100( root, filterCapabilities );
257                    }
258                }
259                // 'EBRIM_Capabilities'-section
260                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
261                     || sectionSet.contains( CatalogueCapabilitiesDocument.EBRIM_CAPABILITIES_NAME ) ) {
262                    EBRIMCapabilities ebrimming = capabilities.getEbrimCaps();
263                    if ( ebrimming != null ) {
264                        appendEBRimCapabilities( root, ebrimming );
265                    }
266                }
267            } catch ( SAXException e ) {
268                LOG.logError( e.getMessage(), e );
269            }
270            return doc;
271        }
272    
273        /**
274         * Appends the DOM representation of the <code>deegreeParams</code>- section to the passed
275         * <code>Element</code>.
276         *
277         * @param root
278         * @param deegreeParams
279         * @throws XMLParsingException
280         */
281        private static void appendDeegreeParams( Element root, CatalogueDeegreeParams deegreeParams )
282                                throws XMLParsingException {
283    
284            Element deegreeParamsNode = XMLTools.getRequiredChildElement( "deegreeParams", DEEGREECSW, root );
285    
286            // 'DefaultOnlineResource'-element
287            appendOnlineResource( deegreeParamsNode, "deegree:DefaultOnlineResource",
288                                  deegreeParams.getDefaultOnlineResource(), DEEGREECSW );
289    
290            // 'CacheSize'-element
291            XMLTools.appendElement( deegreeParamsNode, DEEGREECSW, "deegree:CacheSize", "" + deegreeParams.getCacheSize() );
292    
293            // 'RequestTimeLimit'-element
294            XMLTools.appendElement( deegreeParamsNode, DEEGREECSW, "deegree:RequestTimeLimit",
295                                    "" + deegreeParams.getRequestTimeLimit() );
296    
297            // 'Encoding'-element
298            XMLTools.appendElement( deegreeParamsNode, DEEGREECSW, "deegree:Encoding", deegreeParams.getCharacterSet() );
299    
300            // 'WFSResource'-element
301            Element wfsResourceElement = XMLTools.appendElement( deegreeParamsNode, DEEGREECSW, "deegree:WFSResource" );
302    
303            appendSimpleLinkAttributes( wfsResourceElement, deegreeParams.getWfsResource() );
304    
305            // 'CatalogAddresses'-element
306            OnlineResource[] catalogAddresses = deegreeParams.getCatalogAddresses();
307            if ( catalogAddresses.length > 0 ) {
308                Element catalogAddressesNode = XMLTools.appendElement( deegreeParamsNode, DEEGREECSW,
309                                                                       "deegree:CatalogAddresses" );
310                for ( int i = 0; i < catalogAddresses.length; i++ ) {
311                    appendOnlineResource( catalogAddressesNode, "deegree:CatalogAddress", catalogAddresses[i], DEEGREECSW );
312                }
313            }
314    
315            // 'HarvestRepository'-element
316            if ( deegreeParams.getHarvestRepository() != null ) {
317                JDBCConnection connection = deegreeParams.getHarvestRepository();
318                Element harvestRepositoryNode = XMLTools.appendElement( deegreeParamsNode, DEEGREECSW,
319                                                                        "deegree:HarvestRepository" );
320                // 'Connection'-element
321                Element connectionNode = XMLTools.appendElement( harvestRepositoryNode, DEEGREECSW, "deegree:Connection" );
322                // 'Driver'-element
323                XMLTools.appendElement( connectionNode, DEEGREECSW, "deegree:Driver", connection.getDriver() );
324                // 'Logon'-element
325                XMLTools.appendElement( connectionNode, DEEGREECSW, "deegree:Logon", connection.getURL() );
326                // 'User'-element
327                XMLTools.appendElement( connectionNode, DEEGREECSW, "deegree:User", connection.getUser() );
328                // 'Password'-element
329                XMLTools.appendElement( connectionNode, DEEGREECSW, "deegree:Password", connection.getPassword() );
330            }
331        }
332    
333        /**
334         * Appends the DOM representation of the <code>OperationsMetadata</code>- section to the
335         * passed <code>Element</code>.
336         *
337         * @param root
338         */
339        protected static void appendOperationsMetadata( Element root, OperationsMetadata operationsMetadata,
340                                                        boolean capabilities ) {
341    
342            // 'ows:OperationsMetadata'-element
343            Element operationsMetadataNode = XMLTools.appendElement( root, OWSNS, "ows:OperationsMetadata" );
344    
345            // append all Operations
346            Operation[] operations = operationsMetadata.getOperations();
347            for ( int i = 0; i < operations.length; i++ ) {
348                Operation operation = operations[i];
349    
350                // 'ows:Operation'-element
351                Element operationElement = XMLTools.appendElement( operationsMetadataNode, OWSNS, "ows:Operation" );
352    
353                operationElement.setAttribute( "name", operation.getName() );
354    
355                // 'ows:DCP'-elements
356                DCPType[] dcps = operation.getDCPs();
357                for ( int j = 0; j < dcps.length; j++ ) {
358                    appendDCP( operationElement, dcps[j] );
359                }
360    
361                // 'ows:Parameter'-elements
362                OWSDomainType[] parameters = operation.getParameters();
363                for ( int j = 0; j < parameters.length; j++ ) {
364                    if ( parameters[j] instanceof CatalogueOutputSchemaParameter ) {
365                        appendParameter( operationElement, (CatalogueOutputSchemaParameter) parameters[j], "ows:Parameter",
366                                         capabilities );
367                    } else if ( parameters[j] instanceof CatalogueTypeNameSchemaParameter ) {
368                        appendParameter( operationElement, (CatalogueTypeNameSchemaParameter) parameters[j],
369                                         "ows:Parameter", capabilities );
370                    } else {
371                        appendParameter( operationElement, parameters[j], "ows:Parameter" );
372                    }
373                }
374    
375                // 'ows:Metadata'-elements
376                Object[] metadata = operation.getMetadata();
377                if ( metadata != null ) {
378                    for ( int j = 0; j < metadata.length; j++ ) {
379                        appendMetadata( operationElement, metadata[j] );
380                    }
381                }
382            }
383    
384            // append general parameters
385            OWSDomainType[] parameters = operationsMetadata.getParameter();
386            for ( int i = 0; i < parameters.length; i++ ) {
387                appendParameter( operationsMetadataNode, parameters[i], "ows:Parameter" );
388            }
389    
390            // append constraints
391            OWSDomainType[] constraints = operationsMetadata.getConstraints();
392            for ( int i = 0; i < constraints.length; i++ ) {
393                appendParameter( operationsMetadataNode, constraints[i], "ows:Constraint" );
394            }
395        }
396    
397        /**
398         * Appends the DOM representation of a <code>OWSDomainType</code> instance to the passed
399         * <code>Element</code>.
400         *
401         * @param root
402         * @param parameter
403         */
404        protected static void appendParameter( Element root, CatalogueOutputSchemaParameter parameter, String elementName,
405                                               boolean capabilities ) {
406    
407            // 'ows:Parameter'-element
408            Element parameterNode = XMLTools.appendElement( root, OWSNS, elementName );
409            parameterNode.setAttribute( "name", parameter.getName() );
410    
411            // 'ows:Value'-elements
412            CatalogueOutputSchemaValue[] values = parameter.getSpecializedValues();
413            for ( int i = 0; i < values.length; i++ ) {
414                Element elem = XMLTools.appendElement( parameterNode, OWSNS, "ows:Value", values[i].getValue() );
415                if ( !capabilities ) {
416                    elem.setAttribute( "deegree:input", values[i].getInXsl() );
417                    elem.setAttribute( "deegree:ouput", values[i].getOutXsl() );
418                }
419            }
420        }
421    
422        /**
423         * Appends the DOM representation of a <code>OWSDomainType</code> instance to the passed
424         * <code>Element</code>.
425         *
426         * @param root
427         * @param parameter
428         */
429        protected static void appendParameter( Element root, CatalogueTypeNameSchemaParameter parameter,
430                                               String elementName, boolean capabilities ) {
431    
432            // 'ows:Parameter'-element
433            Element parameterNode = XMLTools.appendElement( root, OWSNS, elementName );
434            parameterNode.setAttribute( "name", parameter.getName() );
435    
436            // 'ows:Value'-elements
437            CatalogueTypeNameSchemaValue[] values = parameter.getSpecializedValues();
438            for ( int i = 0; i < values.length; i++ ) {
439                Element elem = XMLTools.appendElement( parameterNode, OWSNS, "ows:Value", values[i].getValue() );
440                if ( !capabilities ) {
441                    elem.setAttribute( "deegree:schema", values[i].getSchema() );
442                }
443            }
444        }
445    
446    }