KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > opc > invoice > 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.opc.invoice;
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
57     // Constructor
58
public Invoice() {}
59
60     public Invoice(String JavaDoc invoiceId, String JavaDoc opcPoId, String JavaDoc supplier,
61        String JavaDoc status) {
62   this.invoiceId = invoiceId;
63   this.opcPoId = opcPoId;
64   this.supplierId = supplier;
65   this.status = status;
66     }
67
68     // getter methods
69
public String JavaDoc getInvoiceId() {
70   return invoiceId;
71     }
72
73     public String JavaDoc getOpcPoId() {
74   return opcPoId;
75     }
76
77     public String JavaDoc getSupplierId() {
78   return supplierId;
79     }
80
81     public String JavaDoc getStatus() {
82   return status;
83     }
84
85     // setter methods
86
public void setInvoiceId(String JavaDoc invoiceId) {
87   this.invoiceId = invoiceId;
88     }
89
90     public void setOpcPoId(String JavaDoc id) {
91   this.opcPoId = id;
92     }
93
94     public void setSupplierId(String JavaDoc id) {
95   this.supplierId = id;
96     }
97
98     public void setStatus(String JavaDoc stat) {
99   this.status = stat;
100     }
101
102     //XML deserialization methods
103

104     public static Invoice fromXML(String JavaDoc invDoc) throws XMLException {
105   Invoice invObj = null;
106   try {
107           
108       InputSource source = new InputSource(new StringReader(invDoc));
109       DocumentBuilderFactory docBuilderFactory =
110     DocumentBuilderFactory.newInstance();
111       docBuilderFactory.setNamespaceAware(true);
112       DocumentBuilder docBuilder =
113     docBuilderFactory.newDocumentBuilder();
114
115       //parse the source doc and extract details
116
Document doc = docBuilder.parse(source);
117       Element elem = (Element)doc.getDocumentElement().getFirstChild();
118       invObj = new Invoice();
119       invObj.setInvoiceId(((Text)(elem.getFirstChild())).getData());
120       elem = getNextSibling(elem);
121       invObj.setOpcPoId(((Text)(elem.getFirstChild())).getData());
122       elem = getNextSibling(elem);
123       invObj.setSupplierId(((Text)(elem.getFirstChild())).getData());
124       elem = getNextSibling(elem);
125       invObj.setStatus(((Text)(elem.getFirstChild())).getData());
126   } catch(Exception JavaDoc exe){
127       throw new XMLException("Invoice XML Exception");
128   }
129   return invObj;
130     }
131
132     public static Element getNextSibling(Element elem) {
133   for(Node sib=elem.getNextSibling(); sib!=null;
134       sib=sib.getNextSibling()){
135       if(sib.getNodeType() == Node.ELEMENT_NODE){
136     return (Element) sib;
137       }
138   }
139   return null;
140     }
141 }
142
Popular Tags