001    //$HeadURL$
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    
037    package org.deegree.ogcwebservices.csw.discovery;
038    
039    import java.io.IOException;
040    import java.net.URI;
041    import java.net.URISyntaxException;
042    import java.net.URL;
043    import java.util.List;
044    import java.util.Map;
045    
046    import org.deegree.framework.log.ILogger;
047    import org.deegree.framework.log.LoggerFactory;
048    import org.deegree.framework.util.StringTools;
049    import org.deegree.framework.xml.XMLParsingException;
050    import org.deegree.framework.xml.XMLTools;
051    import org.deegree.i18n.Messages;
052    import org.deegree.ogcbase.ExceptionCode;
053    import org.deegree.ogcwebservices.InvalidParameterValueException;
054    import org.deegree.ogcwebservices.MissingParameterValueException;
055    import org.deegree.ogcwebservices.OGCWebServiceException;
056    import org.w3c.dom.Element;
057    import org.w3c.dom.Node;
058    import org.xml.sax.SAXException;
059    
060    /**
061     *
062     *
063     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
064     * @author last edited by: $Author: poth $
065     *
066     * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
067     */
068    public class DescribeRecordDocument_2_0_2 extends DescribeRecordDocument {
069    
070        private static final long serialVersionUID = -1840952437026263106L;
071    
072        private static final ILogger LOG = LoggerFactory.getLogger( DescribeRecordDocument_2_0_2.class );
073    
074        private static final String XML_TEMPLATE = "DescribeRecord2.0.2Template.xml";
075    
076        @Override
077        public DescribeRecord parse( String id )
078                                throws MissingParameterValueException, InvalidParameterValueException,
079                                OGCWebServiceException {
080    
081            String version;
082            Map<String, String> vendorSpecificParameter = null;
083            Map<String, URI> namespaceMappings = null;
084            String[] typeNames = null;
085            String outputFormat = "application/xml";
086            URI schemaLanguage = null;
087    
088            try {
089                // '<csw202:DescribeRecord>'-element (required)
090                Node contextNode = XMLTools.getRequiredNode( this.getRootElement(), "self::csw202:DescribeRecord",
091                                                             nsContext );
092    
093                // 'service'-attribute (required, must be CSW)
094                String service = XMLTools.getRequiredNodeAsString( contextNode, "@service", nsContext );
095                if ( !service.equals( "CSW" ) ) {
096                    ExceptionCode code = ExceptionCode.INVALIDPARAMETERVALUE;
097                    throw new InvalidParameterValueException( "DescribeRecords", "'service' must be 'CSW'", code );
098                }
099    
100                // 'version'-attribute (required)
101                version = XMLTools.getRequiredNodeAsString( contextNode, "@version", nsContext );
102                if ( !"2.0.2".equals( version ) ) {
103                    throw new OGCWebServiceException( "DescribeRecordDocument_2_0_2",
104                                                      Messages.getMessage( "CSW_NOT_SUPPORTED_VERSION",
105                                                                           GetRecords.DEFAULT_VERSION, "2.0.2", version ),
106                                                      ExceptionCode.INVALIDPARAMETERVALUE );
107                }
108    
109                // 'outputFormat'-attribute (optional, default="text/xml")
110                outputFormat = XMLTools.getNodeAsString( contextNode, "@outputFormat", nsContext, outputFormat );
111    
112                // 'schemaLanguage'-attribute (optional,
113                // default="http://www.w3.org/XML/Schema")
114                String schemaLanguageString = XMLTools.getNodeAsString( contextNode, "@schemaLanguage", nsContext,
115                                                                        "http://www.w3.org/XML/Schema" );
116                try {
117                    schemaLanguage = new URI( schemaLanguageString );
118                } catch ( URISyntaxException e ) {
119                    throw new XMLParsingException( "Value '" + schemaLanguageString + "' for attribute 'schemaLanguage' "
120                                                   + "is invalid. Must denote a valid URI." );
121                }
122    
123                // 'csw202:TypeName' elements
124                List<Node> nl = XMLTools.getNodes( contextNode, "csw202:TypeName", nsContext );
125                typeNames = new String[nl.size()];
126                for ( int i = 0; i < typeNames.length; i++ ) {
127                    typeNames[i] = parseTypeName( (Element) nl.get( i ) );
128                }
129    
130                // vendorspecific attributes; required by deegree rights management
131                vendorSpecificParameter = parseDRMParams( this.getRootElement() );
132            } catch ( Exception e ) {
133                LOG.logError( e.getMessage(), e );
134                ExceptionCode code = ExceptionCode.INVALID_FORMAT;
135                throw new OGCWebServiceException( "CatalogGetCapabilities", StringTools.stackTraceToString( e ), code );
136            }
137    
138            return new DescribeRecord( id, version, vendorSpecificParameter, namespaceMappings, typeNames, outputFormat,
139                                       schemaLanguage );
140        }
141    
142        @Override
143        /*
144         * (non-Javadoc)
145         *
146         * @see org.deegree.framework.xml.XMLFragment#createEmptyDocument()
147         */
148        void createEmptyDocument()
149                                throws IOException, SAXException {
150            URL url = GetRecordsDocument.class.getResource( XML_TEMPLATE );
151            if ( url == null ) {
152                throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." );
153            }
154            load( url );
155        }
156    
157    }