xml.cpp
author insilmaril
Sat, 23 Jul 2005 10:26:29 +0000
changeset 132 fd7f08a85971
parent 106 4083860dd82e
child 160 72cc3873306a
permissions -rw-r--r--
updated api.*
     1 #include "xml.h"
     2 
     3 #include <qmessagebox.h>
     4 #include <qcolor.h>
     5 #include <qstylesheet.h>
     6 #include <iostream>
     7 
     8 #include "misc.h"
     9 #include "settings.h"
    10 #include "linkablemapobj.h"
    11 
    12 #include "version.h"
    13 
    14 static BranchObj *lastBranch;
    15 static FloatObj *lastFloat;
    16 
    17 extern Settings settings;
    18 
    19 mapBuilderHandler::mapBuilderHandler() {}
    20 
    21 mapBuilderHandler::~mapBuilderHandler() {}
    22 
    23 QString mapBuilderHandler::errorProtocol() { return errorProt; }
    24 
    25 bool mapBuilderHandler::startDocument()
    26 {
    27     errorProt = "";
    28     state = StateInit;
    29     laststate = StateInit;
    30     branchDepth=0;
    31 	htmldata="";
    32 	isVymPart=false;
    33     return true;
    34 }
    35 
    36 
    37 QString mapBuilderHandler::parseHREF(QString href)
    38 {
    39 	QString type=href.section(":",0,0);
    40 	QString path=href.section(":",1,1);
    41 	if (!tmpDir.endsWith("/"))
    42 		return tmpDir + "/" + path;
    43 	else	
    44 		return tmpDir + path;
    45 }
    46 
    47 bool mapBuilderHandler::startElement  ( const QString&, const QString&,
    48                     const QString& eName, const QXmlAttributes& atts ) 
    49 {
    50     QColor col;
    51 	//cout << "startElement <"<<eName<<">  state="<<state <<"  laststate="<<laststate<<"   loadMode="<<loadMode<<endl;
    52     if ( state == StateInit && (eName == "vymmap")  ) 
    53 	{
    54         state = StateMap;
    55 		if (!atts.value( "version").isEmpty() ) 
    56 		{
    57 			mc->setVersion(atts.value( "version" ));
    58 			if (!mc->checkVersion())
    59 				QMessageBox::warning( 0, "Warning: Version Problem" ,
    60 				   "<h3>Map is newer than VYM</h3>"
    61 				   "<p>The map you are just trying to load was "
    62 				   "saved using vym " +atts.value("version")+". "
    63 				   "The version of this vym is " __VYM_VERSION__
    64 				   ". If you run into problems after pressing "
    65 				   "the ok-button below, updating vym should help.");
    66 
    67 		}
    68 		if (loadMode==NewMap)
    69 		{
    70 			if (!atts.value( "author").isEmpty() )
    71 			{
    72 				mc->setAuthor(atts.value( "author" ) );
    73 			}
    74 			if (!atts.value( "comment").isEmpty() )
    75 			{
    76 				mc->setComment (atts.value( "comment" ) );
    77 			}
    78 			if (!atts.value( "backgroundColor").isEmpty() )
    79 			{
    80 				col.setNamedColor(atts.value("backgroundColor"));
    81 				mc->getCanvas()->setBackgroundColor(col);
    82 			}	    
    83 			if (!atts.value( "linkColorHint").isEmpty() ) 
    84 			{
    85 				if (atts.value("linkColorHint")=="HeadingColor")
    86 					me->setLinkColorHint(HeadingColor);
    87 				else
    88 					me->setLinkColorHint(DefaultColor);
    89 			}
    90 			if (!atts.value( "linkStyle").isEmpty() ) 
    91 			{
    92 				QString s=atts.value("linkStyle");
    93 				if (s=="StyleLine")
    94 					me->setLinkStyle(StyleLine);
    95 				else	
    96 					if (s=="StyleParabel")
    97 						me->setLinkStyle(StyleParabel);
    98 					else	
    99 						if (s=="StylePolyLine")
   100 							me->setLinkStyle(StylePolyLine);
   101 						else	
   102 							me->setLinkStyle(StylePolyParabel);
   103 			}	
   104 			if (!atts.value( "linkColor").isEmpty() ) 
   105 			{
   106 				col.setNamedColor(atts.value("linkColor"));
   107 				me->setLinkColor(col);
   108 			}	
   109 			if (!atts.value( "defXLinkColor").isEmpty() ) 
   110 			{
   111 				col.setNamedColor(atts.value("defXLinkColor"));
   112 				me->setDefXLinkColor(col);
   113 			}	
   114 			if (!atts.value( "defXLinkWidth").isEmpty() ) 
   115 			{
   116 				me->setDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
   117 			}	
   118 		}	
   119 	} else if ( eName == "select" && state == StateMap ) 
   120 	{
   121 		state=StateMapSelect;
   122 	} else if ( eName == "setting" && state == StateMap ) 
   123 	{
   124 		state=StateMapSetting;
   125 		if (loadMode==NewMap)
   126 			readSettingAttr (atts);
   127 	} else if ( eName == "mapcenter" && state == StateMap ) 
   128 	{
   129 		state=StateMapCenter;
   130 		if (loadMode==NewMap)
   131 		{	
   132 			// Really use the found mapcenter as MCO in a new map
   133 			lastBranch=mc;	// avoid empty pointer
   134 		} else
   135 		{
   136 			// Treat the found mapcenter as a branch 
   137 			// in an existing map
   138 			LinkableMapObj* lmo=me->getSelection();
   139 			if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   140 			        || (typeid(*lmo) == typeid(MapCenterObj) ) )
   141 			{
   142 				lastBranch=(BranchObj*)(lmo);
   143 				if (loadMode==ImportAdd)
   144 				{
   145 					lastBranch->addBranch();
   146 					lastBranch=lastBranch->getLastBranch();
   147 				} else
   148 					lastBranch->clear();
   149 			} else
   150 				return false;
   151 		}
   152 		readBranchAttr (atts);
   153 	} else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter) 
   154 	{
   155 		state=StateMapCenterStandardFlag;
   156 	} else if ( eName == "heading" && state == StateMapCenter) 
   157 	{
   158 		state=StateMapCenterHeading;
   159 		if (!atts.value( "textColor").isEmpty() ) 
   160 		{
   161 			col.setNamedColor(atts.value("textColor"));
   162 			lastBranch->setColor(col ,false );
   163 		}	    
   164 	} else if ( eName == "note" && state == StateMapCenter) 
   165 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   166 		state=StateMapCenterNote;
   167 		if (!readNoteAttr (atts) ) return false;
   168 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   169 	{
   170 		laststate=state;
   171 		state=StateHtmlNote;
   172     } else if ( eName == "floatimage" && state == StateMapCenter ) 
   173 	{
   174 		state=StateMapCenterFloatImage;
   175         lastBranch->addFloatImage();
   176 		lastFloat=lastBranch->getLastFloatImage();
   177 		if (!readFloatImageAttr(atts)) return false;
   178 	} else if ( eName == "branch" && state == StateMap) 
   179 	{
   180 		// This is used in vymparts, which have no mapcenter!
   181 		isVymPart=true;
   182 		state=StateBranch;
   183 		LinkableMapObj* lmo=me->getSelection();
   184 		if (!lmo)
   185 		{
   186 			// If a vym part is _loaded_ (not imported), 
   187 			// selection==lmo==NULL
   188 			// Treat it like ImportAdd then...
   189 			loadMode=ImportAdd;
   190 			lmo=mc;
   191 		}	
   192 		if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   193 				|| (typeid(*lmo) == typeid(MapCenterObj) ) )
   194 		{
   195 			lastBranch=(BranchObj*)(lmo);
   196 			if (loadMode==ImportAdd)
   197 			{
   198 				lastBranch->addBranch();
   199 				lastBranch=lastBranch->getLastBranch();
   200 			} else
   201 				lastBranch->clear();
   202 		} else
   203 			return false;
   204 		branchDepth=1;
   205 		readBranchAttr (atts);
   206 	} else if ( eName == "branch" && state == StateMapCenter) 
   207 	{
   208 		state=StateBranch;
   209 		branchDepth=1;
   210 		lastBranch->addBranch();
   211 		lastBranch=lastBranch->getLastBranch();
   212 		readBranchAttr (atts);
   213 	} else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch) 
   214 	{
   215 		state=StateBranchStandardFlag;
   216 	} else if ( eName == "heading" && state == StateBranch) 
   217 	{
   218 		state=StateBranchHeading;
   219 		if (!atts.value( "textColor").isEmpty() ) 
   220 		{
   221 			col.setNamedColor(atts.value("textColor"));
   222 			lastBranch->setColor(col ,false );
   223 		}	    
   224     } else if ( eName == "note" && state == StateBranch) 
   225 	{
   226         state=StateBranchNote;
   227 		if (!readNoteAttr (atts) ) return false;
   228 	} else if ( eName == "htmlnote" && state == StateBranch) 
   229 	{
   230 		laststate=state;
   231 		state=StateHtmlNote;
   232 		no.clear();
   233 		if (!atts.value( "fonthint").isEmpty() ) 
   234 			no.setFontHint(atts.value ("fonthint") );
   235     } else if ( eName == "floatimage" && state == StateBranch ) 
   236 	{
   237 		state=StateBranchFloatImage;
   238         lastBranch->addFloatImage();
   239 		lastFloat=lastBranch->getLastFloatImage();
   240 		if (!readFloatImageAttr(atts)) return false;
   241     } else if ( eName == "xlink" && state == StateBranch ) 
   242 	{
   243 		state=StateBranchXLink;
   244 		if (!readXLinkAttr (atts)) return false;
   245     } else if ( eName == "branch" && state == StateBranch ) 
   246 	{
   247         lastBranch->addBranch();
   248 		lastBranch=lastBranch->getLastBranch();		
   249         branchDepth++;
   250 		readBranchAttr (atts);
   251     } else if ( eName == "html" && state == StateHtmlNote ) 
   252 	{
   253 		state=StateHtml;
   254 		htmldata="<"+eName;
   255 		readHtmlAttr(atts);
   256 		htmldata+=">";
   257     } else if ( state == StateHtml ) 
   258 	{
   259 		// accept all while in html mode,
   260 		htmldata+="<"+eName;
   261 		readHtmlAttr(atts);
   262 		htmldata+=">";
   263     } else
   264         return false;   // Error
   265     return true;
   266 }
   267 
   268 bool mapBuilderHandler::endElement  ( const QString&, const QString&, const QString &eName)
   269 {
   270 //	cout << "endElement </"<<eName<<">  state="<<state <<"  laststate="<<laststate<<endl;
   271     switch ( state ) 
   272 	{
   273         case StateMapSelect: state=StateMap;  return true;
   274         case StateMapSetting: state=StateMap;  return true;
   275         case StateMapCenter: state=StateMap;  return true;
   276         case StateMapCenterStandardFlag: state=StateMapCenter;  return true;
   277         case StateMapCenterHeading: state=StateMapCenter;  return true;
   278         case StateMapCenterNote: state=StateMapCenter;  return true;
   279         case StateMapCenterFloatImage: state=StateMapCenter;  return true;
   280         case StateBranch: 
   281             if (branchDepth>1) 
   282 			{
   283                 branchDepth--;
   284                 state=StateBranch;
   285             } else  
   286 			{
   287                 branchDepth=0;
   288 				if (isVymPart)
   289 					state=StateMap;
   290 				else
   291 					state=StateMapCenter;
   292             }   
   293 			lastBranch=(BranchObj*)(lastBranch->getParObj());
   294              return true;
   295         case StateBranchStandardFlag: state=StateBranch;  return true;
   296         case StateBranchHeading: state=StateBranch;  return true;
   297         case StateBranchNote: state=StateBranch; return true;
   298         case StateBranchFloatImage: state=StateBranch;  return true;
   299         case StateBranchXLink: state=StateBranch;  return true;
   300         case StateHtmlNote: state=laststate; return true;
   301         case StateHtml: 
   302 			htmldata+="</"+eName+">";
   303 			if (eName=="html")
   304 			{
   305 				state=StateHtmlNote;  
   306 				htmldata.replace ("<br></br>","<br />");
   307 				no.setNote (htmldata);
   308 				lastBranch->setNote (no);
   309 				return true;
   310 			}	else
   311 			{
   312 				return true;
   313 			}	
   314         case StateMap: state=StateInit;  return true;
   315         default : 
   316 			// even for HTML includes, this should never be reached
   317 			return false;
   318     }   
   319 }
   320 
   321 bool mapBuilderHandler::characters   ( const QString& ch)
   322 {
   323 	//cout << "characters \""<<ch<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   324 
   325 	QString ch_org=quotemeta (ch);
   326     QString ch_simplified=ch.simplifyWhiteSpace();
   327     if ( ch_simplified.isEmpty() ) return true;
   328 
   329     switch ( state ) 
   330     {
   331         case StateInit: break;
   332         case StateMap: break; 
   333 		case StateMapSelect:
   334 			me->select(ch_simplified);
   335 			break;
   336 		case StateMapSetting:break;
   337         case StateMapCenter: break;
   338         case StateMapCenterStandardFlag: 
   339             lastBranch->activateStandardFlag(ch_simplified); 
   340             break;
   341         case StateMapCenterHeading: 
   342             lastBranch->setHeading(ch_simplified); 
   343             break;
   344         case StateMapCenterNote:
   345 			lastBranch->setNote(ch_simplified);
   346 			break;
   347         case StateBranch: break;
   348         case StateBranchStandardFlag: 
   349             lastBranch->activateStandardFlag(ch_simplified); 
   350             break;
   351         case StateBranchHeading: 
   352             lastBranch->setHeading(ch_simplified);
   353             break;
   354         case StateBranchNote: 
   355 			lastBranch->setNote(ch_simplified);
   356 			break;
   357         case StateBranchFloatImage: break;
   358         case StateHtmlNote: break;
   359         case StateHtml:
   360 			htmldata+=ch_org;
   361 			break;
   362         default: 
   363 			return false;
   364     }
   365     return true;
   366 }
   367 
   368 QString mapBuilderHandler::errorString() 
   369 {
   370     return "the document is not in the VYM file format";
   371 }
   372 
   373 bool mapBuilderHandler::fatalError( const QXmlParseException& exception ) 
   374 {
   375     errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n")
   376     .arg( exception.message() )
   377     .arg( exception.lineNumber() )
   378     .arg( exception.columnNumber() );
   379 
   380     return QXmlDefaultHandler::fatalError( exception );
   381 }
   382 
   383 void mapBuilderHandler::setMapEditor (MapEditor* e)
   384 {
   385     me=e;
   386 	mc=me->getMapCenter();
   387 }
   388 
   389 void mapBuilderHandler::setTmpDir (QString tp)
   390 {
   391 	tmpDir=tp;
   392 }
   393 
   394 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
   395 {
   396 	loadMode=lm;
   397 }
   398 
   399 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
   400 {
   401 	bool okx,oky;
   402 	int x,y;
   403 	if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2) 
   404 	{
   405 		if (!a.value( "absPosY").isEmpty() ) 
   406 		{
   407 			x=a.value("absPosX").toInt (&okx, 10);
   408 			y=a.value("absPosY").toInt (&oky, 10);
   409 			if (okx && oky) 
   410 				lastBranch->move(x,y);
   411 			else
   412 				return false;   // Couldn't read absPos
   413 		}           
   414 	}           
   415 	if (!a.value( "scrolled").isEmpty() )
   416 		lastBranch->toggleScroll();
   417 	if (!a.value( "url").isEmpty() ) 
   418 		lastBranch->setURL (a.value ("url"));
   419 	if (!a.value( "vymLink").isEmpty() ) 
   420 		lastBranch->setVymLink (a.value ("vymLink"));
   421 	if (!a.value( "frameType").isEmpty() ) 
   422 		lastBranch->setFrameType (a.value("frameType"));
   423 	return true;	
   424 }
   425 
   426 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
   427 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   428 	no.clear();
   429 	QString fn;
   430 	if (!a.value( "href").isEmpty() ) 
   431 	{
   432 		// Load note
   433 		fn=parseHREF(a.value ("href") );
   434 		QFile file (fn);
   435 		QString s;						// Reading a note
   436 
   437 		if ( !file.open( IO_ReadOnly) )
   438 		{
   439 			qWarning ("mapBuilderHandler::readNoteAttr:  Couldn't load "+fn);
   440 			return false;
   441 		}	
   442 		QTextStream stream( &file );
   443 		QString lines;
   444 		while ( !stream.eof() ) {
   445 			lines += stream.readLine()+"\n"; 
   446 		}
   447 		file.close();
   448 		// Convert to richtext
   449 		if ( !QStyleSheet::mightBeRichText( lines ) )
   450 		{
   451 			// Here we are workarounding the QT conversion method:
   452 			// convertFromPlainText does not generate valid xml, needed
   453 			// for the parser, but just <p> and <br> without closing tags.
   454 			// So we have to add those by ourselves
   455 			//lines=quotemeta (lines);
   456 			lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal );
   457 			lines.replace ("<br>","<br />");
   458 		}	
   459 
   460 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   461 		no.setNote (lines);
   462 	}		
   463 	if (!a.value( "fonthint").isEmpty() ) 
   464 		no.setFontHint(a.value ("fonthint") );
   465 	if (state == StateMapCenterNote) 	
   466 		mc->setNote(no);
   467 	else
   468 		lastBranch->setNote(no);
   469 	return true;
   470 }
   471 
   472 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
   473 {
   474 	if (!a.value( "useOrientation").isEmpty() ) 
   475 	{
   476 		if (a.value ("useOrientation") =="true")
   477 			lastFloat->setUseOrientation (true);
   478 		else	
   479 			lastFloat->setUseOrientation (false);
   480 	}	
   481 	if (!a.value( "href").isEmpty() )
   482 	{
   483 		// Load FloatImage
   484 		if (!lastFloat->load (parseHREF(a.value ("href") ) ))
   485 		{
   486 			QMessageBox::warning( 0, "Warning: " ,
   487 				"Couldn't load float image\n"+parseHREF(a.value ("href") ));
   488 			lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
   489 			lastFloat=NULL;
   490 			return true;
   491 		}
   492 		
   493 	}	
   494 	if (!a.value( "floatExport").isEmpty() ) 
   495 	{
   496 		if (a.value ("floatExpofrt") =="true")
   497 			lastFloat->setFloatExport (true);
   498 		else	
   499 			lastFloat->setFloatExport (false);
   500 	}	
   501 	if (!a.value( "zPlane").isEmpty() ) 
   502 		lastFloat->setZ (a.value("zPlane").toInt ());
   503     int x,y;
   504     bool okx,oky;
   505 	if (!a.value( "relPosX").isEmpty() ) 
   506 	{
   507 		if (!a.value( "relPosY").isEmpty() ) 
   508 		{
   509 			// read relPos
   510 			x=a.value("relPosX").toInt (&okx, 10);
   511 			y=a.value("relPosY").toInt (&oky, 10);
   512 			if (okx && oky) 
   513 				lastFloat->setRelPos (QPoint (x,y) );
   514 			else
   515 				// Couldn't read relPos
   516 				return false;  
   517 		}           
   518 	}	
   519 	return true;
   520 }
   521 
   522 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
   523 {
   524 	QColor col;
   525 	bool okx;
   526 	bool success=false;
   527 	XLinkObj *xlo=new XLinkObj (mc->getCanvas());
   528 	if (!a.value( "color").isEmpty() ) 
   529 	{
   530 		col.setNamedColor(a.value("color"));
   531 		xlo->setColor (col);
   532 	}
   533 
   534 	if (!a.value( "width").isEmpty() ) 
   535 	{
   536 		xlo->setWidth(a.value ("width").toInt (&okx, 10));
   537 	}
   538 
   539 	if (!a.value( "beginBranch").isEmpty() ) 
   540 	{
   541 		if (!a.value( "endBranch").isEmpty() ) 
   542 		{
   543 			LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
   544 			if (lmo && typeid (*lmo)==typeid (BranchObj))
   545 			{
   546 				xlo->setBegin ((BranchObj*)(lmo));
   547 				lmo=mc->findObjBySelect (a.value( "endBranch"));
   548 				if (lmo && typeid (*lmo)==typeid (BranchObj))
   549 				{
   550 					xlo->setEnd ((BranchObj*)(lmo));
   551 					xlo->activate();
   552 				}
   553 			}
   554 			success=true; // Not all branches there yet, no error
   555 		}           
   556 	}	
   557 	if (!success) delete (xlo);
   558 	return success;
   559 }
   560 
   561 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
   562 {
   563 	for (int i=1; i<=a.count(); i++)
   564 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   565 	return true;
   566 }
   567 
   568 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
   569 {
   570 	if (!a.value( "key").isEmpty() ) 
   571 	{
   572 		if (!a.value( "value").isEmpty() ) 
   573 			settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));
   574 		else
   575 			return false;
   576 		
   577 	} else
   578 		return false;
   579 	
   580 	return true;
   581 }