Skip to content

Commit

Permalink
#2 close
Browse files Browse the repository at this point in the history
Class util XMLConverter
  • Loading branch information
floranpagliai committed May 20, 2015
1 parent 18434bc commit 5396d27
Show file tree
Hide file tree
Showing 31 changed files with 146 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
MyPokerLab.iml
com.MyPokerLab.iml
.idea
12 changes: 12 additions & 0 deletions MyPokerLab.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file removed out/production/MyPokerLab/Controler/HandReader.class
Binary file not shown.
Binary file removed out/production/MyPokerLab/Model/Action.class
Binary file not shown.
Binary file removed out/production/MyPokerLab/Model/Hand.class
Binary file not shown.
Binary file removed out/production/MyPokerLab/Model/Player.class
Binary file not shown.
Binary file removed out/production/MyPokerLab/Model/eAction.class
Binary file not shown.
Binary file removed out/production/MyPokerLab/Model/eRoom.class
Binary file not shown.
Binary file removed out/production/MyPokerLab/Model/eRound.class
Binary file not shown.
Binary file removed out/production/MyPokerLab/Model/eStake.class
Binary file not shown.
Binary file removed out/production/MyPokerLab/MyPokerLab.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/MyPokerLab/com/Model/Action.class
Binary file not shown.
Binary file added out/production/MyPokerLab/com/Model/Hand.class
Binary file not shown.
Binary file added out/production/MyPokerLab/com/Model/Player.class
Binary file not shown.
Binary file added out/production/MyPokerLab/com/Model/eAction.class
Binary file not shown.
Binary file added out/production/MyPokerLab/com/Model/eRoom.class
Binary file not shown.
Binary file added out/production/MyPokerLab/com/Model/eRound.class
Binary file not shown.
Binary file added out/production/MyPokerLab/com/Model/eStake.class
Binary file not shown.
Binary file added out/production/MyPokerLab/com/MyPokerLab.class
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package Controler;
package com.Controler;


import Model.*;
import com.Model.*;
import com.Utils.XMLConverter;

import javax.xml.bind.JAXBException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* Controler in MyPokerLab
* com.Controler in com.MyPokerLab
* Made by Floran Pagliai <floran.pagliai@gmail.com>
* Started on 15/05/15 at 23:08
*/
Expand Down Expand Up @@ -40,7 +43,18 @@ public void run() {
reader.close();
}
} catch (Exception e) {
System.err.format("Exception occurred trying to read '%s'.", "test");
e.printStackTrace();
}

