1 4 5 package com.sun.j2ee.blueprints.docoriented.client; 6 7 import java.util.*; 8 import java.io.*; 9 import java.text.*; 10 11 import javax.xml.parsers.*; 12 import javax.xml.soap.*; 13 import javax.xml.transform.*; 14 import javax.xml.transform.dom.*; 15 import javax.xml.transform.stream.*; 16 17 import org.w3c.dom.*; 18 19 23 public class PurchaseOrder { 24 25 private String poId; 26 private Calendar createDate; 27 private Address shipTo; 28 private Address billTo; 29 private LineItem[] items; 30 31 public PurchaseOrder() {} 32 33 public PurchaseOrder(String poId, Calendar createDate, 34 Address shipTo, Address billTo, LineItem[] items) { 35 36 this.poId = poId; 37 this.shipTo = shipTo; 38 this.createDate = createDate; 39 this.billTo = billTo; 40 this.items = items; 41 } 42 43 public String getPoId() { 45 return poId; 46 } 47 48 public Calendar getCreateDate() { 49 return createDate; 50 } 51 52 public Address getShipTo() { 53 return shipTo; 54 } 55 56 public Address getBillTo() { 57 return billTo; 58 } 59 60 public LineItem[] getItems() { 61 return items; 62 } 63 64 public void setPoId(String poId) { 66 this.poId = poId; 67 } 68 69 public void setCreateDate(Calendar createDate) { 70 this.createDate = createDate; 71 } 72 73 public void setShipTo(Address shipTo) { 74 this.shipTo = shipTo; 75 } 76 77 public void setBillTo(Address billTo) { 78 this.billTo = billTo; 79 } 80 81 public void setItems(LineItem[] item) { 82 this.items = item; 83 } 84 85 87 public String toXMLString() { 88 String po = null; 89 try { 90 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 92 docBuilderFactory.setNamespaceAware(true); 93 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 94 Document doc = docBuilder.newDocument(); 95 Element poElem = doc.createElement("PurchaseOrder"); 96 poElem.setAttribute("xmlns:xsi", 97 "http://www.w3.org/2001/XMLSchema-instance"); 98 poElem.setAttribute("xsi:schemaLocation", 99 "http://java.sun.com/blueprints/ns/po " + 100 "http://java.sun.com/blueprints/schemas/PurchaseOrder.xsd"); 101 poElem.setAttribute("xmlns", "http://java.sun.com/blueprints/ns/po"); 102 doc.appendChild(poElem); 103 Element elem = doc.createElement("poId"); 104 elem.appendChild(doc.createTextNode(poId)); 105 poElem.appendChild(elem); 106 elem = doc.createElement("createDate"); 107 elem.appendChild(doc.createTextNode((new SimpleDateFormat("yyyy-MM-dd")).format(createDate.getTime()))); 108 poElem.appendChild(elem); 109 elem = doc.createElement("shipTo"); 110 poElem.appendChild(shipTo.toDOM(doc, elem)); 111 elem = doc.createElement("billTo"); 112 poElem.appendChild(billTo.toDOM(doc, elem)); 113 for(int i = 0; i < items.length; ++i){ 114 poElem.appendChild(items[i].toDOM(doc)); 115 } 116 117 ByteArrayOutputStream baStream = new ByteArrayOutputStream(); 119 Result res = new StreamResult(baStream); 120 TransformerFactory transFactory = TransformerFactory.newInstance(); 121 Transformer transformer = transFactory.newTransformer(); 122 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 123 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 124 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 125 transformer.transform(new DOMSource(doc), res); 126 po = baStream.toString("UTF-8"); 127 128 } catch(ParserConfigurationException pce) { 129 pce.printStackTrace(System.err); 130 throw new RuntimeException (pce.getMessage(), pce); 131 } catch(UnsupportedEncodingException use) { 132 use.printStackTrace(System.err); 133 throw new RuntimeException (use.getMessage(), use); 134 } catch(TransformerException te) { 135 te.printStackTrace(System.err); 136 throw new RuntimeException (te.getMessage(), te); 137 } 138 139 return po; 140 } 141 142 143 public SOAPElement toXMLSOAPElement(boolean wrapper) { 144 SOAPElement soapElem = null; 145 try { 146 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 148 docBuilderFactory.setNamespaceAware(true); 149 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 150 Document doc = docBuilder.newDocument(); 151 Element poElem = doc.createElement("PurchaseOrder"); 152 if(wrapper){ 153 Element docElem = doc.createElement("BusinessDocumentRequest"); 154 doc.appendChild(docElem); 155 docElem.appendChild(poElem); 156 } else{ 157 doc.appendChild(poElem); 158 } 159 Element elem = doc.createElement("poId"); 160 elem.appendChild(doc.createTextNode(poId)); 161 poElem.appendChild(elem); 162 elem = doc.createElement("createDate"); 163 elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(createDate.getTime()))); 164 poElem.appendChild(elem); 165 elem = doc.createElement("shipTo"); 166 poElem.appendChild(shipTo.toDOM(doc, elem)); 167 elem = doc.createElement("billTo"); 168 poElem.appendChild(billTo.toDOM(doc, elem)); 169 for(int i = 0; i < items.length; ++i){ 170 poElem.appendChild(items[i].toDOM(doc)); 171 } 172 173 SOAPElement parent = SOAPFactory.newInstance().createElement("dummy"); 175 TransformerFactory factory = TransformerFactory.newInstance(); 176 Transformer transformer = factory.newTransformer(); 177 transformer.transform(new DOMSource(doc), new DOMResult(parent)); 178 soapElem = (SOAPElement) parent.getChildElements().next(); 179 } catch(TransformerException te) { 180 te.printStackTrace(System.err); 181 throw new RuntimeException (te.getMessage(), te); 182 } catch(ParserConfigurationException pce) { 183 pce.printStackTrace(System.err); 184 throw new RuntimeException (pce.getMessage(), pce); 185 } catch(SOAPException se) { 186 se.printStackTrace(System.err); 187 throw new RuntimeException (se.getMessage(), se); 188 } 189 return soapElem; 190 } 191 } 192 193 | Popular Tags |