class CrosswordIO {
public static void writePuzzle(Crossword c, String file) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("CROSSWORD");
doc.appendChild(rootElement);
Element title = doc.createElement("TITLE");
title.appendChild(doc.createTextNode(c.title));
rootElement.appendChild(title);
Element size = doc.createElement("SIZE");
size.appendChild(doc.createTextNode(Integer.toString(c.size)));
rootElement.appendChild(size);
for(Clue aClue : c.acrossClues) {
Element cluesAcross = doc.createElement("CLUESACROSS");
rootElement.appendChild(cluesAcross);
Element clue = doc.createElement("CLUE");
cluesAcross.appendChild(clue);
Element number = doc.createElement("NUMBER");
number.appendChild(doc.createTextNode(Integer.toString(aClue.number)));
clue.appendChild(number);
Element x = doc.createElement("ROW");
x.appendChild(doc.createTextNode(Integer.toString(aClue.x)));
clue.appendChild(x);
Element y = doc.createElement("COLUMN");
y.appendChild(doc.createTextNode(Integer.toString(aClue.y)));
clue.appendChild(y);
Element clueName = doc.createElement("NAME");
clueName.appendChild(doc.createTextNode(aClue.clue));
clue.appendChild(clueName);
Element clueAnswer = doc.createElement("ANSWER");
clueAnswer.appendChild(doc.createTextNode(aClue.answer));
clue.appendChild(clueAnswer);
}
for(Clue dClue : c.downClues) {
Element cluesDown = doc.createElement("CLUESDOWN");
rootElement.appendChild(cluesDown);
Element clue = doc.createElement("CLUE");
cluesDown.appendChild(clue);
Element number = doc.createElement("NUMBER");
number.appendChild(doc.createTextNode(Integer.toString(dClue.number)));
clue.appendChild(number);
Element x = doc.createElement("ROW");
x.appendChild(doc.createTextNode(Integer.toString(dClue.x)));
clue.appendChild(x);
Element y = doc.createElement("COLUMN");
y.appendChild(doc.createTextNode(Integer.toString(dClue.y)));
clue.appendChild(y);
Element clueName = doc.createElement("NAME");
clueName.appendChild(doc.createTextNode(dClue.clue));
clue.appendChild(clueName);
Element clueAnswer = doc.createElement("ANSWER");
clueAnswer.appendChild(doc.createTextNode(dClue.answer));
clue.appendChild(clueAnswer);
}
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
// Output to console for testing
//StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
static String title;
static int size;
static ArrayList<Clue> acrossClues;
static ArrayList<Clue> downClues;
public static Crossword readPuzzle(File file) {
acrossClues = new ArrayList<Clue>();
downClues = new ArrayList<Clue>();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
int clueNumber;
int x;
int y;
String clueName;
String answer;
boolean bTitle = false;
boolean bSize = false;
boolean bClueAcross = false;
boolean bClueDown = false;
boolean bClueNumber = false;
boolean bClueX = false;
boolean bClueY = false;
boolean bClueName = false;
boolean bAnswer = false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("TITLE")) {
bTitle = true;
}
if (qName.equalsIgnoreCase("SIZE")) {
bSize = true;
}
if (qName.equalsIgnoreCase("NUMBER")) {
bClueNumber = true;
}
if (qName.equalsIgnoreCase("ROW")) {
bClueX = true;
}
if (qName.equalsIgnoreCase("COLUMN")) {
bClueY = true;
}
if (qName.equalsIgnoreCase("NAME")) {
bClueName = true;
}
if (qName.equalsIgnoreCase("ANSWER")) {
bAnswer = true;
}
if (qName.equalsIgnoreCase("CLUESACROSS")) {
bClueAcross = true;
}
if (qName.equalsIgnoreCase("CLUESDOWN")) {
bClueDown = true;
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if(qName.equalsIgnoreCase("CLUESACROSS")) {
acrossClues.add(new Clue(clueNumber, x, y, clueName, answer));
}
if(qName.equalsIgnoreCase("CLUESDOWN")) {
downClues.add(new Clue(clueNumber, x, y, clueName, answer));
}
}
public void characters(char ch[], int start, int length) throws SAXException {
if(bTitle) {
title = new String(ch, start, length);
bTitle = false;
}
if(bSize) {
size = Integer.parseInt(new String(ch, start, length));
bSize = false;
}
if(bClueNumber) {
clueNumber = Integer.parseInt(new String(ch, start, length));
bClueNumber = false;
}
if(bClueX) {
x = Integer.parseInt(new String(ch, start, length));
System.out.println("x: " + x);
bClueX = false;
}
if(bClueY) {
y = Integer.parseInt(new String(ch, start, length));
bClueY = false;
}
if(bClueName) {
clueName = new String(ch, start, length);
bClueName = false;
}
if(bAnswer) {
answer = new String(ch, start, length);
bAnswer= false;
}
}
};
saxParser.parse(file, handler);
} catch (Exception e) {
e.printStackTrace();
}
return new Crossword(title, size, acrossClues, downClues);
}
}