//////////////////////////////////////////////////////////////////////////////////////////
///Creates the XML database file from a set of filepaths and returns number of new paths
int FileSniffer::createDatabase(std::set<std::string>& filelist)
{
	int count=0;
	try{
	XmlWriter writer;	//create new xml 
	writer.addDeclaration();
	writer.start("filelist");
	newfilesList.clear();
	for (std::set<std::string>::iterator it=filelist.begin(); it != filelist.end(); it++)	//add all paths in fileset to the xml
	{
		if(!isInExcluded(*it))
		{
			count++;
			XmlWriter childnode;
			childnode.start("filepath");	//add filepath tag for each path
			childnode.addBody(*it);		//add path
			childnode.end();				//add ending tag
			writer.addBody(childnode.xml());
			newfilesList.insert(*it);
		}
	}
	writer.end();
	std::ofstream myfile;	//create new file and write the xml in it
	myfile.open (databaseFilePath.c_str());
	myfile << writer.xml();
	myfile.close();
	StoreResult();
	}
	catch(std::exception ex)
    {
      std::cout << "\n  " << ex.what() << "\n\n";
    }
	return count;
}
//////////////////////////////////////////////////////////////
///Stores results of analysis into a text file
void FileSniffer::StoreResult()
{
	try{
	std::stringstream ss;
	ss<<newfilesList.size();
	XmlWriter writer;	//create new xml 
	writer.addDeclaration();
	writer.start("filelist");
	XmlWriter childnode1;
	childnode1.start("listsize");	//add filepath tag for each path
	childnode1.addBody(ss.str());		//add path
	childnode1.end();				//add ending tag
	writer.addBody(childnode1.xml());
	for (std::set<std::string>::iterator it=newfilesList.begin(); it != newfilesList.end(); it++)	//add all paths in fileset to the xml
	{
		XmlWriter childnode;
		childnode.start("filepath");	//add filepath tag for each path
		childnode.addBody(*it);		//add path
		childnode.end();				//add ending tag
		writer.addBody(childnode.xml());
		
	}
	writer.end();
	std::ofstream myfile;	//create new file and write the xml in it
	myfile.open (resultFilePath.c_str());
	myfile << writer.xml();
	myfile.close();
	}
	catch(std::exception ex)
    {
      std::cout << "\n  " << ex.what() << "\n\n";
    }

}
/////////////////////////////////////////////////////////
//chunk textMessage and write to xmlfile
//the coming message formate: 
//username*versionNumber*dependency$goingtosendfile'sName
////////////////////////////////////////////////////////
std::string writeToXML(std::string msg)
{
	bool v; std::string retmsg;
	XmlWriter myxml;
	std::string msgOwner,msgNumber,msgDependency;
	int pos1,pos2,pos3;
    pos3 = msg.find_last_of("$");
	std::string sendingFileName = msg.substr(pos3+1);
	pos1 = msg.find_first_of("*");
	msgOwner = msg.substr(0,pos1);
	msg = msg.substr(pos1+1);
	pos2 = msg.find_last_of("*");
	msgNumber = msg.substr(0,pos2);
	msgDependency = msg.substr(pos2+1);
	int pos = sendingFileName.find(".");
	std::string justname= sendingFileName.substr(0,pos);
	bool check = checkFileAccessValidation(sendingFileName);
   if(check){ //file not in repository	
	std::string folderPath = "..//Reciever//storedPackage//"+justname;
	v = FileSystem::Directory::create(folderPath);

	myxml.addDeclaration();
	myxml.addComment("XML MetaData for file "+sendingFileName);
	myxml.start("root");
	myxml.element("owner",msgOwner);
	myxml.element("versionNumber",msgNumber);
	myxml.element("dependency",msgDependency);
	myxml.end();
	//if(v){
	std::ofstream out(folderPath+"//"+justname+"_xmlMetadata.xml");
	if(!out.good()){  throw std::invalid_argument(std::string("can't open file ") + sendingFileName);}
	out << myxml.xml().c_str();
	out.close();
	retmsg = "successfully stored xmlMetadata for file "+sendingFileName;
   }else{//file is already in the repository 
	  retmsg = checkXMLMetaData(justname,msgOwner);
   }
	return retmsg;
}
Exemplo n.º 4
0
void main()
{
  //mOut("Testing XmlWriter");
  //mOut("=================");

  //mOut("creating XML writer object:"); 
  XmlWriter wtr;

  //mOut("adding XML declaration:");
  wtr.addDeclaration();
  //mOut(wtr.xml());

  //mOut("adding comment:");
  wtr.addComment("top level comment");
  //mOut(wtr.xml());

  //mOut("adding root element:");
  wtr.start("root");
  //mOut(wtr.xml());

  //mOut("adding attributes:");
  wtr.addAttribute("att1","val1");
  wtr.addAttribute("att2","val2");
  //mOut(wtr.xml());

  //mOut("adding comment:");
  wtr.addComment("comment in root's body");
  //mOut(wtr.xml());

  //mOut("Creating self-closing element:");
  XmlWriter sub1;
  sub1.start("child1 /");
 // mOut(sub1.xml());

  //mOut("adding attribute:");
  sub1.addAttribute("c1name","c1value");
  
  XmlWriter subsub1;
  subsub1.start("grand");
  subsub1.addBody("test grandchild");
  subsub1.end();
  sub1.addBody(subsub1.xml());


  //mOut(sub1.xml());
  
  //mOut("adding child to root's body:");
  wtr.addBody(sub1.xml());
  //mOut(wtr.xml());

  //mOut("adding another comment");
  wtr.addComment("another root's body comment");
  //mOut(wtr.xml());

  //mOut("adding string to root's body:"); 
  wtr.addBody("root's body");
  //mOut(wtr.xml());

  //mOut("closing root element:\n");
  wtr.end();
  mOut(wtr.xml());

  mOut("\n  writing XML to file \"Test.xml\":");
  std::ofstream out("test.xml");
  if(out.good())
  {
    out << wtr.xml().c_str();
    out.close();
  }
  std::cout << std::endl;

 /* mOut("creating composite element:");
  XmlWriter cwtr, bcwtr1, bcwtr2;
  std::string temp = 
    bcwtr1.element("child1","child1's body")
    .element("child2","child2's body").xml();
  std::cout << "\n  " << bcwtr1.xml();
  bcwtr1.clear();

  std::cout << "\n  " <<
    cwtr.start("parent")
    .addBody(bcwtr1.element("child1","child1's body").xml())
    .addBody(bcwtr2.element("child2","body2").xml())
    .end().xml();
  std::cout << "\n\n";*/
}