diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 (patch) | |
tree | 67208f7c145782a7e90b123b982ca78d88cc2c87 /kode/kxml_compiler | |
download | tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.tar.gz tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kode/kxml_compiler')
-rw-r--r-- | kode/kxml_compiler/Makefile.am | 9 | ||||
-rw-r--r-- | kode/kxml_compiler/creator.cpp | 903 | ||||
-rw-r--r-- | kode/kxml_compiler/creator.h | 115 | ||||
-rw-r--r-- | kode/kxml_compiler/kxml_compiler.cpp | 177 | ||||
-rw-r--r-- | kode/kxml_compiler/parser.cpp | 299 | ||||
-rw-r--r-- | kode/kxml_compiler/parser.h | 134 |
6 files changed, 1637 insertions, 0 deletions
diff --git a/kode/kxml_compiler/Makefile.am b/kode/kxml_compiler/Makefile.am new file mode 100644 index 000000000..d42867185 --- /dev/null +++ b/kode/kxml_compiler/Makefile.am @@ -0,0 +1,9 @@ +INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/libkdepim $(all_includes) + +bin_PROGRAMS = kxml_compiler + +kxml_compiler_LDFLAGS = $(all_libraries) $(KDE_RPATH) +kxml_compiler_LDADD = ../libkode.la -lkdecore +kxml_compiler_SOURCES = parser.cpp creator.cpp kxml_compiler.cpp + +METASOURCES = AUTO diff --git a/kode/kxml_compiler/creator.cpp b/kode/kxml_compiler/creator.cpp new file mode 100644 index 000000000..1277d8d71 --- /dev/null +++ b/kode/kxml_compiler/creator.cpp @@ -0,0 +1,903 @@ +/* + This file is part of KDE. + + Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "creator.h" + +#include <kode/code.h> +#include <kode/printer.h> +#include <kode/typedef.h> +#include <kode/statemachine.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> +#include <kglobal.h> +#include <kconfig.h> +#include <ksimpleconfig.h> +#include <kstandarddirs.h> + +#include <qfile.h> +#include <qtextstream.h> +#include <qdom.h> +#include <qregexp.h> +#include <qmap.h> + +#include <iostream> + +Creator::Creator( XmlParserType p, XmlWriterType w ) + : mXmlParserType( p ), mXmlWriterType( w ) +{ + setExternalClassNames(); +} + +void Creator::setExternalClassPrefix( const QString &prefix ) +{ + mExternalClassPrefix = prefix; + + setExternalClassNames(); +} + +void Creator::setExternalClassNames() +{ + mParserClass.setName( mExternalClassPrefix + "Parser" ); + mWriterClass.setName( mExternalClassPrefix + "Writer" ); +} + +KODE::File &Creator::file() { return mFile; } + +QString Creator::upperFirst( const QString &str ) +{ + return KODE::Style::upperFirst( str ); +} + +QString Creator::lowerFirst( const QString &str ) +{ + return KODE::Style::lowerFirst( str ); +} + +void Creator::createProperty( KODE::Class &c, const QString &type, + const QString &name ) +{ + KODE::MemberVariable v( name, type ); + c.addMemberVariable( v ); + + KODE::Function mutator( "set" + upperFirst( name ), "void" ); + mutator.addArgument( "const " + type + " &v" ); + mutator.addBodyLine( v.name() + " = v;" ); + c.addFunction( mutator ); + + KODE::Function accessor( name, type ); + accessor.setConst( true ); + accessor.addBodyLine( "return " + v.name() + ";" ); + c.addFunction( accessor ); +} + +void Creator::createElementFunctions( KODE::Class &c, Element *e ) +{ + if ( e->hasText ) { + createProperty( c, "QString", e->name ); + if ( mXmlParserType == XmlParserCustomExternal ) { + createTextElementParserCustom( c, e ); + } + return; + } + + QString type = upperFirst( e->name ); + + if ( !mFile.hasClass( type ) ) { + createClass( e ); + } + + QString name = lowerFirst( e->name ); + + if ( e->pattern.oneOrMore || e->pattern.zeroOrMore || + e->pattern.choice || e->pattern.optional ) { + registerListTypedef( type ); + + c.addHeaderInclude( "qvaluelist.h" ); + type = type + "::List"; + QString className = upperFirst( name ); + name = name + "List"; + + KODE::Function adder( "add" + className, "void" ); + adder.addArgument( className + " *v" ); + + KODE::Code code; + code += "m" + upperFirst( name ) + ".append( v );"; + + adder.setBody( code ); + + c.addFunction( adder ); + } + + createProperty( c, type, name ); +} + +void Creator::createClass( Element *element ) +{ + QString className = upperFirst( element->name ); + + if ( mProcessedClasses.find( className ) != mProcessedClasses.end() ) { + return; + } + + KODE::Class c( className ); + + mProcessedClasses.append( className ); + + QValueList<Attribute *>::ConstIterator itA; + for( itA = element->attributes.begin(); + itA != element->attributes.end(); ++itA ) { + Attribute *a = *itA; + + createProperty( c, "QString", a->name ); + } + + QValueList<Element *>::ConstIterator itE; + for( itE = element->elements.begin(); itE != element->elements.end(); + ++itE ) { + createElementFunctions( c, *itE ); + } + + QValueList<Reference *>::ConstIterator itR; + for( itR = element->references.begin(); itR != element->references.end(); + ++itR ) { + Element e; + e.name = (*itR)->name; + e.pattern = (*itR)->pattern; + createElementFunctions( c, &e ); + } + + createElementParser( c, element ); + createElementWriter( c, element ); + + mFile.insertClass( c ); +} + +void Creator::createElementWriter( KODE::Class &c, Element *element ) +{ + KODE::Function writer( "writeElement", "QString" ); + + KODE::Code code; + + code += "QString xml;"; + + QString tag = "<" + element->name; + + QValueList<Attribute *>::ConstIterator it3; + for( it3 = element->attributes.begin(); it3 != element->attributes.end(); + ++it3 ) { + tag += " " + (*it3)->name + "=\\\"\" + " + (*it3)->name + "() + \"\\\""; + } + + if ( element->isEmpty ) { + tag += "/"; + } + + tag += ">\\n"; + + code += "xml += indent() + \"" + tag + "\";"; + + if ( !element->isEmpty ) { + code += "indent( 2 );"; + + QValueList<Element *>::ConstIterator it; + for( it = element->elements.begin(); it != element->elements.end(); ++it ) { + Element *e = *it; + QString type = upperFirst( e->name ); + if ( e->pattern.oneOrMore || e->pattern.zeroOrMore ) { + code += type + "::List list = " + e->name + "List();"; + code += type + "::List::ConstIterator it;"; + code += "for( it = list.begin(); it != list.end(); ++it ) {"; + code.indent(); + code += "xml += (*it)->writeElement();"; + code.unindent(); + code += "}"; + } else { + if ( e->hasText ) { + code += "xml += indent() + \"<" + e->name + ">\" + " + e->name + "() + \"</" + + e->name + ">\\n\";"; + } else { + code += "xml += " + type + "()->writeElement()"; + } + } + } + + QValueList<Reference *>::ConstIterator it2; + for( it2 = element->references.begin(); it2 != element->references.end(); + ++it2 ) { + Reference *r = *it2; + QString type = upperFirst( r->name ); + if ( r->pattern.oneOrMore || r->pattern.zeroOrMore ) { + code += type + "::List list2 = " + r->name + "List();"; + code += type + "::List::ConstIterator it2;"; + code += "for( it2 = list2.begin(); it2 != list2.end(); ++it2 ) {"; + code.indent(); + code += "xml += (*it2)->writeElement();"; + code.unindent(); + code += "}"; + } else { + code += "xml += " + type + "()->writeElement()"; + } + } + + code += "indent( -2 );"; + + code += "xml += indent() + \"</" + element->name + ">\\n\";"; + } + + code += "return xml;"; + + writer.setBody( code ); + + c.addFunction( writer ); +} + +void Creator::createElementParser( KODE::Class &c, Element *e ) +{ + switch ( mXmlParserType ) { + case XmlParserDom: + case XmlParserDomExternal: + createElementParserDom( c, e ); + break; + case XmlParserCustomExternal: + createElementParserCustom( c, e ); + break; + } +} + +void Creator::createTextElementParserCustom( KODE::Class &, Element *e ) +{ + KODE::Function parser( "parseElement" + upperFirst( e->name ), "QString" ); + + KODE::Code code; + + code += "QString result;"; + code.newLine(); + + KODE::StateMachine sm; + + KODE::Code stateCode; + + stateCode += "if ( c == '<' ) {"; + stateCode += " state = TAG;"; + stateCode += " tagStart = mRunning;"; + stateCode += "} else if ( c == '&' ) {"; + stateCode += " entityStart = mRunning + 1;"; + stateCode += "} else if ( entityStart >= 0 && c == ';' ) {"; + stateCode += " QString entity = mBuffer.mid( entityStart, mRunning - entityStart );"; + stateCode += " if ( entity == \"quot\" ) result += '\"';"; + stateCode += " entityStart = -1;"; + stateCode += "} else if ( entityStart < 0 ) {"; + stateCode += " result += c;"; + stateCode += "}"; + + sm.setState( "TEXT", stateCode ); + + stateCode.clear(); + stateCode += "if ( c == '/' ) {"; + stateCode += " state = ENDTAG;"; + stateCode += "} else {"; + stateCode += " state = STARTTAG;"; + stateCode += "}"; + + sm.setState( "TAG", stateCode ); + + stateCode.clear(); + stateCode += "if ( c == '>' ) {"; + stateCode += " state = TEXT;"; + stateCode += " result += mBuffer.mid( tagStart, mRunning - tagStart + 1 );"; + stateCode += "}"; + + sm.setState( "STARTTAG", stateCode ); + + stateCode.clear(); + stateCode += "if ( c == '>' ) {"; + stateCode += " state = TEXT;"; + stateCode += " result += mBuffer.mid( tagStart, mRunning - tagStart + 1 );"; + stateCode += "} else if ( foundText" + upperFirst( e->name ) + "() ) {"; + stateCode += " return result;"; + stateCode += "}"; + + sm.setState( "ENDTAG", stateCode ); + + sm.setInitialState( "STARTTAG" ); + + code.addBlock( sm.stateDefinition() ); + code.newLine(); + code += "int tagStart = -1;"; + code += "int entityStart = -1;"; + code.newLine(); + code += "while ( mRunning < mBuffer.length() ) {"; + code.indent(); + code += "QChar c = mBuffer[ mRunning ];"; + code.addBlock( sm.transitionLogic() ); + code += "++mRunning;"; + code.unindent(); + code += "}"; + code.newLine(); + code += "return result;"; + + parser.setBody( code ); + + mParserClass.addFunction( parser ); +} + +void Creator::createElementParserCustom( KODE::Class &c, Element *e ) +{ + KODE::Function parser( "parseElement" + upperFirst( e->name ), + c.name() + " *" ); + + KODE::Code code; + + code += c.name() + " *result = new " + c.name() + "();"; + code.newLine(); + + KODE::StateMachine sm; + + if ( !e->isEmpty ) { + KODE::Code stateCode; + stateCode += "if ( c == '<' ) state = TAG;"; + + sm.setState( "WHITESPACE", stateCode ); + + stateCode.clear(); + stateCode += "if ( c == '/' ) {"; + stateCode += " state = ENDTAG;"; + stateCode += "} else {"; + stateCode += " state = STARTTAG;"; + stateCode += "}"; + + sm.setState( "TAG", stateCode ); + + stateCode.clear(); + if ( e->attributes.isEmpty() ) { + stateCode += " if ( c == '/' ) {"; + stateCode += " return result;"; + stateCode += " }"; + } + stateCode += "if ( c == '>' ) {"; + stateCode += " state = WHITESPACE;"; + Element::List::ConstIterator it; + for( it = e->elements.begin(); it != e->elements.end(); ++it ) { + createFoundTextFunction( (*it)->name ); + + QString eName = upperFirst( (*it)->name ); + stateCode += "} else if ( foundText" + eName + "() ) {"; + QString line = " result->"; + if ( (*it)->hasText ) line += "set"; + else line += "add"; + line += eName + "( parseElement" + eName + "() );"; + stateCode += line; + stateCode += " state = WHITESPACE;"; + } + Reference::List::ConstIterator it3; + for( it3 = e->references.begin(); it3 != e->references.end(); ++it3 ) { + createFoundTextFunction( (*it3)->name ); + + QString eName = upperFirst( (*it3)->name ); + stateCode += "} else if ( foundText" + eName + "() ) {"; + stateCode += " result->add" + eName + "( parseElement" + eName + "() );"; + stateCode += " state = WHITESPACE;"; + } + stateCode += "}"; + + sm.setState( "STARTTAG", stateCode ); + + stateCode.clear(); + stateCode += "if ( c == '>' ) {"; + stateCode += " state = WHITESPACE;"; + stateCode += "} else if ( foundText" + c.name() + "() ) {"; + stateCode += " return result;"; + stateCode += "}"; + + sm.setState( "ENDTAG", stateCode ); + + if ( !e->attributes.isEmpty() ) { + stateCode.clear(); + stateCode += "if ( c == '>' ) {"; + stateCode += " state = WHITESPACE;"; + stateCode += "}"; + + Attribute::List::ConstIterator it2; + for( it2 = e->attributes.begin(); it2 != e->attributes.end(); ++it2 ) { + bool first = it2 == e->attributes.begin(); + stateCode.addBlock( createAttributeScanner( *it2, first ) ); + } + stateCode += "} else if ( c =='/' ) {"; + stateCode += " return result;"; + stateCode += "}"; + + sm.setState( "ATTRIBUTES", stateCode ); + + sm.setInitialState( "ATTRIBUTES" ); + } else { + sm.setInitialState( "STARTTAG" ); + } + + code.addBlock( sm.stateDefinition() ); + code.newLine(); + } + + if ( !e->attributes.isEmpty() ) { + Attribute::List::ConstIterator it; + for( it = e->attributes.begin(); it != e->attributes.end(); ++it ) { + code += "bool found" + upperFirst( (*it)->name ) + " = false;"; + } + code.newLine(); + code += "int attrValueStart = -1;"; + code.newLine(); + } + + code += "while ( mRunning < mBuffer.length() ) {"; + code.indent(); + code += "QChar c = mBuffer[ mRunning ];"; + + if ( e->isEmpty ) { + code += "if ( c == '>' ) {"; + code += " return result;"; + code += "}"; + + if ( !e->attributes.isEmpty() ) { + Attribute::List::ConstIterator it; + for( it = e->attributes.begin(); it != e->attributes.end(); ++it ) { + code.addBlock( createAttributeScanner( *it, + it == e->attributes.begin() ) ); + } + code += "}"; + } + } else { + code.addBlock( sm.transitionLogic() ); + } + + code += "++mRunning;"; + code.unindent(); + code += "}"; + code.newLine(); + code += "return result;"; + + parser.setBody( code ); + + mParserClass.addFunction( parser ); +} + +KODE::Code Creator::createAttributeScanner( Attribute *a, bool firstAttribute ) +{ + KODE::Code code; + + QString aName = upperFirst( a->name ); + + createFoundTextFunction( a->name ); + + QString line; + if ( !firstAttribute ) line = "} else "; + line += "if ( foundText" + aName + "() ) {"; + code += line; + code += " found" + aName + "= true;"; + code += "} else if ( found" + aName + " && c == '\"' ) {"; + code += " if ( attrValueStart < 0 ) {"; + code += " attrValueStart = mRunning + 1;"; + code += " } else {"; + code += " result->set" + aName + "( mBuffer.mid( attrValueStart,"; + code += " mRunning - attrValueStart ) );"; + code += " attrValueStart = -1;"; + code += " found" + aName + " = false;"; + code += " }"; + + return code; +} + +void Creator::createElementParserDom( KODE::Class &c, Element *e ) +{ + QString functionName; + if ( externalParser() ) functionName = "parseElement" + c.name(); + else functionName = "parseElement"; + + KODE::Function parser( functionName, c.name() + " *" ); + parser.setStatic( true ); + parser.setDocs( "Parse XML object from DOM element." ); + + parser.addArgument( "const QDomElement &element" ); + + KODE::Code code; + + code += "if ( element.tagName() != \"" + e->name + "\" ) {"; + code.indent(); + code += "kdError() << \"Expected '" + e->name + "', got '\" << " + + "element.tagName() << \"'.\" << endl;"; + code += "return 0;"; + code.unindent(); + code += "}"; + code.newLine(); + + code += c.name() + " *result = new " + c.name() + "();"; + code.newLine(); + + code += "QDomNode n;"; + code += "for( n = element.firstChild(); !n.isNull();" + " n = n.nextSibling() ) {"; + code.indent(); + code += "QDomElement e = n.toElement();"; + + QValueList<Element *>::ConstIterator it; + for( it = e->elements.begin(); it != e->elements.end(); ++it ) { + QString condition; + if ( it != e->elements.begin() ) condition = "else "; + condition += "if"; + + code += condition + " ( e.tagName() == \"" + (*it)->name + "\" ) {"; + code.indent(); + + QString className = upperFirst( (*it)->name ); + + if ( (*it)->hasText ) { + code += "result->set" + className + "( e.text() );"; + } else { + QString line = className + " *o = "; + if ( externalParser() ) { + line += "parseElement" + className; + } else { + line += className + "::parseElement"; + } + line += "( e );"; + code += line; + + code += "if ( o ) result->add" + className + "( o );"; + } + + code.unindent(); + code += "}"; + } + + code.newLine(); + + QValueList<Reference *>::ConstIterator it3; + for( it3 = e->references.begin(); it3 != e->references.end(); ++it3 ) { + QString condition; + if ( it3 != e->references.begin() ) condition = "else "; + condition += "if"; + + code += condition + " ( e.tagName() == \"" + (*it3)->name + "\" ) {"; + code.indent(); + + QString className = upperFirst( (*it3)->name ); + + QString line = className + " *o = "; + if ( externalParser() ) { + line += "parseElement" + className; + } else { + line += className + "::parseElement"; + } + line += "( e );"; + code += line; + + code += "if ( o ) result->add" + className + "( o );"; + + code.unindent(); + code += "}"; + } + + code.unindent(); + code += "}"; + code.newLine(); + + QValueList<Attribute *>::ConstIterator it2; + for( it2 = e->attributes.begin(); it2 != e->attributes.end(); ++it2 ) { + code += "result->set" + upperFirst( (*it2)->name ) + + "( element.attribute( \"" + (*it2)->name + "\" ) );"; + } + code.newLine(); + + code += "return result;"; + + parser.setBody( code ); + + if ( externalParser() ) { + mParserClass.addFunction( parser ); + } else { + c.addFunction( parser ); + } +} + +void Creator::registerListTypedef( const QString &type ) +{ + if ( !mListTypedefs.contains( type ) ) mListTypedefs.append( type ); +} + +void Creator::createListTypedefs() +{ + QStringList::ConstIterator it; + for( it = mListTypedefs.begin(); it != mListTypedefs.end(); ++it ) { + KODE::Class c = mFile.findClass( *it ); + if ( !c.isValid() ) continue; + c.addTypedef( KODE::Typedef( "QValueList<" + *it + " *>", "List" ) ); + mFile.insertClass( c ); + } +} + +void Creator::createIndenter( KODE::File &file ) +{ + KODE::Function indenter( "indent", "QString" ); + indenter.addArgument( "int n = 0" ); + + KODE::Code code; + + code += "static int i = 0;"; + code += "i += n;"; + code += "QString space;"; + code += "return space.fill( ' ', i );"; + + indenter.setBody( code ); + + file.addFileFunction( indenter ); +} + +void Creator::createFileWriter( Element *element, const QString &dtd ) +{ + QString className = upperFirst( element->name ); + + KODE::Class c = mFile.findClass( className ); + + c.addInclude( "kdebug.h" ); + c.addInclude( "qtextstream.h" ); + c.addInclude( "qfile.h" ); + + if ( !externalWriter() ) { + createIndenter( mFile ); + } + + KODE::Function writer( "writeFile", "bool" ); + + writer.addArgument( "const QString &filename" ); + + c.addInclude( "qfile.h" ); + + KODE::Code code; + + code += "QFile file( filename );"; + code += "if ( !file.open( IO_WriteOnly ) ) {"; + code += " kdError() << \"Unable to open file '\" << filename << \"'\" << endl;"; + code += " return false;"; + code += "}"; + code += ""; + code += "QTextStream ts( &file );"; + + code += "ts << \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\n\";"; + code += "ts << \"<!DOCTYPE features SYSTEM \\\"" + dtd + "\\\">\\n\";"; + + code += "ts << writeElement();"; + code += "file.close();"; + code += ""; + code += "return true;"; + + writer.setBody( code ); + + c.addFunction( writer ); + + mFile.insertClass( c ); +} + +void Creator::createFileParser( Element *element ) +{ + switch ( mXmlParserType ) { + case XmlParserDom: + case XmlParserDomExternal: + createFileParserDom( element ); + break; + case XmlParserCustomExternal: + createFileParserCustom( element ); + break; + } +} + +void Creator::createFileParserCustom( Element *element ) +{ + kdDebug() << "Creator::createFileParserCustom()" << endl; + + QString className = upperFirst( element->name ); + + KODE::Function parser( "parseFile", className + " *" ); + + parser.addArgument( "const QString &filename" ); + + mParserClass.addInclude( "qfile.h" ); + mParserClass.addInclude( "kdebug.h" ); + + mParserClass.addMemberVariable( KODE::MemberVariable( "mBuffer", + "QString" ) ); + mParserClass.addMemberVariable( KODE::MemberVariable( "mRunning", + "unsigned int" ) ); + + KODE::Code code; + + code += "QFile file( filename );"; + code += "if ( !file.open( IO_ReadOnly ) ) {"; + code += " kdError() << \"Unable to open file '\" << filename << \"'\" << endl;"; + code += " return 0;"; + code += "}"; + code += ""; + code += "QTextStream ts( &file );"; + code += "mBuffer = ts.read();"; + code += ""; + code += "mRunning = 0;"; + code.newLine(); + + KODE::StateMachine sm; + + KODE::Code stateCode; + + stateCode += "if ( c == '<' ) state = TAG;"; + + sm.setState( "WHITESPACE", stateCode ); + + stateCode.clear(); + + stateCode += "if ( c == '>' ) {"; + stateCode += " state = WHITESPACE;"; + stateCode += "} else if ( foundText" + className + "() ) {"; + stateCode += " " + element->name + " = parseElement" + className + "();"; + stateCode += " state = WHITESPACE;"; + stateCode += "}"; + + createFoundTextFunction( element->name ); + + sm.setState( "TAG", stateCode ); + + code.addBlock( sm.stateDefinition() ); + code.newLine(); + + code += className + " *" + element->name + " = 0;"; + code.newLine(); + + code += "while ( mRunning < mBuffer.length() ) {"; + code.indent(); + code += "QChar c = mBuffer[ mRunning ];"; + code.addBlock( sm.transitionLogic() ); + code += "++mRunning;"; + code.unindent(); + code += "}"; + code.newLine(); + + code += "return " + element->name + ";"; + + parser.setBody( code ); + + mParserClass.addFunction( parser ); +} + +void Creator::createFoundTextFunction( const QString &text ) +{ + QString functionName = "foundText" + upperFirst( text ); + + if ( mParserClass.hasFunction( functionName ) ) return; + + KODE::Function f( functionName, "bool" ); + + KODE::Code code; + + code += "if ( mBuffer[ mRunning ] != '" + text.right( 1 ) + "' ) return false;"; + code += ""; + code += "return mBuffer.mid( mRunning - " + + QString::number( text.length() - 1 ) + ", " + + QString::number( text.length() ) + " ) == \"" + text + "\";"; + + f.setBody( code ); + + mParserClass.addFunction( f ); +} + +void Creator::createFileParserDom( Element *element ) +{ + kdDebug() << "Creator::createFileParserDom()" << endl; + + QString className = upperFirst( element->name ); + + KODE::Class c; + + if ( externalParser() ) { + c = mParserClass; + } else { + c = mFile.findClass( className ); + } + + KODE::Function parser( "parseFile", className + " *" ); + parser.setStatic( true ); + + parser.addArgument( "const QString &filename" ); + + c.addInclude( "qfile.h" ); + c.addInclude( "qdom.h" ); + c.addInclude( "kdebug.h" ); + + KODE::Code code; + + code += "QFile file( filename );"; + code += "if ( !file.open( IO_ReadOnly ) ) {"; + code += " kdError() << \"Unable to open file '\" << filename << \"'\" << endl;"; + code += " return 0;"; + code += "}"; + code += ""; + code += "QString errorMsg;"; + code += "int errorLine, errorCol;"; + code += "QDomDocument doc;"; + code += "if ( !doc.setContent( &file, false, &errorMsg, &errorLine, &errorCol ) ) {"; + code += " kdError() << errorMsg << \" at \" << errorLine << \",\" << errorCol << endl;"; + code += " return 0;"; + code += "}"; + code += ""; + code += "kdDebug() << \"CONTENT:\" << doc.toString() << endl;"; + + code += ""; + + QString line = className + " *c = parseElement"; + if ( externalParser() ) line += className; + line += "( doc.documentElement() );"; + code += line; + + code += "return c;"; + + parser.setBody( code ); + + c.addFunction( parser ); + + if ( externalParser() ) { + mParserClass = c; + } else { + mFile.insertClass( c ); + } +} + +void Creator::printFiles( KODE::Printer &printer ) +{ + if ( externalParser() ) { + KODE::File parserFile( file() ); + parserFile.setFilename( file().filename() + "_parser" ); + + parserFile.clearCode(); + + mParserClass.addHeaderInclude( file().filename() + ".h" ); + parserFile.insertClass( mParserClass ); + + kdDebug() << "Print external parser." << endl; + printer.printHeader( parserFile ); + printer.printImplementation( parserFile ); + } + + kdDebug() << "Print header" << endl; + printer.printHeader( file() ); + + kdDebug() << "Print implementation" << endl; + printer.printImplementation( file() ); + +} + +bool Creator::externalParser() const +{ + return mXmlParserType == XmlParserDomExternal || + mXmlParserType == XmlParserCustomExternal; +} + +bool Creator::externalWriter() const +{ + return mXmlWriterType == XmlWriterCustomExternal; +} diff --git a/kode/kxml_compiler/creator.h b/kode/kxml_compiler/creator.h new file mode 100644 index 000000000..6e0d6f8b3 --- /dev/null +++ b/kode/kxml_compiler/creator.h @@ -0,0 +1,115 @@ +/* + This file is part of KDE. + + Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +#ifndef CREATOR_H +#define CREATOR_H + +#include "parser.h" + +#include <kode/code.h> +#include <kode/printer.h> +#include <kode/typedef.h> +#include <kode/file.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> +#include <kglobal.h> +#include <kconfig.h> +#include <ksimpleconfig.h> +#include <kstandarddirs.h> + +#include <qfile.h> +#include <qtextstream.h> +#include <qdom.h> +#include <qregexp.h> +#include <qmap.h> + +#include <iostream> + +class Creator +{ + public: + enum XmlParserType { XmlParserDom, XmlParserDomExternal, + XmlParserCustomExternal }; + enum XmlWriterType { XmlWriterCustom, XmlWriterCustomExternal }; + + Creator( XmlParserType p = XmlParserDom, + XmlWriterType w = XmlWriterCustom ); + + void setExternalClassPrefix( const QString & ); + + bool externalParser() const; + bool externalWriter() const; + + KODE::File &file(); + + QString upperFirst( const QString &str ); + QString lowerFirst( const QString &str ); + + void createProperty( KODE::Class &c, const QString &type, + const QString &name ); + void createElementFunctions( KODE::Class &c, Element *e ); + void createClass( Element *element ); + + void registerListTypedef( const QString &type ); + + void createListTypedefs(); + + void createFileParser( Element *element ); + + void createFileWriter( Element *element, const QString &dtd ); + + void printFiles( KODE::Printer & ); + + protected: + void setExternalClassNames(); + + void createFileParserDom( Element *element ); + void createFileParserCustom( Element *element ); + + void createElementParser( KODE::Class &c, Element *e ); + + void createElementParserDom( KODE::Class &c, Element *e ); + + void createElementParserCustom( KODE::Class &c, Element *e ); + void createTextElementParserCustom( KODE::Class &c, Element *e ); + KODE::Code createAttributeScanner( Attribute *a, bool firstAttribute ); + void createFoundTextFunction( const QString &text ); + + void createElementWriter( KODE::Class &c, Element *e ); + + void createIndenter( KODE::File & ); + + private: + XmlParserType mXmlParserType; + XmlWriterType mXmlWriterType; + QString mExternalClassPrefix; + + KODE::File mFile; + KODE::Class mParserClass; + KODE::Class mWriterClass; + QStringList mProcessedClasses; + QStringList mListTypedefs; +}; + +#endif diff --git a/kode/kxml_compiler/kxml_compiler.cpp b/kode/kxml_compiler/kxml_compiler.cpp new file mode 100644 index 000000000..62b2162c5 --- /dev/null +++ b/kode/kxml_compiler/kxml_compiler.cpp @@ -0,0 +1,177 @@ +/* + This file is part of KDE. + + Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "parser.h" +#include "creator.h" + +#include <kode/code.h> +#include <kode/printer.h> +#include <kode/typedef.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> +#include <kglobal.h> +#include <kconfig.h> +#include <ksimpleconfig.h> +#include <kstandarddirs.h> + +#include <qfile.h> +#include <qtextstream.h> +#include <qdom.h> +#include <qregexp.h> +#include <qmap.h> + +#include <iostream> + +static const KCmdLineOptions options[] = +{ + { "d", 0, 0 }, + { "directory <dir>", I18N_NOOP("Directory to generate files in"), "." }, + { "+dtd", I18N_NOOP("DTD of XML file"), 0 }, + { "external-parser", I18N_NOOP("Generate parser in separate source file"), + 0 }, + { "custom-parser", I18N_NOOP("Generate parser customized for schema"), 0 }, + KCmdLineLastOption +}; + +int main( int argc, char **argv ) +{ + KAboutData aboutData( "kxml_compiler", I18N_NOOP("KDE xml compiler"), "0.1", + I18N_NOOP("KDE XML Compiler") , KAboutData::License_LGPL ); + aboutData.addAuthor( "Cornelius Schumacher", 0, "schumacher@kde.org" ); + + KCmdLineArgs::init( argc, argv, &aboutData ); + KCmdLineArgs::addCmdLineOptions( options ); + + KInstance app( &aboutData ); + + KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); + + if ( args->count() < 1 ) { + kdError() << "Too few arguments." << endl; + return 1; + } + if ( args->count() > 1 ) { + kdError() << "Too many arguments." << endl; + return 1; + } + + QString baseDir = QFile::decodeName( args->getOption( "directory" ) ); + if ( !baseDir.endsWith( "/" ) ) baseDir.append( "/" ); + + QString dtdFilename = args->url( 0 ).path(); + + QString baseName = args->url( 0 ).fileName(); + int pos = baseName.findRev( '.' ); + if ( pos > 0 ) baseName = baseName.left( pos ); + + + QFile dtdFile( dtdFilename ); + if ( !dtdFile.open( IO_ReadOnly ) ) { + kdError() << "Unable to open '" << dtdFilename << "'" << endl; + return 1; + } + + QString errorMsg; + int errorLine, errorCol; + QDomDocument doc; + if ( !doc.setContent( &dtdFile, false, &errorMsg, &errorLine, &errorCol ) ) { + kdError() << errorMsg << " at " << errorLine << "," << errorCol << endl; + return 1; + } + + kdDebug() << "Begin parsing" << endl; + + Parser p; + Element *start = p.parse( doc.documentElement() ); + if ( !start ) { + kdError() << "Could not find start element" << endl; + return 1; + } + + p.dumpDefinitionMap(); + +// return 0; + + p.substituteReferences( start ); + +#if 0 + std::cout << "--- TREE:" << std::endl; + p.dumpTree( start ); +#endif + + kdDebug() << "Begin creating code" << endl; + + Creator::XmlParserType pt; + if ( args->isSet( "custom-parser" ) ) { + pt = Creator::XmlParserCustomExternal; + } else if ( args->isSet( "external-parser" ) ) { + pt = Creator::XmlParserDomExternal; + } else { + pt = Creator::XmlParserDom; + } + + Creator c( pt ); + + kdDebug() << "Create classes" << endl; + QValueList<Element *>::ConstIterator it; + for( it = start->elements.begin(); it != start->elements.end(); ++it ) { + c.createClass( *it ); + } + kdDebug() << "Create parser" << endl; + for( it = start->elements.begin(); it != start->elements.end(); ++it ) { + c.setExternalClassPrefix( c.upperFirst( (*it)->name ) ); + c.createFileParser( *it ); + c.createFileWriter( *it, dtdFilename.replace( "rng", "dtd" ) ); + } + + c.createListTypedefs(); + +#if 0 + QValueList<Reference *>::ConstIterator it2; + for( it2 = start->references.begin(); it2 != start->references.end(); + ++it2 ) { + Element e; + e.name = (*it2)->name; + e.pattern = (*it2)->pattern; + c.createClass( &e ); + } +#endif + + kdDebug() << "Begin printing code" << endl; + + KODE::File &f = c.file(); + + f.setFilename( baseName ); + + KODE::Printer printer; + printer.setCreationWarning( true ); + printer.setGenerator( argv[0] ); + printer.setOutputDirectory( baseDir ); + printer.setSourceFile( args->url( 0 ).fileName() ); + + c.printFiles( printer ); + + kdDebug() << "Finished." << endl; +} diff --git a/kode/kxml_compiler/parser.cpp b/kode/kxml_compiler/parser.cpp new file mode 100644 index 000000000..bc451ce0d --- /dev/null +++ b/kode/kxml_compiler/parser.cpp @@ -0,0 +1,299 @@ +/* + This file is part of KDE. + + Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "parser.h" + +#include <kode/code.h> +#include <kode/printer.h> +#include <kode/typedef.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> +#include <kglobal.h> +#include <kconfig.h> +#include <ksimpleconfig.h> +#include <kstandarddirs.h> + +#include <qfile.h> +#include <qtextstream.h> +#include <qdom.h> +#include <qregexp.h> +#include <qmap.h> + +#include <iostream> + +Pattern::Pattern() + : optional( false ), zeroOrMore( false ), oneOrMore( false ), + choice( false ) +{ +} + +bool Pattern::isEmpty() +{ + return !optional && !zeroOrMore && !oneOrMore && !choice; +} + +QString Pattern::asString() +{ + if ( isEmpty() ) return ""; + QString str = "( "; + if ( optional ) str += "optional "; + if ( zeroOrMore ) str += "zeroOrMore "; + if ( oneOrMore ) str += "oneOrMore "; + if ( choice ) str += "choice "; + str += ")"; + return str; +} + +void Pattern::merge( Pattern p ) +{ + if ( p.optional ) optional = true; + if ( p.zeroOrMore ) zeroOrMore = true; + if ( p.oneOrMore ) oneOrMore = true; + if ( p.choice ) choice = true; +} + +Element::Element() + : hasText( false ), isEmpty( false ) +{ +} + +Parser::Parser() +{ +} + +Element *Parser::parse( const QDomElement &docElement ) +{ + Element *start = 0; + + QDomNode n1; + for( n1 = docElement.firstChild(); !n1.isNull(); n1 = n1.nextSibling() ) { + QDomElement e1 = n1.toElement(); + kdDebug() << "TOP LEVEL element " << e1.tagName() << endl; + if ( e1.tagName() == "define" ) { + Element *d = new Element; + d->name = e1.attribute( "name" ); + parseElement( e1, d, Pattern() ); + Element::List definitions; + QMap<QString,Element::List >::ConstIterator it; + it = mDefinitionMap.find( d->name ); + if ( it != mDefinitionMap.end() ) definitions = *it; + definitions.append( d ); + mDefinitionMap.replace( d->name, definitions ); + } else if ( e1.tagName() == "start" ) { + start = new Element; + parseElement( e1, start, Pattern() ); + } else { + kdDebug() << "parseGrammar: Unrecognized tag: " << e1.tagName() << endl; + } + } + + return start; +} + +Reference *Parser::parseReference( const QDomElement &referenceElement ) +{ + Reference *r = new Reference; + r->name = referenceElement.attribute( "name" ); + return r; +} + +bool Parser::parseAttribute( const QDomElement &attributeElement, + Attribute *a ) +{ + a->name = attributeElement.attribute( "name" ); + + return true; +} + +bool Parser::parseElement( const QDomElement &elementElement, Element *e, + Pattern pattern ) +{ + kdDebug() << "parseElement " << e->name << endl; + + QDomNode n1; + for( n1 = elementElement.firstChild(); !n1.isNull(); n1 = n1.nextSibling() ) { + QDomElement e1 = n1.toElement(); + if ( e1.tagName() == "element" ) { + Element *element = new Element; + element->name = e1.attribute( "name" ); + element->pattern = pattern; + parseElement( e1, element, Pattern() ); + e->elements.append( element ); + } else if ( e1.tagName() == "attribute" ) { + Attribute *a = new Attribute; + a->name = e1.attribute( "name" ); + a->pattern = pattern; + kdDebug() << "ATTRIBUTE: " << a->name << " " << a->pattern.asString() + << endl; + parseAttribute( e1, a ); + e->attributes.append( a ); + } else if ( e1.tagName() == "ref" ) { + Reference *r = parseReference( e1 ); + r->pattern = pattern; + e->references.append( r ); + } else if ( e1.tagName() == "text" ) { + e->hasText = true; + } else if ( e1.tagName() == "empty" ) { + e->isEmpty = true; + } else { + Pattern p = pattern; + if ( e1.tagName() == "optional" ) p.optional = true; + else if ( e1.tagName() == "zeroOrMore" ) p.zeroOrMore = true; + else if ( e1.tagName() == "oneOrMore" ) p.oneOrMore = true; + else if ( e1.tagName() == "choice" ) p.choice = true; + else { + kdDebug() << "Unsupported pattern '" << e1.tagName() << "'" << endl; + } + parseElement( e1, e, p ); + } + } + + return true; +} + +void Parser::substituteReferences( Element *s ) +{ + kdDebug() << "substituteReferences for '" << s->name << "'" << endl; + Reference::List::Iterator it = s->references.begin(); + while( it != s->references.end() ) { + Reference *r = *it; + kdDebug() << "REF " << r->name << endl; + if ( r->name == s->name ) { + kdDebug() << "Don't resolve self reference" << endl; + return; + } + if ( r->substituted ) { + kdDebug() << "Already substituted." << endl; + ++it; + continue; + } else { + r->substituted = true; + } + QMap<QString,Element::List >::ConstIterator it1; + it1 = mDefinitionMap.find( r->name ); + if ( it1 != mDefinitionMap.end() ) { + Element::List elements = *it1; + Element::List::ConstIterator it4; + for( it4 = elements.begin(); it4 != elements.end(); ++it4 ) { + Element *d = *it4; + substituteReferences( d ); + Element::List::ConstIterator it2; + for( it2 = d->elements.begin(); it2 != d->elements.end(); ++it2 ) { + Element *e = *it2; + e->pattern.merge( r->pattern ); + substituteReferences( e ); + s->elements.append( e ); + } + Attribute::List::ConstIterator it3; + for( it3 = d->attributes.begin(); it3 != d->attributes.end(); + ++it3 ) { + Attribute *a = *it3; + a->pattern.merge( r->pattern ); + s->attributes.append( a ); + } + } + it = s->references.erase( it ); + } else { + kdDebug() << "Reference not found" << endl; + ++it; + } + } +} + +void Parser::doIndent( int cols ) +{ + for( int i = 0; i < cols; ++i ) std::cout << " "; +} + +void Parser::dumpPattern( Pattern pattern ) +{ + std::cout << pattern.asString().utf8(); +} + +void Parser::dumpReferences( const Reference::List &references, int indent ) +{ + Reference::List::ConstIterator it; + for( it = references.begin(); it != references.end(); ++it ) { + Reference *r = *it; + doIndent( indent ); + std::cout << "REFERENCE " << r->name.utf8(); + dumpPattern( r->pattern ); + std::cout << std::endl; + } +} + +void Parser::dumpAttributes( const Attribute::List &attributes, int indent ) +{ + Attribute::List::ConstIterator it; + for( it = attributes.begin(); it != attributes.end(); ++it ) { + Attribute *a = *it; + doIndent( indent ); + std::cout << "ATTRIBUTE " << a->name.utf8(); + dumpPattern( a->pattern ); + std::cout << std::endl; + } +} + +void Parser::dumpElements( const Element::List &elements, int indent ) +{ + Element::List::ConstIterator it; + for( it = elements.begin(); it != elements.end(); ++it ) { + Element *e = *it; + dumpElement( e, indent ); + } +} + +void Parser::dumpElement( Element *e, int indent ) +{ + doIndent( indent ); + std::cout << "ELEMENT " << e->name.utf8(); + dumpPattern( e->pattern ); + std::cout << std::endl; + + if ( e->hasText ) { + doIndent( indent + 2 ); + std::cout << "TEXT" << std::endl; + } + + dumpAttributes( e->attributes, indent + 2 ); + dumpElements( e->elements, indent + 2 ); + dumpReferences( e->references, indent + 2 ); +} + +void Parser::dumpTree( Element *s ) +{ + std::cout << "START " << s->name.utf8() << std::endl; + dumpElements( s->elements, 2 ); + dumpReferences( s->references, 2 ); +} + +void Parser::dumpDefinitionMap() +{ + std::cout << "DEFINITION MAP" << std::endl; + QMap<QString,Element::List >::ConstIterator it; + for( it = mDefinitionMap.begin(); it != mDefinitionMap.end(); ++it ) { + dumpElements( *it, 2 ); + } +} diff --git a/kode/kxml_compiler/parser.h b/kode/kxml_compiler/parser.h new file mode 100644 index 000000000..b9ff542c0 --- /dev/null +++ b/kode/kxml_compiler/parser.h @@ -0,0 +1,134 @@ +/* + This file is part of KDE. + + Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +#ifndef PARSER_H +#define PARSER_H + +#include <kode/code.h> +#include <kode/printer.h> +#include <kode/typedef.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> +#include <kglobal.h> +#include <kconfig.h> +#include <ksimpleconfig.h> +#include <kstandarddirs.h> + +#include <qfile.h> +#include <qtextstream.h> +#include <qdom.h> +#include <qregexp.h> +#include <qmap.h> + +#include <iostream> + +class Pattern +{ + public: + Pattern(); + + bool isEmpty(); + + QString asString(); + + void merge( Pattern p ); + + bool optional; + bool zeroOrMore; + bool oneOrMore; + bool choice; +}; + +class Reference +{ + public: + typedef QValueList<Reference *> List; + + Reference() : substituted( false ) {} + + QString name; + Pattern pattern; + + bool substituted; +}; + +class Attribute +{ + public: + typedef QValueList<Attribute *> List; + + QString name; + QValueList<QString> choices; + QString defaultValue; + Pattern pattern; +}; + +class Element +{ + public: + typedef QValueList<Element *> List; + + Element(); + + QString name; + Element::List elements; + Attribute::List attributes; + Reference::List references; + Pattern pattern; + bool hasText; + bool isEmpty; +}; + +class Parser +{ + public: + Parser(); + + Element *parse( const QDomElement &docElement ); + + Reference *parseReference( const QDomElement &referenceElement ); + bool parseAttribute( const QDomElement &attributeElement, + Attribute *a ); + bool parseElement( const QDomElement &elementElement, Element *e, + Pattern pattern ); + + void substituteReferences( Element *s ); + + void doIndent( int cols ); + + void dumpPattern( Pattern pattern ); + void dumpReferences( const Reference::List &references, + int indent ); + void dumpAttributes( const Attribute::List &attributes, + int indent ); + void dumpElements( const Element::List &elements, int indent ); + void dumpElement( Element *element, int indent ); + void dumpTree( Element *s ); + void dumpDefinitionMap(); + + private: + QMap<QString,Element::List> mDefinitionMap; +}; + +#endif |