1 37 38 package com.sun.j2ee.blueprints.opc.purchaseorder; 39 40 import java.util.*; 41 import java.text.*; 42 import java.io.*; 43 import javax.xml.parsers.*; 44 import org.w3c.dom.*; 45 import org.xml.sax.*; 46 import javax.xml.transform.*; 47 import javax.xml.transform.dom.*; 48 import javax.xml.transform.stream.*; 49 50 public class CreditCard implements Serializable{ 51 protected String cardNumber; 52 protected String cardExpiryDate; 53 protected String cardType; 54 55 public CreditCard() {} 56 57 public CreditCard(String cardNumber, String cardExpiryDate, String cardType) { 58 this.cardNumber = cardNumber; 59 this.cardExpiryDate = cardExpiryDate; 60 this.cardType = cardType; 61 return; 62 } 63 64 66 public String getCardNumber() { 67 return cardNumber; 68 } 69 70 public String getCardExpiryDate() { 71 return cardExpiryDate; 72 } 73 74 public String getCardType() { 75 return cardType; 76 } 77 78 80 public void setCardNumber(String cardNumber) { 81 this.cardNumber = cardNumber; 82 } 83 84 public void setCardExpiryDate(String cardExpiryDate) { 85 this.cardExpiryDate = cardExpiryDate; 86 } 87 88 public void setCardType(String cardType) { 89 this.cardType = cardType; 90 } 91 92 public String toXML() throws XMLException{ 94 95 String credCard = null; 96 try{ 97 98 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 100 docBuilderFactory.setNamespaceAware(true); 101 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 102 Document doc = docBuilder.newDocument(); 103 Element ccElem = doc.createElement("CreditCard"); 104 doc.appendChild(ccElem); 105 Element elem = doc.createElement("CardNumber"); 106 elem.appendChild(doc.createTextNode(cardNumber)); 107 ccElem.appendChild(elem); 108 elem = doc.createElement("CardExpiryDate"); 109 elem.appendChild(doc.createTextNode(cardExpiryDate)); 110 ccElem.appendChild(elem); 111 elem = doc.createElement("CardType"); 112 elem.appendChild(doc.createTextNode(cardType)); 113 ccElem.appendChild(elem); 114 115 ByteArrayOutputStream baStream = new ByteArrayOutputStream(); 117 Result res = new StreamResult(baStream); 118 TransformerFactory transFactory = TransformerFactory.newInstance(); 119 Transformer transformer = transFactory.newTransformer(); 120 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 121 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 122 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 123 transformer.transform(new DOMSource(doc), res); 124 credCard = baStream.toString("UTF-8"); 125 126 } catch(Exception exe){ 127 throw new XMLException(exe); 128 } 129 return credCard; 130 } 131 132 } 133 | Popular Tags |