KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > opc > purchaseorder > CreditCard


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

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 JavaDoc cardNumber;
52   protected String JavaDoc cardExpiryDate;
53   protected String JavaDoc cardType;
54
55   public CreditCard() {}
56
57   public CreditCard(String JavaDoc cardNumber, String JavaDoc cardExpiryDate, String JavaDoc cardType) {
58     this.cardNumber = cardNumber;
59     this.cardExpiryDate = cardExpiryDate;
60     this.cardType = cardType;
61     return;
62   }
63
64   // getter methods
65

66   public String JavaDoc getCardNumber() {
67     return cardNumber;
68   }
69
70   public String JavaDoc getCardExpiryDate() {
71     return cardExpiryDate;
72   }
73
74   public String JavaDoc getCardType() {
75     return cardType;
76   }
77
78   // setter methods
79

80   public void setCardNumber(String JavaDoc cardNumber) {
81     this.cardNumber = cardNumber;
82   }
83
84   public void setCardExpiryDate(String JavaDoc cardExpiryDate) {
85     this.cardExpiryDate = cardExpiryDate;
86   }
87
88   public void setCardType(String JavaDoc cardType) {
89     this.cardType = cardType;
90   }
91
92   //XML serialization methods
93
public String JavaDoc toXML() throws XMLException{
94           
95       String JavaDoc credCard = null;
96       try{
97           
98           //construct the DOM tree
99
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           //process the source tree
116
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 JavaDoc exe){
127           throw new XMLException(exe);
128       }
129       return credCard;
130   }
131
132 }
133
Popular Tags