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