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 instance
064     * 
065     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
066     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
067     * @author last edited by: $Author: apoth $
068     * 
069     * @version $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
070     * 
071     */
072    public class XMLFactory_2_0_2 extends XMLFactory_2_0_0 {
073    
074        /**
075         * Exports a <code>CatalogCapabilities</code> instance as an <code>XmlDocument</code>.
076         * 
077         * @param capabilities
078         * @param sections
079         *            names of sections to be exported, may contain 'All'
080         * @return DOM representation of the <code>CatalogCapabilities</code>
081         * @throws IOException
082         *             if XML template could not be loaded
083         */
084        public static CatalogueCapabilitiesDocument export( CatalogueCapabilities capabilities, String[] sections )
085                                throws IOException {
086    
087            // no sections specified? => return all sections
088            if ( sections == null || sections.length == 0 ) {
089                sections = new String[] { OWSCommonCapabilitiesDocument.ALL_NAME };
090            }
091    
092            // build HashSet with the names of the sections to be exported
093            Set<String> sectionSet = new HashSet<String>();
094            for ( int i = 0; i < sections.length; i++ ) {
095                sectionSet.add( sections[i] );
096            }
097    
098            CatalogueCapabilitiesDocument_2_0_2 doc = new CatalogueCapabilitiesDocument_2_0_2();
099            try {
100                doc.createEmptyDocument();
101                Element root = doc.getRootElement();
102    
103                // 'ServiceIdentification'-section
104                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
105                     || sectionSet.contains( OWSCommonCapabilitiesDocument.SERVICE_IDENTIFICATION_NAME ) ) {
106                    ServiceIdentification serviceIdentification = capabilities.getServiceIdentification();
107                    if ( serviceIdentification != null ) {
108                        appendServiceIdentification( root, serviceIdentification );
109                    }
110                }
111    
112                // 'ServiceProvider'-section
113                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
114                     || sectionSet.contains( OWSCommonCapabilitiesDocument.SERVICE_PROVIDER_NAME ) ) {
115                    ServiceProvider serviceProvider = capabilities.getServiceProvider();
116                    if ( serviceProvider != null ) {
117                        appendServiceProvider( root, capabilities.getServiceProvider() );
118                    }
119                }
120    
121                // 'OperationsMetadata'-section
122                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
123                     || sectionSet.contains( OWSCommonCapabilitiesDocument.OPERATIONS_METADATA_NAME ) ) {
124                    OperationsMetadata operationsMetadata = capabilities.getOperationsMetadata();
125                    if ( operationsMetadata != null ) {
126                        appendOperationsMetadata( root, operationsMetadata, true );
127                    }
128                }
129    
130                // 'Contents'-section
131                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
132                     || sectionSet.contains( OWSCommonCapabilitiesDocument.CONTENTS_NAME ) ) {
133                    Contents contents = capabilities.getContents();
134                    if ( contents != null ) {
135                        // appendContents(root, contents);
136                    }
137                }
138    
139                // 'Filter_Capabilities'-section
140                if ( sectionSet.contains( OWSCommonCapabilitiesDocument.ALL_NAME )
141                     || sectionSet.contains( CatalogueCapabilitiesDocument.FILTER_CAPABILITIES_NAME )
142                     || sectionSet.contains( "Filter_Capabilities" ) ) {
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 passed <code>Element</code>
158         * .
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 <code>Element</code>.
222         * 
223         * @param root
224         * @param parameter
225         */
226        protected static void appendParameter( Element root, CatalogueOutputSchemaParameter parameter, String elementName,
227                                               boolean capabilities ) {
228    
229            // 'ows:Parameter'-element
230            Element parameterNode = XMLTools.appendElement( root, OWSNS, elementName );
231            parameterNode.setAttribute( "name", parameter.getName() );
232    
233            // 'ows:Value'-elements
234            CatalogueOutputSchemaValue[] values = parameter.getSpecializedValues();
235            for ( int i = 0; i < values.length; i++ ) {
236                Element elem = XMLTools.appendElement( parameterNode, OWSNS, "ows:Value", values[i].getValue() );
237                if ( !capabilities ) {
238                    elem.setAttribute( "deegree:input", values[i].getInXsl() );
239                    elem.setAttribute( "deegree:ouput", values[i].getOutXsl() );
240                }
241            }
242        }
243    
244        /**
245         * Appends the DOM representation of a <code>OWSDomainType</code> instance to the passed <code>Element</code>.
246         * 
247         * @param root
248         * @param parameter
249         */
250        protected static void appendParameter( Element root, CatalogueTypeNameSchemaParameter parameter,
251                                               String elementName, boolean capabilities ) {
252    
253            // 'ows:Parameter'-element
254            Element parameterNode = XMLTools.appendElement( root, OWSNS, elementName );
255            parameterNode.setAttribute( "name", parameter.getName() );
256    
257            // 'ows:Value'-elements
258            CatalogueTypeNameSchemaValue[] values = parameter.getSpecializedValues();
259            for ( int i = 0; i < values.length; i++ ) {
260                Element elem = XMLTools.appendElement( parameterNode, OWSNS, "ows:Value", values[i].getValue() );
261                if ( !capabilities ) {
262                    elem.setAttribute( "deegree:schema", values[i].getSchema() );
263                }
264            }
265        }
266    
267        /**
268         * Appends the DOM representation of the <code>ServiceIdentification</code>- section to the passed
269         * <code>Element</code>.
270         * 
271         * @param root
272         * @param serviceIdentification
273         * @throws XMLParsingException
274         */
275        protected static void appendServiceIdentification( Element root, ServiceIdentification serviceIdentification ) {
276    
277            // 'ServiceIdentification'-element
278            Element serviceIdentificationNode = XMLTools.appendElement( root, OWSNS, "ows:ServiceIdentification" );
279    
280            // 'Title'-element
281            XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:Title", serviceIdentification.getTitle() );
282    
283            // 'Abstract'-element
284            if ( serviceIdentification.getAbstract() != null ) {
285                XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:Abstract",
286                                        serviceIdentification.getAbstract() );
287            }
288    
289            // 'Keywords'-element
290            appendOWSKeywords( serviceIdentificationNode, serviceIdentification.getKeywords() );
291    
292            // 'ServiceType'-element
293            XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:ServiceType",
294                                    serviceIdentification.getServiceType().getCode() );
295    
296            // 'ServiceTypeVersion'-elements
297            String[] versions = serviceIdentification.getServiceTypeVersions();
298            for ( int i = 0; i < versions.length; i++ ) {
299                XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:ServiceTypeVersion", versions[i] );
300            }
301    
302            // 'Fees'-element
303            XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:Fees", serviceIdentification.getFees() );
304    
305            // 'AccessConstraints'-element
306            String[] constraints = serviceIdentification.getAccessConstraints();
307            if ( constraints != null ) {
308                for ( int i = 0; i < constraints.length; i++ ) {
309                    XMLTools.appendElement( serviceIdentificationNode, OWSNS, "ows:AccessConstraints", constraints[i] );
310                }
311            }
312        }
313    
314    }