Menu

[r11]: / branches / dasho / DOM / src / DOMImplementation.C  Maximize  Restore  History

Download this file

125 lines (94 with data), 2.9 kB

  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
#include "DOM.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
boolean DOMImplementation::hasFeature(String feature, String version)
{
for (int i=0; i<strlen(feature); i++)
feature[i] = toupper(feature[i]);
if ( strcmp(feature, "XML")==0 && strcmp(version, "1.0")==0 )
return true;
else return 0;
}
Node* DOMImplementation::setCurrentNode(Node* newNode)
{
Node* oldNode = getCurrentNode();
currentNode = newNode;
return oldNode;
}
boolean DOMImplementation::docParse(String xmlFile, String dtdFile)
{
if ( xmlParse(xmlFile) == NULL )
{
fprintf(stderr, "couldn't parse xml file: %s \n", xmlFile);
return 0;
}
if ( dtdParse(dtdFile) == NULL )
{
fprintf(stderr, "couldn't parse dtd file: %s \n", dtdFile);
return 0;
}
return 1;
}
//////////////////// xmlParse ////////////////////////////////
#include "callbacks.c"
DocumentFragment* DOMImplementation::xmlParse(String fileName)
{
Document* doc = getOwnerDoc();
DocumentFragment* docTree = doc->createDocumentFragment();
setCurrentNode(docTree);
depth = 0; // used for debugging expat, delete this later
XML_Parser parser = XML_ParserCreate(NULL);
XML_SetUserData(parser, this);
// set event handlers
installCallbacs(parser);
// open file
FILE* inf = fopen(fileName, "r");
if (inf == NULL)
{
fprintf(stderr, "DOMImplementation::xmlParse: cannot open file %s for reading\n", fileName);
return NULL;
}
// parse
char buf[BUFSIZ];
int done;
do
{
size_t len = fread(buf, 1, sizeof(buf), inf);
done = len < sizeof(buf);
if (!XML_Parse(parser, buf, len, done))
{
fprintf(stderr, "%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser));
return NULL; // not parsed correctly
}
} while (!done);
XML_ParserFree(parser);
Element* docRoot = doc->createElement("xml");
doc->setDocumentElement(docRoot);
docRoot->appendChild(docTree);
return docTree;
}
///////////////// dtdParse ////////////////////////
#include "y.tab.c"
DocumentType* DOMImplementation::dtdParse(String fileName)
{
// redirect stdin to fileName
if ( freopen(fileName, "r", stdin) == NULL )
{
fprintf(stderr, "DOMImplementation::dtdParse: cannot open file: %s for reading", fileName);
return NULL;
}
// initialize global variables used by dtd parser
xmlDoc = getOwnerDoc(); // the owner of this DOMImplementation instance
docType = xmlDoc->getDoctype();
elems = docType->getElements();
attribs = docType->getAttributes();
// now parse by calling "yyparse"
// this builds the dtd structure
try { yyparse(); }
catch (DOMException& e)
{ cout << e << endl; return NULL; }
return docType;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.