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