home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / xml / glue / xmlss.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.2 KB  |  317 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 a semi-parsing CSS. Its used to
  21.    output html from XML.
  22.    This will be changed when we incorporate the real xml parser 
  23.    from James Clark will replace this  within the next few weeks.
  24.  
  25.    For more information on this file, contact rjc or guha 
  26.    For more information on RDF, look at the RDF section of www.mozilla.org
  27.    For more info on XML in navigator, look at the XML section
  28.    of www.mozilla.org.
  29. */
  30.  
  31. #include "xmlss.h"
  32. #define href "href"
  33. #define XLL   "XML-Link"
  34.  
  35. #define BUF_SIZE 500
  36.  
  37. int
  38. parseNextXMLCSSBlob (NET_StreamClass *stream, char* blob, int32 size)
  39. {
  40.   StyleSheet ss;
  41.   int32 n, last, m;
  42.   PRBool somethingseenp = false;
  43.   n = last = 0;
  44.   
  45.   ss = (StyleSheet)(stream->data_object);
  46.   if ((ss == NULL) || (size < 0)) {
  47.     return MK_INTERRUPTED;
  48.   }
  49.   
  50.   while (n < size) {
  51.     char c = blob[n];
  52.     m = 0;
  53.     memset(ss->line, '\0', ss->lineSize);
  54.     if (ss->holdOver[0] != '\0') {
  55.       memcpy(ss->line, ss->holdOver, strlen(ss->holdOver));
  56.       m = strlen(ss->holdOver);
  57.       memset(ss->holdOver, '\0', BUF_SIZE);
  58.     }
  59.     while ((m < 300) && (c != '{') && (c != '}')) {
  60.       ss->line[m] = c;
  61.       m++;
  62.       somethingseenp = (somethingseenp || ((c != ' ') && 
  63.                        (c != '\r') && (c != '\n')));
  64.       n++;
  65.       if (n < size) {
  66.     c = blob[n]; 
  67.       }  else break;
  68.     }
  69.     if (c == '}') ss->line[m] = c;
  70.     n++;
  71.     if (m > 0) {
  72.       if ((c == '}') || (c == '{')) {
  73.     last = n;
  74.     if (c == '{') ss->holdOver[0] = '{'; 
  75.     if (somethingseenp) {
  76.       parseNextXMLCSSElement(ss, ss->line);
  77.     } else if (size > last) {
  78.       memcpy(ss->holdOver, ss->line, m);
  79.     }
  80.       } else if (c == '{') ss->holdOver[0] = '{';
  81.     }
  82.   }
  83.   return(size);
  84. }
  85.  
  86.  
  87.  
  88. void
  89. parseNextXMLCSSElement (StyleSheet ss, char* ele)
  90. {
  91.   if (ele[0] == '{') {
  92.     char* sty = getMem(strlen(ele)-1);
  93.     memcpy(sty, &ele[1], strlen(ele)-2);
  94.     ss->el->style = sty;
  95.   } else {
  96.      StyleElement se = (StyleElement) getMem(sizeof(StyleElementStruct));
  97.      size_t numTags,  size, n, count;
  98.      se->next = ss->el;
  99.      ss->el = se;
  100.      size = strlen(ele);
  101.      count = n = 0;
  102.      numTags = countTagStackSize(ele);
  103.      while (count < numTags) {
  104.        size_t st = 0; 
  105.        char** tgs =  (char**) getMem(numTags * sizeof(char**));
  106.        se->tagStack = tgs;
  107.        while (st < numTags) {     
  108.      *(tgs + st++) = getNextSSTag(ele, &n);
  109.        }
  110.        count++;
  111.      }
  112.   }
  113. }
  114.  
  115.  
  116.  
  117. size_t
  118. countSSNumTags (char* ele)
  119. {
  120.   size_t n = 0;
  121.   size_t m = 1;
  122.   size_t sz = strlen(ele);
  123.   while (n < sz) {
  124.     if (ele[n++] == ',') m++;
  125.   }
  126.   return m;
  127. }
  128.  
  129.  
  130.  
  131. size_t
  132. countTagStackSize (char* ele)
  133. {
  134.   size_t n = 0;
  135.   size_t m = 0;
  136.   PRBool inSpace = 1;
  137.   size_t sz = strlen(ele);
  138.   while (n < sz) {
  139.     if (ele[n] == ',') break;
  140.     if ((ele[n] != ' ') && (ele[n] != '\r') && (ele[n] != '\n')) {
  141.       if (inSpace) {
  142.           m++;
  143.           inSpace = 0;
  144.       }
  145.     } else inSpace = 1;
  146.     n++;
  147.   }
  148.   return m;
  149. }
  150.  
  151.  
  152.  
  153. char*
  154. getNextSSTag (char* ele, size_t* n)
  155. {
  156.   char tag[100];
  157.   size_t m = 0;
  158.   char c = ele[*n];
  159.   size_t s = strlen(ele);
  160.   memset(tag, '\0', 100);
  161.   while ((c == '\r') || (c == ' ') || (c == '\n')) {
  162.     *n = *n + 1;
  163.     c = ele[*n];
  164.   }
  165.   while ((c != ' ') && (c != ',') && (*n < s)) {
  166.     tag[m++] = c;
  167.     *n = *n + 1;
  168.     c = ele[*n];
  169.   }
  170.   return copyString(tag);
  171. }
  172.  
  173.  
  174.  
  175. void
  176. outputAttributes(XMLFile f, char** attlist)
  177. {
  178.   uint32 n = 0;
  179.   PRBool xmlinkp = (getAttributeValue(attlist, XLL) != NULL);
  180.   if (!attlist) return ;
  181.   while ((n < 2*MAX_ATTRIBUTES) && (*(attlist + n) != NULL)) {
  182.     if (!(xmlinkp && stringEquals(*(attlist + n), href))) {
  183.       outputToStream(f, *(attlist + n));
  184.       outputToStream(f, "=\"");
  185.       outputToStream(f, *(attlist + n + 1));
  186.       outputToStream(f, "\" ");
  187.     }
  188.  
  189.     n = n + 2;
  190.   }
  191. }
  192.  
  193.  
  194.  
  195. void
  196. outputAsHTML (XMLFile f, XMLElement el)
  197. {
  198.   if (strcmp(el->tag, "xml:content") == 0) {
  199.     outputToStream(f, el->content);
  200.   } else {
  201.     char* htmlEquiv = getAttributeValue(el->attributes, "html");
  202.     char* linkTag   = getAttributeValue(el->attributes, XLL);
  203.     char*  htmlBuff = getMem(1024);
  204.     XMLElement child = el->child;
  205.     PRBool suppressTag = !startsWith("html:", el->tag) ; 
  206.  
  207.     if (linkTag && (stringEquals(linkTag, "LINK"))) {
  208.       char* hrefVal = getAttributeValue(el->attributes, href);
  209.       if (hrefVal) {
  210.         char* type = getAttributeValue(el->attributes, "Role");
  211.         char* show = getAttributeValue(el->attributes, "Show");
  212.         if (show &&(strcmp(show, "EMBED") == 0)) {
  213.           if (type && (strcmp(type, "HTML") == 0)) {
  214.             int32 n = 0;
  215.             XMLHTMLInclusion incl = (XMLHTMLInclusion) el->content;
  216.             while (incl && *(incl->content + n)) {              
  217.               outputToStream(f, (*(incl->content + n)));
  218.               n++;
  219.             }           
  220.           } else if (type && (strcmp(type, "IMAGE") == 0)) {
  221.             sprintf(htmlBuff, "<img src=%s>\n", hrefVal);
  222.             outputToStream(f, htmlBuff);
  223.           }
  224.           freeMem(htmlBuff);
  225.           return;
  226.         } else {
  227.           sprintf(htmlBuff, "<a href=\"%s\">", hrefVal);
  228.           outputToStream(f, htmlBuff);
  229.        }
  230.       } else {
  231.         linkTag = 0;
  232.       }
  233.     }
  234.  
  235.     outputStyleSpan(f, el, 0);
  236.  
  237.     if (htmlEquiv) {
  238.       sprintf(htmlBuff, "<%s ", htmlEquiv);
  239.       outputToStream(f, htmlBuff);
  240.       outputAttributes(f, el->attributes);
  241.       outputToStream(f, ">\n");
  242.     } else if (!suppressTag){
  243.       sprintf(htmlBuff, "<%s ", &((el->tag)[5]));
  244.       outputToStream(f, htmlBuff);
  245.       outputAttributes(f, el->attributes);
  246.       outputToStream(f, ">\n");
  247.     }
  248.     
  249.     while (child) {
  250.       outputAsHTML(f, child);
  251.       child = child->next;
  252.     }
  253.     if (htmlEquiv) {
  254.       sprintf(htmlBuff, "</%s>\n", htmlEquiv);
  255.       outputToStream(f, htmlBuff);
  256.     } else  if (!suppressTag) {
  257.       sprintf(htmlBuff, "</%s>\n", &((el->tag)[5]));
  258.       outputToStream(f, htmlBuff);
  259.     }
  260.     outputStyleSpan(f, el, 1);
  261.     if (linkTag) outputToStream(f, "</a>");
  262.     freeMem(htmlBuff);
  263.   }
  264. }
  265.  
  266.  
  267.  
  268. void
  269. convertToHTML (XMLFile xf) {
  270.   XMLElement el = xf->top;
  271.   xf->numOpenStreams--;
  272.   if (xf->numOpenStreams < 1) {
  273.     outputToStream(xf, "<html><body>");
  274.     outputAsHTML(xf, el); 
  275.     outputToStream(xf, "</body></html>");
  276.  
  277.   }
  278. }
  279.  
  280.  
  281. void
  282. outputStyleSpan (XMLFile f, XMLElement el, PRBool endp)
  283. {
  284.   StyleSheet ss ;
  285.   StyleElement se;
  286.   for (ss = f->ss; (ss != NULL) ; ss = ss->next) {    
  287.     for (se = ss->el; (se != NULL) ; se = se->next) {
  288.      
  289.       if (stringEquals((se->tagStack)[0],  el->tag)) {
  290.         PRBool divp = startsWith("Display:Block;", se->style);
  291.         PRBool listp = startsWith("Display:List-item;", se->style);
  292.         if (!endp) {
  293.           if (divp) {
  294.             outputToStream(f, "<div style=\"");
  295.             outputToStream(f, &(se->style)[14]);
  296.           } else  if (listp) {
  297.             outputToStream(f, "<UL><LI><span style=\"");
  298.             outputToStream(f, &(se->style)[20]);
  299.           } else   {
  300.             outputToStream(f, "<span style=\"");
  301.             outputToStream(f, se->style);
  302.           }
  303.           outputToStream(f, "\">\n");
  304.         } else {
  305.           if (divp) {
  306.             outputToStream(f, "</div>"); 
  307.           } else if (listp) {
  308.             outputToStream(f, "</UL></span>");
  309.           } else {
  310.             outputToStream(f, "</span>");
  311.           }
  312.         }
  313.       }
  314.     }
  315.   }
  316. }
  317.