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