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