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