try {
XMLConverter converter = new XMLConverter();
// for (int i = 1 ; hands.get(i) != null ; i++)
converter.convertFromObjectToXML(hands.get(3), "/Users/floran/Documents/test.xml");


} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -149,7 +163,7 @@ public Action getAction(String str, eRound round, Hand hand) {
action.setRound(round);
if (str.contains("posts")) {
action.setAction(eAction.posts);
action.setPlayer(new Player(str.substring(0, str.indexOf("posts")-1)));
action.setPlayer(hand.findPlayer(str.substring(0, str.indexOf("posts")-1)));
action.setAmount(this.getAmount(str, "blind"));
} else if (str.contains("Dealt")) {
action.setAction(eAction.dealt);
Expand Down Expand Up @@ -181,7 +195,7 @@ public Action getAction(String str, eRound round, Hand hand) {
} else {
return null;
}
action.update();
// action.update();
return action;
}

Expand Down
9 changes: 8 additions & 1 deletion src/Model/Action.java → src/com/Model/Action.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package Model;
package com.Model;

import javax.xml.bind.annotation.*;
import java.util.Arrays;

@XmlType(propOrder = {"round", "player", "action", "amount", "cards"})
public class Action {
private Player player;
private eAction action;
Expand All @@ -19,6 +21,7 @@ public void setPlayer(Player player) {
this.player = player;
}

@XmlAttribute
public eAction getAction() {
return action;
}
Expand All @@ -27,6 +30,7 @@ public void setAction(eAction action) {
this.action = action;
}

@XmlAttribute
public eRound getRound() {
return round;
}
Expand All @@ -35,6 +39,7 @@ public void setRound(eRound round) {
this.round = round;
}

@XmlAttribute
public Double getAmount() {
return amount;
}
Expand All @@ -43,6 +48,8 @@ public void setAmount(Double amount) {
this.amount = amount;
}

@XmlElementWrapper(name="cards")
@XmlElement(name="card")
public String[] getCards() {
return cards;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Model/Hand.java → src/com/Model/Hand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package Model;
package com.Model;

import javax.xml.bind.annotation.*;
import java.util.*;

@XmlRootElement(name = "hand")
@XmlSeeAlso({Player.class})
@XmlType(propOrder = {"roomName", "roomHandId", "timestamp", "tableName", "stake", "buttonSeatPos", "players", "actions", "board"})
public class Hand {
private eRoom roomName;
private String roomHandId;
Expand Down Expand Up @@ -53,6 +57,7 @@ public void setStake(String stake) {
this.stake = stake;
}

@XmlElementWrapper(name="seats")
public Map<Integer, Player> getPlayers() {
return players;
}
Expand Down Expand Up @@ -81,6 +86,8 @@ public void setButtonSeatPos(Integer buttonSeatPos) {
this.buttonSeatPos = buttonSeatPos;
}

@XmlElementWrapper(name="actions")
@XmlElements({@XmlElement(name="action", type=Action.class)})
public ArrayList<Action> getActions() {
return actions;
}
Expand All @@ -93,6 +100,8 @@ public void addAction(Action action) {
this.actions.add(action);
}

@XmlElementWrapper(name="board")
@XmlElement(name="card")
public String[] getBoard() {
return board;
}
Expand Down
9 changes: 7 additions & 2 deletions src/Model/Player.java → src/com/Model/Player.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package Model;
package com.Model;

import javax.xml.bind.annotation.*;

/**
* Model in MyPokerLab
* com.Model in com.MyPokerLab
* Made by Floran Pagliai <floran.pagliai@gmail.com>
* Started on 15/05/15 at 22:41
*/

@XmlType(propOrder = {"name", "stack"})
public class Player {
private String name;
private double stack;
Expand All @@ -17,6 +20,7 @@ public Player(String name) {
this.name = name;
}

@XmlAttribute
public String getName() {
return name;
}
Expand All @@ -25,6 +29,7 @@ public void setName(String name) {
this.name = name;
}

@XmlAttribute
public double getStack() {
return stack;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Model/eAction.java → src/com/Model/eAction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Model;
package com.Model;

/**
* Model in MyPokerLab
* com.Model in com.MyPokerLab
* Made by Floran Pagliai <floran.pagliai@gmail.com>
* Started on 15/05/15 at 05:34
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Model/eRoom.java → src/com/Model/eRoom.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Model;
package com.Model;

public enum eRoom {
Winamax;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/eRound.java → src/com/Model/eRound.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Model;
package com.Model;

public enum eRound {
antebinds,
Expand Down
4 changes: 2 additions & 2 deletions src/Model/eStake.java → src/com/Model/eStake.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Model;
package com.Model;

/**
* Model in MyPokerLab
* com.Model in com.MyPokerLab
* Made by Floran Pagliai <floran.pagliai@gmail.com>
* Started on 15/05/15 at 04:30
*/
Expand Down
8 changes: 3 additions & 5 deletions src/MyPokerLab.java → src/com/MyPokerLab.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import Controler.HandReader;
package com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import com.Controler.HandReader;

/**
* PACKAGE_NAME in MyPokerLab
* PACKAGE_NAME in com.MyPokerLab
* Made by Floran Pagliai <floran.pagliai@gmail.com>
* Started on 20/05/15 at 00:22
*/
Expand Down
79 changes: 79 additions & 0 deletions src/com/Utils/XMLConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.Utils;

import com.Model.Hand;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* com.Utils in com.MyPokerLab
* Made by Floran Pagliai <floran.pagliai@gmail.com>
* Started on 20/05/15 at 11:36
*/

public class XMLConverter {
private Marshaller marshaller;
private Unmarshaller unmarshaller;

public XMLConverter() throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Hand.class);
marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
unmarshaller = jaxbContext.createUnmarshaller();
}

public Marshaller getMarshaller() {
return marshaller;
}

public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}

public Unmarshaller getUnmarshaller() {
return unmarshaller;
}

public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}

public void convertFromObjectToXML(Object object, String filepath)
throws IOException {

FileOutputStream os = null;
try {
os = new FileOutputStream(filepath);
getMarshaller().marshal(object, new StreamResult(os));
} catch (JAXBException e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
}
}

public Object convertFromXMLToObject(String xmlfile) throws IOException {

FileInputStream is = null;
try {
is = new FileInputStream(xmlfile);
return getUnmarshaller().unmarshal(new StreamSource(is));
} catch (JAXBException e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
}
return null;
}
}

0 comments on commit 5396d27

Please sign in to comment.