xml.cpp
author insilmaril
Mon, 18 Apr 2005 06:17:00 +0000
changeset 95 f688a9913724
parent 83 e90f5bef70c8
child 97 0b048b6bb6f4
permissions -rw-r--r--
added basic xLink functions
     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 		}	
   110 	} else if ( eName == "select" && state == StateMap ) 
   111 	{
   112 		state=StateMapSelect;
   113 	} else if ( eName == "setting" && state == StateMap ) 
   114 	{
   115 		state=StateMapSetting;
   116 		if (loadMode==NewMap)
   117 			readSettingAttr (atts);
   118 	} else if ( eName == "mapcenter" && state == StateMap ) 
   119 	{
   120 		state=StateMapCenter;
   121 		if (loadMode==NewMap)
   122 		{	
   123 			// Really use the found mapcenter as MCO in a new map
   124 			lastBranch=mc;	// avoid empty pointer
   125 		} else
   126 		{
   127 			// Treat the found mapcenter as a branch 
   128 			// in an existing map
   129 			LinkableMapObj* lmo=me->getSelection();
   130 			if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   131 			        || (typeid(*lmo) == typeid(MapCenterObj) ) )
   132 			{
   133 				lastBranch=(BranchObj*)(lmo);
   134 				if (loadMode==ImportAdd)
   135 				{
   136 					lastBranch->addBranch();
   137 					lastBranch=lastBranch->getLastBranch();
   138 				} else
   139 					lastBranch->clear();
   140 			} else
   141 				return false;
   142 		}
   143 		readBranchAttr (atts);
   144 	} else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter) 
   145 	{
   146 		state=StateMapCenterStandardFlag;
   147 	} else if ( eName == "heading" && state == StateMapCenter) 
   148 	{
   149 		state=StateMapCenterHeading;
   150 		if (!atts.value( "textColor").isEmpty() ) 
   151 		{
   152 			col.setNamedColor(atts.value("textColor"));
   153 			lastBranch->setColor(col ,false );
   154 		}	    
   155 	} else if ( eName == "note" && state == StateMapCenter) 
   156 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   157 		state=StateMapCenterNote;
   158 		if (!readNoteAttr (atts) ) return false;
   159 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   160 	{
   161 		laststate=state;
   162 		state=StateHtmlNote;
   163     } else if ( eName == "floatimage" && state == StateMapCenter ) 
   164 	{
   165 		state=StateMapCenterFloatImage;
   166         lastBranch->addFloatImage();
   167 		lastFloat=lastBranch->getLastFloatImage();
   168 		if (!readFloatImageAttr(atts)) return false;
   169 	} else if ( eName == "branch" && state == StateMap) 
   170 	{
   171 		// This is used in vymparts, which have no mapcenter!
   172 		isVymPart=true;
   173 		state=StateBranch;
   174 		LinkableMapObj* lmo=me->getSelection();
   175 		if (!lmo)
   176 		{
   177 			// If a vym part is _loaded_ (not imported), 
   178 			// selection==lmo==NULL
   179 			// Treat it like ImportAdd then...
   180 			loadMode=ImportAdd;
   181 			lmo=mc;
   182 		}	
   183 		if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   184 				|| (typeid(*lmo) == typeid(MapCenterObj) ) )
   185 		{
   186 			lastBranch=(BranchObj*)(lmo);
   187 			if (loadMode==ImportAdd)
   188 			{
   189 				lastBranch->addBranch();
   190 				lastBranch=lastBranch->getLastBranch();
   191 			} else
   192 				lastBranch->clear();
   193 		} else
   194 			return false;
   195 		branchDepth=1;
   196 		readBranchAttr (atts);
   197 	} else if ( eName == "branch" && state == StateMapCenter) 
   198 	{
   199 		state=StateBranch;
   200 		branchDepth=1;
   201 		lastBranch->addBranch();
   202 		lastBranch=lastBranch->getLastBranch();
   203 		readBranchAttr (atts);
   204 	} else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch) 
   205 	{
   206 		state=StateBranchStandardFlag;
   207 	} else if ( eName == "heading" && state == StateBranch) 
   208 	{
   209 		state=StateBranchHeading;
   210 		if (!atts.value( "textColor").isEmpty() ) 
   211 		{
   212 			col.setNamedColor(atts.value("textColor"));
   213 			lastBranch->setColor(col ,false );
   214 		}	    
   215     } else if ( eName == "note" && state == StateBranch) 
   216 	{
   217         state=StateBranchNote;
   218 		if (!readNoteAttr (atts) ) return false;
   219 	} else if ( eName == "htmlnote" && state == StateBranch) 
   220 	{
   221 		laststate=state;
   222 		state=StateHtmlNote;
   223 		no.clear();
   224 		if (!atts.value( "fonthint").isEmpty() ) 
   225 			no.setFontHint(atts.value ("fonthint") );
   226     } else if ( eName == "floatimage" && state == StateBranch ) 
   227 	{
   228 		state=StateBranchFloatImage;
   229         lastBranch->addFloatImage();
   230 		lastFloat=lastBranch->getLastFloatImage();
   231 		if (!readFloatImageAttr(atts)) return false;
   232     } else if ( eName == "xlink" && state == StateBranch ) 
   233 	{
   234 		state=StateBranchXLink;
   235 		if (!readXLinkAttr (atts)) return false;
   236     } else if ( eName == "branch" && state == StateBranch ) 
   237 	{
   238         lastBranch->addBranch();
   239 		lastBranch=lastBranch->getLastBranch();		
   240         branchDepth++;
   241 		readBranchAttr (atts);
   242     } else if ( eName == "html" && state == StateHtmlNote ) 
   243 	{
   244 		state=StateHtml;
   245 		htmldata="<"+eName;
   246 		readHtmlAttr(atts);
   247 		htmldata+=">";
   248     } else if ( state == StateHtml ) 
   249 	{
   250 		// accept all while in html mode,
   251 		htmldata+="<"+eName;
   252 		readHtmlAttr(atts);
   253 		htmldata+=">";
   254     } else
   255         return false;   // Error
   256     return true;
   257 }
   258 
   259 bool mapBuilderHandler::endElement  ( const QString&, const QString&, const QString &eName)
   260 {
   261 //	cout << "endElement </"<<eName<<">  state="<<state <<"  laststate="<<laststate<<endl;
   262     switch ( state ) 
   263 	{
   264         case StateMapSelect: state=StateMap;  return true;
   265         case StateMapSetting: state=StateMap;  return true;
   266         case StateMapCenter: state=StateMap;  return true;
   267         case StateMapCenterStandardFlag: state=StateMapCenter;  return true;
   268         case StateMapCenterHeading: state=StateMapCenter;  return true;
   269         case StateMapCenterNote: state=StateMapCenter;  return true;
   270         case StateMapCenterFloatImage: state=StateMapCenter;  return true;
   271         case StateBranch: 
   272             if (branchDepth>1) 
   273 			{
   274                 branchDepth--;
   275                 state=StateBranch;
   276             } else  
   277 			{
   278                 branchDepth=0;
   279 				if (isVymPart)
   280 					state=StateMap;
   281 				else
   282 					state=StateMapCenter;
   283             }   
   284 			lastBranch=(BranchObj*)(lastBranch->getParObj());
   285              return true;
   286         case StateBranchStandardFlag: state=StateBranch;  return true;
   287         case StateBranchHeading: state=StateBranch;  return true;
   288         case StateBranchNote: state=StateBranch; return true;
   289         case StateBranchFloatImage: state=StateBranch;  return true;
   290         case StateBranchXLink: state=StateBranch;  return true;
   291         case StateHtmlNote: state=laststate; return true;
   292         case StateHtml: 
   293 			htmldata+="</"+eName+">";
   294 			if (eName=="html")
   295 			{
   296 				state=StateHtmlNote;  
   297 				htmldata.replace ("<br></br>","<br />");
   298 				no.setNote (htmldata);
   299 				lastBranch->setNote (no);
   300 				return true;
   301 			}	else
   302 			{
   303 				return true;
   304 			}	
   305         case StateMap: state=StateInit;  return true;
   306         default : 
   307 			// even for HTML includes, this should never be reached
   308 			return false;
   309     }   
   310 }
   311 
   312 bool mapBuilderHandler::characters   ( const QString& ch)
   313 {
   314 	//cout << "characters \""<<ch<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   315 
   316 	QString ch_org=quotemeta (ch);
   317     QString ch_simplified=ch.simplifyWhiteSpace();
   318     if ( ch_simplified.isEmpty() ) return true;
   319 
   320     switch ( state ) 
   321     {
   322         case StateInit: break;
   323         case StateMap: break; 
   324 		case StateMapSelect:
   325 			me->select(ch_simplified);
   326 			break;
   327 		case StateMapSetting:break;
   328         case StateMapCenter: break;
   329         case StateMapCenterStandardFlag: 
   330             lastBranch->activateStandardFlag(ch_simplified); 
   331             break;
   332         case StateMapCenterHeading: 
   333             lastBranch->setHeading(ch_simplified); 
   334             break;
   335         case StateMapCenterNote:
   336 			lastBranch->setNote(ch_simplified);
   337 			break;
   338         case StateBranch: break;
   339         case StateBranchStandardFlag: 
   340             lastBranch->activateStandardFlag(ch_simplified); 
   341             break;
   342         case StateBranchHeading: 
   343             lastBranch->setHeading(ch_simplified);
   344             break;
   345         case StateBranchNote: 
   346 			lastBranch->setNote(ch_simplified);
   347 			break;
   348         case StateBranchFloatImage: break;
   349         case StateHtmlNote: break;
   350         case StateHtml:
   351 			htmldata+=ch_org;
   352 			break;
   353         default: 
   354 			return false;
   355     }
   356     return true;
   357 }
   358 
   359 QString mapBuilderHandler::errorString() 
   360 {
   361     return "the document is not in the VYM file format";
   362 }
   363 
   364 bool mapBuilderHandler::fatalError( const QXmlParseException& exception ) 
   365 {
   366     errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n")
   367     .arg( exception.message() )
   368     .arg( exception.lineNumber() )
   369     .arg( exception.columnNumber() );
   370 
   371     return QXmlDefaultHandler::fatalError( exception );
   372 }
   373 
   374 void mapBuilderHandler::setMapEditor (MapEditor* e)
   375 {
   376     me=e;
   377 	mc=me->getMapCenter();
   378 }
   379 
   380 void mapBuilderHandler::setTmpDir (QString tp)
   381 {
   382 	tmpDir=tp;
   383 }
   384 
   385 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
   386 {
   387 	loadMode=lm;
   388 }
   389 
   390 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
   391 {
   392 	bool okx,oky;
   393 	int x,y;
   394 	if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2) 
   395 	{
   396 		if (!a.value( "absPosY").isEmpty() ) 
   397 		{
   398 			x=a.value("absPosX").toInt (&okx, 10);
   399 			y=a.value("absPosY").toInt (&oky, 10);
   400 			if (okx && oky) 
   401 				lastBranch->move(x,y);
   402 			else
   403 				return false;   // Couldn't read absPos
   404 		}           
   405 	}           
   406 	if (!a.value( "scrolled").isEmpty() )
   407 		lastBranch->toggleScroll();
   408 	if (!a.value( "url").isEmpty() ) 
   409 		lastBranch->setURL (a.value ("url"));
   410 	if (!a.value( "vymLink").isEmpty() ) 
   411 		lastBranch->setVymLink (a.value ("vymLink"));
   412 	if (!a.value( "frameType").isEmpty() ) 
   413 		lastBranch->setFrameType (a.value("frameType"));
   414 	return true;	
   415 }
   416 
   417 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
   418 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   419 	no.clear();
   420 	QString fn;
   421 	if (!a.value( "href").isEmpty() ) 
   422 	{
   423 		// Load note
   424 		fn=parseHREF(a.value ("href") );
   425 		QFile file (fn);
   426 		QString s;						// Reading a note
   427 
   428 		if ( !file.open( IO_ReadOnly) )
   429 		{
   430 			qWarning ("mapBuilderHandler::readNoteAttr:  Couldn't load "+fn);
   431 			return false;
   432 		}	
   433 		QTextStream stream( &file );
   434 		QString lines;
   435 		while ( !stream.eof() ) {
   436 			lines += stream.readLine()+"\n"; 
   437 		}
   438 		file.close();
   439 		// Convert to richtext
   440 		if ( !QStyleSheet::mightBeRichText( lines ) )
   441 		{
   442 			// Here we are workarounding the QT conversion method:
   443 			// convertFromPlainText does not generate valid xml, needed
   444 			// for the parser, but just <p> and <br> without closing tags.
   445 			// So we have to add those by ourselves
   446 			//lines=quotemeta (lines);
   447 			lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal );
   448 			lines.replace ("<br>","<br />");
   449 		}	
   450 
   451 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   452 		no.setNote (lines);
   453 	}		
   454 	if (!a.value( "fonthint").isEmpty() ) 
   455 		no.setFontHint(a.value ("fonthint") );
   456 	if (state == StateMapCenterNote) 	
   457 		mc->setNote(no);
   458 	else
   459 		lastBranch->setNote(no);
   460 	return true;
   461 }
   462 
   463 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
   464 {
   465 	if (!a.value( "useOrientation").isEmpty() ) 
   466 	{
   467 		if (a.value ("useOrientation") =="true")
   468 			lastFloat->setUseOrientation (true);
   469 		else	
   470 			lastFloat->setUseOrientation (false);
   471 	}	
   472 	if (!a.value( "href").isEmpty() )
   473 	{
   474 		// Load FloatImage
   475 		if (!lastFloat->load (parseHREF(a.value ("href") ) ))
   476 		{
   477 			QMessageBox::warning( 0, "Warning: " ,
   478 				"Couldn't load float image\n"+parseHREF(a.value ("href") ));
   479 			lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
   480 			lastFloat=NULL;
   481 			return true;
   482 		}
   483 		
   484 	}	
   485 	if (!a.value( "floatExport").isEmpty() ) 
   486 	{
   487 		if (a.value ("floatExpofrt") =="true")
   488 			lastFloat->setFloatExport (true);
   489 		else	
   490 			lastFloat->setFloatExport (false);
   491 	}	
   492 	if (!a.value( "zPlane").isEmpty() ) 
   493 		lastFloat->setZ (a.value("zPlane").toInt ());
   494     int x,y;
   495     bool okx,oky;
   496 	if (!a.value( "relPosX").isEmpty() ) 
   497 	{
   498 		if (!a.value( "relPosY").isEmpty() ) 
   499 		{
   500 			// read relPos
   501 			x=a.value("relPosX").toInt (&okx, 10);
   502 			y=a.value("relPosY").toInt (&oky, 10);
   503 			if (okx && oky) 
   504 				lastFloat->setRelPos (QPoint (x,y) );
   505 			else
   506 				// Couldn't read relPos
   507 				return false;  
   508 		}           
   509 	}	
   510 	return true;
   511 }
   512 
   513 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
   514 {
   515 	QColor col;
   516 	XLinkObj *xlo=new XLinkObj (mc->getCanvas());
   517 	if (!a.value( "xLinkColor").isEmpty() ) 
   518 	{
   519 		col.setNamedColor(a.value("xLinkColor"));
   520 		xlo->setColor (col);
   521 	}
   522 
   523 	if (!a.value( "beginBranch").isEmpty() ) 
   524 	{
   525 		if (!a.value( "endBranch").isEmpty() ) 
   526 		{
   527 			LinkableMapObj *lmo=mc->findObj (a.value( "beginBranch"));
   528 			if (lmo && typeid (*lmo)==typeid (BranchObj))
   529 			{
   530 				xlo->setBegin ((BranchObj*)(lmo));
   531 				lmo=mc->findObj (a.value( "endBranch"));
   532 				if (lmo && typeid (*lmo)==typeid (BranchObj))
   533 				{
   534 					xlo->setEnd ((BranchObj*)(lmo));
   535 					xlo->activate();
   536 					return true;
   537 				}
   538 			}
   539 			return true;	// Not all branches there yet, no error
   540 		}           
   541 	}	
   542 	return false; 
   543 }
   544 
   545 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
   546 {
   547 	for (int i=1; i<=a.count(); i++)
   548 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   549 	return true;
   550 }
   551 
   552 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
   553 {
   554 	if (!a.value( "key").isEmpty() ) 
   555 	{
   556 		if (!a.value( "value").isEmpty() ) 
   557 			settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));
   558 		else
   559 			return false;
   560 		
   561 	} else
   562 		return false;
   563 	
   564 	return true;
   565 }