1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/*
This is some glue code for a semi-parsing CSS. Its used to
output html from XML.
This will be changed when we incorporate the real xml parser
from James Clark will replace this within the next few weeks.
For more information on this file, contact rjc or guha
For more information on RDF, look at the RDF section of www.mozilla.org
For more info on XML in navigator, look at the XML section
of www.mozilla.org.
*/
#include "xmlss.h"
#define href "href"
#define XLL "XML-Link"
#define BUF_SIZE 4096
int
parseNextXMLCSSBlob (NET_StreamClass *stream, char* blob, int32 size)
{
StyleSheet ss;
int32 n, last, m;
PRBool somethingseenp = false;
n = last = 0;
ss = (StyleSheet)(stream->data_object);
if ((ss == NULL) || (size < 0)) {
return MK_INTERRUPTED;
}
while (n < size) {
char c = blob[n];
m = 0;
memset(ss->line, '\0', ss->lineSize);
if (ss->holdOver[0] != '\0') {
memcpy(ss->line, ss->holdOver, strlen(ss->holdOver));
m = strlen(ss->holdOver);
somethingseenp = 1;
memset(ss->holdOver, '\0', BUF_SIZE);
}
while ((m < 300) && (c != '{') && (c != '}')) {
ss->line[m] = c;
m++;
somethingseenp = (somethingseenp || ((c != ' ') &&
(c != '\r') && (c != '\n')));
n++;
if (n < size) {
c = blob[n];
} else break;
}
if (c == '}') ss->line[m] = c;
n++;
if (m > 0) {
if ((c == '}') || (c == '{')) {
last = n;
if (c == '{') ss->holdOver[0] = '{';
if (somethingseenp) {
parseNextXMLCSSElement(ss, ss->line);
}
} else if (size > last) {
memcpy(ss->holdOver, ss->line, m);
}
} else if (c == '{') ss->holdOver[0] = '{';
}
return(size);
}
static int32 style_id_counter = 0;
void
parseNextXMLCSSElement (StyleSheet ss, char* ele)
{
if (ele[0] == '{') {
char* sty = getMem(strlen(ele)-1);
memcpy(sty, &ele[1], strlen(ele)-2);
ss->el->style = sty;
} else {
StyleElement se = (StyleElement) getMem(sizeof(StyleElementStruct));
size_t numTags, size, n, count;
se->id = ++style_id_counter;
se->next = ss->el;
ss->el = se;
size = strlen(ele);
count = n = 0;
numTags = countTagStackSize(ele);
while (count < numTags) {
size_t st = 0;
char** tgs = (char**) getMem(numTags * sizeof(char**));
se->tagStack = tgs;
while (st < numTags) {
*(tgs + st++) = getNextSSTag(ele, &n);
}
count++;
}
}
}
size_t
countSSNumTags (char* ele)
{
size_t n = 0;
size_t m = 1;
size_t sz = strlen(ele);
while (n < sz) {
if (ele[n++] == ',') m++;
}
return m;
}
size_t
countTagStackSize (char* ele)
{
size_t n = 0;
size_t m = 0;
PRBool inSpace = 1;
size_t sz = strlen(ele);
while (n < sz) {
if (ele[n] == ',') break;
if ((ele[n] != ' ') && (ele[n] != '\r') && (ele[n] != '\n')) {
if (inSpace) {
m++;
inSpace = 0;
}
} else inSpace = 1;
n++;
}
return m;
}
char*
getNextSSTag (char* ele, size_t* n)
{
char tag[100];
size_t m = 0;
char c = ele[*n];
size_t s = strlen(ele);
memset(tag, '\0', 100);
while ((c == '\r') || (c == ' ') || (c == '\n')) {
*n = *n + 1;
c = ele[*n];
}
while ((c != ' ') && (c != ',') && (*n < s)) {
tag[m++] = c;
*n = *n + 1;
c = ele[*n];
}
return copyString(tag);
}
void
outputAttributes(XMLFile f, char** attlist)
{
uint32 n = 0;
PRBool xmlinkp = (getAttributeValue(attlist, XLL) != NULL);
if (!attlist) return ;
while ((n < 2*MAX_ATTRIBUTES) && (*(attlist + n) != NULL)) {
if (!(xmlinkp && stringEquals(*(attlist + n), href))) {
outputToStream(f, *(attlist + n));
outputToStream(f, "=\"");
outputToStream(f, *(attlist + n + 1));
outputToStream(f, "\" ");
}
n = n + 2;
}
}
void
outputAsHTML (XMLFile f, XMLElement el)
{
if (strcmp(el->tag, "xml:content") == 0) {
outputToStream(f, el->content);
} else {
char* htmlEquiv = getAttributeValue(el->attributes, "html");
char* linkTag = getAttributeValue(el->attributes, XLL);
char* htmlBuff = getMem(1024);
XMLElement child = el->child;
PRBool suppressTag = !startsWith("html:", el->tag) ;
if (linkTag && (stringEquals(linkTag, "LINK"))) {
char* hrefVal = getAttributeValue(el->attributes, href);
if (hrefVal) {
char* type = getAttributeValue(el->attributes, "Role");
char* show = getAttributeValue(el->attributes, "Show");
if (show &&(strcmp(show, "EMBED") == 0)) {
if (type && (strcmp(type, "HTML") == 0)) {
int32 n = 0;
XMLHTMLInclusion incl = (XMLHTMLInclusion) el->content;
while (incl && *(incl->content + n)) {
outputToStream(f, (*(incl->content + n)));
n++;
}
} else if (type && (strcmp(type, "IMAGE") == 0)) {
sprintf(htmlBuff, "<img src=%s>\n", hrefVal);
outputToStream(f, htmlBuff);
}
freeMem(htmlBuff);
return;
} else {
sprintf(htmlBuff, "<a href=\"%s\">", hrefVal);
outputToStream(f, htmlBuff);
}
} else {
linkTag = 0;
}
}
outputStyleSpan(f, el, 0);
if (htmlEquiv) {
sprintf(htmlBuff, "<%s ", htmlEquiv);
outputToStream(f, htmlBuff);
outputAttributes(f, el->attributes);
outputToStream(f, ">\n");
} else if (!suppressTag){
sprintf(htmlBuff, "<%s ", &((el->tag)[5]));
outputToStream(f, htmlBuff);
outputAttributes(f, el->attributes);
outputToStream(f, ">\n");
}
while (child) {
outputAsHTML(f, child);
child = child->next;
}
if (htmlEquiv) {
sprintf(htmlBuff, "</%s>\n", htmlEquiv);
outputToStream(f, htmlBuff);
} else if (!suppressTag) {
sprintf(htmlBuff, "</%s>\n", &((el->tag)[5]));
outputToStream(f, htmlBuff);
}
outputStyleSpan(f, el, 1);
if (linkTag) outputToStream(f, "</a>");
freeMem(htmlBuff);
}
}
void
outputAllStyles(XMLFile xf)
{
char *ssline;
StyleSheet ss ;
StyleElement se;
outputToStream(xf, "<style>\n");
for (ss = xf->ss; (ss != NULL) ; ss = ss->next) {
for (se = ss->el; (se != NULL) ; se = se->next) {
if (se->style) {
ssline = PR_smprintf(".xml%d {%s}\n", se->id, se->style);
PR_ASSERT(ssline);
if (!ssline)
return;
outputToStream(xf, ssline);
free(ssline);
}
}
}
outputToStream(xf, "</style>\n");
}
void
convertToHTML (XMLFile xf) {
XMLElement el = xf->top;
xf->numOpenStreams--;
if (xf->numOpenStreams < 1) {
outputToStream(xf, "<html><body>");
outputAllStyles(xf);
outputAsHTML(xf, el);
outputToStream(xf, "</body></html>");
}
}
void
outputStyleSpan (XMLFile f, XMLElement el, PRBool endp)
{
char *ssid;
StyleSheet ss ;
StyleElement se;
for (ss = f->ss; (ss != NULL) ; ss = ss->next) {
for (se = ss->el; (se != NULL) ; se = se->next) {
if (se->style && stringEquals((se->tagStack)[0], el->tag)) {
/* check for se->style from Steve Wingard <swingard@spyglass.com> */
PRBool divp = startsWith("Display:Block;", se->style);
PRBool listp = startsWith("Display:List-item;", se->style);
if (!endp) {
if (divp) {
outputToStream(f, "<div ");
} else if (listp) {
outputToStream(f, "<UL><LI><span ");
} else {
outputToStream(f, "<span ");
}
ssid=PR_smprintf("class=xml%d>\n", se->id);
PR_ASSERT(ssid);
if (!ssid)
return;
outputToStream(f, ssid);
free(ssid);
} else {
if (divp) {
outputToStream(f, "</div>");
} else if (listp) {
outputToStream(f, "</UL></span>");
} else {
outputToStream(f, "</span>");
}
}
}
}
}
}
|