Sun Java System Portal Server 7.2 Developer's Guide

An Introductory Example

You will find several examples of the use of the Search API in PortalServer-base/sdk/rdm/examples directory.

This section discusses an example that is similar to (but not necessarily identical to) example1.c. It shows how to iterate through a Search stream and print the URL and number of attributes of each Search in the stream.

This example assumes that you have already created a file containing a Search stream which is available on stdin. For example, you could have created a Search stream containing one or more RDs from the search engine database, which you would do by using the routines in RDM.h.

This example uses Search_ParseInitFile() to create a Search stream from the standard input.


Example 24–1 Simple Search Stream Parsing Example


/* Example 1 - Simple Search Stream Parsing */

#include <stdio.h>
#include <stdlib.h>
#include “search.h”

int main(int argc, char *argv[])

{
/* Define a SearchStream and Search */
SearchStream *ss;
Search *s;
char *titleptr;

/* Open a Search stream that gets its Search from stdin */
ss = Search_ParseInitFile(stdin);
/* SearchStream_IsEOS() checks if this is the end of the stream */

while (!SearchStream_IsEOS(ss)) {
  if (!(s = SearchStream_Parse(ss)))
  /* Exit the loop if the Search is invalid */
  break;

/* Print the URL for each Search (will be “-” if there is no URL)*/
printf(“URL = %s\\n”, s->url);

/* Print the title if it exists. */
 titleptr = Search_Findval(s, “title”);
 printf(“Title = %s\\n”, titleptr ? titleptr : “(none)”)

/* Print the number of attributes in the Search*/
 printf(“# of Attributes = %d\\n”, Search_GetAttributeCount(s));

 /* release the memory used by the Search */
 Search_Free(s);
 }
 /* Close the SearchStream and exit */
 SearchStream_Finish(ss);
 exit(0);
}