home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / xml / glue / xmldom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.7 KB  |  207 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 file is used to build a persistent XML parse tree.
  21.  
  22. Eventually, this file will also contain the code to implement
  23. the DOM APIs for access to this tree (any volunteers?)
  24.  
  25. If you have questions, send them to guha@netscape.com
  26. */
  27.  
  28. #include "xmlglue.h"
  29. #define href "href"
  30. #define XLL   "XML-Link"
  31.  
  32. void
  33. XMLDOM_CharHandler (XMLFile f, const char* content, int len) {
  34.   XMLElement xmle = (XMLElement)getMem(sizeof(XMLElementStruct));  
  35.   xmle->content = getMem(len+1);
  36.   memcpy(xmle->content, content, len);
  37.   xmle->tag = "xml:content";
  38.   addChild(f->current, xmle);
  39. }
  40.  
  41.  
  42. char *
  43. xmlgetAttributeValue (const char** attlist, char* elName)
  44. {
  45.   size_t n = 0;
  46.   if (!attlist) return NULL;
  47.   while ((n < 2*MAX_ATTRIBUTES) && (*(attlist + n) != NULL)) {
  48.     if (strcmp(*(attlist + n), elName) == 0) return (char*)*(attlist + n + 1);
  49.     n = n + 2;
  50.   }
  51.   return NULL;
  52. }
  53.  
  54. char **
  55. copyCharStarList (char** list)
  56. {
  57.   size_t length = 0;
  58.   while (*(list + length++)){}
  59.   if (length == 0) {
  60.     return NULL;
  61.   } else {
  62.     char** ans = getMem(length * sizeof(char*));
  63.     size_t n = 0;
  64.     while (*(list + n++)) {
  65.       *(ans + n - 1) = copyString(*(list + n - 1));
  66.     }
  67.     return ans;
  68.   }
  69. }
  70.  
  71.  
  72.  
  73. char *
  74. makeAbsoluteURL (char* p1, char* p2)
  75. {
  76.   return NET_MakeAbsoluteURL(p1, p2);
  77.  
  78. }
  79.  
  80.  
  81. void
  82. addStyleSheet(XMLFile f, StyleSheet ss)
  83. {
  84.   if (f->ss == NULL) {
  85.     f->ss = ss;
  86.   } else {
  87.     StyleSheet nss = f->ss;
  88.     while (nss->next != NULL) nss = nss->next;
  89.     nss->next = ss;
  90.   }
  91. }
  92.  
  93.  
  94. void
  95. addChild (XMLElement parent, XMLElement child)
  96. {
  97.   if (parent->child == NULL) {
  98.     parent->child = child;
  99.   } else {
  100.     XMLElement nc = parent->child;
  101.     while (nc->next) {nc = nc->next;}
  102.     nc->next = child;
  103.   }
  104. }
  105.  
  106. void XMLDOM_EndHandler (XMLFile f, const char* elementName) {
  107.   f->current = f->current->parent;
  108. }
  109.  
  110. void XMLDOM_StartHandler (XMLFile f, const char* elementName, const char** attlist) {
  111.   XMLElement xmle = (XMLElement)getMem(sizeof(XMLElementStruct));
  112.   xmle->parent = f->current;
  113.   if (f->top == NULL) f->top = xmle;  
  114.   xmle->tag = copyString(elementName);
  115.   xmle->attributes = copyCharStarList((char**)attlist);
  116.   if (f->current)  addChild(f->current, xmle);
  117.   f->current = xmle;
  118.   if (xmlgetAttributeValue(xmle->attributes, XLL) != NULL) {
  119.     char* linkTag = xmlgetAttributeValue(xmle->attributes, XLL);
  120.     char* hrefVal = xmlgetAttributeValue(xmle->attributes, href);
  121.     char* type = xmlgetAttributeValue(xmle->attributes, "Role");
  122.     char* show = xmlgetAttributeValue(xmle->attributes, "Show");
  123.  
  124.     if (linkTag && (stringEquals(linkTag, "LINK")) &&
  125.         hrefVal && type && stringEquals(type, "HTML") &&
  126.         show && (stringEquals(show, "Embed"))) {
  127.       XMLHTMLInclusion incl = (XMLHTMLInclusion)getMem(sizeof(XMLHTMLInclusionStruct));
  128.       incl->xml = f;
  129.       incl->content = (char**)getMem(400);
  130.       xmle->content = (char*) incl;
  131.       f->numOpenStreams++;
  132.       readHTML(makeAbsoluteURL(f->address, hrefVal), incl);
  133.     }
  134.   }
  135. }
  136.  
  137. void XMLDOM_PIHandler (XMLFile f, const char *elementName, const char *data) {  
  138.   if (startsWith("xml:stylesheet", elementName))   {
  139.     char* url ; 
  140.     char* attlist[2*MAX_ATTRIBUTES+1];
  141.     char* attv;
  142.     char* xdata = ((data == NULL) ? NULL : copyString(data));
  143.     StyleSheet ss;
  144.     tokenizeXMLElement ((char*) xdata, (char**)attlist);
  145.     attv = xmlgetAttributeValue(attlist, "type");
  146.     if (!(attv && (startsWith("text/css", attv)))) {
  147.         freeMem(xdata);
  148.         return;
  149.     }
  150.     url = xmlgetAttributeValue(attlist, href);
  151.     ss = (StyleSheet) getMem(sizeof(StyleSheetStruct));
  152.     ss->address = makeAbsoluteURL(f->address, url);
  153.     ss->line = (char*)getMem(XML_BUF_SIZE);
  154.     ss->holdOver = (char*)getMem(XML_BUF_SIZE);
  155.     ss->lineSize = XML_BUF_SIZE;
  156.     ss->xmlFile = f;
  157.     f->numOpenStreams++;
  158.     addStyleSheet(f, ss);
  159.     readCSS(ss);
  160.     freeMem(xdata); 
  161.     return;
  162.   }
  163.   
  164. }
  165.   
  166.  
  167. void
  168. tokenizeXMLElement (char* attr, char** attlist)
  169. {
  170.   size_t n = 0;
  171.   size_t s = strlen(attr); 
  172.   char c ;
  173.   size_t m = 0;
  174.   size_t atc = 0;
  175.   PRBool inAttrNamep = 1; 
  176.   while (atc < 2*MAX_ATTRIBUTES+1) {*(attlist + atc++) = NULL;}
  177.   atc = 0;
  178.   while (n < s) {
  179.     PRBool attributeOpenStringSeenp = 0;
  180.     m = 0;
  181.     c = attr[n++];
  182.     while ((n <= s) && (atc < 2*MAX_ATTRIBUTES)) {
  183.       if (inAttrNamep && (m > 0) && (wsc(c) || (c == '='))) {
  184.         attr[n-1] = '\0';
  185.         *(attlist + atc++) = &attr[n-m-1];
  186.         break;
  187.       }
  188.       if  (!inAttrNamep && attributeOpenStringSeenp && (c == '"')) {
  189.         attr[n-1] = '\0';
  190.         *(attlist + atc++) = &attr[n-m-1];
  191.         break;
  192.       }
  193.       if (inAttrNamep) {
  194.         if ((m > 0) || (!wsc(c))) m++;
  195.       } else {
  196.         if (c == '"') {
  197.           attributeOpenStringSeenp = 1;
  198.         } else {
  199.           if ((m > 0) || (!(wsc(c)))) m++;
  200.         }
  201.       }
  202.       c = attr[n++];
  203.     }
  204.     inAttrNamep = (inAttrNamep ? 0 : 1);
  205.   }
  206. }
  207.