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 07:47:04 -0800 (Wed, 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
098: .equals("2.0.2") ? CSW202NS : CSWNS));
099: }
100:
101: /**
102: * Extracts a <code>GetRecordsResult</code> representation of this object.
103: *
104: * @param request
105: *
106: * @return the actual GetRecordResult as a bean.
107: * @throws MissingParameterValueException
108: * @throws InvalidParameterValueException
109: * @throws OGCWebServiceException
110: */
111: public GetRecordsResult parseGetRecordsResponse(GetRecords request)
112: throws MissingParameterValueException,
113: InvalidParameterValueException, OGCWebServiceException {
114: try {
115: String requestId = null;
116: SearchStatus searchStatus = null;
117: SearchResults searchResults = null;
118: // '<csw:GetRecordsResponse>'-element (required)
119: Node contextNode = XMLTools.getRequiredNode(
120: getRootElement(), "self::csw:GetRecordsResponse",
121: nsContext);
122:
123: // 'version'-attribute (optional)
124: String version = XMLTools.getNodeAsString(contextNode,
125: "@version", nsContext, GetRecords.DEFAULT_VERSION);
126:
127: // '<csw:RequestId>'-element (optional)
128: requestId = XMLTools.getNodeAsString(contextNode,
129: "csw:RequestId", nsContext, requestId);
130:
131: // '<csw:SearchStatus>'-element (required)
132: String status = XMLTools.getRequiredNodeAsString(
133: contextNode, "csw:SearchStatus/@status", nsContext);
134: String timestamp = XMLTools.getNodeAsString(contextNode,
135: "csw:SearchStatus/@timestamp", nsContext, null);
136: searchStatus = new SearchStatus(status, timestamp);
137:
138: // '<csw:SearchResults>'-element (required)
139: contextNode = XMLTools.getRequiredNode(contextNode,
140: "csw:SearchResults", nsContext);
141:
142: // 'requestId'-attribute (optional)
143: requestId = XMLTools.getNodeAsString(contextNode,
144: "@requestId", nsContext, requestId);
145:
146: // 'resultSetId'-attribute (optional)
147: String resultSetId = XMLTools.getNodeAsString(contextNode,
148: "@resultSetId", nsContext, null);
149:
150: // 'elementSet'-attribute (optional)
151: String elementSet = XMLTools.getNodeAsString(contextNode,
152: "@elementSet", nsContext, null);
153:
154: // 'recordSchema'-attribute (optional)
155: String recordSchema = XMLTools.getNodeAsString(contextNode,
156: "@recordSchema", nsContext, null);
157:
158: // 'numberOfRecordsMatched'-attribute (required)
159: int numberOfRecordsMatched = XMLTools.getRequiredNodeAsInt(
160: contextNode, "@numberOfRecordsMatched", nsContext);
161:
162: // 'numberOfRecordsReturned'-attribute (required)
163: int numberOfRecordsReturned = XMLTools
164: .getRequiredNodeAsInt(contextNode,
165: "@numberOfRecordsReturned", nsContext);
166:
167: // 'nextRecord'-attribute (required)
168: int nextRecord = XMLTools.getRequiredNodeAsInt(contextNode,
169: "@nextRecord", nsContext);
170:
171: // 'expires'-attribute (optional)
172: String expires = XMLTools.getNodeAsString(contextNode,
173: "@expires", nsContext, "null");
174:
175: searchResults = new SearchResults(requestId, resultSetId,
176: elementSet, recordSchema, numberOfRecordsReturned,
177: numberOfRecordsMatched, nextRecord, contextNode,
178: expires);
179: return new GetRecordsResult(request, version, searchStatus,
180: searchResults);
181: } catch (XMLParsingException e) {
182: ExceptionCode code = ExceptionCode.INVALID_FORMAT;
183: throw new OGCWebServiceException(
184: "GetRecordsResponseDocument", StringTools
185: .stackTraceToString(e), code);
186: }
187:
188: }
189:
190: /**
191: * creates an emtpy document as defined by the template
192: *
193: * @throws IOException
194: * if the template could not be found
195: * @throws SAXException
196: * if an error occurs while creating the rootnode.
197: */
198: public void createEmptyDocument() throws IOException, SAXException {
199: URL url = GetRecordsResultDocument.class
200: .getResource(XML_TEMPLATE);
201: if (url == null) {
202: throw new IOException("The resource '" + XML_TEMPLATE
203: + " could not be found.");
204: }
205: load(url);
206: }
207: }
|