001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/ogcwebservices/csw/XMLFactory.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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: 7904 $, $Date: 2007-08-06 10:26:11 +0200 (Mo, 06 Aug 2007) $
086     * 
087     */
088    public class XMLFactory 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                e.printStackTrace();
146                LOG.logError( e.getMessage(), e );
147            } catch ( XMLParsingException e ) {
148                e.printStackTrace();
149                LOG.logError( e.getMessage(), e );
150            } catch ( Exception e ) {
151                e.printStackTrace();
152                LOG.logError( e.getMessage(), e );
153            }
154            return configurationDocument;
155        }
156    
157        /**
158         * @param root
159         * @param ebrimming
160         */
161        private static void appendEBRimCapabilities( Element root, EBRIMCapabilities ebrimming ) {
162            Element serviceFeatures = XMLTools.appendElement( root, CommonNamespaces.WRS_EBRIMNS,
163                                                              CommonNamespaces.WRS_EBRIM_PREFIX + ":ServiceFeatures" );
164            Map<URI, CSWFeature> features = ebrimming.getServiceFeatures();
165            Set<URI> keys = features.keySet();
166            for ( URI key : keys ) {
167                Element feature = XMLTools.appendElement( serviceFeatures, CommonNamespaces.WRS_EBRIMNS,
168                                                          CommonNamespaces.WRS_EBRIM_PREFIX + ":feature" );
169                feature.setAttribute( "name", key.toString() );
170                CSWFeature featureBean = features.get( key );
171                appendProperties( feature, featureBean.getAllProperties() );
172            }
173            Element serviceProperties = XMLTools.appendElement( root, CommonNamespaces.WRS_EBRIMNS,
174                                                                CommonNamespaces.WRS_EBRIM_PREFIX + ":ServiceProperties" );
175            appendProperties( serviceProperties, ebrimming.getServiceProperties() );
176            Element wsdl_services = XMLTools.appendElement( root, CommonNamespaces.WRS_EBRIMNS,
177                                                            CommonNamespaces.WRS_EBRIM_PREFIX + ":WSDL-services" );
178            appendSimpleLinkAttributes( wsdl_services, ebrimming.getWsdl_services() );
179    
180        }
181    
182        private static void appendProperties( Element theElement, Map<URI, List<String>> properties ) {
183            Set<URI> keys = properties.keySet();
184            for ( URI key : keys ) {
185                Element property = XMLTools.appendElement( theElement, CommonNamespaces.WRS_EBRIMNS,
186                                                           CommonNamespaces.WRS_EBRIM_PREFIX + ":property" );
187                property.setAttribute( "name", key.toString() );
188                List<String> values = properties.get( key );
189                for ( String value : values ) {
190                    XMLTools.appendElement( property, CommonNamespaces.WRS_EBRIMNS, CommonNamespaces.WRS_EBRIM_PREFIX
191                                                                                    + ":value", value );
192                }
193            }
194        }
195    
196        /**
197         * Exports a <code>CatalogCapabilities</code> instance as an <code>XmlDocument</code>.
198         * 
199         * @param capabilities
200         * @param sections
201         *            names of sections to be exported, may contain 'All'
202         * @return DOM representation of the <code>CatalogCapabilities</code>
203         * @throws IOException
204         *             if XML template could not be loaded
205         */
206        public static CatalogueCapabilitiesDocument export( CatalogueCapabilities capabilities, String[] sections )
207                                throws IOException {
208    
209            // no sections specified? => return all sections
210            if ( sections == null || sections.length == 0 ) {
211                sections = new String[] { OWSCommonCapabilitiesDocument.ALL_NAME };
212            }
213    
214            // build HashSet with the names of the sections to be exported
215            Set<String> sectionSet = new HashSet<String>();
216            for ( int i = 0; i < sections.length; i++ ) {
217                sectionSet.add( sections[i] );
218            }
219    
220            CatalogueCapabilitiesDocument doc = new CatalogueCapabilitiesDocument();
221            try {
222                doc.createEmptyDocument();
223                Element root = doc.getRootElement();
224    
225                // 'ServiceIdentification'-section
226                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
227                     || sectionSet.contains( OWSCommonCapabilitiesDocument.SERVICE_IDENTIFICATION_NAME ) ) {
228                    ServiceIdentification serviceIdentification = capabilities.getServiceIdentification();
229                    if ( serviceIdentification != null ) {
230                        appendServiceIdentification( root, serviceIdentification );
231                    }
232                }
233    
234                // 'ServiceProvider'-section
235                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
236                     || sectionSet.contains( OWSCommonCapabilitiesDocument.SERVICE_PROVIDER_NAME ) ) {
237                    ServiceProvider serviceProvider = capabilities.getServiceProvider();
238                    if ( serviceProvider != null ) {
239                        appendServiceProvider( root, capabilities.getServiceProvider() );
240                    }
241                }
242    
243                // 'OperationsMetadata'-section
244                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
245                     || sectionSet.contains( OWSCommonCapabilitiesDocument.OPERATIONS_METADATA_NAME ) ) {
246                    OperationsMetadata operationsMetadata = capabilities.getOperationsMetadata();
247                    if ( operationsMetadata != null ) {
248                        appendOperationsMetadata( root, operationsMetadata, true );
249                    }
250                }
251    
252                // 'Contents'-section
253                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
254                     || sectionSet.contains( OWSCommonCapabilitiesDocument.CONTENTS_NAME ) ) {
255                    Contents contents = capabilities.getContents();
256                    if ( contents != null ) {
257                        // appendContents(root, contents);
258                    }
259                }
260    
261                // 'Filter_Capabilities'-section
262                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
263                     || sectionSet.contains( CatalogueCapabilitiesDocument.FILTER_CAPABILITIES_NAME ) ) {
264                    FilterCapabilities filterCapabilities = capabilities.getFilterCapabilities();
265                    if ( filterCapabilities != null ) {
266                        org.deegree.model.filterencoding.XMLFactory.appendFilterCapabilities100( root, filterCapabilities );
267                    }
268                }
269                // 'EBRIM_Capabilities'-section
270                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
271                     || sectionSet.contains( CatalogueCapabilitiesDocument.EBRIM_CAPABILITIES_NAME ) ) {
272                    EBRIMCapabilities ebrimming = capabilities.getEbrimCaps();
273                    if ( ebrimming != null ) {
274                        appendEBRimCapabilities( root, ebrimming );
275                    }
276                }
277            } catch ( SAXException e ) {
278                LOG.logError( e.getMessage(), e );
279            }
280            return doc;
281        }
282    
283        /**
284         * Appends the DOM representation of the <code>deegreeParams</code>- section to the passed
285         * <code>Element</code>.
286         * 
287         * @param root
288         * @param deegreeParams
289         * @throws XMLParsingException
290         */
291        private static void appendDeegreeParams( Element root, CatalogueDeegreeParams deegreeParams )
292                                throws XMLParsingException {
293    
294            Element deegreeParamsNode = XMLTools.getRequiredChildElement( "deegreeParams", DEEGREECSW, root );
295    
296            // 'DefaultOnlineResource'-element
297            appendOnlineResource( deegreeParamsNode, "deegree:DefaultOnlineResource",
298                                  deegreeParams.getDefaultOnlineResource(), DEEGREECSW );
299    
300            // 'CacheSize'-element
301            XMLTools.appendElement( deegreeParamsNode, DEEGREECSW, "deegree:CacheSize", "" + deegreeParams.getCacheSize() );
302    
303            // 'RequestTimeLimit'-element
304            XMLTools.appendElement( deegreeParamsNode, DEEGREECSW, "deegree:RequestTimeLimit",
305                                    "" + deegreeParams.getRequestTimeLimit() );
306    
307            // 'Encoding'-element
308            XMLTools.appendElement( deegreeParamsNode, DEEGREECSW, "deegree:Encoding", deegreeParams.getCharacterSet() );
309    
310            // 'WFSResource'-element
311            Element wfsResourceElement = XMLTools.appendElement( deegreeParamsNode, DEEGREECSW, "deegree:WFSResource" );
312    
313            appendSimpleLinkAttributes( wfsResourceElement, deegreeParams.getWfsResource() );
314    
315            // 'CatalogAddresses'-element
316            OnlineResource[] catalogAddresses = deegreeParams.getCatalogAddresses();
317            if ( catalogAddresses.length > 0 ) {
318                Element catalogAddressesNode = XMLTools.appendElement( deegreeParamsNode, DEEGREECSW,
319                                                                       "deegree:CatalogAddresses" );
320                for ( int i = 0; i < catalogAddresses.length; i++ ) {
321                    appendOnlineResource( catalogAddressesNode, "deegree:CatalogAddress", catalogAddresses[i], DEEGREECSW );
322                }
323            }
324    
325            // 'HarvestRepository'-element
326            if ( deegreeParams.getHarvestRepository() != null ) {
327                JDBCConnection connection = deegreeParams.getHarvestRepository();
328                Element harvestRepositoryNode = XMLTools.appendElement( deegreeParamsNode, DEEGREECSW,
329                                                                        "deegree:HarvestRepository" );
330                // 'Connection'-element
331                Element connectionNode = XMLTools.appendElement( harvestRepositoryNode, DEEGREECSW, "deegree:Connection" );
332                // 'Driver'-element
333                XMLTools.appendElement( connectionNode, DEEGREECSW, "deegree:Driver", connection.getDriver() );
334                // 'Logon'-element
335                XMLTools.appendElement( connectionNode, DEEGREECSW, "deegree:Logon", connection.getURL() );
336                // 'User'-element
337                XMLTools.appendElement( connectionNode, DEEGREECSW, "deegree:User", connection.getUser() );
338                // 'Password'-element
339                XMLTools.appendElement( connectionNode, DEEGREECSW, "deegree:Password", connection.getPassword() );
340            }
341        }
342    
343        /**
344         * Appends the DOM representation of the <code>OperationsMetadata</code>- section to the
345         * passed <code>Element</code>.
346         * 
347         * @param root
348         */
349        protected static void appendOperationsMetadata( Element root, OperationsMetadata operationsMetadata,
350                                                        boolean capabilities ) {
351    
352            // 'ows:OperationsMetadata'-element
353            Element operationsMetadataNode = XMLTools.appendElement( root, OWSNS, "ows:OperationsMetadata" );
354    
355            // append all Operations
356            Operation[] operations = operationsMetadata.getOperations();
357            for ( int i = 0; i < operations.length; i++ ) {
358                Operation operation = operations[i];
359    
360                // 'ows:Operation'-element
361                Element operationElement = XMLTools.appendElement( operationsMetadataNode, OWSNS, "ows:Operation" );
362    
363                operationElement.setAttribute( "name", operation.getName() );
364    
365                // 'ows:DCP'-elements
366                DCPType[] dcps = operation.getDCPs();
367                for ( int j = 0; j < dcps.length; j++ ) {
368                    appendDCP( operationElement, dcps[j] );
369                }
370    
371                // 'ows:Parameter'-elements
372                OWSDomainType[] parameters = operation.getParameters();
373                for ( int j = 0; j < parameters.length; j++ ) {
374                    if ( parameters[j] instanceof CatalogueOutputSchemaParameter ) {
375                        appendParameter( operationElement, (CatalogueOutputSchemaParameter) parameters[j], "ows:Parameter",
376                                         capabilities );
377                    } else if ( parameters[j] instanceof CatalogueTypeNameSchemaParameter ) {
378                        appendParameter( operationElement, (CatalogueTypeNameSchemaParameter) parameters[j],
379                                         "ows:Parameter", capabilities );
380                    } else {
381                        appendParameter( operationElement, parameters[j], "ows:Parameter" );
382                    }
383                }
384    
385                // 'ows:Metadata'-elements
386                Object[] metadata = operation.getMetadata();
387                if ( metadata != null ) {
388                    for ( int j = 0; j < metadata.length; j++ ) {
389                        appendMetadata( operationElement, metadata[j] );
390                    }
391                }
392            }
393    
394            // append general parameters
395            OWSDomainType[] parameters = operationsMetadata.getParameter();
396            for ( int i = 0; i < parameters.length; i++ ) {
397                appendParameter( operationsMetadataNode, parameters[i], "ows:Parameter" );
398            }
399    
400            // append constraints
401            OWSDomainType[] constraints = operationsMetadata.getConstraints();
402            for ( int i = 0; i < constraints.length; i++ ) {
403                appendParameter( operationsMetadataNode, constraints[i], "ows:Constraint" );
404            }
405        }
406    
407        /**
408         * Appends the DOM representation of a <code>OWSDomainType</code> instance to the passed
409         * <code>Element</code>.
410         * 
411         * @param root
412         * @param parameter
413         */
414        protected static void appendParameter( Element root, CatalogueOutputSchemaParameter parameter, String elementName,
415                                               boolean capabilities ) {
416    
417            // 'ows:Parameter'-element
418            Element parameterNode = XMLTools.appendElement( root, OWSNS, elementName );
419            parameterNode.setAttribute( "name", parameter.getName() );
420    
421            // 'ows:Value'-elements
422            CatalogueOutputSchemaValue[] values = parameter.getSpecializedValues();
423            for ( int i = 0; i < values.length; i++ ) {
424                Element elem = XMLTools.appendElement( parameterNode, OWSNS, "ows:Value", values[i].getValue() );
425                if ( !capabilities ) {
426                    elem.setAttribute( "deegree:input", values[i].getInXsl() );
427                    elem.setAttribute( "deegree:ouput", values[i].getOutXsl() );
428                }
429            }
430        }
431    
432        /**
433         * Appends the DOM representation of a <code>OWSDomainType</code> instance to the passed
434         * <code>Element</code>.
435         * 
436         * @param root
437         * @param parameter
438         */
439        protected static void appendParameter( Element root, CatalogueTypeNameSchemaParameter parameter,
440                                               String elementName, boolean capabilities ) {
441    
442            // 'ows:Parameter'-element
443            Element parameterNode = XMLTools.appendElement( root, OWSNS, elementName );
444            parameterNode.setAttribute( "name", parameter.getName() );
445    
446            // 'ows:Value'-elements
447            CatalogueTypeNameSchemaValue[] values = parameter.getSpecializedValues();
448            for ( int i = 0; i < values.length; i++ ) {
449                Element elem = XMLTools.appendElement( parameterNode, OWSNS, "ows:Value", values[i].getValue() );
450                if ( !capabilities ) {
451                    elem.setAttribute( "deegree:schema", values[i].getSchema() );
452                }
453            }
454        }
455    
456    }