1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the NPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the NPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
43
char* error_string = NULL;
46
static HashTable resourceHash = NULL;
47
static RDF_Resource gURL = NULL;
50
getResource (char* key, int createp) {
51
RDF_Resource existing = (RDF_Resource) HashLookup(resourceHash, key);
55
existing = (RDF_Resource)fgetMem(sizeof(RDF_ResourceStruct));
56
existing->url = fcopyString(key);
57
HashAdd(resourceHash, existing->url, existing);
59
gURL = (RDF_Resource)fgetMem(sizeof(RDF_ResourceStruct));
60
gURL->url = fcopyString("URL");
61
HashAdd(resourceHash, gURL->url, gURL);
63
remoteStoreAdd(NULL, existing, gURL, existing->url, RDF_STRING_TYPE,1);
69
RDF_ResourceID (RDF_Resource u) {
73
static char* MemBlock = 0;
75
#define MEM_BLOCK_SIZE 10000
78
fgetMem (size_t rsize) {
80
size_t size = rsize + (4 - ldiv(rsize, 4).rem);
81
if (!MemBlock || (size >= (MEM_BLOCK_SIZE - allocated))) {
82
MemBlock = getMem(MEM_BLOCK_SIZE);
86
MemBlock = MemBlock + size;
87
allocated = allocated + size;
91
void readRDFFile (char* file) {
92
FILE* f = fopen(file, "r");
94
RDFT rf = (RDFT)getRDFT(file, 1) ;
96
char* buff = (char*) malloc(100 * 1024);
99
memset(buff, '\0', (100 * 1024));
100
memset(rf, '\0', sizeof(RDF_FileStruct));
102
rf->line = (char*)getMem(RDF_BUF_SIZE);
103
rf->holdOver = (char*)getMem(RDF_BUF_SIZE);
105
rf->lastItem = rf->stack[0] ;
106
while ((len = fread(buff, 1, (100 * 1024) -1, f)) > 0) {
108
printf("[%i] ", i++);
110
if (!(ok = parseNextRDFXMLBlobInt(rf, buff, len))) {
111
printf("Error in RDF File\n");
117
freeMem(rf->holdOver);
120
printf("Finished reading %s\n", file);
121
} else printf("Could not find %s\n", file);
124
static HashTable rdftHash = NULL;
127
getRDFT (char* key, int createp) {
128
RDFT existing = (RDFT) HashLookup(rdftHash, key);
132
existing = (RDFT)getMem(sizeof(RDF_FileStruct));
133
existing->url = fcopyString(key);
134
HashAdd(rdftHash, existing->url, existing);
141
error_string = getMem(1000);
142
resourceHash = NewHashTable((int)0x00000FFF);
143
rdftHash = NewHashTable((int)0x00000FFF);
147
rdf_DigestNewStuff (char* url, char* data, int len) {
148
RDFT rf = (RDFT)getRDFT(url, 1) ;
152
memset(rf, '\0', sizeof(RDF_FileStruct));
153
rf->line = (char*)getMem(RDF_BUF_SIZE);
154
rf->holdOver = (char*)getMem(RDF_BUF_SIZE);
156
rf->lastItem = rf->stack[0] ;
157
ok = parseNextRDFXMLBlobInt(rf, data, len);
158
/* if (!ok) unloadRDFT(rf); */
161
freeMem(rf->holdOver);
168
startsWith (const char* pattern, const char* uuid) {
169
int l1 = strlen(pattern);
170
int l2 = strlen(uuid);
172
if (l2 < l1) return 0;
173
for (n = 0; n < l1; n++) {
174
if (pattern[n] != uuid[n]) return 0;
181
return (char*) calloc(1, n);
185
freeMem(void* item) {
190
decodeEntityRef (char* string, int* stringIndexPtr, int len) {
191
if (startsWith("lt;", string)) {
192
*stringIndexPtr = *stringIndexPtr + 3;
194
} else if (startsWith("gt;", string)) {
195
*stringIndexPtr = *stringIndexPtr + 3;
197
} else if (startsWith("amp;", string)) {
198
*stringIndexPtr = *stringIndexPtr + 4;
204
copyStringIgnoreWhiteSpace(char* string)
206
int len = strlen(string);
207
char* buf = (char*)fgetMem(len + 1);
208
int inWhiteSpace = 1;
212
while (stringIndex < len) {
213
char nextChar = *(string + stringIndex);
214
int wsp = wsCharp(nextChar);
216
if (nextChar == '&') {
217
*(buf + buffIndex++) = decodeEntityRef(&string[stringIndex+1],
218
&stringIndex, len-stringIndex);
220
*(buf + buffIndex++) = nextChar;
223
} else if (!inWhiteSpace) {
224
*(buf + buffIndex++) = ' ';
236
getHref(char** attlist)
238
char* ans = getAttributeValue(attlist, "resource");
239
if (!ans) ans = getAttributeValue(attlist, "rdf:resource");
245
getID(char** attlist)
247
char* ans = getAttributeValue(attlist, "id");
248
if (!ans) ans = getAttributeValue(attlist, "about");
249
if (!ans) ans = getAttributeValue(attlist, "rdf:about");
255
parseNextRDFXMLBlobInt(RDFT f, char* blob, int size) {
257
int somethingseenp = 0;
261
if ((c == '\n') || (c == '\r')) lineNumber++;
264
/* memset(f->line, '\0', RDF_BUF_SIZE-1); */
265
if (f->holdOver[0] != '\0') {
266
memcpy(f->line, f->holdOver, strlen(f->holdOver));
267
m = strlen(f->holdOver);
269
f->holdOver[0] = '\0';
270
/* memset(f->holdOver, '\0', RDF_BUF_SIZE-1); */
272
while ((n < size) && (wsCharp(c)) && (!somethingseenp)) {
274
if ((c == '\n') || (c == '\r')) lineNumber++;
276
while ((m < RDF_BUF_SIZE-1) && (c != '<') && (c != '>')) {
279
somethingseenp = (somethingseenp || (!(wsCharp(c))));
281
if (n < size) c = blob[n];
283
if ((c == '\n') || (c == '\r')) lineNumber++;
287
if (c == '>') f->line[m] = c;
290
if ((c == '<') || (c == '>')) {
293
f->holdOver[0] = '<';
294
f->holdOver[1] = '\0';
296
if (somethingseenp == 1) {
297
parseNextRDFToken(f, f->line);
299
} else if (size > last) {
300
memcpy(f->holdOver, f->line, m);
301
f->holdOver[m] = '\0';
303
} else if (c == '<') {
304
f->holdOver[0] = '<';
305
f->holdOver[1] = '\0';
312
getAttributeValue (char** attlist, char* elName)
315
if (!attlist) return NULL;
316
while ((n < 2*MAX_ATTRIBUTES) && (*(attlist + n) != NULL)) {
317
char* attname = *(attlist + n);
318
char* base = strchr(attname, ':');
319
if (base) attname = base + 1;
320
if (strcmp(attname, elName) == 0) return *(attlist + n + 1);
327
copyString (char* str) {
328
char* ans = getMem(strlen(str)+1);
330
memcpy(ans, str, strlen(str));
336
fcopyString (char* str) {
337
char* ans = fgetMem(strlen(str)+1);
339
memcpy(ans, str, strlen(str));
347
addElementProps (char** attlist, char* elementName, RDFT f, RDF_Resource obj)
350
while (count < 2*MAX_ATTRIBUTES) {
351
char* attName = attlist[count++];
352
char* attValue = attlist[count++];
354
if ((attName == NULL) || (attValue == NULL)) break;
355
baseName = strchr(attName, ':');
356
if (baseName) attName = baseName + 1;
357
if (startsWith("xmlns", attName)) {
358
/* addNameSpace(attName, attValue, f); */
359
} else if (!stringEquals(attName, "resource") &&
360
!stringEquals(attName, "rdf:resource") &&
361
!stringEquals(attName, "about") &&
362
!stringEquals(attName, "rdf:about") &&
363
!stringEquals(attName, "tv") &&
364
!stringEquals(attName, "id")) {
365
remoteStoreAdd(f, obj, getResource(attName, 1),
366
copyStringIgnoreWhiteSpace(attValue),
373
parseNextRDFToken (RDFT f, char* token)
375
char* attlist[2*MAX_ATTRIBUTES+1];
378
if (token[0] != '<') {
379
if ((f->status == EXPECTING_OBJECT) && (f->depth > 1)) {
380
RDF_Resource u = f->stack[f->depth-2];
381
RDF_Resource s = f->stack[f->depth-1];
382
char* val = copyStringIgnoreWhiteSpace(token);
383
remoteStoreAdd(f, u, s, val , RDF_STRING_TYPE, 1);
386
printf(error_string, "Did not expect \n\"%s\".\n Was expecting a tag.", token);
389
} else if (startsWith("<!--", token)) {
391
} else if (token[1] == '?') {
393
} else if (token[1] == '/') {
394
if ((f->status != EXPECTING_OBJECT) && (f->status != EXPECTING_PROPERTY)) {
395
printf(error_string, "Did not expect %s. Something pretty screwed up", token);
398
if (f->depth > 0) f->depth--;
399
f->status = (f->status == EXPECTING_OBJECT ? EXPECTING_PROPERTY : EXPECTING_OBJECT);
401
} else if ((f->status == 0) && (startsWith("<RDF:RDF", token) ||
402
startsWith("<RDF", token))) {
403
f->status = EXPECTING_OBJECT;
406
int emptyElementp = (token[strlen(token)-2] == '/');
407
if ((f->status != EXPECTING_OBJECT) && (f->status != EXPECTING_PROPERTY)) return 1;
408
if (!tokenizeElement(token, attlist, &elementName)) return 0;
409
if (f->status == EXPECTING_OBJECT) {
413
url = getID(attlist);
415
if (f->tagDepth > 2) {
416
printf(error_string, "Unbalanced tags ");
418
printf(error_string, "Require a \"about\" attribute on %s", token);
422
obj = getResource(url, 1);
423
addElementProps (attlist, elementName, f, obj) ;
424
if (!stringEquals(elementName, "RDF:Description")) {
425
RDF_Resource eln = getResource(elementName, 1);
426
remoteStoreAdd(f, obj, getResource("type", 1),
427
eln, RDF_RESOURCE_TYPE,
431
remoteStoreAdd(f, f->stack[f->depth-2], f->stack[f->depth-1], obj,
432
RDF_RESOURCE_TYPE, 1);
434
if (!emptyElementp) {
435
f->stack[f->depth++] = obj;
436
f->status = EXPECTING_PROPERTY;
438
} else if (f->status == EXPECTING_PROPERTY) {
442
url = getHref(attlist) ;
444
RDF_Resource eln = getResource(elementName, 1);
445
obj = getResource(url, 1);
446
addElementProps (attlist, elementName, f, obj) ;
447
remoteStoreAdd(f, f->stack[f->depth-1], eln, obj, RDF_RESOURCE_TYPE, 1);
448
/* printf("%s %s %s\n", RDF_ResourceID(f->stack[f->depth-1]),
449
RDF_ResourceID(eln), url); */
451
if (!emptyElementp) {
452
f->stack[f->depth++] = getResource(elementName, 1);
453
f->status = EXPECTING_OBJECT;
463
tokenizeElement (char* attr, char** attlist, char** elementName)
466
size_t s = strlen(attr);
471
int emptyTagp = (attr[s-2] == '/');
477
*elementName = &attr[n-1];
479
if (wsCharp(c)) break;
483
while (atc < 2*MAX_ATTRIBUTES+1) {*(attlist + atc++) = NULL;}
485
s = (emptyTagp ? s-2 : s-1);
487
int attributeOpenStringSeenp = 0;
490
while ((n <= s) && (atc < 2*MAX_ATTRIBUTES)) {
491
if (inAttrNamep && (m > 0) && (wsCharp(c) || (c == '='))) {
493
*(attlist + atc++) = &attr[n-m-1];
496
if (!inAttrNamep && attributeOpenStringSeenp && (c == '"')) {
498
*(attlist + atc++) = &attr[n-m-1];
502
if ((m > 0) || (!wsCharp(c))) m++;
505
attributeOpenStringSeenp = 1;
507
if ((m > 0) || (!(wsCharp(c)))) m++;
512
inAttrNamep = (inAttrNamep ? 0 : 1);
514
base = strchr(*elementName, ':');
515
if (base) *elementName = base+1;