001 /*---------------- FILE HEADER ------------------------------------------ 002 003 This file is part of deegree. 004 Copyright (C) 2001-2008 by: 005 EXSE, Department of Geography, University of Bonn 006 http://www.giub.uni-bonn.de/deegree/ 007 lat/lon GmbH 008 http://www.lat-lon.de 009 010 This library is free software; you can redistribute it and/or 011 modify it under the terms of the GNU Lesser General Public 012 License as published by the Free Software Foundation; either 013 version 2.1 of the License, or (at your option) any later version. 014 015 This library is distributed in the hope that it will be useful, 016 but WITHOUT ANY WARRANTY; without even the implied warranty of 017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 018 Lesser General Public License for more details. 019 020 You should have received a copy of the GNU Lesser General Public 021 License along with this library; if not, write to the Free Software 022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 023 024 Contact: 025 026 Andreas Poth 027 lat/lon GmbH 028 Aennchenstr. 19 029 53115 Bonn 030 Germany 031 E-Mail: poth@lat-lon.de 032 033 Prof. Dr. Klaus Greve 034 Department of Geography 035 University of Bonn 036 Meckenheimer Allee 166 037 53115 Bonn 038 Germany 039 E-Mail: greve@giub.uni-bonn.de 040 041 ---------------------------------------------------------------------------*/ 042 043 package org.deegree.ogcwebservices.csw.discovery; 044 045 import java.io.IOException; 046 import java.net.URL; 047 048 import org.deegree.datatypes.QualifiedName; 049 import org.deegree.framework.util.StringTools; 050 import org.deegree.framework.xml.XMLFragment; 051 import org.deegree.framework.xml.XMLParsingException; 052 import org.deegree.framework.xml.XMLTools; 053 import org.deegree.ogcbase.ExceptionCode; 054 import org.deegree.ogcwebservices.InvalidParameterValueException; 055 import org.deegree.ogcwebservices.MissingParameterValueException; 056 import org.deegree.ogcwebservices.OGCWebServiceException; 057 import org.w3c.dom.Node; 058 import org.xml.sax.SAXException; 059 import static org.deegree.ogcbase.CommonNamespaces.*; 060 061 /** 062 * Represents an XML GetRecordsResponse document of an OGC CSW 2.0 compliant service. 063 * <p> 064 * The <GetRecordsResponse> element is a container for the response of the GetRecords 065 * operation. Three levels of detail may be contained in the response document. 066 * <ul> 067 * <li>The <RequestId> element may be used to correlate the response to a GetRecords request 068 * for which a value was defined for the requestId attribute. 069 * <li><SearchStatus> element must be present and indicates the status of the response. The 070 * status attribute is used to indicate the completion status of the GetRecords operation. Table 65 071 * shows the possible values for the status attribute. 072 * <li>The <SearchResults> element is a generic XML container for the actual response to a 073 * GetRecords request. The content of the <SearchResults> element is the set of records 074 * returned by the GetRecords operation. The actual records returned by the catalogue should 075 * substitute for the element <csw:AbstractRecord>. 076 * </ul> 077 * 078 * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a> 079 * 080 * @author last edited by: $Author: aschmitz $ 081 * 082 * @version $Revision: 9499 $, $Date: 2008-01-09 16:47:04 +0100 (Mi, 09 Jan 2008) $ 083 * 084 * 085 */ 086 public class GetRecordsResultDocument extends XMLFragment { 087 088 private static final long serialVersionUID = 2796229558893029054L; 089 090 private static final String XML_TEMPLATE = "GetRecordsResponseTemplate.xml"; 091 092 public GetRecordsResultDocument() { 093 // load later 094 } 095 096 public GetRecordsResultDocument( String version ) { 097 super( new QualifiedName( "csw", "GetRecordsResponse", version.equals( "2.0.2" ) ? CSW202NS : CSWNS ) ); 098 } 099 100 /** 101 * Extracts a <code>GetRecordsResult</code> representation of this object. 102 * 103 * @param request 104 * 105 * @return the actual GetRecordResult as a bean. 106 * @throws MissingParameterValueException 107 * @throws InvalidParameterValueException 108 * @throws OGCWebServiceException 109 */ 110 public GetRecordsResult parseGetRecordsResponse( GetRecords request ) 111 throws MissingParameterValueException, InvalidParameterValueException, 112 OGCWebServiceException { 113 try { 114 String requestId = null; 115 SearchStatus searchStatus = null; 116 SearchResults searchResults = null; 117 // '<csw:GetRecordsResponse>'-element (required) 118 Node contextNode = XMLTools.getRequiredNode( getRootElement(), "self::csw:GetRecordsResponse", nsContext ); 119 120 // 'version'-attribute (optional) 121 String version = XMLTools.getNodeAsString( contextNode, "@version", nsContext, GetRecords.DEFAULT_VERSION ); 122 123 // '<csw:RequestId>'-element (optional) 124 requestId = XMLTools.getNodeAsString( contextNode, "csw:RequestId", nsContext, requestId ); 125 126 // '<csw:SearchStatus>'-element (required) 127 String status = XMLTools.getRequiredNodeAsString( contextNode, "csw:SearchStatus/@status", nsContext ); 128 String timestamp = XMLTools.getNodeAsString( contextNode, "csw:SearchStatus/@timestamp", nsContext, null ); 129 searchStatus = new SearchStatus( status, timestamp ); 130 131 // '<csw:SearchResults>'-element (required) 132 contextNode = XMLTools.getRequiredNode( contextNode, "csw:SearchResults", nsContext ); 133 134 // 'requestId'-attribute (optional) 135 requestId = XMLTools.getNodeAsString( contextNode, "@requestId", nsContext, requestId ); 136 137 // 'resultSetId'-attribute (optional) 138 String resultSetId = XMLTools.getNodeAsString( contextNode, "@resultSetId", nsContext, null ); 139 140 // 'elementSet'-attribute (optional) 141 String elementSet = XMLTools.getNodeAsString( contextNode, "@elementSet", nsContext, null ); 142 143 // 'recordSchema'-attribute (optional) 144 String recordSchema = XMLTools.getNodeAsString( contextNode, "@recordSchema", nsContext, null ); 145 146 // 'numberOfRecordsMatched'-attribute (required) 147 int numberOfRecordsMatched = XMLTools.getRequiredNodeAsInt( contextNode, "@numberOfRecordsMatched", 148 nsContext ); 149 150 // 'numberOfRecordsReturned'-attribute (required) 151 int numberOfRecordsReturned = XMLTools.getRequiredNodeAsInt( contextNode, "@numberOfRecordsReturned", 152 nsContext ); 153 154 // 'nextRecord'-attribute (required) 155 int nextRecord = XMLTools.getRequiredNodeAsInt( contextNode, "@nextRecord", nsContext ); 156 157 // 'expires'-attribute (optional) 158 String expires = XMLTools.getNodeAsString( contextNode, "@expires", nsContext, "null" ); 159 160 searchResults = new SearchResults( requestId, resultSetId, elementSet, recordSchema, 161 numberOfRecordsReturned, numberOfRecordsMatched, nextRecord, 162 contextNode, expires ); 163 return new GetRecordsResult( request, version, searchStatus, searchResults ); 164 } catch ( XMLParsingException e ) { 165 ExceptionCode code = ExceptionCode.INVALID_FORMAT; 166 throw new OGCWebServiceException( "GetRecordsResponseDocument", StringTools.stackTraceToString( e ), code ); 167 } 168 169 } 170 171 /** 172 * creates an emtpy document as defined by the template 173 * 174 * @throws IOException 175 * if the template could not be found 176 * @throws SAXException 177 * if an error occurs while creating the rootnode. 178 */ 179 public void createEmptyDocument() 180 throws IOException, SAXException { 181 URL url = GetRecordsResultDocument.class.getResource( XML_TEMPLATE ); 182 if ( url == null ) { 183 throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." ); 184 } 185 load( url ); 186 } 187 }