Sun Java System Portal Server 7.1 Developer's Guide

RAF Definition Example

This section shows an example definition for a robot application function.

This function copies a specified source data to a multi-valued field in an RD. For example, the search engine stores category or classification information in the classification field of an RD. The copy_mv function allows the robot to get the value of an HTML <META> tag of any name and store the value in the classification field in the database. For example, using this function, you could instruct the robot to get the content of the <META NAME="topic"> tag, and store it as the classification of the resource.

You would invoke this function with a directive such as the following:

Generate fn=copy_mv src=topic dst=classification

RAF Definition Example shows a sample function definition.


Example 22–3 Robot Application Function Example


/******** example robot application function ********/
#include robotapi.h
#include pblock.h
#include log.h
#include objlog.h

NSAPI Public int copy_mv(libcs_pblock *pb, CSFilter *csf,
CSResource *csr)
{
char *s, *mv, *mvp;

/* Use the libcs_pblock_findval function to get the values of the
 * "src" and "dst" parameters, which were specified by the
 * directive that invoked this function */

char *src = libcs_pblock_findval("src", pb);
char *dst = libcs_pblock_findval("dst", pb);

if(!src || !dst) {
 cslog_error(1, 1,
 ("<URL:%s>: Error: No source or destination available."
 csr->url,)
 return REQ_PROCEED;
}

/* If the current document does not have a META tag whose name
 * matches the src parameter, just return, otherwise put the
 * src value in the string s */

/* The function Search_Findval(search, attribute) is defined
 * in sdk/rdm/include/search.h. It gets the value of the
 * given attribute from the given resource.
 * The rd in the CSResource is a search that describes the resource
*/

if(!(s = (char *)Search_Findval(csr->rd, src)))
 return REQ_PROCEED;

/* Now insert the string s into the
 * Classification field of the RD */
/* Deal with possibility that the classification field
 * already has one or more values */
 if((mv = libcs_pblock_findval(dst, csr->sources)) != NULL) {
   sprintf(mvp, "%s;%s", mv, s);
   mvp = malloc((strlen(mv)+strlen(s)+2));
   /* append the new value to the existing values in the
   * classification field, separated by ’;’ */
   libcs_pblock_nvinsert(dst, mvp, csr->sources);
   /* do some clean up */
   free(mvp);
}

/* if no values already exist, do a simple value insert */
else
{
   libcs_pblock_nvinsert(dst, s, csr->sources);
}

/* We’re all done. Return a status code */
return REQ_PROCEED;
}