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