home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / rdf / src / bmk2mcf.c next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.1 KB  |  444 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. /* Reading bookmarks.htm into rdf.
  20.    tags in the bookmark file.
  21.    <TITLE>
  22.    <H1>
  23.    <H3>
  24.    <DL></DL>
  25.    <DT>
  26.    <P>
  27.  
  28.   <DT> indicates that an item is coming. 
  29.        If the next item is an <a then we have a url
  30.        If the next item is a h3, we have a folder.
  31.   <DL> indicates that the previous item (which should have been a folder)
  32.         is the parent of the next set.
  33.   </DL> indicates pop out a level
  34.   <P> ignore this on reading, but write out one after each <DL>
  35.   <DD> the description for the previous <DT>
  36.  
  37.   Category urls. Make it up out of the add dates. */
  38.  
  39.  
  40. /* 
  41.    This file translates netscape bookmarks into the rdf data model.
  42.    For more information on this file, contact rjc or guha 
  43.    For more information on RDF, look at the RDF section of www.mozilla.org
  44. */
  45.  
  46.  
  47. #include "bmk2mcf.h"
  48. #include "glue.h"
  49. #include "mcff2mcf.h"
  50. #include "utils.h"
  51.  
  52.  
  53.     /* extern declarations */
  54. PR_PUBLIC_API(void) HT_WriteOutAsBookmarks (RDF r, PRFileDesc *fp, RDF_Resource u);    /* XXX this should be elsewhere */
  55. extern    char    *gBookmarkURL;
  56. extern    RDF    gNCDB;
  57.  
  58.     /* globals */
  59. uint16 separatorCounter = 0;
  60. static char* gBkFolderDate;
  61.  
  62.  
  63.  
  64. RDF_Resource
  65. createSeparator(void)
  66. {
  67.   char url[50];
  68.   RDF_Resource sep;
  69.   PR_snprintf(url, 50, "separator%i", separatorCounter++);
  70.   sep = RDF_GetResource(NULL, url, 1);
  71.   return sep;
  72. }
  73.  
  74.  
  75.  
  76. RDF_Resource
  77. createContainer (char* id)
  78. {
  79.   RDF_Resource r = RDF_GetResource(NULL, id, true);
  80.   setContainerp(r, 1);
  81.   return r;
  82. }
  83.  
  84.  
  85.  
  86. char *
  87. resourceDescription (RDF rdf, RDF_Resource r)
  88. {
  89.   return (char*)RDF_GetSlotValue(rdf, r, gWebData->RDF_description, RDF_STRING_TYPE, false, true);
  90. }
  91.  
  92.  
  93.  
  94. char *
  95. resourceLastVisitDate (RDF rdf, RDF_Resource r)
  96. {
  97.   return (char*)RDF_GetSlotValue(rdf, r, gWebData->RDF_lastVisitDate, RDF_STRING_TYPE, false, true);
  98. }
  99.  
  100.  
  101.  
  102. char *
  103. resourceLastModifiedDate (RDF rdf, RDF_Resource r)
  104. {
  105.   return (char*)RDF_GetSlotValue(rdf, r, gWebData->RDF_lastModifiedDate, RDF_STRING_TYPE, false, true);
  106. }
  107.  
  108.  
  109.  
  110. void
  111. parseNextBkBlob (RDFFile f, char* blob, int32 size)
  112. {
  113.   int32 n, last, m;
  114.   PRBool somethingseenp = false;
  115.   n = last = 0;
  116.   
  117.   while (n < size) {
  118.     char c = blob[n];
  119.     m = 0;
  120.     somethingseenp = false;
  121.     memset(f->line, '\0', f->lineSize);
  122.     if (f->holdOver[0] != '\0') {
  123.       memcpy(f->line, f->holdOver, strlen(f->holdOver));
  124.       m = strlen(f->holdOver);
  125.       somethingseenp = true;
  126.       memset(f->holdOver, '\0', RDF_BUF_SIZE);
  127.     }
  128.     while ((m < 300) && (c != '<') && (c != '>') && (n < size)) {
  129.       f->line[m] = c;
  130.       m++;
  131.       somethingseenp = (somethingseenp || ((c != ' ') && (c != '\r') && (c != '\n')));
  132.       n++;
  133.       c = blob[n];
  134.     }
  135.     if (c == '>') f->line[m] = c;
  136.     n++;
  137.     if (m > 0) {
  138.       if ((c == '<') || (c == '>')) {
  139.     last = n;
  140.     if (c == '<') f->holdOver[0] = '<'; 
  141.     if (somethingseenp == true) parseNextBkToken(f, f->line);
  142.       } else if (size > last) {
  143.     memcpy(f->holdOver, f->line, m);
  144.       }
  145.     } else if (c == '<') f->holdOver[0] = '<';
  146.   }
  147. }
  148.  
  149.  
  150.  
  151. void
  152. parseNextBkToken (RDFFile f, char* token)
  153. {
  154.  /*    printf(token); */
  155.   if (token[0] == '<') {
  156.     bkStateTransition(f, token);
  157.   } else {
  158.     /* ok, we have a piece of content.
  159.        can be the title, or a description or */
  160.     if ((f->status == IN_TITLE) || (f->status == IN_H3) || 
  161.     (f->status == IN_ITEM_TITLE)) {
  162.       if (IN_H3 && gBkFolderDate) {
  163.     char url[150];
  164.     RDF_Resource newFolder;
  165.     sprintf(url, "%s%s.rdf", gBkFolderDate, token);
  166.     newFolder = createContainer(url);
  167.     addSlotValue(f,newFolder, gCoreVocab->RDF_parent, f->stack[f->depth-1], 
  168.              RDF_RESOURCE_TYPE, true);
  169.     freeMem(gBkFolderDate);
  170.     gBkFolderDate = NULL;
  171.     f->lastItem = newFolder;
  172.       }
  173.       addSlotValue(f, f->lastItem, gCoreVocab->RDF_name, 
  174.            copyString(token), RDF_STRING_TYPE, true);
  175.       if (startsWith("Personal Toolbar", token) && (containerp(f->lastItem)))
  176.     nlocalStoreAssert(gLocalStore, f->lastItem, gCoreVocab->RDF_instanceOf, 
  177.               gNavCenter->RDF_PersonalToolbarFolderCategory, 
  178.               RDF_RESOURCE_TYPE, true);
  179.     } else if (f->status == IN_ITEM_DESCRIPTION) {
  180.       addDescription(f, f->lastItem, token);
  181.     }
  182.   }
  183. }
  184.  
  185.  
  186.  
  187. void
  188. addDescription (RDFFile f, RDF_Resource r, char* token)
  189. {
  190.   char* desc = (char*)  nlocalStoreGetSlotValue(gLocalStore, r, gWebData->RDF_description, 
  191.                        RDF_STRING_TYPE, false, true);
  192.   if (desc == NULL) {
  193.     addSlotValue(f, f->lastItem, gWebData->RDF_description, copyString(token),
  194.          RDF_STRING_TYPE, true);
  195.   } else {
  196.    addSlotValue(f, f->lastItem, gWebData->RDF_description, 
  197.          append2Strings(desc, token), RDF_STRING_TYPE, true); 
  198.     nlocalStoreUnassert(gLocalStore, f->lastItem, gWebData->RDF_description, desc, RDF_STRING_TYPE);
  199.   }
  200. }
  201.  
  202.  
  203.  
  204. void
  205. bkStateTransition (RDFFile f, char* token)
  206. {
  207.   if (startsWith("<A", token)) {
  208.     newLeafBkItem(f, token);
  209.     f->status = IN_ITEM_TITLE;
  210.   } else if (startsWith(OPEN_H3_STRING, token)) {
  211.     newFolderBkItem(f, token);
  212.     f->status = IN_H3;
  213.   } else if (startsWith(OPEN_TITLE_STRING, token)) {
  214.     f->status = IN_TITLE;
  215.   } else if (startsWith(OPEN_H3_STRING, token)) {
  216.     f->status = IN_H3;
  217.   } else if (startsWith(DD_STRING, token)) {
  218.     if (nlocalStoreGetSlotValue(gLocalStore, f->lastItem, gWebData->RDF_description, 
  219.                 RDF_STRING_TYPE, false, true) 
  220.         == NULL) f->status = IN_ITEM_DESCRIPTION;
  221.   } else if (startsWith(OPEN_DL_STRING, token)) {
  222.     f->stack[f->depth++] = f->lastItem;
  223.   } else if (startsWith(CLOSE_DL_STRING, token)) {
  224.     f->depth--;
  225.   } else if (startsWith("<HR>", token)) {
  226.     addSlotValue(f, createSeparator(), gCoreVocab->RDF_parent, f->stack[f->depth-1], 
  227.          RDF_RESOURCE_TYPE, true);
  228.     f->status = 0;
  229.   } else if ((f->status == IN_ITEM_DESCRIPTION) && (startsWith("<BR>", token))) {
  230.     addDescription(f, f->lastItem, token);
  231.   } else f->status = 0;
  232. }
  233.  
  234.  
  235.  
  236. void
  237. newFolderBkItem(RDFFile f, char* token)
  238. {
  239.   int16 start, end;
  240.   start = charSearch('"', token);
  241.   end   = revCharSearch('"', token);
  242.   token[end] = '\0';
  243.   gBkFolderDate = copyString(&token[start+1]);
  244. }
  245.  
  246.  
  247.  
  248. void
  249. newLeafBkItem (RDFFile f, char* token)
  250. {
  251.   char* url = NULL;
  252.   char* addDate = NULL;
  253.   char* lastVisit = NULL;
  254.   char* lastModified = NULL;
  255.   uint8 current = 0;
  256.   int32 len = strlen(token); 
  257.   int32 n = 0;
  258.   char c = token[n++];
  259.   PRBool inString = false;
  260.   RDF_Resource newR;
  261.   
  262.   while (n < len) {
  263.     if (c == '"') {
  264.       if (inString) {
  265.     token[n-1] = '\0';
  266.     inString = false;
  267.       } else {
  268.     inString = true;
  269.     if (current == 0) {
  270.       url = &token[n];
  271.     } else if (current == 1) {
  272.       addDate = &token[n];
  273.     } else if (current == 2) {
  274.       lastVisit = &token[n];
  275.     } else if (current == 3) {
  276.       lastModified = &token[n];
  277.     }
  278.     current++;
  279.       }
  280.     }
  281.     c = token[n++];
  282.   }
  283.   if (url == NULL) return;
  284.   newR = RDF_GetResource(NULL, url, true);
  285.   addSlotValue(f, newR, gCoreVocab->RDF_parent, f->stack[f->depth-1],  
  286.            RDF_RESOURCE_TYPE, true);
  287.   /* addSlotValue(f, newR, gWebData->RDF_URL, (void*)copyString(url), 
  288.            RDF_STRING_TYPE, true); */
  289.   if (addDate != NULL)
  290.     {
  291.       addSlotValue(f, newR, gNavCenter->RDF_bookmarkAddDate, (void*)copyString(addDate), 
  292.            RDF_STRING_TYPE, true);
  293.     }
  294.   if (lastVisit != NULL)
  295.     {
  296.       addSlotValue(f, newR, gWebData->RDF_lastVisitDate, (void*)copyString(lastVisit), 
  297.            RDF_STRING_TYPE, true);
  298.     }
  299.   if (lastModified != NULL)
  300.     {
  301.       addSlotValue(f, newR, gWebData->RDF_lastModifiedDate, (void*)copyString(lastModified),
  302.            RDF_STRING_TYPE, true);
  303.     }
  304.   f->lastItem = newR;
  305. }
  306.  
  307.  
  308.  
  309. char *
  310. numericDate(char *url)
  311. {
  312.     char        *date = NULL;
  313.     int        len=0;
  314.  
  315.     if (!url) return NULL;
  316.     while (url[len])
  317.     {
  318.         if (!isdigit(url[len]))    break;
  319.         ++len;
  320.     }
  321.     if (len > 0)
  322.     {
  323.         if (date = getMem(len+1))
  324.         {
  325.             strncpy(date, url, len);
  326.         }
  327.     }
  328.     return(date);
  329. }
  330.  
  331.  
  332.  
  333. PRBool
  334. bookmarkSlotp (RDF_Resource s)
  335. {
  336.   return ((s == gCoreVocab->RDF_parent) || (s == gWebData->RDF_lastVisitDate) || (s == gWebData->RDF_description) ||
  337.       (s == gNavCenter->RDF_bookmarkAddDate) || (s == gWebData->RDF_lastModifiedDate) || 
  338.       (s == gCoreVocab->RDF_name));
  339. }
  340.  
  341.  
  342. void
  343. HT_WriteOutAsBookmarks1 (RDF rdf, PRFileDesc *fp, RDF_Resource u, RDF_Resource top, int indent)
  344. {
  345.     RDF_Cursor c = RDF_GetSources(rdf, u, gCoreVocab->RDF_parent, RDF_RESOURCE_TYPE, true);
  346.     RDF_Resource next;
  347.     char *date, *name, *url;
  348.     int loop;
  349.  
  350.     if (c == NULL) return;
  351.     if (u == top) {
  352.       name = RDF_GetResourceName(rdf, u);
  353.       ht_rjcprintf(fp, "<!DOCTYPE NETSCAPE-Bookmark-file-1>\n", NULL);
  354.       ht_rjcprintf(fp, "<!-- This is an automatically generated file.\n", NULL);
  355.       ht_rjcprintf(fp, "It will be read and overwritten.\n", NULL);
  356.       ht_rjcprintf(fp, "Do Not Edit! -->\n", NULL);
  357.  
  358.       ht_rjcprintf(fp, "<TITLE>%s</TITLE>\n", (name) ? name:"");
  359.       ht_rjcprintf(fp, "<H1>%s</H1>\n<DL><p>\n", (name) ? name:"");
  360.     }
  361.     while (next = RDF_NextValue(c)) {
  362.  
  363.       url = resourceID(next);
  364.       if (containerp(next) && (!startsWith("ftp:",url)) && (!startsWith("file:",url))
  365.         && (!startsWith("IMAP:", url)) && (!startsWith("nes:", url))
  366.         && (!startsWith("mail:", url)) && (!startsWith("cache:", url))
  367.         && (!startsWith("ldap:", url))) {
  368.         for (loop=0; loop<indent; loop++)    ht_rjcprintf(fp, "    ", NULL);
  369.  
  370.         date = numericDate(resourceID(next));
  371.         ht_rjcprintf(fp, "<DT><H3 ADD_DATE=\"%s\">", (date) ? date:"");
  372.         if (date) freeMem(date);
  373.         name = RDF_GetResourceName(rdf, next);
  374.         ht_rjcprintf(fp, "%s</H3>\n", name);
  375.  
  376.         for (loop=0; loop<indent; loop++)    ht_rjcprintf(fp, "    ", NULL);
  377.         ht_rjcprintf(fp, "<DL><p>\n", NULL);
  378.         HT_WriteOutAsBookmarks1(rdf, fp, next, top, indent+1);
  379.  
  380.         for (loop=0; loop<indent; loop++)    ht_rjcprintf(fp, "    ", NULL);
  381.  
  382.         ht_rjcprintf(fp, "</DL><p>\n", NULL);
  383.       }
  384.       else if (isSeparator(next)) {
  385.     for (loop=0; loop<indent; loop++)    ht_rjcprintf(fp, "    ", NULL);
  386.     ht_rjcprintf(fp, "<HR>\n", NULL);
  387.       }
  388.       else {
  389.     char* bkAddDate = (char*)RDF_GetSlotValue(rdf, next, 
  390.                           gNavCenter->RDF_bookmarkAddDate, 
  391.                           RDF_STRING_TYPE, false, true);
  392.  
  393.         for (loop=0; loop<indent; loop++)    ht_rjcprintf(fp, "    ", NULL);
  394.  
  395.     ht_rjcprintf(fp, "<DT><A HREF=\"%s\" ", resourceID(next));
  396.     date = numericDate(bkAddDate);
  397.     ht_rjcprintf(fp, "ADD_DATE=\"%s\" ", (date) ? date: "");
  398.     if (date) freeMem(date);
  399.     ht_rjcprintf(fp, "LAST_VISIT=\"%s\" ", resourceLastVisitDate(rdf, next));
  400.     ht_rjcprintf(fp, "LAST_MODIFIED=\"%s\">", resourceLastModifiedDate(rdf, next));
  401.     ht_rjcprintf(fp, "%s</A>\n", RDF_GetResourceName(rdf, next));
  402.  
  403.     if (resourceDescription(rdf, next) != NULL) {
  404.       ht_rjcprintf(fp, "<DD>%s\n", resourceDescription(rdf, next));
  405.     }
  406.       }
  407.     }
  408.     RDF_DisposeCursor(c);
  409.     if (u == top) {
  410.       ht_rjcprintf(fp, "</DL>\n", NULL);
  411.     }
  412. }
  413.  
  414.  
  415.  
  416. PR_PUBLIC_API(void)
  417. HT_WriteOutAsBookmarks (RDF r, PRFileDesc *fp, RDF_Resource u)
  418. {
  419.     HT_WriteOutAsBookmarks1 (r, fp, u, u, 1);
  420. }
  421.  
  422.  
  423.  
  424. void
  425. flushBookmarks()
  426. {
  427.     PRFileDesc        *bkfp;
  428.  
  429.     if (gBookmarkURL != NULL)
  430.     {
  431.         /*
  432.           delete bookmark.htm as PROpen() with PR_TRUNCATE appears broken (at least on Mac)
  433.         */
  434.         CallPRDeleteFileUsingFileURL(gBookmarkURL);
  435.  
  436.         if ((bkfp = CallPROpenUsingFileURL(gBookmarkURL, (PR_WRONLY|PR_CREATE_FILE|PR_TRUNCATE),
  437.                         0644)) != NULL)
  438.         {
  439.             HT_WriteOutAsBookmarks(gNCDB, bkfp, gNavCenter->RDF_BookmarkFolderCategory);
  440.             PR_Close(bkfp);
  441.         }
  442.     }
  443. }
  444.