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