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