/////////////////////////////////////////////////////////
//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;
}
예제 #2
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";*/
}