adding a bunch of the api (which isn't yet hooked up.)

This commit is contained in:
Lee Thomason 2012-02-10 08:50:51 -08:00
parent 56bdd0259e
commit 751da529d9
2 changed files with 136 additions and 110 deletions

View File

@ -235,18 +235,18 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
} }
bool XMLDocument::Accept( XMLVisitor* visitor ) const bool XMLDocument::Accept( XMLVisitor* visitor ) const
{ {
if ( visitor->VisitEnter( *this ) ) if ( visitor->VisitEnter( *this ) )
{ {
for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
{ {
if ( !node->Accept( visitor ) ) if ( !node->Accept( visitor ) )
break; break;
} }
} }
return visitor->VisitExit( *this ); return visitor->VisitExit( *this );
} }
// --------- XMLNode ----------- // // --------- XMLNode ----------- //
@ -363,12 +363,13 @@ void XMLNode::DeleteChild( XMLNode* node )
} }
void XMLNode::Print( XMLStreamer* streamer ) /*void XMLNode::Print( XMLStreamer* streamer )
{ {
for( XMLNode* node = firstChild; node; node=node->next ) { for( XMLNode* node = firstChild; node; node=node->next ) {
node->Print( streamer ); node->Print( streamer );
} }
} }
*/
char* XMLNode::ParseDeep( char* p ) char* XMLNode::ParseDeep( char* p )
@ -401,11 +402,13 @@ char* XMLText::ParseDeep( char* p )
} }
/*
void XMLText::Print( XMLStreamer* streamer ) void XMLText::Print( XMLStreamer* streamer )
{ {
const char* v = value.GetStr(); const char* v = value.GetStr();
streamer->PushText( v ); streamer->PushText( v );
} }
*/
bool XMLText::Accept( XMLVisitor* visitor ) const bool XMLText::Accept( XMLVisitor* visitor ) const
@ -427,12 +430,14 @@ XMLComment::~XMLComment()
} }
/*
void XMLComment::Print( XMLStreamer* streamer ) void XMLComment::Print( XMLStreamer* streamer )
{ {
// XMLNode::Print( fp, depth ); // XMLNode::Print( fp, depth );
// fprintf( fp, "<!--%s-->\n", value.GetStr() ); // fprintf( fp, "<!--%s-->\n", value.GetStr() );
streamer->PushComment( value.GetStr() ); streamer->PushComment( value.GetStr() );
} }
*/
char* XMLComment::ParseDeep( char* p ) char* XMLComment::ParseDeep( char* p )
@ -442,10 +447,10 @@ char* XMLComment::ParseDeep( char* p )
} }
bool XMLComment::Accept( XMLVisitor* visitor ) const bool XMLComment::Accept( XMLVisitor* visitor ) const
{ {
return visitor->Visit( *this ); return visitor->Visit( *this );
} }
// --------- XMLAttribute ---------- // // --------- XMLAttribute ---------- //
@ -462,13 +467,14 @@ char* XMLAttribute::ParseDeep( char* p )
} }
/*
void XMLAttribute::Print( XMLStreamer* streamer ) void XMLAttribute::Print( XMLStreamer* streamer )
{ {
// fixme: sort out single vs. double quote // fixme: sort out single vs. double quote
//fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() ); //fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
streamer->PushAttribute( name.GetStr(), value.GetStr() ); streamer->PushAttribute( name.GetStr(), value.GetStr() );
} }
*/
// --------- XMLElement ---------- // // --------- XMLElement ---------- //
XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ), XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
@ -580,6 +586,7 @@ char* XMLElement::ParseDeep( char* p )
} }
/*
void XMLElement::Print( XMLStreamer* streamer ) void XMLElement::Print( XMLStreamer* streamer )
{ {
//if ( !parent || !parent->IsTextParent() ) { //if ( !parent || !parent->IsTextParent() ) {
@ -599,21 +606,22 @@ void XMLElement::Print( XMLStreamer* streamer )
} }
streamer->CloseElement(); streamer->CloseElement();
} }
*/
bool XMLElement::Accept( XMLVisitor* visitor ) const bool XMLElement::Accept( XMLVisitor* visitor ) const
{ {
if ( visitor->VisitEnter( *this, rootAttribute ) ) if ( visitor->VisitEnter( *this, rootAttribute ) )
{ {
for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
{ {
if ( !node->Accept( visitor ) ) if ( !node->Accept( visitor ) )
break; break;
} }
} }
return visitor->VisitExit( *this ); return visitor->VisitExit( *this );
} }
// --------- XMLDocument ----------- // // --------- XMLDocument ----------- //
@ -687,9 +695,10 @@ void XMLDocument::Print( XMLStreamer* streamer )
XMLStreamer stdStreamer( stdout ); XMLStreamer stdStreamer( stdout );
if ( !streamer ) if ( !streamer )
streamer = &stdStreamer; streamer = &stdStreamer;
for( XMLNode* node = firstChild; node; node=node->next ) { //for( XMLNode* node = firstChild; node; node=node->next ) {
node->Print( streamer ); // node->Print( streamer );
} //}
Accept( streamer );
} }
@ -872,3 +881,35 @@ void XMLStreamer::PushComment( const char* comment )
PrintSpace( depth ); PrintSpace( depth );
fprintf( fp, "<!--%s-->\n", comment ); fprintf( fp, "<!--%s-->\n", comment );
} }
bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
{
OpenElement( element.Name() );
while ( attribute ) {
PushAttribute( attribute->Name(), attribute->Value() );
attribute = attribute->Next();
}
return true;
}
bool XMLStreamer::VisitExit( const XMLElement& element )
{
CloseElement();
return true;
}
bool XMLStreamer::Visit( const XMLText& text )
{
PushText( text.Value() );
return true;
}
bool XMLStreamer::Visit( const XMLComment& comment )
{
PushComment( comment.Value() );
return true;
}

