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