home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / xml / glue / xmlglue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.0 KB  |  351 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* 
  20.    This is some glue code for feeding data into the xml parser.
  21.    It also has glue code for using CSS to specify display properties
  22.    for an xml file.
  23.  
  24.    For more info on XML in navigator, look at the XML section
  25.    of www.mozilla.org.
  26.  
  27.    If you have questions, send them to guha@netscape.com
  28. */
  29.  
  30. #include "xmlglue.h"
  31. #include "xmlparse.h"
  32. #include "xmlss.h"
  33.  
  34.  
  35.  
  36. unsigned int
  37. xml_write_ready(NET_StreamClass *stream)
  38. {
  39.    return XML_LINE_SIZE;
  40.  
  41. }
  42.  
  43.  
  44.  
  45. int xml_parse_abort (NET_StreamClass *stream) {
  46.   XMLFile obj= (XMLFile) stream->data_object;
  47.   NET_StreamClass *newstream;
  48.   URL_Struct *urls = obj->urls;
  49.   const char* error = XML_ErrorString(XML_GetErrorCode(obj->parser));
  50.   char*  buff  = getMem(150);
  51.   int n = XML_GetErrorLineNumber(obj->parser);
  52.   URL_Struct *nurls =   NET_CreateURLStruct(copyString(urls->address), NET_DONT_RELOAD);
  53.   StrAllocCopy(nurls->content_type, TEXT_HTML);
  54.   newstream = NET_StreamBuilder(1,  nurls, (MWContext*)obj->mwcontext);
  55.   sprintf(buff, "<B>Bad XML at line %i :</B><BR><P> ERROR = ", n);
  56.   (*(newstream->put_block))(newstream, buff, strlen(buff)); 
  57.   (*(newstream->put_block))(newstream, error, strlen(error)); 
  58.   newstream->complete(newstream);
  59.   freeMem(buff);
  60.   NET_FreeURLStruct(nurls); 
  61.   return -1;
  62. }
  63.  
  64. void
  65. xml_abort(NET_StreamClass *stream, int status)
  66. {
  67.     XMLFile obj= (XMLFile) stream->data_object;
  68.     if (obj->status != 1) {
  69.        xml_parse_abort(stream);
  70.        } else {
  71.     XML_Parse(obj->parser, NULL, 0, 1);
  72.        }
  73.     XML_ParserFree(obj->parser);
  74. }
  75.  
  76.  
  77. int
  78. xml_write(NET_StreamClass *stream, char* data, int32 len)
  79. {
  80.     XMLFile obj= (XMLFile) stream->data_object;
  81.     char* ndata = getMem(len+1);
  82.     memcpy(ndata, data, len);
  83.     if (obj->status == 1) obj->status = XML_Parse(obj->parser,  ndata, len, 0);    
  84.     return 0;
  85. }
  86.  
  87.  
  88. void
  89. xml_complete (NET_StreamClass *stream)
  90. {
  91.   NET_StreamClass *newstream;
  92.   XMLFile obj= (XMLFile) stream->data_object;
  93.   URL_Struct *urls = obj->urls;
  94.    if (obj->status != 1) {
  95.        xml_parse_abort(stream);
  96.        return;
  97.   } 
  98.   XML_Parse(obj->parser, NULL, 0, 1);
  99.   XML_ParserFree(obj->parser);
  100.   obj->numOpenStreams--;
  101.   
  102.   if (obj->numOpenStreams < 1) {
  103.   /* direct the stream to the html parser */
  104.     URL_Struct *nurls =   NET_CreateURLStruct(copyString(urls->address), NET_DONT_RELOAD);
  105.     StrAllocCopy(nurls->content_type, TEXT_HTML);
  106.  
  107.     newstream = NET_StreamBuilder(1,  nurls, (MWContext*)obj->mwcontext);
  108.     obj->stream = newstream;
  109.     convertToHTML(obj);
  110.     newstream->complete(newstream);
  111.     NET_FreeURLStruct(nurls); 
  112.   }
  113. }
  114.  
  115.  
  116.  
  117. void
  118. outputToStream (XMLFile f, char* s)
  119. {
  120.   int ans = 0;
  121.   NET_StreamClass *stream = (NET_StreamClass*) f->stream;
  122.   char* buff = copyString(s);
  123. #ifdef DEBUG
  124. /*  FE_Trace(s); */
  125. #endif
  126.   
  127.   if (buff) (*(stream->put_block))(stream, buff, strlen(s)); 
  128.   freeMem(buff);
  129. }
  130.  
  131.  
  132.  
  133. #ifdef    XP_MAC
  134. PR_PUBLIC_API(NET_StreamClass *)
  135. #else
  136. PUBLIC NET_StreamClass *
  137. #endif
  138.  
  139. XML_XMLConverter(FO_Present_Types  format_out, void *data_object, URL_Struct *URL_s, MWContext  *window_id)
  140. {
  141.     NET_StreamClass* stream;
  142.     XMLFile  xmlf; 
  143.     TRACEMSG(("Setting up display stream. Have URL: %s\n", URL_s->address));
  144.  
  145.     xmlf = (XMLFile) getMem(sizeof(XMLFileStruct));
  146.     xmlf->urls = URL_s;
  147.     xmlf->parser = XML_ParserCreate(NULL) ;
  148.     XML_SetElementHandler(xmlf->parser, (void (*)(void*, const char*, const char**))XMLDOM_StartHandler, (void (*)(void*, const char*))XMLDOM_EndHandler);
  149.     XML_SetCharacterDataHandler(xmlf->parser, (void (*)(void*, const char*, int))XMLDOM_CharHandler);
  150.     XML_SetProcessingInstructionHandler(xmlf->parser, (void (*)(void*, const char*, const char*))XMLDOM_PIHandler);
  151.     XML_SetUserData(xmlf->parser, xmlf);
  152.     xmlf->status = 1;
  153.     xmlf->address = copyString(URL_s->address);
  154.     xmlf->mwcontext = window_id;
  155.     xmlf->numOpenStreams = 1;
  156.     /*    URL_s->fedata = xmlf; */
  157.     stream = NET_NewStream("XML", (MKStreamWriteFunc)xml_write, 
  158.                (MKStreamCompleteFunc)xml_complete,
  159.                (MKStreamAbortFunc)xml_abort, 
  160.                (MKStreamWriteReadyFunc)xml_write_ready, xmlf, window_id);
  161.     if (stream == NULL) freeMem(xmlf);
  162.     
  163.     return(stream);
  164. }
  165.  
  166.  
  167.  
  168. /* XML-CSS stuff */
  169.  
  170.  
  171.  
  172. unsigned int
  173. xmlcss_write_ready(NET_StreamClass *stream)
  174. {
  175.    return XML_LINE_SIZE;
  176.  
  177. }
  178.  
  179.  
  180.  
  181. void
  182. xmlcss_abort(NET_StreamClass *stream, int status)
  183. {
  184.   StyleSheet obj=stream->data_object;
  185.   XMLFile xml = obj->xmlFile;
  186.   xml->numOpenStreams--;
  187.   freeMem(obj->holdOver);
  188.   freeMem(obj->line);
  189.   obj->line = obj->holdOver = NULL;
  190. }
  191.  
  192.  
  193.  
  194. void
  195. xmlcss_complete  (NET_StreamClass *stream)
  196. {
  197.   StyleSheet ss =stream->data_object;
  198.   URL_Struct *urls = ss->urls;
  199.   XMLFile xml = ss->xmlFile;
  200.   freeMem(ss->holdOver);
  201.   freeMem(ss->line);
  202.   ss->line = ss->holdOver = NULL; 
  203.   xml->numOpenStreams--;
  204.   if (xml->numOpenStreams == 0) { 
  205.   /* direct the stream to the html parser */
  206.     NET_StreamClass *newstream;
  207.     URL_Struct *nurls =   NET_CreateURLStruct(copyString(xml->address), NET_DONT_RELOAD);
  208.     StrAllocCopy(nurls->content_type, TEXT_HTML);
  209.     newstream = NET_StreamBuilder(1,  nurls, (MWContext*) xml->mwcontext);
  210.     xml->stream = newstream;
  211.     convertToHTML(xml);
  212.     newstream->complete(newstream);
  213.     NET_FreeURLStruct(nurls); 
  214.   }     
  215. }
  216.  
  217.  
  218.  
  219. #ifdef    XP_MAC
  220. PR_PUBLIC_API(NET_StreamClass *)
  221. #else
  222. PUBLIC NET_StreamClass *
  223. #endif
  224.  
  225. XML_CSSConverter(FO_Present_Types  format_out, void *data_object, URL_Struct *URL_s, MWContext  *window_id)
  226. {
  227.     StyleSheet  ss = (StyleSheet) URL_s->fe_data;
  228.     TRACEMSG(("Setting up style sheet stream. Have URL: %s\n", URL_s->address));
  229.  
  230.     return NET_NewStream("XML-CSS", (MKStreamWriteFunc)parseNextXMLCSSBlob, 
  231.                (MKStreamCompleteFunc)xmlcss_complete,
  232.                (MKStreamAbortFunc)xmlcss_abort, 
  233.                (MKStreamWriteReadyFunc)xmlcss_write_ready, ss, window_id);
  234. }
  235.  
  236.  
  237.  
  238. void
  239. xmlcss_GetUrlExitFunc (URL_Struct *urls, int status, MWContext *cx)
  240. {
  241.  
  242. }
  243.  
  244.  
  245.  
  246. void
  247. readCSS (StyleSheet ss)
  248. {
  249.   URL_Struct                      *urls;
  250.   urls = NET_CreateURLStruct(ss->address, NET_DONT_RELOAD);
  251.   if (urls == NULL)  {
  252.     ss->xmlFile->numOpenStreams--;
  253.     return;
  254.   }
  255.   urls->fe_data = ss;
  256.   NET_GetURL(urls, FO_CACHE_AND_XMLCSS, ss->xmlFile->mwcontext, xmlcss_GetUrlExitFunc);
  257. }
  258.  
  259.  
  260.  
  261. int
  262. xmlhtml_write(NET_StreamClass *stream, const char *str, int32 len)
  263. {
  264.   XMLHTMLInclusion obj=stream->data_object;
  265.   char* newStr;
  266.   if (obj == NULL  || len < 0) {
  267.       return MK_INTERRUPTED;
  268.   }
  269.  newStr = getMem(len+1);
  270.  memcpy(newStr, str, len);
  271.  *(obj->content + obj->n++) = newStr;
  272.  return (len);
  273. }
  274.  
  275.  
  276.  
  277. unsigned int
  278. xmlhtml_write_ready(NET_StreamClass *stream)
  279. {
  280.    return XML_LINE_SIZE;
  281.  
  282. }
  283.  
  284.  
  285.  
  286. void
  287. xmlhtml_abort(NET_StreamClass *stream, int status)
  288. {
  289. }
  290.  
  291.  
  292.  
  293. void
  294. xmlhtml_complete  (NET_StreamClass *stream)
  295. {
  296.   XMLHTMLInclusion ss =stream->data_object;
  297.   XMLFile xml = ss->xml;
  298.   xml->numOpenStreams--;
  299.   if (xml->numOpenStreams == 0) { 
  300.   /* direct the stream to the html parser */
  301.     NET_StreamClass *newstream;
  302.     URL_Struct *nurls =   NET_CreateURLStruct(copyString(xml->address), NET_DONT_RELOAD);
  303.     StrAllocCopy(nurls->content_type, TEXT_HTML);
  304.     newstream = NET_StreamBuilder(1,  nurls, (MWContext*) xml->mwcontext);
  305.     xml->stream = newstream;
  306.     convertToHTML(xml);
  307.     newstream->complete(newstream);
  308.     NET_FreeURLStruct(nurls); 
  309.   }     
  310. }
  311.  
  312.  
  313.  
  314. #ifdef    XP_MAC
  315. PR_PUBLIC_API(NET_StreamClass *)
  316. #else
  317. PUBLIC NET_StreamClass *
  318. #endif
  319.  
  320. XML_HTMLConverter(FO_Present_Types  format_out, void *data_object, URL_Struct *URL_s, MWContext  *window_id)
  321. {
  322.     XMLHTMLInclusion  ss = (XMLHTMLInclusion) URL_s->fe_data;
  323.  
  324.     return NET_NewStream("XML-HTML", (MKStreamWriteFunc)xmlhtml_write, 
  325.                (MKStreamCompleteFunc)xmlhtml_complete,
  326.                (MKStreamAbortFunc)xmlhtml_abort, 
  327.                (MKStreamWriteReadyFunc)xmlhtml_write_ready, ss, window_id);
  328. }
  329.  
  330.  
  331.  
  332. void
  333. xmlhtml_GetUrlExitFunc (URL_Struct *urls, int status, MWContext *cx)
  334. {
  335. }
  336.  
  337.  
  338.  
  339. void
  340. readHTML (char* url, XMLHTMLInclusion ss)
  341. {
  342.   URL_Struct                      *urls;
  343.   urls = NET_CreateURLStruct(url, NET_DONT_RELOAD);
  344.   if (urls == NULL)  {
  345.     ss->xml->numOpenStreams--;
  346.     return;
  347.   }
  348.   urls->fe_data = ss;
  349.   NET_GetURL(urls, FO_CACHE_AND_XMLHTML, ss->xml->mwcontext, xmlhtml_GetUrlExitFunc);
  350. }
  351.