package mop.gamespace.dkp;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**parseobj class
 * @author Administrator
 *
 */
public class ParseRaidTracker {
 
 private String RaidTime;
 
 private ArrayList joinXml;

 private ArrayList leaveXml;

 private ArrayList lootXml;
 
 private String savefilename = "./temp.xml";

 public ParseRaidTracker() {

  RaidTime = new String();
  joinXml = new ArrayList();
  leaveXml = new ArrayList();
  lootXml = new ArrayList();
 }
 
 /**parseObjFromStr
  * @param str
  * @return ArrayList
  */
 public ArrayList parseObjFromStr(String str)
 {
  String checkstr;
  checkstr = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
  if(str.indexOf(checkstr)== -1)
   str = checkstr +str;
  File file = new File(savefilename);
  BufferedWriter bw = null;
  try {
   bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
   bw.write(str);
   bw.flush();
  } catch (FileNotFoundException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  } catch (IOException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  finally
  {
   try {
    if(bw != null)
    {
     bw.close();
    }
   } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   }
   
  }
  return parseObjFromFile(savefilename);
  
 }
 /**parseObjFromFile
  * @param filename
  * @return ArrayList
  */
 public ArrayList parseObjFromFile(String filename) {
  ArrayList returnAL = new ArrayList();
  try {
   SAXReader saxReader = new SAXReader();
   Document document = saxReader.read(new File(filename));
   Element ee = document.getRootElement();

   Iterator t;
   Element element;
   t = ee.elementIterator("key");
   element = (Element) t.next();
   RaidTime = element.getStringValue();
   t = ee.elementIterator("Join");
   element = (Element) t.next();
   joinXml = parsejoinElement(element);
   t= ee.elementIterator("Leave");
   element = (Element) t.next();
   leaveXml = parseleaveElement(element);
   t = ee.elementIterator("Loot");
   element = (Element) t.next();
   lootXml = parselootElement(element);
   
   returnAL.add(RaidTime);
   returnAL.add(joinXml);
   returnAL.add(leaveXml);
   returnAL.add(lootXml);

  } catch (DocumentException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  } finally {
   
  }
  return returnAL;

 }

 /**parsejoinElement
  * @param _element
  * @return ArrayList
  */
 public ArrayList parsejoinElement(Element _element)
 {
  ArrayList eleArr = new ArrayList();
  Element elementKey;
  Element element;
  JoinPlayer obj;
  JoinPlayer tmpobj;
  Iterator tmpIterator;
  boolean flg = true;
  int j = 0;
  for (Iterator i = _element.elementIterator(); i.hasNext();)
  {
   flg = true;
   elementKey = (Element) i.next();
   obj = new JoinPlayer();
   tmpIterator = elementKey.elementIterator("player");
   element = (Element) tmpIterator.next();
   obj.setplayer(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("race");
   element = (Element) tmpIterator.next();
   obj.setrace(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("class");
   element = (Element) tmpIterator.next();
   obj.setclass(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("level");
   element = (Element) tmpIterator.next();
   obj.setlevel(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("time");
   element = (Element) tmpIterator.next();
   obj.settime(element.getStringValue().trim().replace("\’","\\\’"));
   for(j=0;j<eleArr.size();j++)
   {
    tmpobj = new JoinPlayer();
    tmpobj = (JoinPlayer)eleArr.get(j);
    if(tmpobj.getplayer().equals(obj.getplayer()))
    {
     flg = false;
     break;
    }
   }
   if(flg)
    eleArr.add(obj);
  }
  return eleArr;

 }
 /**parseleaveElement
  * @param _element
  * @return ArrayList
  */
 public ArrayList parseleaveElement(Element _element)
 {
  ArrayList eleArr = new ArrayList();
  Element elementKey;
  Element element;
  LeavePlayer obj;
  Iterator tmpIterator;

  for (Iterator i = _element.elementIterator(); i.hasNext();)
  {
   elementKey = (Element) i.next();
   obj = new LeavePlayer();
   tmpIterator = elementKey.elementIterator("player");
   element = (Element) tmpIterator.next();
   obj.setplayer(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("time");
   element = (Element) tmpIterator.next();
   obj.settime(element.getStringValue().trim().replace("\’","\\\’"));

   eleArr.add(obj);
  }
  return eleArr;

 }
 /**parselootElement
  * @param _element
  * @return ArrayList
  */
 public ArrayList parselootElement(Element _element)
 {
  ArrayList eleArr = new ArrayList();
  Element elementKey;
  Element element;
  LootItem obj;
  Iterator tmpIterator;

  for (Iterator i = _element.elementIterator(); i.hasNext();)
  {
   elementKey = (Element) i.next();
   obj = new LootItem();
   tmpIterator = elementKey.elementIterator("ItemName");
   element = (Element) tmpIterator.next();
   obj.setitemname(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("ItemID");
   element = (Element) tmpIterator.next();
   obj.setitemid(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("Color");
   element = (Element) tmpIterator.next();
   obj.setcolor(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("Count");
   element = (Element) tmpIterator.next();
   obj.setcount(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("Player");
   element = (Element) tmpIterator.next();
   obj.setplayer(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("Time");
   element = (Element) tmpIterator.next();
   obj.settime(element.getStringValue().trim().replace("\’","\\\’"));
   
   eleArr.add(obj);
  }
  return eleArr;

 }

}


评论

该日志第一篇评论

发表评论

评论也有版权!