/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ /* This file implements parsing support for the rdf data model. For more information on this file, contact rjc or guha For more information on RDF, look at the RDF section of www.mozilla.org */ #include "rdfparse.h" #include "mcf.h" #include "mcff2mcf.h" #include "plstr.h" #define wsCharp(c) ((c == '\r') || (c == '\t') || (c == ' ') || (c == '\n')) char decodeEntityRef (char* string, int32* stringIndexPtr, int32 len) { if (startsWith("lt;", string)) { *stringIndexPtr = *stringIndexPtr + 3; return '<'; } else if (startsWith("gt;", string)) { *stringIndexPtr = *stringIndexPtr + 3; return '>'; } else if (startsWith("amp;", string)) { *stringIndexPtr = *stringIndexPtr + 4; return '&'; } else return -1; } char * copyStringIgnoreWhiteSpace(char* string) { int32 len = strlen(string); char* buf = (char*)getMem(len + 1); PRBool inWhiteSpace = 1; int32 buffIndex = 0; int32 stringIndex = 0; while (stringIndex < len) { char nextChar = *(string + stringIndex); PRBool wsp = wsCharp(nextChar); if (!wsp) { if (nextChar == '&') { *(buf + buffIndex++) = decodeEntityRef(&string[stringIndex+1], &stringIndex, len-stringIndex); } else { *(buf + buffIndex++) = nextChar; } inWhiteSpace = 0; } else if (!inWhiteSpace) { *(buf + buffIndex++) = ' '; inWhiteSpace = 1; } else { inWhiteSpace = 1; } stringIndex++; } return buf; } char * getHref(char** attlist) { char* ans = getAttributeValue(attlist, "rdf:href"); if (!ans) ans = getAttributeValue(attlist, "RDF:href"); if (!ans) ans = getAttributeValue(attlist, "href"); return ans; } int parseNextRDFXMLBlobInt(RDFFile f, char* blob, int32 size) { int32 n, last, m; PRBool somethingseenp = 0; n = last = 0; while (n < size) { char c = blob[n]; m = 0; somethingseenp = 0; memset(f->line, '\0', RDF_BUF_SIZE-1); if (f->holdOver[0] != '\0') { memcpy(f->line, f->holdOver, strlen(f->holdOver)); m = strlen(f->holdOver); somethingseenp = 1; memset(f->holdOver, '\0', RDF_BUF_SIZE-1); } while ((n < size) && (wsc(c))) {c = blob[++n];} /* f->line[m++] = c; c = blob[++n]; */ while ((m < RDF_BUF_SIZE) && (c != '<') && (c != '>')) { f->line[m] = c; m++; somethingseenp = (somethingseenp || (!(wsc(c)))); n++; if (n < size) c = blob[n]; else break; } if (c == '>') f->line[m] = c; n++; if (m > 0) { if ((c == '<') || (c == '>')) { last = n; if (c == '<') f->holdOver[0] = '<'; if (somethingseenp == 1) parseNextRDFToken(f, f->line); } else if (size > last) { memcpy(f->holdOver, f->line, m); } } else if (c == '<') f->holdOver[0] = '<'; } return(size); } int parseNextRDFXMLBlob (NET_StreamClass *stream, char* blob, int32 size) { RDFFile f; f = (RDFFile)stream->data_object; if ((f == NULL) || (size < 0)) { return MK_INTERRUPTED; } return parseNextRDFXMLBlobInt(f, blob, size); } void parseRDFProcessingInstruction (RDFFile f, char* token) { char* attlist[2*MAX_ATTRIBUTES+1]; char* elementName; tokenizeElement(token, attlist, &elementName); if (strcmp(elementName, "?xml:namespace") == 0) { char* as = getAttributeValue(attlist, "prefix"); char* url = getAttributeValue(attlist, "ns"); if ((as != NULL) && (url != NULL)) { XMLNameSpace ns = (XMLNameSpace)getMem(sizeof(struct XMLNameSpaceStruct)); size_t urln = strlen(url); XP_Bool addSlash = (url[urln-1] != '/'); if(addSlash) urln++; ns->url = (char*)getMem(sizeof(char) * (urln + 1)); sprintf(ns->url, "%s%s", url, addSlash ? "/" : ""); ns->as = XP_STRDUP(as); ns->next = f->namespaces; f->namespaces = ns; } } } void freeNamespaces (RDFFile f) { XMLNameSpace ns1 = f->namespaces; while (ns1) { XMLNameSpace next = ns1->next; freeMem(ns1->as); freeMem(ns1->url); freeMem(ns1); ns1 = next; } f->namespaces = NULL; } PR_PUBLIC_API(char *) getAttributeValue (char** attlist, char* elName) { size_t n = 0; if (!attlist) return NULL; while ((n < 2*MAX_ATTRIBUTES) && (*(attlist + n) != NULL)) { if (strcmp(*(attlist + n), elName) == 0) return *(attlist + n + 1); n = n + 2; } return NULL; } PRBool tagEquals (RDFFile f, char* tag1, char* tag2) { return (strcmp(tag1, tag2) == 0); } void addElementProps (char** attlist, char* elementName, RDFFile f, RDF_Resource obj) { uint32 count = 0; char *tv = NULL; tv = getAttributeValue(attlist, "tv"); if (tv == NULL) tv = "true"; while (count < 2*MAX_ATTRIBUTES) { char* attName = attlist[count++]; char* attValue = attlist[count++]; if ((attName == NULL) || (attValue == NULL)) break; if (!tagEquals(f, attName, "href") && !tagEquals(f, attName, "rdf:href") && !tagEquals(f, attName, "RDF:href") && !tagEquals(f, attName, "tv") && !tagEquals(f, attName, "id")) { addSlotValue(f, obj, ResourceFromElementName(f, attName), copyStringIgnoreWhiteSpace(attValue), RDF_STRING_TYPE, tv); } } } PRBool knownObjectElement (char* eln) { return (strcmp(eln, "RDF:Description") == 0); } char * possiblyMakeAbsolute (RDFFile f, char* url) { if (strchr(url, ':') != NULL) { return copyString(url); } else { char* ans = getMem(strlen(f->url) + strlen(url)+2); sprintf(ans, "%s#%s", f->url, url); return ans; } } PRBool containerTagp (RDFFile f, char* elementName) { return (tagEquals(f, elementName, "Container") || tagEquals(f, elementName, "Topic") || (tagEquals(f, elementName, "RelatedLinks"))); } #define DC_TITLE "http://purl.org/metadata/dublin_core/title" #define SM_CHILD "http://purl.org/metadata/sitemap/child" RDF_Resource ResourceFromElementName (RDFFile f, char* elementName) { if(!elementName || (strchr(elementName, ':') == NULL) ) { return RDF_GetResource(NULL, elementName, 1); } else { XMLNameSpace ns = f->namespaces; while (ns) { if (startsWith(ns->as, elementName)) { RDF_Resource ans; size_t asn = strlen(ns->as); size_t urln = strlen(ns->url); char* url = getMem(strlen(ns->url) + strlen(elementName)-asn); memcpy(url, ns->url, urln); strcat(url, &elementName[asn+1]); if (strcmp(url, DC_TITLE) == 0) { ans = gCoreVocab->RDF_name; } else if (strcmp(url, SM_CHILD) == 0) { ans = gCoreVocab->RDF_child; } else ans = RDF_GetResource(NULL, url, 1); freeMem(url); return ans; } ns = ns->next; } return RDF_GetResource(NULL, elementName, 1); } } void parseNextRDFToken (RDFFile f, char* token) { char* attlist[2*MAX_ATTRIBUTES+1]; char* elementName; if (token[0] != '<') { if ((f->status == EXPECTING_OBJECT) && (f->depth > 1)) { RDF_Resource u = f->stack[f->depth-2]; RDF_Resource s = f->stack[f->depth-1]; addSlotValue(f, u, s, copyStringIgnoreWhiteSpace(token), RDF_STRING_TYPE, NULL); } } else if (startsWith("