PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/GSearch-1.1/Src/GSearch.Patent/GPatentSearch.cs

#
C# | 278 lines | 93 code | 22 blank | 163 comment | 16 complexity | 4ff1fa68249d22123d8af2ad645f2698 MD5 | raw file
  1. /******************************************************************************
  2. * GPatentSearch.cs
  3. *
  4. * This module implements the GPatentSearch search class and related types.
  5. *
  6. * Date: 12/2008
  7. *
  8. * Copyright (c) 2009, Mark Betz
  9. *
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Author nor the names of contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY MARK BETZ ''AS IS'' AND ANY EXPRESS OR
  24. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26. * IN NO EVENT SHALL MARK BETZ BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  29. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  31. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  32. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. using System;
  36. namespace GSearch
  37. {
  38. /// <summary>
  39. /// Type of the event arguments to GPatentSearch events
  40. /// </summary>
  41. public class PatentSearchEventArgs : SearchEventArgs
  42. {
  43. /// <summary>
  44. /// Constructor used to initialize from a SearchEventArgs object
  45. /// </summary>
  46. /// <param name="se"></param>
  47. public PatentSearchEventArgs(SearchEventArgs se)
  48. : base(se.Status, se.Result, se.Ex)
  49. {
  50. }
  51. /// <summary>
  52. /// Returns a reference to a <see cref="GSearch.GPatentResponse"/> object
  53. /// that contains the response, for certain event types
  54. /// </summary>
  55. public new GPatentResponse Result
  56. { get { return (GPatentResponse)(base.Result); } }
  57. }
  58. /// <summary>
  59. /// Selects the types of results returned in patent searches, where All
  60. /// selects all results, Filed selects filed patents only, and Issued
  61. /// selects issued patents only.
  62. /// </summary>
  63. public enum SearchPatentStatus
  64. {
  65. /// <summary>
  66. /// Select patents regardless of Status
  67. /// </summary>
  68. All,
  69. /// <summary>
  70. /// Selects only patents which have been filed but not issued
  71. /// </summary>
  72. Filed,
  73. /// <summary>
  74. /// Selects only patents which have been issued
  75. /// </summary>
  76. Issued
  77. };
  78. /// <summary>
  79. /// PatentSearchArgs derives from <see cref="GSearch.SearchArgs"/> and implements
  80. /// patent search-specific arguments.
  81. /// <example>
  82. /// The following code illustrates how to create and use the PatentSearchArgs class:
  83. /// <code>
  84. /// GPatentSearch gps = new GPatentSearch();
  85. /// PatentSearchArgs psa = new PatentSearchArgs();
  86. /// psa.Terms = "Perpetual Motion";
  87. /// GPatentResponse resp = gps.Search(psa);
  88. /// </code>
  89. /// </example>
  90. /// </summary>
  91. public class PatentSearchArgs : SearchArgs
  92. {
  93. /// <summary>
  94. /// Default constructor for the PatentSearchArgs class.
  95. /// </summary>
  96. public PatentSearchArgs()
  97. {
  98. Scoring = SearchScoring.Relevance;
  99. Types = SearchPatentStatus.All;
  100. }
  101. /// <summary>
  102. /// ToQueryString overrides the base class version. It calls the
  103. /// base class version to get the root of the query string, and then
  104. /// builds the rest of the parameters.
  105. /// </summary>
  106. /// <returns>A System.String containing the complete set of querystring
  107. /// arguments for the specified search</returns>
  108. public override string ToQueryString()
  109. {
  110. string qry = base.ToQueryString();
  111. if ( SearchPatentStatus.Filed == Types )
  112. qry += "&as_psra=1";
  113. else if ( SearchPatentStatus.Issued == Types )
  114. qry += "&as_psrg=1";
  115. if ( SearchScoring.Date == Scoring )
  116. qry += "&scoring=d";
  117. else if ( SearchScoring.DateAscending == Scoring )
  118. qry += "&scoring=ad";
  119. return qry;
  120. }
  121. /// <summary>
  122. /// A value from the <see cref="GSearch.SearchPatentStatus"/> enum giving
  123. /// the types of results desired from the search.
  124. /// </summary>
  125. public SearchPatentStatus Types { get; set; }
  126. /// <summary>
  127. /// A value from the <see cref="GSearch.SearchScoring"/> enum giving the
  128. /// type of ranking applied to search results.
  129. /// </summary>
  130. public SearchScoring Scoring { get; set; }
  131. }
  132. /// <summary>
  133. /// The GPatentSearch class provides an interface for searching patents using Google.
  134. /// It derives from <see cref="GSearch.GSearchCore"/> which does a large part of
  135. /// the work.
  136. /// <example>
  137. /// The following example shows how to use the GPatentSearch class to search for
  138. /// patents using Google:
  139. /// <code>
  140. /// GPatentSearch gps = new GPatentSearch();
  141. /// PatentSearchArgs psa = new PatentSearchArgs();
  142. /// psa.Terms = "Perpetual Motion";
  143. /// GPatentResponse resp = gps.Search(psa);
  144. /// if ( resp.Response.Results.Length > 0 )
  145. /// // process results
  146. /// </code>
  147. /// </example>
  148. /// </summary>
  149. public class GPatentSearch : GSearchCore
  150. {
  151. #if (!SILVERLIGHT)
  152. /// <summary>
  153. /// Performs a syncronous search for patents using the supplied arguments. The
  154. /// calling thread blocks until results are returned or an exception is thrown.
  155. /// This method is not available in the Silverlight version of the libraries.
  156. /// </summary>
  157. /// <param name="args"><see cref="GSearch.PatentSearchArgs"/> containing the search arguments</param>
  158. /// <returns><see cref="GSearch.GPatentResponse"/> containing the results</returns>
  159. /// <exception cref="Exception">if an error occurs during the search</exception>
  160. public GPatentResponse Search(PatentSearchArgs args)
  161. {
  162. return (GPatentResponse)base.Search(args);
  163. }
  164. #endif
  165. /// <summary>
  166. /// Peforms an asyncronous search for patents using the supplied arguments. The
  167. /// calling thread returns immediately, and results or errors are indicated by
  168. /// raising the SearchFailed, SearchComplete, or SearchProgressChanged events.
  169. /// Results are returned on the UI thread.
  170. /// </summary>
  171. /// <param name="args"><see cref="GSearch.PatentSearchArgs"/> containing the search arguments</param>
  172. /// <exception cref="Exception">if an error occurs during the search</exception>
  173. public void SearchAsync(PatentSearchArgs args)
  174. {
  175. base.SearchAsync(args);
  176. }
  177. /// <summary>
  178. /// This event is raised when the search is complete and the results
  179. /// are ready to be returned to the client.
  180. /// </summary>
  181. public event EventHandler<PatentSearchEventArgs> SearchComplete;
  182. /// <summary>
  183. /// This event is raised when the search fails for some reason. The event
  184. /// arguments Ex property will reference the exception.
  185. /// </summary>
  186. public event EventHandler<PatentSearchEventArgs> SearchFailed;
  187. /// <summary>
  188. /// This event is raised when key steps in the search process are
  189. /// completed. See the SearchStatus enumeration for possible values
  190. /// for the PatentSearchEventArgs.Status parameter.
  191. /// <seealso cref="GSearch.SearchStatus"/>
  192. /// </summary>
  193. public event EventHandler<PatentSearchEventArgs> SearchProgressChanged;
  194. /// <summary>
  195. /// Returns a System.String containing a complete uri for the patent
  196. /// search service
  197. /// </summary>
  198. public override string ServiceURI
  199. {
  200. get { return base.ServiceURI + PatentResources.ServiceURI; }
  201. }
  202. /// <summary>
  203. /// Called by GSearchCore to raise the SearchComplete event
  204. /// </summary>
  205. /// <param name="e"><see cref="SearchEventArgs"/> containing
  206. /// information about the event</param>
  207. protected override void OnSearchComplete(SearchEventArgs e)
  208. {
  209. EventHandler<PatentSearchEventArgs> ev = SearchComplete;
  210. if (null != ev)
  211. {
  212. PatentSearchEventArgs ie = new PatentSearchEventArgs(e);
  213. ev(this, ie);
  214. }
  215. }
  216. /// <summary>
  217. /// Called by GSearchCore to raise the SearchFailed event
  218. /// </summary>
  219. /// <param name="e"><see cref="SearchEventArgs"/> containing
  220. /// information about the event</param>
  221. protected override void OnSearchFailed(SearchEventArgs e)
  222. {
  223. EventHandler<PatentSearchEventArgs> ev = SearchFailed;
  224. if (null != ev)
  225. {
  226. PatentSearchEventArgs ie = new PatentSearchEventArgs(e);
  227. ev(this, ie);
  228. }
  229. }
  230. /// <summary>
  231. /// Called by GSearchCore to raise the SearchProgressChanged event
  232. /// </summary>
  233. /// <param name="e"><see cref="SearchEventArgs"/> containing
  234. /// information about the event</param>
  235. protected override void OnSearchProgressChanged(SearchEventArgs e)
  236. {
  237. EventHandler<PatentSearchEventArgs> ev = SearchProgressChanged;
  238. if (null != ev)
  239. {
  240. PatentSearchEventArgs ie = new PatentSearchEventArgs(e);
  241. ev(this, ie);
  242. }
  243. }
  244. /// <summary>
  245. /// Deserialize is called from the base when the search returns a valid
  246. /// result stream, it refers the call to the base class DeserializeImpl
  247. /// which actually does the work of deserialization.
  248. /// </summary>
  249. /// <param name="result">Stream result passed from the base class</param>
  250. /// <returns>Instance of <see cref="GSearch.GPatentResponse"/> as GResponse</returns>
  251. protected override GResponse Deserialize(System.IO.Stream result)
  252. {
  253. return DeserializeImpl<GPatentResponse>(result);
  254. }
  255. }
  256. }