View File

@ -231,56 +231,6 @@ private:
}; };
/*
class StringStack
{
public:
StringStack();
virtual ~StringStack();
void Push( const char* str );
const char* Pop();
int NumPositive() const { return nPositive; }
private:
DynArray< char, 50 > mem;
int nPositive; // number of strings with len > 0
};
*/
/*
class StringPool
{
public:
enum { INIT_SIZE=20 };
StringPool() : size( 0 ) {
const char** mem = pool.PushArr( INIT_SIZE );
memset( (void*)mem, 0, sizeof(char)*INIT_SIZE );
}
~StringPool() {}
const char* Intern( const char* str );
private:
// FNV hash
int Hash( const char* s ) {
#define FNV_32_PRIME ((int)0x01000193)
int hval = 0;
while (*s) {
hval *= FNV_32_PRIME;
hval ^= (int)*s++;
}
return hval;
}
int size;
DynArray< const char*, INIT_SIZE > pool; // the hash table
StringStack store; // memory for the interned strings
};
*/
/** /**
Implements the interface to the "Visitor pattern" (see the Accept() method.) Implements the interface to the "Visitor pattern" (see the Accept() method.)
@ -359,6 +309,7 @@ class XMLNode
friend class XMLDocument; friend class XMLDocument;
friend class XMLElement; friend class XMLElement;
public: public:
const XMLDocument* GetDocument() const { return document; }
XMLDocument* GetDocument() { return document; } XMLDocument* GetDocument() { return document; }
virtual XMLElement* ToElement() { return 0; } virtual XMLElement* ToElement() { return 0; }
@ -366,9 +317,20 @@ public:
virtual XMLComment* ToComment() { return 0; } virtual XMLComment* ToComment() { return 0; }
virtual XMLDocument* ToDocument() { return 0; } virtual XMLDocument* ToDocument() { return 0; }
virtual const XMLElement* ToElement() const { return 0; }
virtual const XMLText* ToText() const { return 0; }
virtual const XMLComment* ToComment() const { return 0; }
virtual const XMLDocument* ToDocument() const { return 0; }
const char* Value() const { return value.GetStr(); } const char* Value() const { return value.GetStr(); }
void SetValue( const char* val ) { value.SetInternedStr( val ); } void SetValue( const char* val ) { value.SetInternedStr( val ); }
const XMLNode* Parent() const { return parent; }
XMLNode* Parent() { return parent; }
/// Returns true if this node has no children.
bool NoChildren() const { return !firstChild; }
const XMLNode* FirstChild() const { return firstChild; } const XMLNode* FirstChild() const { return firstChild; }
XMLNode* FirstChild() { return firstChild; } XMLNode* FirstChild() { return firstChild; }
const XMLElement* FirstChildElement( const char* value=0 ) const; const XMLElement* FirstChildElement( const char* value=0 ) const;
@ -400,7 +362,7 @@ public:
void DeleteChild( XMLNode* node ); void DeleteChild( XMLNode* node );
virtual bool Accept( XMLVisitor* visitor ) const = 0; virtual bool Accept( XMLVisitor* visitor ) const = 0;
virtual void Print( XMLStreamer* streamer ); //virtual void Print( XMLStreamer* streamer );
virtual char* ParseDeep( char* ); virtual char* ParseDeep( char* );
void SetTextParent() { isTextParent = true; } void SetTextParent() { isTextParent = true; }
@ -433,12 +395,11 @@ class XMLText : public XMLNode
friend class XMLBase; friend class XMLBase;
friend class XMLDocument; friend class XMLDocument;
public: public:
virtual void Print( XMLStreamer* streamer ); //virtual void Print( XMLStreamer* streamer );
const char* Value() { return value.GetStr(); }
void SetValue( const char* );
virtual bool Accept( XMLVisitor* visitor ) const; virtual bool Accept( XMLVisitor* visitor ) const;
virtual XMLText* ToText() { return this; } virtual XMLText* ToText() { return this; }
virtual const XMLText* ToText() const { return this; }
char* ParseDeep( char* ); char* ParseDeep( char* );
@ -455,10 +416,9 @@ class XMLComment : public XMLNode
friend class XMLBase; friend class XMLBase;
friend class XMLDocument; friend class XMLDocument;
public: public:
virtual void Print( XMLStreamer* ); virtual XMLComment* ToComment() { return this; }
virtual XMLComment* ToComment() { return this; } virtual const XMLComment* ToComment() const { return this; }
const char* Value() { return value.GetStr(); }
virtual bool Accept( XMLVisitor* visitor ) const; virtual bool Accept( XMLVisitor* visitor ) const;
char* ParseDeep( char* ); char* ParseDeep( char* );
@ -475,15 +435,19 @@ class XMLAttribute
{ {
friend class XMLElement; friend class XMLElement;
public: public:
virtual void Print( XMLStreamer* streamer ); //virtual void Print( XMLStreamer* streamer );
const char* Name() const { return name.GetStr(); }
const char* Value() const { return value.GetStr(); }
const XMLAttribute* Next() const { return next; }
private: private:
XMLAttribute( XMLElement* element ) : next( 0 ) {} XMLAttribute( XMLElement* element ) : next( 0 ) {}
virtual ~XMLAttribute() {} virtual ~XMLAttribute() {}
char* ParseDeep( char* p ); char* ParseDeep( char* p );
StrPair name; mutable StrPair name;
StrPair value; mutable StrPair value;
XMLAttribute* next; XMLAttribute* next;
MemPool* memPool; MemPool* memPool;
}; };
@ -497,11 +461,30 @@ public:
const char* Name() const { return Value(); } const char* Name() const { return Value(); }
void SetName( const char* str ) { SetValue( str ); } void SetName( const char* str ) { SetValue( str ); }
virtual void Print( XMLStreamer* ); virtual XMLElement* ToElement() { return this; }
virtual const XMLElement* ToElement() const { return this; }
virtual XMLElement* ToElement() { return this; }
virtual bool Accept( XMLVisitor* visitor ) const; virtual bool Accept( XMLVisitor* visitor ) const;
const char* Attribute( const char* name ) const;
int QueryIntAttribute( const char* name, int* value ) const;
int QueryUnsignedAttribute( const char* name, unsigned int* value ) const;
int QueryBoolAttribute( const char* name, bool* value ) const;
int QueryDoubleAttribute( const char* name, double* _value ) const;
int QueryFloatAttribute( const char* name, float* _value ) const;
void SetAttribute( const char* name, const char* value );
void SetAttribute( const char* name, int value );
void SetAttribute( const char* name, unsigned value );
void SetAttribute( const char* name, bool value );
void SetAttribute( const char* name, double value );
void RemoveAttribute( const char* name );
const XMLAttribute* FirstAttribute() const { return rootAttribute; }
const char* GetText() const;
// internal: // internal:
virtual bool IsClosingElement() const { return closing; } virtual bool IsClosingElement() const { return closing; }
char* ParseDeep( char* p ); char* ParseDeep( char* p );
@ -515,7 +498,7 @@ private:
bool closing; bool closing;
XMLAttribute* rootAttribute; XMLAttribute* rootAttribute;
XMLAttribute* lastAttribute; XMLAttribute* lastAttribute; // fixme: remove
}; };
@ -526,7 +509,8 @@ public:
XMLDocument(); XMLDocument();
~XMLDocument(); ~XMLDocument();
virtual XMLDocument* ToDocument() { return this; } virtual XMLDocument* ToDocument() { return this; }
virtual const XMLDocument* ToDocument() const { return this; }
int Parse( const char* ); int Parse( const char* );
int Load( const char* ); int Load( const char* );
@ -561,7 +545,6 @@ private:
const char* errorStr1; const char* errorStr1;
const char* errorStr2; const char* errorStr2;
char* charBuffer; char* charBuffer;
//StringStack stringPool;
MemPoolT< sizeof(XMLElement) > elementPool; MemPoolT< sizeof(XMLElement) > elementPool;
MemPoolT< sizeof(XMLAttribute) > attributePool; MemPoolT< sizeof(XMLAttribute) > attributePool;
@ -570,7 +553,7 @@ private:
}; };
class XMLStreamer class XMLStreamer : public XMLVisitor
{ {
public: public:
XMLStreamer( FILE* file ); XMLStreamer( FILE* file );
@ -583,17 +566,20 @@ public:
void PushText( const char* text ); void PushText( const char* text );
void PushComment( const char* comment ); void PushComment( const char* comment );
virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { return true; }
virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
virtual bool VisitExit( const XMLElement& element );
virtual bool Visit( const XMLText& text );
virtual bool Visit( const XMLComment& comment );
private: private:
void SealElement(); void SealElement();
void PrintSpace( int depth ); void PrintSpace( int depth );
void PrintString( const char* ); // prints out, after detecting entities. void PrintString( const char* ); // prints out, after detecting entities.
/* bool TextOnStack() const {
for( int i=0; i<text.Size(); ++i ) {
if ( text[i] == 'T' )
return true;
}
return false;
}*/
FILE* fp; FILE* fp;
int depth; int depth;
@ -606,7 +592,6 @@ private:
bool entityFlag[ENTITY_RANGE]; bool entityFlag[ENTITY_RANGE];
DynArray< const char*, 10 > stack; DynArray< const char*, 10 > stack;
//DynArray< char, 10 > text;
}; };