KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > lodgingsupplier > pomessagebean > Invoice


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.lodgingsupplier.pomessagebean;
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 Invoice implements Serializable {
51
52     protected String JavaDoc invoiceId;
53     protected String JavaDoc opcPoId;
54     protected String JavaDoc supplierId;
55     protected String JavaDoc status;
56     protected String JavaDoc hotelId;
57     protected String JavaDoc hotelAddress;
58     protected String JavaDoc cancelPolicy;
59
60     // Constructor
61
public Invoice() {}
62
63     public Invoice(String JavaDoc invoiceId, String JavaDoc opcPoId, String JavaDoc supplier,
64        String JavaDoc status, String JavaDoc hotelId, String JavaDoc hotelAddress,
65        String JavaDoc cancelPolicy) {
66   this.invoiceId = invoiceId;
67   this.opcPoId = opcPoId;
68   this.supplierId = supplier;
69   this.status = status;
70         this.hotelId = hotelId;
71         this.hotelAddress = hotelAddress;
72         this.cancelPolicy = cancelPolicy;
73     }
74
75     // getter methods
76
public String JavaDoc getInvoiceId() {
77   return invoiceId;
78     }
79
80     public String JavaDoc getOpcPoId() {
81   return opcPoId;
82     }
83
84     public String JavaDoc getSupplierId() {
85   return supplierId;
86     }
87
88     public String JavaDoc getStatus() {
89   return status;
90     }
91
92     // setter methods
93
public void setInvoiceId(String JavaDoc invoiceId) {
94   this.invoiceId = invoiceId;
95     }
96
97     public void setOpcPoId(String JavaDoc id) {
98   this.opcPoId = id;
99     }
100
101     public void setSupplierId(String JavaDoc id) {
102   this.supplierId = id;
103     }
104
105     public void setStatus(String JavaDoc stat) {
106   this.status = stat;
107     }
108
109     //XML serialization/deserialization methods
110
public String JavaDoc toXML() throws ParserConfigurationException,
111                                  TransformerConfigurationException,
112                                  TransformerException,
113                                  UnsupportedEncodingException {
114   String JavaDoc inv = null;
115
116   //construct the DOM tree
117
DocumentBuilderFactory docBuilderFactory =
118     DocumentBuilderFactory.newInstance();
119   docBuilderFactory.setNamespaceAware(true);
120   DocumentBuilder docBuilder =
121     docBuilderFactory.newDocumentBuilder();
122   Document doc = docBuilder.newDocument();
123   Element invElem = doc.createElement("Invoice");
124   invElem.setAttribute("xmlns:xsi",
125            "http://www.w3.org/2001/XMLSchema-instance");
126   invElem.setAttribute("xsi:schemaLocation",
127          "http://java.sun.com/blueprints/ns/invoice " +
128          "http://java.sun.com/blueprints/schemas/invoice-lodging.xsd");
129   invElem.setAttribute("xmlns",
130            "http://java.sun.com/blueprints/ns/invoice");
131   doc.appendChild(invElem);
132   Element elem = doc.createElement("ID");
133   elem.appendChild(doc.createTextNode(invoiceId));
134   invElem.appendChild(elem);
135   elem = doc.createElement("OPCPoId");
136   elem.appendChild(doc.createTextNode(opcPoId));
137   invElem.appendChild(elem);
138   elem = doc.createElement("SupplierId");
139   elem.appendChild(doc.createTextNode(supplierId));
140   invElem.appendChild(elem);
141   elem = doc.createElement("status");
142   elem.appendChild(doc.createTextNode(status));
143   invElem.appendChild(elem);
144   elem = doc.createElement("HotelId");
145   elem.appendChild(doc.createTextNode(hotelId));
146   invElem.appendChild(elem);
147   elem = doc.createElement("HotelAddress");
148   elem.appendChild(doc.createTextNode(hotelAddress));
149   invElem.appendChild(elem);
150   elem = doc.createElement("CancelPolicy");
151   elem.appendChild(doc.createTextNode(cancelPolicy));
152   invElem.appendChild(elem);
153   //process the source tree
154
ByteArrayOutputStream baStream = new ByteArrayOutputStream();
155   Result res = new StreamResult(baStream);
156   TransformerFactory transFactory = TransformerFactory.newInstance();
157   Transformer transformer = transFactory.newTransformer();
158   transformer.setOutputProperty(OutputKeys.METHOD, "xml");
159   transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
160   transformer.setOutputProperty(OutputKeys.INDENT, "yes");
161   transformer.transform(new DOMSource(doc), res);
162   inv = baStream.toString("UTF-8");
163   return inv;
164     }
165 }
166
Popular Tags