-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLStrategy.java
92 lines (71 loc) · 3.58 KB
/
XMLStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class XMLStrategy extends Strategy {
@Override
public List<CreditCard> readCardsFromFile(String filename) throws Exception {
List<CreditCard> cards = new ArrayList<>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(filename));
NodeList cardNodes = document.getElementsByTagName("CARD");
for (int i = 0; i < cardNodes.getLength(); i++) {
Element cardElement = (Element) cardNodes.item(i);
String cardNumber = cardElement.getElementsByTagName("CARD_NUMBER").item(0).getTextContent();
String expirationDate = cardElement.getElementsByTagName("EXPIRATION_DATE").item(0).getTextContent();
String cardHolderName = cardElement.getElementsByTagName("CARD_HOLDER_NAME").item(0).getTextContent();
String errorMessage = determineErrorMessage(cardNumber);
CreditCard card;
if (!errorMessage.isEmpty()) {
card = new InvalidCreditCard(cardNumber, expirationDate, cardHolderName, errorMessage);
} else {
card = CreditCardFactory.getCreditCard(cardNumber, expirationDate, cardHolderName);
}
if (card != null) {
cards.add(card);
}
}
return cards;
}
@Override
public void writeCardsToFile(List<CreditCard> cards, String filename) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element rootElement = document.createElement("CARDS");
document.appendChild(rootElement);
for (CreditCard card : cards) {
Element cardElement = document.createElement("CARD");
Element cardNumberElement = document.createElement("CARD_NUMBER");
cardNumberElement.setTextContent(card.getCardNumber());
cardElement.appendChild(cardNumberElement);
Element cardTypeElement = document.createElement("CARD_TYPE");
if (card instanceof InvalidCreditCard) {
cardTypeElement.setTextContent(((InvalidCreditCard) card).getErrorMessage());
} else {
cardTypeElement.setTextContent(getCardType(card));
}
cardElement.appendChild(cardTypeElement);
rootElement.appendChild(cardElement);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(filename));
transformer.transform(source, result);
}
private String getCardType(CreditCard card) {
return card.getClass().getSimpleName().replace("CC", "");
}
}