00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "graphml.hpp"
00017 #include "mpsi.hpp"
00018 #include "core/category.hpp"
00019 #include "../edgelist.hpp"
00020 #include "core/utils.hpp"
00021 #include <qdict.h>
00022 #include <qfile.h>
00023 #include <qregexp.h>
00024 #include <qiodevice.h>
00025 #include <qxml.h>
00026
00027 GraphML::GraphML(PropositionGraph* cl)
00028 {
00029 caller = cl;
00030 }
00031
00032 GraphML::~GraphML()
00033 {
00034 }
00035
00036
00037 bool GraphML::startElement(const QString &, const QString &,
00038 const QString &qName,
00039 const QXmlAttributes &attribs)
00040 {
00041 if (qName == "node") {
00042 currentItem = attribs.value("id");
00043 caller->newProposition(currentItem);
00044
00045 }
00046 return true;
00047 }
00048
00049 bool GraphML::characters(const QString &str)
00050 {
00051 currentText += str;
00052 return true;
00053 }
00054
00055 bool GraphML::endElement(const QString &, const QString &,
00056 const QString &qName)
00057 {
00058 if (qName == "graphml")
00059 {
00060 QDictIterator<Proposition> it ( *caller );
00061 for( ; it.current(); ++it ) {
00062 it.current()->setPublicName(it.current()->name());
00063 loadProp(it.current()->name());
00064 }
00065 }
00066 return true;
00067 }
00068
00069 bool GraphML::fatalError(const QXmlParseException &exception)
00070 {
00071 qWarning("Line %d, column %d: %s", exception.lineNumber(),
00072 exception.columnNumber(), exception.message().ascii());
00073
00074 return false;
00075
00076 }
00077
00078 void GraphML::ioError(const QFile file, const QString message)
00079 {
00080 caller->error(file.name() + message + ": " + file.errorString());
00081 }
00082
00083 void GraphML::loadProp(QString pname)
00084 {
00085 Mpsi handler(caller,pname);
00086 QFile file("data/" + pname + ".mpsi");
00087 QXmlSimpleReader reader;
00088 reader.setContentHandler(&handler);
00089 reader.setErrorHandler(&handler);
00090 reader.parse(&file);
00091 file.close();
00092 }
00093
00094 void GraphML::loadCat(QString dir)
00095 {
00096 QFile pfile(dir + "/cats.mpsi");
00097 if ( pfile.open( IO_ReadOnly ) ) {
00098 QRegExp rx("<(.*)>(.*)</.*>");
00099 QString line;
00100 Category *current;
00101 QTextStream stream( &pfile );
00102 while ( !stream.atEnd() ) {
00103 line = stream.readLine();
00104 rx.search(line);
00105 if (rx.cap(1) == "id") {
00106 current = new Category();
00107 current->setId(rx.cap(2));
00108 } else if (rx.cap(1) == "name") {
00109 current->setName(rx.cap(2));
00110 } else if (rx.cap(1) == "color") {
00111
00112 QRegExp rz("(\\d*),(\\d*),(\\d*)");
00113 rz.search(rx.cap(2));
00114 current->setColor(QColor(rz.cap(1).toInt(),rz.cap(2).toInt(),rz.cap(3).toInt()));
00115 } else if (rx.cap(1) == "description") {
00116 current->setDescription(rx.cap(2));
00117 caller->categories->insert(current->name(), current);
00118 }
00119 }
00120 }
00121 }
00122
00123 void GraphML::save(QString dir)
00124 {
00125 QFile wfile( dir + "/prop.graphml" );
00126 if ( wfile.open( IO_WriteOnly | IO_Translate ) ) {
00127 QTextStream stream( &wfile );
00128 QDictIterator<Proposition> it ( *caller );
00129 stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
00130 << "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\r\n"
00131 << " <graph id=\"G\" edgedefault=\"directed\">\r\n"
00132 << " <numnodes>" << caller->lastName() << "</numnodes>\r\n";
00133
00134 for( ; it.current(); ++it ) {
00135 stream << " <node id=\"" << it.current()->name() << "\"/>\r\n";
00136 saveProp(dir, it.current()->name());
00137 }
00138 stream << " </graph>\r\n"
00139 << "</graphml>";
00140
00141 wfile.close();
00142 }
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 void GraphML::saveProp(QString dir, QString prop)
00188 {
00189 Proposition* p = caller->operator[](prop);
00190 QFile wfile( dir + "/"+ prop + ".mpsi" );
00191 if ( wfile.open( IO_WriteOnly | IO_Translate ) ) {
00192 QTextStream stream( &wfile );
00193 stream
00194 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
00195 << "<mpsi>\r\n"
00196 << " <id>" << p->name() << "</id>\r\n"
00197 << " <name>" << escapeXml(p->publicName()) << "</name>\r\n"
00198 << " <description>" << escapeXml(p->description()) << "</description>\r\n"
00199 << " <category>" << p->category() << "</category>\r\n"
00200 << " <references>" << escapeXml(p->references()) << "</references>\r\n";
00201
00202 EdgeList::iterator ix = p->outgoing->begin();
00203 while ( ix != p->outgoing->end()) {
00204 stream
00205 << " <edge id=\"" << (*ix)->name() << "\" target=\"" << (*ix)->targetName() << "\">\r\n"
00206 << " <trust>" << (*ix)->trust() << "</trust>\r\n"
00207 << " </edge>\r\n";
00208 ++ix;
00209 }
00210 stream << "</mpsi>";
00211
00212 caller->operator[](prop)->setChanged(false);
00213 }
00214 }