home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / rdf / src / es2mcf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.0 KB  |  547 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 implements FTP support for the rdf data model.
  21.    For more information on this file, contact rjc or guha 
  22.    For more information on RDF, look at the RDF section of www.mozilla.org
  23. */
  24.  
  25. #include "es2mcf.h"
  26. #include "glue.h"
  27. #include "ht.h"
  28.  
  29.  
  30.     /* externs */
  31. extern    RDF    gNCDB;
  32.  
  33.  
  34.  
  35. RDFT
  36. MakeESFTPStore (char* url)
  37. {
  38.   RDFT ntr = (RDFT)getMem(sizeof(struct RDF_TranslatorStruct));
  39.   ntr->assert = ESAssert;
  40.   ntr->unassert = ESUnassert;
  41.   ntr->getSlotValue = ESGetSlotValue;
  42.   ntr->getSlotValues = ESGetSlotValues;
  43.   ntr->hasAssertion = ESHasAssertion;
  44.   ntr->nextValue = ESNextValue;
  45.   ntr->disposeCursor = ESDisposeCursor;
  46.   ntr->url = copyString(url);
  47.   return ntr;
  48. }
  49.  
  50.  
  51.  
  52. PRBool
  53. ESFTPRT (RDF_Resource u)
  54. {
  55.   return ((resourceType(u) == ES_RT) ||
  56.       (resourceType(u) == FTP_RT));
  57. }
  58.  
  59.  
  60.  
  61. PRBool
  62. ESAssert (RDFT rdf, RDF_Resource u, RDF_Resource s, void* v, 
  63.            RDF_ValueType type, PRBool tv)
  64. {
  65.   if ((s == gCoreVocab->RDF_parent) && (type == RDF_RESOURCE_TYPE)  &&
  66.       (ESFTPRT((RDF_Resource)v)) &&
  67.       (tv) && (containerp((RDF_Resource)v))) {
  68.     ESAddChild((RDF_Resource)v, u);
  69.   } else {
  70.     return 0;
  71.   }
  72. }
  73.  
  74.  
  75.  
  76. PRBool
  77. ESUnassert (RDFT rdf, RDF_Resource u, RDF_Resource s, void* v, 
  78.            RDF_ValueType type)
  79. {
  80.   if ((s == gCoreVocab->RDF_parent) && (type == RDF_RESOURCE_TYPE)  &&
  81.       (ESFTPRT((RDF_Resource)v)) &&
  82.       (containerp((RDF_Resource)v))) {
  83.     ESRemoveChild((RDF_Resource)v, u);
  84.   } else {
  85.     return 0;
  86.   }
  87. }
  88.  
  89.  
  90.  
  91. PRBool
  92. ESDBAdd (RDFT rdf, RDF_Resource u, RDF_Resource s, void* v, 
  93.         RDF_ValueType type)
  94. {
  95.   Assertion nextAs, prevAs, newAs; 
  96.   if ((s == gCoreVocab->RDF_instanceOf) && (v == gWebData->RDF_Container)) {
  97.     setContainerp(u, true);
  98.     return 1;
  99.   }
  100.      
  101.   nextAs = prevAs = u->rarg1;
  102.   while (nextAs != null) {
  103.     if (asEqual(nextAs, u, s, v, type)) return 1;
  104.     prevAs = nextAs;
  105.     nextAs = nextAs->next;
  106.   }
  107.   newAs = makeNewAssertion(u, s, v, type, 1);
  108.   if (prevAs == null) {
  109.     u->rarg1 = newAs;
  110.   } else {
  111.     prevAs->next = newAs;
  112.   }
  113.   if (type == RDF_RESOURCE_TYPE) {
  114.     nextAs = prevAs = ((RDF_Resource)v)->rarg2;
  115.     while (nextAs != null) {
  116.       prevAs = nextAs;
  117.       nextAs = nextAs->invNext;
  118.     }
  119.     if (prevAs == null) {
  120.       ((RDF_Resource)v)->rarg2 = newAs;
  121.     } else {
  122.       prevAs->invNext = newAs;
  123.     }
  124.   }
  125.   sendNotifications2(rdf, RDF_ASSERT_NOTIFY, u, s, v, type, 1);
  126.   return true;
  127. }
  128.  
  129.  
  130.  
  131. PRBool
  132. ESDBRemove (RDFT rdf, RDF_Resource u, RDF_Resource s, void* v, RDF_ValueType type)
  133. {
  134.   Assertion nextAs, prevAs,  ans;
  135.   PRBool found = false;
  136.   nextAs = prevAs = u->rarg1;
  137.   while (nextAs != null) {
  138.     if (asEqual(nextAs, u, s, v, type)) {
  139.       if (prevAs == null) {
  140.     u->rarg1 = nextAs->next;
  141.       } else {
  142.     prevAs->next = nextAs->next;
  143.       }
  144.       found = true;
  145.       ans = nextAs;
  146.       break;
  147.     }
  148.     prevAs = nextAs;
  149.     nextAs = nextAs->next; 
  150.   }
  151.   if (found == false) return false;
  152.   if (type == RDF_RESOURCE_TYPE) {
  153.     nextAs = prevAs = ((RDF_Resource)v)->rarg2;
  154.     while (nextAs != null) {
  155.       if (nextAs == ans) {
  156.     if (prevAs == nextAs) {
  157.       ((RDF_Resource)v)->rarg2 =  nextAs->invNext;
  158.     } else {
  159.       prevAs->invNext = nextAs->invNext;
  160.     }
  161.       }
  162.       prevAs = nextAs;
  163.       nextAs = nextAs->invNext;
  164.     }
  165.   }
  166.   sendNotifications2(rdf, RDF_DELETE_NOTIFY, u, s, v, type, 1);
  167.   return true;
  168. }
  169.  
  170.  
  171.  
  172. PRBool
  173. ESHasAssertion (RDFT rdf, RDF_Resource u, RDF_Resource s, void* v, 
  174.                RDF_ValueType type, PRBool tv)
  175. {
  176.     Assertion    nextAs;
  177.  
  178.     if (!ESFTPRT(u)) return 0;
  179.  
  180.     nextAs = u->rarg1;
  181.     while (nextAs != NULL)
  182.     {
  183.         if (asEqual(nextAs, u, s, v, type) && (nextAs->tv == tv))
  184.         {
  185.             return(true);
  186.         }
  187.         nextAs = nextAs->next;
  188.     }
  189.     possiblyAccessES(rdf, u, s, false);
  190.     return false;
  191. }
  192.  
  193.  
  194.  
  195. void *
  196. ESGetSlotValue (RDFT rdf, RDF_Resource u, RDF_Resource s, RDF_ValueType type,
  197.         PRBool inversep,  PRBool tv)
  198. {
  199.     if (!ESFTPRT(u)) return NULL;
  200.  
  201.     if ((s == gCoreVocab->RDF_name) && (type == RDF_STRING_TYPE) && (tv))
  202.     {
  203.         char *pathname, *name = NULL;
  204.         int16 n,len;
  205.  
  206.         if (pathname = copyString(resourceID(u)))
  207.         {
  208.             len = strlen(pathname);
  209.             if (pathname[len-1] == '/')  pathname[--len] = '\0';
  210.             n = revCharSearch('/', pathname);
  211.             name = unescapeURL(&pathname[n+1]);
  212.             freeMem(pathname);
  213.         }
  214.         return(name);
  215.     }
  216.     else
  217.     if (u->rarg1 == NULL) possiblyAccessES(rdf, u, s, inversep);
  218.     return null;
  219. }
  220.  
  221.  
  222.  
  223. RDF_Cursor
  224. ESGetSlotValues (RDFT rdf, RDF_Resource u, RDF_Resource s,
  225.         RDF_ValueType type,  PRBool inversep, PRBool tv)
  226. {
  227.   Assertion as;
  228.   if (!ESFTPRT(u)) return 0;
  229.   as  = (inversep ? u->rarg2 : u->rarg1);
  230.   if (as == null) {
  231.     possiblyAccessES(rdf, u, s, inversep);
  232.     return null;
  233.   }
  234.   return NULL;
  235. }
  236.  
  237.  
  238.  
  239. void *
  240. ESNextValue (RDFT mcf, RDF_Cursor c)
  241. {
  242.   return null;
  243. }
  244.  
  245.  
  246.  
  247. RDF_Error
  248. ESDisposeCursor (RDFT mcf, RDF_Cursor c)
  249. {
  250.   freeMem(c);
  251.   return noRDFErr;
  252. }
  253.  
  254.  
  255.  
  256. /** To be written **/
  257.  
  258.  
  259.  
  260. void
  261. es_GetUrlExitFunc (URL_Struct *urls, int status, MWContext *cx)
  262. {
  263.     RDF_Resource        parent = NULL, child = NULL, r;
  264.     _esFEData        *feData;
  265.     char            *newURL, *p;
  266.  
  267.     feData = (_esFEData *)urls->fe_data;
  268.     if ((status >= 0) && (feData != NULL))
  269.     {
  270.         parent = RDF_GetResource(gNCDB, feData->parent, false);
  271.         child = RDF_GetResource(gNCDB, feData->child, false);
  272.         if ((parent != NULL) && (child != NULL))
  273.         {
  274.             switch(feData->method)
  275.             {
  276.                 case    URL_POST_METHOD:
  277.                 if (((p = strrchr(resourceID(child), '/')) != NULL) && (*++p != '\0'))
  278.                 {
  279.                     if ((newURL = append2Strings(resourceID(parent), p)) != NULL)
  280.                     {
  281.                         if ((r = RDF_GetResource(gNCDB, newURL, 1)) != NULL)
  282.                         {
  283.                             setContainerp(r, containerp(child));
  284.                             setResourceType(r, resourceType(child));
  285.                         
  286.                             remoteStoreAdd(gRemoteStore, r,
  287.                                 gCoreVocab->RDF_parent, parent,
  288.                                 RDF_RESOURCE_TYPE, 1);
  289.                         }
  290.                         freeMem(newURL);
  291.                     }
  292.                 }
  293.                 break;
  294.  
  295.                 case    URL_DELETE_METHOD:
  296.                 remoteStoreRemove(gRemoteStore, child,
  297.                     gCoreVocab->RDF_parent, parent,
  298.                     RDF_RESOURCE_TYPE);
  299.                 break;
  300.             }
  301.         }
  302.     }
  303.     else if (status < 0)
  304.     {
  305.         if ((cx != NULL) && (urls != NULL) && (urls->error_msg != NULL))
  306.         {
  307.             FE_Alert(cx, urls->error_msg);
  308.         }
  309.     }
  310.     if (feData != NULL)
  311.     {
  312.         esFreeFEData(feData);
  313.     }
  314.         NET_FreeURLStruct (urls);
  315. }
  316.  
  317.  
  318.  
  319. char *
  320. nativeFilename(char *filename)
  321. {
  322.     char        *newName = NULL, *temp;
  323.     int        x = 0;
  324.  
  325.     if (filename == NULL)    return(NULL);
  326.     if ((newName = unescapeURL(filename)) != NULL)
  327.     {
  328.         if ((temp = convertFileURLToNSPRCopaceticPath(newName)) != NULL)
  329.         {
  330.             temp = copyString(temp);
  331.         }
  332.         freeMem(newName);
  333.         newName = temp;
  334. #ifdef    XP_WIN
  335.         if (newName != NULL)
  336.         {
  337.             while (newName[x] != '\0')
  338.             {
  339.                 if (newName[x] == '/')
  340.                 {
  341.                     newName[x] = '\\';
  342.                 }
  343.                 ++x;
  344.             }
  345.         }
  346. #endif
  347.     }
  348.     return(newName);
  349. }
  350.  
  351.  
  352.  
  353. _esFEData *
  354. esMakeFEData(RDF_Resource parent, RDF_Resource child, int method)
  355. {
  356.     _esFEData        *feData;
  357.     
  358.     if ((feData = (_esFEData *)XP_ALLOC(3*sizeof(char *))) != NULL)
  359.     {
  360.         feData->parent = copyString(resourceID(parent));
  361.         feData->child = copyString(resourceID(child));
  362.         feData->method = method;
  363.     }
  364.     return(feData);
  365. }
  366.  
  367.  
  368.  
  369. void
  370. esFreeFEData(_esFEData *feData)
  371. {
  372.     if (feData != NULL)
  373.     {
  374.         if (feData->parent)    freeMem(feData->parent);
  375.         if (feData->child)    freeMem(feData->child);
  376.         freeMem(feData);
  377.     }
  378. }
  379.  
  380.  
  381.  
  382.  
  383.   /** go tell the directory that child got added to parent **/
  384. void
  385. ESAddChild (RDF_Resource parent, RDF_Resource child)
  386. {
  387.     URL_Struct        *urls;
  388.     void            *feData, **files_to_post = NULL;
  389.  
  390.     if ((urls = NET_CreateURLStruct(resourceID(parent), NET_SUPER_RELOAD)) != NULL)
  391.     {
  392.         feData = (void *)esMakeFEData(parent, child, URL_POST_METHOD);
  393.         if ((files_to_post = (char **)XP_ALLOC(2*sizeof(char *))) != NULL)
  394.         {
  395.             files_to_post[0] = nativeFilename(resourceID(child));
  396.             files_to_post[1] = NULL;
  397.         }
  398.         if ((feData != NULL) && (files_to_post != NULL))
  399.         {
  400.             urls->files_to_post = (void *)files_to_post;
  401.             urls->post_to = NULL;
  402.             urls->method = URL_POST_METHOD;
  403.             urls->fe_data = (void *)feData;
  404.             NET_GetURL(urls, FO_PRESENT,
  405.                 (MWContext *)gRDFMWContext(),
  406.                 es_GetUrlExitFunc);
  407.         }
  408.         else
  409.         {
  410.             if (feData != NULL)
  411.             {
  412.                 esFreeFEData(feData);
  413.             }
  414.             if (files_to_post != NULL)
  415.             {
  416.                 if (files_to_post[0] != NULL)    freeMem(files_to_post[0]);
  417.                 XP_FREE(files_to_post);
  418.             }
  419.             NET_FreeURLStruct(urls);
  420.         }
  421.     }
  422. }
  423.  
  424.  
  425.  
  426.   /** remove the child from the directory **/
  427. void
  428. ESRemoveChild (RDF_Resource parent, RDF_Resource child)
  429. {
  430.     URL_Struct        *urls;
  431.     void            *feData;
  432.  
  433.     if ((urls = NET_CreateURLStruct(resourceID(child), NET_SUPER_RELOAD)) != NULL)
  434.     {
  435.         feData = (void *)esMakeFEData(parent, child, URL_DELETE_METHOD);
  436.         if (feData != NULL)
  437.         {
  438.             urls->method = URL_DELETE_METHOD;
  439.             urls->fe_data = (void *)feData;
  440.             NET_GetURL(urls, FO_PRESENT,
  441.                 (MWContext *)gRDFMWContext(),
  442.                 es_GetUrlExitFunc);
  443.         }
  444.         else
  445.         {
  446.             NET_FreeURLStruct(urls);
  447.         }
  448.     }
  449. }
  450.  
  451.  
  452.  
  453. void
  454. possiblyAccessES(RDFT rdf, RDF_Resource u, RDF_Resource s, PRBool inversep)
  455. {
  456.    if ((ESFTPRT(u)) && 
  457.        (s == gCoreVocab->RDF_parent) && (containerp(u))) {
  458.      char* id =  resourceID(u);
  459.      readRDFFile((resourceType(u) == ES_RT ? &id[4] : id), u, false);
  460.    }
  461. }
  462.  
  463.  
  464.  
  465. void
  466. parseNextESFTPLine (RDFFile f, char* line)
  467. {
  468.   int16 i1, i2;
  469.   char url[100];
  470.   RDF_Resource ru;
  471.   PRBool directoryp;
  472.    
  473.   if (f->fileType == FTP_RT) {
  474.       if (!startsWith("201", line)) return;
  475.       line = &line[5];
  476.   }
  477.   i1 = charSearch(' ', line);
  478.   if (i1 == -1) return;
  479.   i2 = charSearch(' ', &line[i1+1]) + i1 + 1;
  480.   line[i1] = '\0';
  481.   directoryp = 0;
  482.   if (strlen(line) > 64) return;
  483.   if (resourceType(f->top) == ES_RT) {
  484.     directoryp = startsWith("Director", &line[i1+1]);
  485.      sprintf(url, "nes:%s%s%s", f->url, line, (directoryp ? "/" : ""));
  486.   } else {
  487.     int i3 = charSearch(' ', &line[i1+i2+1]);
  488.     directoryp = startsWith("Directory", &line[i1+i2+i3+2]);
  489.  
  490. /* this is bad as files can be of zero-length!
  491.     if ((charSearch('.', line) ==-1) && (startsWith("0", &line[i1+1]))) directoryp = 1;
  492. */
  493.  
  494.     sprintf(url, "%s%s%s", f->url, line, (directoryp ? "/" : ""));
  495.    }
  496.   ru = RDF_GetResource(NULL, url, 1);
  497.   setResourceType(ru, resourceType(f->top));
  498.   if (directoryp) setContainerp(ru, 1);
  499. /*
  500.   remoteStoreAdd(gRemoteStore, ru, gCoreVocab->RDF_name, NET_UnEscape(line), RDF_STRING_TYPE, 1);
  501. */
  502.   remoteStoreAdd(gRemoteStore, ru, gCoreVocab->RDF_parent, f->top, RDF_RESOURCE_TYPE, 1);
  503. }
  504.  
  505.  
  506.  
  507. int
  508. parseNextESFTPBlob(NET_StreamClass *stream, char* blob, int32 size)
  509. {
  510.   RDFFile f;
  511.   int32 n, last, m;
  512.   n = last = 0;
  513.  
  514.   f = (RDFFile)stream->data_object;
  515.   if (f == NULL ||  size < 0) {
  516.     return MK_INTERRUPTED;
  517.   }
  518.  
  519.   while (n < size) {
  520.     char c = blob[n];
  521.     m = 0;
  522.     memset(f->line, '\0', f->lineSize);
  523.     if (f->holdOver[0] != '\0') {
  524.       memcpy(f->line, f->holdOver, strlen(f->holdOver));
  525.       m = strlen(f->holdOver);
  526.       memset(f->holdOver, '\0', RDF_BUF_SIZE);
  527.     }
  528.     while ((m < f->lineSize) && (c != '\r') && (c != '\n') && (n < size)) {
  529.       f->line[m] = c;
  530.       m++;
  531.       n++;
  532.       c = blob[n];
  533.     }
  534.     n++;
  535.     if (m > 0) {
  536.       if ((c == '\n') || (c == '\r')) {
  537.     last = n;
  538.     parseNextESFTPLine(f, f->line);
  539.       } else if (size > last) {
  540.     memcpy(f->holdOver, f->line, m);
  541.       }
  542.     }
  543.   }
  544.   return(size);
  545. }
  546.  
  547.