001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/csw/discovery/DescribeRecordDocument.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    
044    package org.deegree.ogcwebservices.csw.discovery;
045    
046    import java.io.IOException;
047    import java.net.URI;
048    import java.net.URISyntaxException;
049    import java.net.URL;
050    import java.util.List;
051    import java.util.Map;
052    
053    import org.deegree.framework.log.ILogger;
054    import org.deegree.framework.log.LoggerFactory;
055    import org.deegree.framework.util.StringTools;
056    import org.deegree.framework.xml.XMLParsingException;
057    import org.deegree.framework.xml.XMLTools;
058    import org.deegree.i18n.Messages;
059    import org.deegree.ogcbase.ExceptionCode;
060    import org.deegree.ogcwebservices.InvalidParameterValueException;
061    import org.deegree.ogcwebservices.MissingParameterValueException;
062    import org.deegree.ogcwebservices.OGCWebServiceException;
063    import org.deegree.ogcwebservices.csw.AbstractCSWRequestDocument;
064    import org.w3c.dom.Element;
065    import org.w3c.dom.Node;
066    import org.xml.sax.SAXException;
067    
068    /**
069     * Represents an XML DescribeRecord document of an OGC CSW 2.0 compliant
070     * service.
071     * 
072     * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </a>
073     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
074     * 
075     * @author last edited by: $Author: apoth $
076     * 
077     * @version 2.0, $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
078     * 
079     * @since 2.0
080     */
081    
082    public class DescribeRecordDocument extends AbstractCSWRequestDocument {
083    
084        private static final long serialVersionUID = 6554937884331546780L;
085    
086        private static final ILogger LOG = LoggerFactory.getLogger( DescribeRecordDocument.class );
087    
088        private static final String XML_TEMPLATE = "DescribeRecordTemplate.xml";
089    
090        /**
091         * Extracts a <code>DescribeRecord</code> representation of this object.
092         * 
093         * TODO use QualifiedName for type names
094         * 
095         * @param id
096         *            unique ID of the request
097         * @return @throws
098         *         MissingParameterValueException
099         * @throws InvalidParameterValueException
100         * @throws OGCWebServiceException
101         */
102        public DescribeRecord parse( String id ) throws MissingParameterValueException,
103                                                InvalidParameterValueException,
104                                                OGCWebServiceException {
105    
106            String version;
107            Map<String,String> vendorSpecificParameter = null;
108            Map namespaceMappings = null;
109            String[] typeNames = null;
110            String outputFormat = "text/xml";
111            URI schemaLanguage = null;
112    
113            try {
114                // '<csw:DescribeRecord>'-element (required)
115                Node contextNode = XMLTools.getRequiredNode( this.getRootElement(),
116                                                             "self::csw:DescribeRecord", nsContext );
117    
118                // 'service'-attribute (required, must be CSW)
119                String service = XMLTools.getNodeAsString( contextNode, "@service", nsContext, "CSW" );
120                if ( !service.equals( "CSW" ) ) {
121                    ExceptionCode code = ExceptionCode.INVALIDPARAMETERVALUE;
122                    throw new InvalidParameterValueException( "DescribeRecords",
123                                                              "'service' must be 'CSW'", code );
124                }
125    
126                // 'version'-attribute (required)
127                version = XMLTools.getNodeAsString( contextNode, "@version", nsContext, "2.0.1" );
128                if ( !"2.0.0".equals( version ) && !"2.0.1".equals( version )) {
129                    throw new OGCWebServiceException( "DescribeRecordDocument",
130                                                      Messages.getMessage( "CSW_NOT_SUPPORTED_VERSION",
131                                                                           GetRecords.DEFAULT_VERSION, "2.0.2", version ),
132                                                      ExceptionCode.INVALIDPARAMETERVALUE );
133                }
134    
135                // 'outputFormat'-attribute (optional, default="text/xml")
136                outputFormat = XMLTools.getNodeAsString( contextNode, "@outputFormat", nsContext,
137                                                         outputFormat );
138    
139                // 'schemaLanguage'-attribute (optional,
140                // default="http://www.w3.org/XML/Schema")
141                String schemaLanguageString = XMLTools.getNodeAsString( contextNode, "@schemaLanguage",
142                                                                        nsContext,
143                                                                        "http://www.w3.org/XML/Schema" );
144                try {
145                    schemaLanguage = new URI( schemaLanguageString );
146                } catch ( URISyntaxException e ) {
147                    throw new XMLParsingException( "Value '" + schemaLanguageString
148                                                   + "' for attribute 'schemaLanguage' " +
149                                                     "is invalid. Must denote a valid URI." );
150                }
151    
152                // 'csw:TypeName' elements
153                List nl = XMLTools.getNodes( contextNode, "csw:TypeName", nsContext );
154                typeNames = new String[nl.size()];
155                for ( int i = 0; i < typeNames.length; i++ ) {
156                    typeNames[i] = parseTypeName( (Element) nl.get( i ) );
157                }
158                
159                // vendorspecific attributes; required by deegree rights management
160                vendorSpecificParameter = parseDRMParams( this.getRootElement() );
161            } catch ( Exception e ) {
162                LOG.logError( e.getMessage(), e );
163                ExceptionCode code = ExceptionCode.INVALID_FORMAT;
164                throw new OGCWebServiceException( "CatalogGetCapabilities",
165                                                  StringTools.stackTraceToString( e ), code );
166            }
167    
168            return new DescribeRecord( id, version, vendorSpecificParameter, namespaceMappings,
169                                       typeNames, outputFormat, schemaLanguage );
170        }
171    
172        /**
173         * Parses a <code>TypeName</code> element into a <code>QualifiedName</code>.
174         * 
175         * TODO respect "targetNamespace" attribute and really return a <code>QualifiedName</code>.
176         * 
177         * @return
178         * @throws XMLParsingException 
179         */
180        protected String parseTypeName( Element root ) throws XMLParsingException {
181            return XMLTools.getRequiredNodeAsString( root, "text()", nsContext );
182        }
183    
184        /*
185         * (non-Javadoc)
186         * 
187         * @see org.deegree.framework.xml.XMLFragment#createEmptyDocument()
188         */
189        void createEmptyDocument() throws IOException,
190                                  SAXException {
191            URL url = DescribeRecordDocument.class.getResource( XML_TEMPLATE );
192            if ( url == null ) {
193                throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." );
194            }
195            load( url );
196        }
197    }