home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / rdf / src / mcff2mcf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.0 KB  |  388 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. #include "mcff2mcf.h"
  20.  
  21.  
  22.  
  23. int
  24. parseNextMCFBlob(NET_StreamClass *stream, char* blob, int32 size)
  25. {
  26.   RDFFile f;
  27.   int32 n, last, m;
  28.   n = last = 0;
  29.  
  30.   f = (RDFFile)stream->data_object;
  31.   if ((f == NULL) || (size < 0)) {
  32.     return MK_INTERRUPTED;
  33.   }
  34.  
  35.   while (n < size) {
  36.     char c = blob[n];
  37.     m = 0;
  38.     memset(f->line, '\0', f->lineSize);
  39.     if (f->holdOver[0] != '\0') {
  40.       memcpy(f->line, f->holdOver, strlen(f->holdOver));
  41.       m = strlen(f->holdOver);
  42.       memset(f->holdOver, '\0', RDF_BUF_SIZE);
  43.     }
  44.     while ((m < f->lineSize) && (c != '\r') && (c != '\n') && (n < size)) {
  45.       f->line[m] = c;
  46.       m++;
  47.       n++;
  48.       c = blob[n];
  49.     }
  50.     n++;
  51.     if (m > 0) {
  52.       if ((c == '\n') || (c == '\r')) {
  53.     last = n;
  54.     parseNextMCFLine(f, f->line);
  55.       } else if (size > last) {
  56.     memcpy(f->holdOver, f->line, m);
  57.       }
  58.     }
  59.   }
  60.   return(size);
  61. }
  62.  
  63.  
  64.  
  65. void
  66. parseNextMCFLine (RDFFile f, char* line)
  67. {
  68.   char* nextToken ;
  69.   int16 offset = 0;
  70.   RDF_Error err;
  71.   
  72.   if ((nullp(line)) || (line[0] == '\0'))return; 
  73.   nextToken = getMem(MAX_URL_SIZE);
  74.   err = getFirstToken(line, nextToken, &offset);
  75.   
  76.   offset++; 
  77.   if ((err != noRDFErr) &&  (nextToken[0] == ';')) {
  78.     freeMem(nextToken);
  79.     return;
  80.   }
  81.   if (startsWith("begin-headers", nextToken)) {
  82.     f->status = HEADERS;
  83.   } else if (startsWith("end-headers", nextToken)) {
  84.     f->status = BODY;
  85.   } else if (startsWith("unit", nextToken)) {
  86.     f->status = BODY; 
  87.     if (!(nullp(f->currentResource))) resourceTransition(f);
  88.     getFirstToken(&line[offset], nextToken, &offset);
  89.     f->currentResource = resolveReference(nextToken, f); 
  90.   } else if (nextToken[strlen(nextToken)-1] == ':') {
  91.     memset(f->currentSlot, '\0', 100);
  92.     memcpy(f->currentSlot, nextToken, strlen(nextToken)-1);
  93.     while (getFirstToken(&line[offset], nextToken, &offset) == noRDFErr) {
  94.       if (f->status == HEADERS) {
  95.     assignHeaderSlot(f, f->currentSlot, nextToken);
  96.       } else if (f->currentResource) {
  97.     assignSlot(f->currentResource, f->currentSlot, nextToken, f); 
  98.       }
  99.       offset++;
  100.     }
  101.   }
  102.   freeMem(nextToken);
  103. }
  104.  
  105.  
  106.  
  107. void
  108. finishMCFParse (RDFFile f)
  109. {
  110.   parseNextMCFLine(f, f->storeAway);
  111.   resourceTransition(f); 
  112.  
  113.  
  114.  
  115. void
  116. resourceTransition (RDFFile f)
  117. {
  118.   if ((f->currentResource)  && (!f->genlAdded))
  119.     addSlotValue(f, f->currentResource, gCoreVocab->RDF_parent, f->rtop, 
  120.          RDF_RESOURCE_TYPE, true); 
  121.   f->genlAdded = false;
  122. }
  123.  
  124.  
  125.  
  126. void
  127. assignHeaderSlot (RDFFile f, char* slot, char* value)
  128. {
  129.   if (startsWith(slot, "expiresOn")) {
  130.     if (f->expiryTime == NULL)  f->expiryTime = (PRTime*)getMem(sizeof(PRTime));
  131.     if (PR_ParseTimeString (value, 0, f->expiryTime) !=  PR_SUCCESS) {
  132.       freeMem(f->expiryTime);
  133.       f->expiryTime = NULL;
  134.     }
  135.   } else if (!(startsWith(slot, "RDFVersion"))) {
  136.     assignSlot(f->top, slot, value, f);
  137.   }    
  138. }
  139.  
  140.  
  141.  
  142. RDF_Error
  143. getFirstToken (char* line, char* nextToken, int16* l)
  144. {
  145.   PRBool in_paren = 0;
  146.   PRBool in_string = 0;
  147.   PRBool in_bracket = 0;
  148.   PRBool something_seen = 0;
  149.   uint16 front_spaces = 0;
  150.   uint16 index;
  151.   RDF_Error ans = -1;
  152.   
  153.   memset(nextToken, '\0', 200);
  154.   
  155.   for (index = 0; index < strlen(line); index++) {
  156.     char c = line[index];
  157.     
  158.     if ((c == '\n') || (c == '\0') || (c == '\r')) {
  159.       *l = index + *l  ;
  160.       return ans;
  161.     }
  162.     
  163.     if ((c == ':') && ((index == 0) || (line[index-1] == ' ')) && 
  164.     ((line[index+1] == ' ')    || (line[index+1] == '\0'))) c = ' ';
  165.     
  166.     if (c != ' ') something_seen = 1;
  167.     
  168.     if (in_paren) {
  169.       if (c == ')') {
  170.     nextToken[index-front_spaces] = c;
  171.     ans = noRDFErr;
  172.     *l = index + *l ;
  173.     return ans;
  174.       } else {
  175.     ans = noRDFErr;
  176.     nextToken[index-front_spaces] = c;
  177.       }
  178.     } else if (in_string) {
  179.       if (c == '"') {
  180.     nextToken[index-front_spaces] = c;
  181.     ans = noRDFErr;
  182.     *l = index + *l ;
  183.     return ans;
  184.       } else {
  185.     ans = noRDFErr;
  186.     nextToken[index-front_spaces] = c;
  187.       }
  188.     } else if (in_bracket) {
  189.       if (c == ']') {
  190.     nextToken[index-front_spaces] = c; 
  191.     *l = index + *l ;
  192.     ans = noRDFErr;
  193.     return ans;
  194.       } else {
  195.     ans = noRDFErr;
  196.     nextToken[index-front_spaces] = c;
  197.       }
  198.     } else if (c == '"') {
  199.       ans = noRDFErr;
  200.       nextToken[index-front_spaces] = c;
  201.       in_string = 1;
  202.     } else if (c == '[') {
  203.       ans = noRDFErr;
  204.       nextToken[index-front_spaces] = c;
  205.       in_bracket = 1;
  206.     } else if (c == '(') {
  207.       ans = noRDFErr;
  208.       nextToken[index-front_spaces] = c;
  209.       in_paren = 1;
  210.     } else if (c == ' '){
  211.       if (something_seen) { 
  212.     *l = index + *l ;
  213.     return ans;
  214.       } else {
  215.     front_spaces++;
  216.       }
  217.     } else {
  218.       ans = noRDFErr;
  219.       nextToken[index-front_spaces] = c;
  220.     }
  221.   }
  222.   *l = index + *l ;
  223.   return ans;
  224. }    
  225.  
  226.  
  227.  
  228. void
  229. addSlotValue (RDFFile f, RDF_Resource u, RDF_Resource s, void* v,
  230.         RDF_ValueType type, PRBool tv)
  231. {
  232.    if (f == NULL || u == NULL || s == NULL || v == NULL)    return;
  233.    if (s == gCoreVocab->RDF_child) {
  234.      RDF_Resource temp = (RDF_Resource)v;
  235.      s = gCoreVocab->RDF_parent;
  236.      v = u;
  237.      u = temp;
  238.    }
  239.    if (s == gCoreVocab->RDF_parent) f->genlAdded = true; 
  240.    (*f->assert)(f, f->db, u, s, v, type, tv);
  241.    if (s == gCoreVocab->RDF_parent) setContainerp((RDF_Resource)v, 1);
  242. }
  243.  
  244.  
  245.  
  246. void
  247. assignSlot (RDF_Resource u, char* slot, char* value, RDFFile f)
  248. {
  249.   PRBool tv = true;
  250.   if (value[0] == '(') {
  251.     tv = false;
  252.     value = &value[1];
  253.     value[strlen(value)-1] = '\0';
  254.   } 
  255.   if (startsWith("default_genl", slot)) return;
  256.   
  257.   if (startsWith("name", slot) || (startsWith("local-name", slot))) { 
  258.     value[strlen(value)-1] = '\0';
  259.     addSlotValue(f, u, gCoreVocab->RDF_name, copyString(&value[1]), RDF_STRING_TYPE, tv);  
  260.   }  else if (startsWith("specs", slot) || (startsWith("child", slot))) {
  261.     RDF_Resource spec = resolveReference(value, f);  
  262.     if (!nullp(spec)) addSlotValue(f, spec, gCoreVocab->RDF_parent, u, RDF_RESOURCE_TYPE, tv); 
  263.   }  else if (startsWith("genls_pos", slot)) {
  264.     RDF_Resource genl = resolveGenlPosReference(value, f);  
  265.     if (!nullp(genl)) addSlotValue(f, u, gCoreVocab->RDF_parent, genl, RDF_RESOURCE_TYPE, tv);
  266.   }  else if ((startsWith("genls", slot)) || (startsWith("parent", slot))) {
  267.     RDF_Resource genl = resolveReference(value, f);
  268.     if (!nullp(genl)) addSlotValue(f, u, gCoreVocab->RDF_parent, genl, RDF_RESOURCE_TYPE, tv);
  269.   } else {
  270.     void* parsed_value;
  271.     RDF_ValueType data_type;
  272.     RDF_Resource s = RDF_GetResource(NULL, slot, true);
  273.     RDF_Error err = parseSlotValue(f, s, value, &parsed_value, &data_type); 
  274.     if ((err == noRDFErr) && (!nullp(parsed_value)))   
  275.       addSlotValue(f, u, s, parsed_value, data_type, tv);
  276.   } 
  277. }
  278.  
  279.  
  280.  
  281. RDF_Error
  282. parseSlotValue (RDFFile f, RDF_Resource s, char* value, void** parsed_value, RDF_ValueType* data_type)
  283. {
  284.   if (value[0] == '"') {
  285.     int32 size = strlen(value)-1;
  286.     *parsed_value = getMem(size);
  287.     value[size]  = '\0';
  288.     *parsed_value = &value[1];
  289.     *data_type = RDF_STRING_TYPE;
  290.     return noRDFErr;
  291.   } else if (value[0] == '#') {
  292.     if (value[1] == '"') {
  293.       value[strlen(value)-1] = '\0';
  294.       value = &value[2];
  295.     } else {
  296.       value = &value[1];
  297.     }
  298.     *parsed_value = resolveReference(value, f);
  299.     return noRDFErr;
  300.   } else if (charSearch('.', value) == -1) {
  301.     int16 ans = 0;
  302.     /* XXX        sscanf(value, "%ld", &ans); */
  303.     *data_type = RDF_INT_TYPE;
  304.     return noRDFErr;
  305.   } else {
  306.     return -1;
  307.   }
  308. }
  309.  
  310.  
  311.  
  312. void
  313. derelativizeURL (char* tok, char* url, RDFFile f)
  314. {
  315.   if ((tok[0] == '/') && (endsWith(".mco", tok))) {
  316.     void stringAppendBase (char* dest, char* addition) ;
  317.     stringAppendBase(url, f->url); 
  318.     stringAppend(url, "#");
  319.     stringAppend(url, tok);
  320.   } else if  ((endsWith(".mco", tok)) && (charSearch('#', tok) == -1)) {
  321.     void stringAppendBase (char* dest, char* addition) ;
  322.     stringAppendBase(url, f->url);
  323.     stringAppend(url, "#");
  324.     stringAppend(url, tok);
  325.   } else {
  326.     memcpy(url, tok, strlen(tok));
  327.   }
  328. }
  329.  
  330.  
  331.  
  332. RDF_Resource
  333. resolveReference (char *tok, RDFFile f)
  334. {
  335.   RDF_Resource existing;
  336.   char* url = getMem(MAX_URL_SIZE);
  337.   if (tok[0] == '#') tok = &tok[1];
  338.   if (tok[strlen(tok)-1] == '"') tok[strlen(tok)-1] = '\0';
  339.   if (tok[0] == '"') tok = &tok[1];
  340.   memset(url, '\0', 200);
  341.   if (charSearch(':', tok) == -1) { 
  342.     derelativizeURL(tok, url, f);
  343.   } else {
  344.     memcpy(url, tok, strlen(tok));
  345.   }
  346.   if (strcmp(url,"this") == 0) {
  347.     existing = f->top;
  348.   } else {
  349.     existing = RDF_GetResource(NULL, url, false);
  350.   }
  351.   if (existing != null) return existing;
  352.   existing = RDF_GetResource(NULL, url, true);
  353.   addToResourceList(f, existing);
  354.   freeMem(url);
  355.   return existing;
  356. }
  357.  
  358.  
  359.  
  360. RDF_Resource
  361. resolveGenlPosReference(char* tok,  RDFFile f)
  362. {
  363.   RDF_Resource ans;
  364.   char* url = (char*)getMem(MAX_URL_SIZE);
  365.   long i1, i2;
  366.   i1 = charSearch('"', tok);
  367.   i2 = revCharSearch('"', tok);
  368.   memcpy(url, &tok[i1], i2-i1+1);
  369.   ans = resolveReference(url, f);
  370.   freeMem(url);
  371.   return ans;
  372. }
  373.  
  374.  
  375.  
  376. char *
  377. getRelURL (RDF_Resource u, RDF_Resource top)
  378. {
  379.   char* uID = resourceID(u);
  380.   char* topID = resourceID(top);
  381.   if (startsWith(topID, uID)) {
  382.     int16 n = charSearch('#', uID);
  383.     if (n == -1) return uID;
  384.     return &(uID)[n+1];
  385.   } else return uID;
  386. }
  387.