home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / rdf / src / xmlparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  11.3 KB  |  430 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 a "stop-gap" xml parser that is being shipped with the free
  21.    source. A real xml parser from James Clark will replace this 
  22.    within the next few weeks.
  23.  
  24.    For more information on this file, contact rjc or guha 
  25.    For more information on RDF, look at the RDF section of www.mozilla.org
  26.    For more info on XML in navigator, look at the XML section
  27.    of www.mozilla.org.
  28.  
  29. */
  30.  
  31. #include "xmlparse.h"
  32. #include "xmlglue.h"
  33.  
  34.  
  35. #define href "HRef"
  36. #define XLL   "XML-Link"
  37.  
  38.  
  39.  
  40. int
  41. parseNextXMLBlob (NET_StreamClass *stream, char* blob, int32 size)
  42. {
  43.   XMLFile f;
  44.   int32 n, last, m;
  45.   PRBool somethingseenp = false;
  46.   n = last = 0;
  47.   
  48.   f = (XMLFile)(stream->data_object);
  49.   if ((f == NULL) || (f->urls == NULL) || (size < 0))
  50.   {
  51.     return MK_INTERRUPTED;
  52.   }
  53.   
  54.   while (n < size) {
  55.     char c = blob[n];
  56.     m = 0;
  57.     somethingseenp = false;
  58.     memset(f->line, '\0', f->lineSize);
  59.     if (f->holdOver[0] != '\0') {
  60.       memcpy(f->line, f->holdOver, strlen(f->holdOver));
  61.       m = strlen(f->holdOver);
  62.       somethingseenp = true;
  63.       memset(f->holdOver, '\0', XML_BUF_SIZE);
  64.     }
  65.     while ((m < 300) && (c != '<') && (c != '>')) {
  66.       f->line[m] = c;
  67.       m++;
  68.       somethingseenp = (somethingseenp || (!wsc(c)));
  69.       n++;
  70.       if (n < size) {
  71.     c = blob[n]; 
  72.       }  else break;
  73.     }
  74.     if (c == '>') f->line[m] = c;
  75.     n++;
  76.     if (m > 0) {
  77.       if ((c == '<') || (c == '>')) {
  78.     last = n;
  79.     if (c == '<') f->holdOver[0] = '<'; 
  80.     if (somethingseenp == true) {
  81.       if (f->line[0] == '<') {
  82.         if (f->line[1] == '/') {
  83.           f->current = f->current->parent;
  84.         } else {
  85.           processNextXMLElement(f, f->line);
  86.         }
  87.       } else {
  88.         processNextXMLContent(f, f->line);
  89.       }
  90.     }
  91.       } else if (size > last) {
  92.     memcpy(f->holdOver, f->line, m);
  93.       }
  94.     } else if (c == '<') f->holdOver[0] = '<';
  95.   }
  96.   return(size);
  97. }
  98.  
  99.  
  100.  
  101. void
  102. processNextXMLContent (XMLFile f, char* content)
  103. {
  104.   XMLElement xmle = (XMLElement)getMem(sizeof(XMLElementStruct));
  105.   xmle->content = copyString(content);
  106.   xmle->tag = "xml:content";
  107.   addChild(f->current, xmle);
  108. }
  109.  
  110.  
  111.  
  112. char **
  113. copyCharStarList (char** list)
  114. {
  115.   size_t length = 0;
  116.   while (*(list + length++)){}
  117.   if (length == 0) {
  118.     return NULL;
  119.   } else {
  120.     char** ans = getMem(length * sizeof(char*));
  121.     size_t n = 0;
  122.     while (*(list + n++)) {
  123.       *(ans + n - 1) = copyString(*(list + n - 1));
  124.     }
  125.     return ans;
  126.   }
  127. }
  128.  
  129.  
  130.  
  131. char *
  132. makeAbsoluteURL (char* p1, char* p2)
  133. {
  134. #ifdef XP_MAC
  135.   return p2;
  136. #else 
  137.   return NET_MakeAbsoluteURL(p1, p2);
  138. #endif
  139. }
  140.  
  141.  
  142.  
  143. void
  144. processNextXMLElement (XMLFile f, char* attr)
  145. {
  146.   size_t n = 1;
  147.   size_t s = strlen(attr); 
  148.   size_t m = 0;
  149.   PRBool linkp = 0;
  150.   char* attv;
  151.   PRBool emptyTagp = ((attr[s-2] == '/') || ((attr[s-2] == '?') && (!startsWith("<?xml", attr))));
  152.   XMLElement xmle = (XMLElement)getMem(sizeof(XMLElementStruct));
  153.   char* attlist[2*MAX_ATTRIBUTES+1];
  154.   char* elementName;
  155.   if (startsWith("<!--", attr)) return;
  156.   tokenizeXMLElement(attr, attlist, &elementName);
  157.   if (stringEquals(elementName, "?StyleSheet")) linkp = 1;
  158.  
  159.   xmle->parent = f->current;
  160.   if (f->top == NULL) f->top = xmle;  
  161.   xmle->tag = copyString(elementName);
  162.   xmle->attributes = copyCharStarList(attlist);
  163.   if (f->current)  addChild(f->current, xmle);
  164.   if (!emptyTagp || (!f->current)) f->current = xmle;
  165.   attv = getAttributeValue(xmle->attributes, "Type");
  166.   if (linkp && attv && (startsWith("css", attv))) {
  167.     char* url = getAttributeValue(xmle->attributes, href);
  168.     StyleSheet ss = (StyleSheet) getMem(sizeof(StyleSheetStruct));
  169.     ss->address = makeAbsoluteURL(f->address, url);
  170.     ss->line = (char*)getMem(XML_BUF_SIZE);
  171.     ss->holdOver = (char*)getMem(XML_BUF_SIZE);
  172.     ss->lineSize = XML_BUF_SIZE;
  173.     ss->xmlFile = f;
  174.     f->numOpenStreams++;
  175.     addStyleSheet(f, ss);
  176.     readCSS(ss);
  177.   }
  178.   if (emptyTagp && (getAttributeValue(xmle->attributes, XLL) != NULL)) {
  179.     char* linkTag = getAttributeValue(xmle->attributes, XLL);
  180.     char* hrefVal = getAttributeValue(xmle->attributes, href);
  181.     char* type = getAttributeValue(xmle->attributes, "Role");
  182.     char* show = getAttributeValue(xmle->attributes, "Show");
  183.  
  184.     if (linkTag && (stringEquals(linkTag, "LINK")) &&
  185.         hrefVal && type && stringEquals(type, "HTML") &&
  186.         show && (stringEquals(show, "Embed"))) {
  187.       XMLHTMLInclusion incl = (XMLHTMLInclusion)getMem(sizeof(XMLHTMLInclusionStruct));
  188.       incl->xml = f;
  189.       incl->content = (char**)getMem(400);
  190.       xmle->content = (char*) incl;
  191.       f->numOpenStreams++;
  192.       readHTML(makeAbsoluteURL(f->address, hrefVal), incl);
  193.     }
  194.   }
  195. }
  196.  
  197.  
  198.  
  199. void
  200. addStyleSheet(XMLFile f, StyleSheet ss)
  201. {
  202.   if (f->ss == NULL) {
  203.     f->ss = ss;
  204.   } else {
  205.     StyleSheet nss = f->ss;
  206.     while (nss->next != NULL) nss = nss->next;
  207.     nss->next = ss;
  208.   }
  209. }
  210.  
  211.  
  212.  
  213.  
  214. void
  215. addChild (XMLElement parent, XMLElement child)
  216. {
  217.   if (parent->child == NULL) {
  218.     parent->child = child;
  219.   } else {
  220.     XMLElement nc = parent->child;
  221.     while (nc->next) {nc = nc->next;}
  222.     nc->next = child;
  223.   }
  224. }
  225.  
  226.  
  227. void
  228. outputAttributes(XMLFile f, char** attlist)
  229. {
  230.   uint32 n = 0;
  231.   PRBool xmlinkp = (getAttributeValue(attlist, XLL) != NULL);
  232.   if (!attlist) return ;
  233.   while ((n < 2*MAX_ATTRIBUTES) && (*(attlist + n) != NULL)) {
  234.     if (!(xmlinkp && stringEquals(*(attlist + n), href))) {
  235.       outputToStream(f, *(attlist + n));
  236.       outputToStream(f, "=\"");
  237.       outputToStream(f, *(attlist + n + 1));
  238.       outputToStream(f, "\" ");
  239.     }
  240.  
  241.     n = n + 2;
  242.   }
  243. }
  244.  
  245.  
  246.  
  247. void
  248. outputAsHTML (XMLFile f, XMLElement el)
  249. {
  250.   if (strcmp(el->tag, "xml:content") == 0) {
  251.     outputToStream(f, el->content);
  252.   } else {
  253.     char* htmlEquiv = getAttributeValue(el->attributes, "html-equiv");
  254.     char* linkTag   = getAttributeValue(el->attributes, XLL);
  255.     char*  htmlBuff = getMem(1024);
  256.     XMLElement child = el->child;
  257.     PRBool suppressTag = stringEquals(el->tag, "Title") ; /*hack to suppress html docbook interaction */
  258.  
  259.   
  260.  
  261.     if (linkTag && (stringEquals(linkTag, "LINK"))) {
  262.       char* hrefVal = getAttributeValue(el->attributes, "HRef");
  263.       if (hrefVal) {
  264.         char* type = getAttributeValue(el->attributes, "Role");
  265.         char* show = getAttributeValue(el->attributes, "Show");
  266.         if (show &&(strcmp(show, "EMBED") == 0)) {
  267.           if (type && (strcmp(type, "HTML") == 0)) {
  268.             int32 n = 0;
  269.             XMLHTMLInclusion incl = (XMLHTMLInclusion) el->content;
  270.             while (*(incl->content + n)) {              
  271.               outputToStream(f, (*(incl->content + n)));
  272.               n++;
  273.             }           
  274.           } else if (type && (strcmp(type, "IMAGE") == 0)) {
  275.             sprintf(htmlBuff, "<img src=%s>\n", hrefVal);
  276.             outputToStream(f, htmlBuff);
  277.           }
  278.           freeMem(htmlBuff);
  279.           return;
  280.         } else {
  281.           sprintf(htmlBuff, "<a href=\"%s\">", hrefVal);
  282.           outputToStream(f, htmlBuff);
  283.        }
  284.       } else {
  285.         linkTag = 0;
  286.       }
  287.     }
  288.  
  289.     outputStyleSpan(f, el, 0);
  290.  
  291.     if (htmlEquiv) {
  292.       sprintf(htmlBuff, "<%s ", htmlEquiv);
  293.       outputToStream(f, htmlBuff);
  294.       outputAttributes(f, el->attributes);
  295.       outputToStream(f, ">\n");
  296.     } else if (!(el->tag[0] == '?') && !suppressTag){
  297.       sprintf(htmlBuff, "<%s ", el->tag);
  298.       outputToStream(f, htmlBuff);
  299.       outputAttributes(f, el->attributes);
  300.       outputToStream(f, ">\n");
  301.     }
  302.     
  303.     while (child) {
  304.       outputAsHTML(f, child);
  305.       child = child->next;
  306.     }
  307.     if (htmlEquiv) {
  308.       sprintf(htmlBuff, "</%s>\n", htmlEquiv);
  309.       outputToStream(f, htmlBuff);
  310.     } else  if (!(el->tag[0] == '?') && !suppressTag) {
  311.       sprintf(htmlBuff, "</%s>\n", el->tag);
  312.       outputToStream(f, htmlBuff);
  313.     }
  314.     outputStyleSpan(f, el, 1);
  315.     if (linkTag) outputToStream(f, "</a>");
  316.     freeMem(htmlBuff);
  317.   }
  318. }
  319.  
  320.  
  321.  
  322. void
  323. convertToHTML (XMLFile xf) {
  324.   XMLElement el = xf->top;
  325.   xf->numOpenStreams--;
  326.   if (xf->numOpenStreams < 1) {
  327.     outputToStream(xf, "<html><body>");
  328.     outputAsHTML(xf, el); 
  329.     outputToStream(xf, "</body></html>");
  330.   }
  331. }
  332.  
  333.  
  334.  
  335.  
  336. void
  337. outputStyleSpan (XMLFile f, XMLElement el, PRBool endp)
  338. {
  339.   StyleSheet ss ;
  340.   StyleElement se;
  341.   for (ss = f->ss; (ss != NULL) ; ss = ss->next) {    
  342.     for (se = ss->el; (se != NULL) ; se = se->next) {
  343.      
  344.       if (stringEquals((se->tagStack)[0],  el->tag)) {
  345.         PRBool divp = startsWith("Display:Block;", se->style);
  346.         PRBool listp = startsWith("Display:List-item;", se->style);
  347.         if (!endp) {
  348.           if (divp) {
  349.             outputToStream(f, "<div style=\"");
  350.             outputToStream(f, &(se->style)[14]);
  351.           } else  if (listp) {
  352.             outputToStream(f, "<UL><LI><span style=\"");
  353.             outputToStream(f, &(se->style)[20]);
  354.           } else   {
  355.             outputToStream(f, "<span style=\"");
  356.             outputToStream(f, se->style);
  357.           }
  358.           outputToStream(f, "\">\n");
  359.         } else {
  360.           if (divp) {
  361.             outputToStream(f, "</div>"); 
  362.           } else if (listp) {
  363.             outputToStream(f, "</UL></span>");
  364.           } else {
  365.             outputToStream(f, "</span>");
  366.           }
  367.         }
  368.       }
  369.     }
  370.   }
  371. }
  372.  
  373.  
  374.  
  375.  
  376. void
  377. tokenizeXMLElement (char* attr, char** attlist, char** elementName)
  378. {
  379.   size_t n = 1;
  380.   size_t s = strlen(attr); 
  381.   char c ;
  382.   size_t m = 0;
  383.   size_t atc = 0;
  384.   PRBool emptyTagp =  (attr[s-2] == '/');
  385.   PRBool inAttrNamep = 1;
  386.   c = attr[n++]; 
  387.   while (wsc(c)) {
  388.     c = attr[n++];
  389.   }
  390.   *elementName = &attr[n-1];
  391.   while (n < s) {
  392.     if (wsc(c)) break;
  393.     c = attr[n++];
  394.   }
  395.   attr[n-1] = '\0';
  396.   while (atc < 2*MAX_ATTRIBUTES+1) {*(attlist + atc++) = NULL;}
  397.   atc = 0;
  398.   s = (emptyTagp ? s-2 : s-1);
  399.   while (n < s) {
  400.     PRBool attributeOpenStringSeenp = 0;
  401.     m = 0;
  402.     c = attr[n++];
  403.     while ((n <= s) && (atc < 2*MAX_ATTRIBUTES)) {
  404.       if (inAttrNamep && (m > 0) && (wsc(c) || (c == '='))) {
  405.     attr[n-1] = '\0';
  406.     *(attlist + atc++) = &attr[n-m-1];
  407.     break;
  408.       }
  409.       if  (!inAttrNamep && attributeOpenStringSeenp && (c == '"')) {
  410.     attr[n-1] = '\0';
  411.     *(attlist + atc++) = &attr[n-m-1];
  412.     break;
  413.       }
  414.       if (inAttrNamep) {
  415.     if ((m > 0) || (!wsc(c))) m++;
  416.       } else {
  417.     if (c == '"') {
  418.       attributeOpenStringSeenp = 1;
  419.     } else {
  420.       if ((m > 0) || (!(wsc(c)))) m++;
  421.     }
  422.       }
  423.       c = attr[n++];
  424.     }
  425.     inAttrNamep = (inAttrNamep ? 0 : 1);
  426.   }
  427. }
  428.  
  429. /* Kludge Alert */
  430.