001    // $HeadURL:
002    // /cvsroot/deegree/src/org/deegree/ogcwebservices/csw/capabilities/CatalogCapabilitiesDocument.java,v
003    // 1.22 2004/08/05 15:40:08 ap Exp $
004    /*----------------    FILE HEADER  ------------------------------------------
005    
006     This file is part of deegree.
007     Copyright (C) 2001-2008 by:
008     EXSE, Department of Geography, University of Bonn
009     http://www.giub.uni-bonn.de/deegree/
010     lat/lon GmbH
011     http://www.lat-lon.de
012    
013     This library is free software; you can redistribute it and/or
014     modify it under the terms of the GNU Lesser General Public
015     License as published by the Free Software Foundation; either
016     version 2.1 of the License, or (at your option) any later version.
017    
018     This library is distributed in the hope that it will be useful,
019     but WITHOUT ANY WARRANTY; without even the implied warranty of
020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
021     Lesser General Public License for more details.
022    
023     You should have received a copy of the GNU Lesser General Public
024     License along with this library; if not, write to the Free Software
025     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026    
027     Contact:
028    
029     Andreas Poth
030     lat/lon GmbH
031     Aennchenstr. 19
032     53115 Bonn
033     Germany
034     E-Mail: poth@lat-lon.de
035    
036     Prof. Dr. Klaus Greve
037     Department of Geography
038     University of Bonn
039     Meckenheimer Allee 166
040     53115 Bonn
041     Germany
042     E-Mail: greve@giub.uni-bonn.de
043     
044     ---------------------------------------------------------------------------*/
045    package org.deegree.ogcwebservices.wass.common;
046    
047    import java.io.IOException;
048    import java.net.MalformedURLException;
049    import java.net.URI;
050    import java.net.URISyntaxException;
051    import java.net.URL;
052    import java.util.ArrayList;
053    import java.util.List;
054    
055    import org.deegree.datatypes.Code;
056    import org.deegree.datatypes.xlink.SimpleLink;
057    import org.deegree.framework.xml.XMLParsingException;
058    import org.deegree.framework.xml.XMLTools;
059    import org.deegree.i18n.Messages;
060    import org.deegree.model.metadata.iso19115.Keywords;
061    import org.deegree.ogcbase.CommonNamespaces;
062    import org.deegree.ogcwebservices.getcapabilities.DCPType;
063    import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
064    import org.deegree.ogcwebservices.getcapabilities.ServiceIdentification;
065    import org.deegree.ogcwebservices.getcapabilities.ServiceProvider;
066    import org.deegree.owscommon.OWSCommonCapabilitiesDocument;
067    import org.deegree.owscommon.OWSDomainType;
068    import org.deegree.owscommon.OWSMetadata;
069    import org.w3c.dom.Element;
070    import org.w3c.dom.Node;
071    import org.xml.sax.SAXException;
072    
073    /**
074     * The owscapabilities 1.0 specification parser. This class is able to parse the changes made in the
075     * ows capabilites specifications 1.0.
076     * 
077     * 
078     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
079     * @author last edited by: $Author: apoth $
080     * 
081     * @version $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
082     */
083    public abstract class OWSCapabilitiesBaseDocument_1_0 extends OWSCommonCapabilitiesDocument {
084    
085        private static final long serialVersionUID = -3518136831402464466L;
086    
087        private static final String PRE = CommonNamespaces.OWS_PREFIX + ":";
088    
089        /**
090         * Creates an empty document loaded from the template given.
091         * 
092         * @param template
093         *            the location/name of the template
094         * @throws IOException
095         * @throws SAXException
096         */
097        public void createEmptyDocument( String template )
098                                throws IOException, SAXException {
099            URL url = OWSCapabilitiesBaseDocument_1_0.class.getResource( template );
100            if ( url == null ) {
101                throw new IOException( Messages.getMessage( "WASS_ERROR_RESOURCE_NOT_FOUND", template ) );
102            }
103            load( url );
104        }
105    
106        /**
107         * Returns the class representation for the <code>ServiceIdentification</code> section of the
108         * document, according to the ows 1.0 spec.
109         * 
110         * @return class representation for the <code>ServiceIdentification</code> section
111         * @throws XMLParsingException
112         */
113        public ServiceIdentification parseServiceIdentification()
114                                throws XMLParsingException {
115    
116            Element element = (Element) XMLTools.getRequiredNode( getRootElement(), PRE + "ServiceIdentification",
117                                                                  nsContext );
118    
119            // 'ServiceType' element (mandatory)
120            Element serviceTypeElement = (Element) XMLTools.getRequiredNode( element, PRE + "ServiceType", nsContext );
121            Code serviceType = null;
122            try {
123                String codeSpace = XMLTools.getAttrValue( serviceTypeElement, null, "codeSpace", null );
124                URI uri = codeSpace != null ? new URI( codeSpace ) : null;
125                serviceType = new Code( XMLTools.getStringValue( serviceTypeElement ), uri );
126            } catch ( URISyntaxException e ) {
127                String s = XMLTools.getAttrValue( serviceTypeElement, null, "codeSpace", null );
128                throw new XMLParsingException( Messages.getMessage( "WASS_ERROR_CODESPACE_NOT_URI",
129                                                                new Object[] { s, OWSNS.toString() } ) );
130            }
131    
132            // 'ServiceTypeVersion' elements (mandatory)
133            String[] serviceTypeVersions = XMLTools.getRequiredNodeAsStrings( element, PRE + "ServiceTypeVersion",
134                                                                              nsContext, ",;" );
135    
136            // 'Title' element (optional)
137            String title = XMLTools.getNodeAsString( element, PRE + "Title", nsContext,
138                                                     Messages.getMessage( "WASS_NO_TITLE" ) );
139    
140            // 'Abstract' element (optional)
141            String serviceAbstract = XMLTools.getNodeAsString(
142                                                               element,
143                                                               PRE + "Abstract",
144                                                               nsContext,
145                                                               Messages.getMessage( "WASS_NO_ABSTRACT_DOCUMENTATION" ) );
146    
147            // 'Keywords' elements (optional)
148            List<Node> keywordsList = XMLTools.getNodes( element, PRE + "Keywords", nsContext );
149            Keywords[] keywords = getKeywords( keywordsList );
150    
151            // 'Fees' element (optional)
152            String fees = XMLTools.getNodeAsString( element, PRE + "Fees", nsContext, null );
153    
154            // 'AccessConstraints' elements (optional)
155            String[] accessConstraints = XMLTools.getNodesAsStrings( element, PRE + "AccessConstraints", nsContext );
156    
157            ServiceIdentification serviceIdentification = new ServiceIdentification( serviceType, serviceTypeVersions,
158                                                                                     title, serviceAbstract, keywords,
159                                                                                     fees, accessConstraints );
160            return serviceIdentification;
161        }
162    
163        /**
164         * Wrapper to make it consistent.
165         * 
166         * @return the ServiceProvider representation
167         * @throws XMLParsingException
168         */
169        public ServiceProvider parseServiceProvider()
170                                throws XMLParsingException {
171            return getServiceProvider();
172        }
173    
174        /**
175         * @return the OperationsMetada representation
176         * @throws XMLParsingException
177         * @throws InvalidCapabilitiesException
178         * @throws URISyntaxException
179         */
180        public OperationsMetadata_1_0 parseOperationsMetadata()
181                                throws XMLParsingException, InvalidCapabilitiesException, URISyntaxException {
182            
183            Element root = (Element) XMLTools.getRequiredNode( getRootElement(), PRE + "OperationsMetadata", nsContext );
184    
185            ArrayList<Operation_1_0> operations = new ArrayList<Operation_1_0>();
186    
187            List<Node> nl = XMLTools.getRequiredNodes( root, PRE + "Operation", nsContext );
188    
189            for ( int i = 0; i < nl.size(); ++i ) {
190                operations.add( parseOperation( (Element) nl.get( i ) ) );
191            }
192    
193            List<Node> parameterList = XMLTools.getNodes( root, PRE + "Parameter", nsContext );
194            OWSDomainType[] parameters = new OWSDomainType[parameterList.size()];
195            for ( int i = 0; i < parameters.length; i++ ) {
196                parameters[i] = parseOWSDomainType_1_0( (Element) parameterList.get( i ) );
197            }
198    
199            List<Node> constraintList = XMLTools.getNodes( root, PRE + "Constraint", nsContext );
200            OWSDomainType[] constraints = new OWSDomainType[constraintList.size()];
201            for ( int i = 0; i < constraints.length; i++ ) {
202                constraints[i] = parseOWSDomainType_1_0( (Element) constraintList.get( i ) );
203            }
204    
205            String extendedCapabilities = XMLTools.getNodeAsString( root, PRE + "ExtendedCapabilities", nsContext, null );
206    
207            // find the !not specified in the gdinrw! describeuser
208            Operation_1_0 describeUser = null;
209            // and find the GetCapabilities operation too.
210            Operation_1_0 getCapa = null;
211            for ( int i = 0; i < operations.size(); ++i ) {
212                if ( operations.get( i ).getName().equalsIgnoreCase( "describeUser" ) ) {
213                    describeUser = operations.get( i );
214                }
215                if ( operations.get( i ).getName().equalsIgnoreCase( "GetCapabilities" ) ) {
216                    getCapa = operations.get( i );
217                }
218            }
219    
220            if ( getCapa == null ) {
221                throw new InvalidCapabilitiesException(
222                                                        Messages.getMessage(
223                                                                         "WASS_ERROR_CAPABILITIES_MISSING_REQUIRED_OPERATION",
224                                                                         "GetCapabilities" ) );
225            }
226            if ( describeUser == null ) {
227                throw new InvalidCapabilitiesException(
228                                                        Messages.getMessage(
229                                                                         "WASS_ERROR_CAPABILITIES_MISSING_REQUIRED_OPERATION",
230                                                                         "DescribeUser" ) );
231            }
232    
233            OperationsMetadata_1_0 om = new OperationsMetadata_1_0(
234                                                                    operations.toArray( new Operation_1_0[operations.size()] ),
235                                                                    parameters, constraints, extendedCapabilities,
236                                                                    describeUser, getCapa );
237    
238            
239    
240            return om;
241        }
242    
243        /**
244         * @param root
245         *            the root node of the Operation xml fragment.
246         * @return The operation representation 1.0 conform
247         * @throws XMLParsingException
248         * @throws URISyntaxException
249         */
250        @SuppressWarnings("unchecked")
251        public Operation_1_0 parseOperation( Element root )
252                                throws XMLParsingException, URISyntaxException {
253            
254            DCPType[] dcps = getDCPs( XMLTools.getRequiredNodes( root, PRE + "DCP", nsContext ) );
255    
256            String name = XMLTools.getAttrValue( root, null, "name", null );
257    
258            List parameterList = XMLTools.getNodes( root, PRE + "Parameter", nsContext );
259            OWSDomainType[] parameters = new OWSDomainType[parameterList.size()];
260            for ( int i = 0; i < parameters.length; i++ ) {
261                parameters[i] = parseOWSDomainType_1_0( (Element) parameterList.get( i ) );
262            }
263    
264            List constraintList = XMLTools.getNodes( root, PRE + "Constraint", nsContext );
265            OWSDomainType[] constraints = new OWSDomainType[constraintList.size()];
266            for ( int i = 0; i < constraints.length; i++ ) {
267                constraints[i] = parseOWSDomainType_1_0( (Element) constraintList.get( i ) );
268            }
269    
270            List metadata = XMLTools.getNodes( root, PRE + "Metadata", nsContext );
271    
272            ArrayList<OWSMetadata> metadatas = new ArrayList<OWSMetadata>();
273    
274            for ( int i = 0; i < metadata.size(); ++i ) {
275                metadatas.add( parseOWSMetadata( (Element) metadata.get( i ), name ) );
276            }
277    
278            Operation_1_0 operation = new Operation_1_0(
279                                                         name,
280                                                         dcps,
281                                                         parameters,
282                                                         constraints,
283                                                         (OWSMetadata[]) metadata.toArray( new OWSMetadata[metadata.size()] ) );
284    
285            
286            return operation;
287        }
288    
289        private OWSMetadata parseOWSMetadata( Element root, String name )
290                                throws XMLParsingException, URISyntaxException {
291            
292            SimpleLink link = parseSimpleLink( root );
293            URI about = new URI( XMLTools.getAttrValue( root, null, "about", null ) );
294    
295            // TODO: find out real name
296            OWSMetadata md = new OWSMetadata( about, link, name );
297            
298            return md;
299        }
300    
301        /**
302         * @param element
303         *            The element to get the Domaintype from.
304         * @return the OWSDomainType representation.
305         * @throws XMLParsingException
306         * 
307         */
308        private OWSDomainType parseOWSDomainType_1_0( Element element )
309                                throws XMLParsingException {
310            return getOWSDomainType( null, element );
311        }
312    
313        /**
314         * @param nameSpace
315         *            The namespace of the supportedauthenticationlist should be wss or was
316         * @return ArrayList<SupportedAuthenticationMethod>
317         * @throws XMLParsingException
318         * @throws MalformedURLException
319         */
320        public ArrayList<SupportedAuthenticationMethod> parseSupportedAuthenticationMethods( String nameSpace )
321                                throws XMLParsingException, MalformedURLException {
322            Element capability = (Element) XMLTools.getRequiredNode( getRootElement(), nameSpace + ":Capability", nsContext );
323            Element methodList = (Element) XMLTools.getRequiredNode( capability, nameSpace
324                                                                                 + ":SupportedAuthenticationMethodList",
325                                                                     nsContext );
326            ArrayList<SupportedAuthenticationMethod> result = new AuthenticationDocument().parseSupportedAuthenticationMethodList( methodList );
327     
328            return result;
329        }
330    
331    }