libwww_parse_rdf.c

00001 /* File:      libwww_parse_rdf.c
00002 ** Author(s): kifer
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 2000
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: libwww_parse_rdf.c,v 1.5 2003/12/31 01:04:51 kifer Exp $
00022 ** 
00023 */
00024 
00025 
00026 #include "libwww_util.h"
00027 #include "libwww_parse.h"
00028 #include "libwww_parse_rdf.h"
00029 
00030 
00031 /* BOOL, PRIVATE, PUBLIC, etc., are defined in a Libwww header */
00032 
00033 
00034 void set_rdf_conversions()
00035 {
00036   /* Must delete old converter and create new. Apparently something in libwww
00037      releases the atoms used in thes converters, which causes it to crash 
00038      in HTStreamStack() on the second call to rdfparse. */
00039   HTPresentation_deleteAll(RDF_converter);
00040   RDF_converter = HTList_new();
00041 
00042   HTConversion_add(RDF_converter,"*/*", "www/debug",
00043                    HTBlackHoleConverter, 1.0, 0.0, 0.0);
00044   HTConversion_add(RDF_converter,"message/rfc822", "*/*",
00045                    HTMIMEConvert, 1.0, 0.0, 0.0);
00046   HTConversion_add(RDF_converter,"message/x-rfc822-foot", "*/*",
00047                    HTMIMEFooter, 1.0, 0.0, 0.0);
00048   HTConversion_add(RDF_converter,"message/x-rfc822-head", "*/*",
00049                    HTMIMEHeader, 1.0, 0.0, 0.0);
00050   HTConversion_add(RDF_converter,"message/x-rfc822-cont", "*/*",
00051                    HTMIMEContinue, 1.0, 0.0, 0.0);
00052   HTConversion_add(RDF_converter,"message/x-rfc822-upgrade","*/*",
00053                    HTMIMEUpgrade, 1.0, 0.0, 0.0);
00054   HTConversion_add(RDF_converter,"message/x-rfc822-partial", "*/*",
00055                    HTMIMEPartial, 1.0, 0.0, 0.0);
00056   HTConversion_add(RDF_converter,"multipart/*", "*/*",
00057                    HTBoundary, 1.0, 0.0, 0.0);
00058   HTConversion_add(RDF_converter,"text/x-http", "*/*",
00059                    HTTPStatus_new, 1.0, 0.0, 0.0);
00060   /* www/rdf is invented for servers that don't recognize RDF */
00061   HTConversion_add(RDF_converter,"text/html", "www/rdf",
00062                    HTRDFToTriples, 1.0, 0.0, 0.0);
00063   HTConversion_add(RDF_converter,"text/xml", "www/rdf",
00064                    HTRDFToTriples, 1.0, 0.0, 0.0);
00065   HTConversion_add(RDF_converter,"text/plain", "www/rdf",
00066                    HTRDFToTriples, 1.0, 0.0, 0.0);
00067   HTConversion_add(RDF_converter,"www/present", "www/rdf",
00068                    HTRDFToTriples, 1.0, 0.0, 0.0);
00069   HTConversion_add(RDF_converter, "text/rdf", "*/*", 
00070                    HTRDFToTriples, 1.0, 0.0, 0.0);
00071   HTConversion_add(RDF_converter, "application/rdf", "*/*",
00072                    HTRDFToTriples, 1.0, 0.0, 0.0);
00073 }
00074 
00075 
00076 void libwww_newRDF_parserHandler (HTStream *            me,
00077                                   HTRequest *           request,
00078                                   HTFormat              target_format,
00079                                   HTStream *            target_stream,
00080                                   HTRDF *               rdfparser,
00081                                   void *                context)
00082 {
00083   if (rdfparser) {
00084     
00085     /* Create userdata and pass rdfparser there */
00086     USERDATA *userdata = rdf_create_userData(rdfparser,request,target_stream);
00087     
00088     /* Register the triple callback */
00089     HTRDF_registerNewTripleCallback (rdfparser,
00090                                      rdf_new_triple_handler,
00091                                      userdata);
00092   }
00093 }
00094 
00095 
00096 /* context here is of type USERDATA */
00097 PRIVATE void rdf_new_triple_handler (HTRDF *rdfp, HTTriple *t, void *context)
00098 {
00099   USERDATA *userdata = (USERDATA *)context;
00100 
00101 #ifdef LIBWWW_DEBUG
00102   xsb_dbgmsg((LOG_DEBUG,"***In rdf_new_triple_handler(%s)", RequestID(userdata->request)));
00103 #endif
00104   /* create a new triple */
00105   if (rdfp && t) {
00106     prolog_term ptriple = p2p_car(userdata->parsed_term_tail);
00107 
00108     c2p_functor("rdftriple",3,ptriple);
00109     if (HTTriple_predicate(t))
00110       c2p_string(HTTriple_predicate(t), p2p_arg(ptriple,1));
00111     else
00112       c2p_string("rdfunknown", p2p_arg(ptriple,1));
00113     if (HTTriple_subject(t))
00114       c2p_string(HTTriple_subject(t), p2p_arg(ptriple,2));
00115     else
00116       c2p_string("rdfunknown", p2p_arg(ptriple,2));
00117     if (HTTriple_object(t))
00118       c2p_string(HTTriple_object(t), p2p_arg(ptriple,3));
00119     else
00120       c2p_string("rdfunknown", p2p_arg(ptriple,3));
00121 
00122 #ifdef LIBWWW_DEBUG_VERBOSE
00123     print_prolog_term(userdata->parsed_term_tail, "Current result tail");
00124 #endif
00125 
00126     userdata->parsed_term_tail = p2p_cdr(userdata->parsed_term_tail);
00127     c2p_list(userdata->parsed_term_tail);
00128   }
00129 }
00130 
00131 
00132 PRIVATE USERDATA *rdf_create_userData(HTRDF     *parser,
00133                                       HTRequest *request,
00134                                       HTStream  *target_stream)
00135 {
00136   USERDATA *me = NULL;
00137 #ifdef LIBWWW_DEBUG
00138   xsb_dbgmsg((LOG_DEBUG,"***Start rdf_create_userData: Request %s", RequestID(request)));
00139 #endif
00140   if (parser) {
00141     /* make sure that MIME type is appropriate for RDF */
00142     if (!verifyMIMEformat(request, RDFPARSE)) {
00143       /*
00144         HTStream * input = HTRequest_inputStream(request);
00145         (*input->isa->abort)(input, NULL);
00146         HTRequest_setInputStream(request,NULL);
00147         HTRequest_kill(request);
00148         return NULL;
00149       */
00150       xsb_abort("[LIBWWW_REQUEST] Bug: Request type/MIME type mismatch");
00151     }
00152     if ((me = (USERDATA *) HT_CALLOC(1, sizeof(USERDATA))) == NULL)
00153       HT_OUTOFMEM("libwww_parse_rdf");
00154     me->delete_method = rdf_delete_userData;
00155     me->parser = parser;
00156     me->request = request;
00157     me->target = target_stream;
00158     me->parsed_term = p2p_new();
00159     c2p_list(me->parsed_term);
00160     me->parsed_term_tail = me->parsed_term;
00161   }
00162   
00163 #ifdef LIBWWW_DEBUG
00164   xsb_dbgmsg((LOG_DEBUG,"***End rdf_create_userData: Request %s", RequestID(request)));
00165 #endif
00166 
00167   /* Hook up userdata to the request context */
00168   ((REQUEST_CONTEXT *)HTRequest_context(request))->userdata = (void *)me;
00169 
00170   return me;
00171 }
00172 
00173 
00174 
00175 PRIVATE void rdf_delete_userData(void *userdata)
00176 {
00177   prolog_term parsed_result, status_term;
00178   USERDATA *me = (USERDATA *)userdata;
00179   HTRequest *request = me->request;
00180 
00181   if (request) {
00182     parsed_result =
00183       ((REQUEST_CONTEXT *)HTRequest_context(request))->request_result;
00184     status_term =
00185       ((REQUEST_CONTEXT *)HTRequest_context(request))->status_term;
00186   }
00187   else return;
00188 
00189 #ifdef LIBWWW_DEBUG
00190   xsb_dbgmsg((LOG_DEBUG,"***In rdf_delete_userData(%s)", RequestID(request)));
00191 #endif
00192 
00193 #ifdef LIBWWW_DEBUG_VERBOSE
00194   print_prolog_term(me->parsed_term, "Current parse value");
00195 #endif
00196 
00197   /* terminate the parsed prolog terms list */
00198   c2p_nil(me->parsed_term_tail);
00199 
00200   /* pass the result to the outside world */
00201   if (is_var(me->parsed_term))
00202     p2p_unify(parsed_result, me->parsed_term);
00203   else
00204     xsb_abort("[LIBWWW_REQUEST] Request %s: Arg 4 (Result) must be unbound variable",
00205               RequestID(request));
00206 
00207   HT_FREE(me);
00208 
00209 #ifdef LIBWWW_DEBUG
00210   xsb_dbgmsg((LOG_DEBUG,"***Request %s: freed the USERDATA object", RequestID(request)));
00211 #endif
00212 
00213   return;
00214 }
00215 

Generated on Wed Jul 26 13:30:45 2006 for XSB by  doxygen 1.4.5