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 Transportation implements Serializable{ 51 52 protected String transportationId; 53 protected String carrier; 54 protected String origin; 55 protected String destination; 56 protected Calendar departureDate; 57 protected String departureTime; 58 protected float price; 59 protected String travelClass; 60 protected int headCount; 61 62 public Transportation() {} 64 65 public Transportation(String transportationId, String carrier, String origin, 66 String destination, Calendar departureDate, 67 String departureTime, float price, String travelClass, int headCount) { 68 this.transportationId = transportationId; 69 this.carrier = carrier; 70 this.origin = origin; 71 this.destination = destination; 72 this.departureDate = departureDate; 73 this.price = price; 74 this.travelClass = travelClass; 75 this.departureTime = departureTime; 76 this.headCount = headCount; 77 } 78 79 public String getTransportationId() { 81 return transportationId; 82 } 83 84 public String getCarrier() { 85 return carrier; 86 } 87 88 public String getOrigin() { 89 return origin; 90 } 91 92 public String getDestination() { 93 return destination; 94 } 95 96 public Calendar getDepartureDate() { 97 return departureDate; 98 } 99 100 public String getDepartureTime() { 101 return departureTime; 102 } 103 104 public float getPrice() { 105 return price; 106 } 107 108 public String getTravelClass() { 109 return travelClass; 110 } 111 public int getHeadCount() { 112 return headCount; 113 } 114 115 public void setTransportationId(String transportationId) { 117 this.transportationId = transportationId; 118 } 119 120 public void setCarrier(String carrier) { 121 this.carrier = carrier; 122 } 123 124 public void setOrigin(String origin) { 125 this.origin = origin; 126 } 127 128 public void setDestination(String destination) { 129 this.destination = destination; 130 } 131 132 public void setDepartureDate(Calendar departureDate) { 133 this.departureDate = departureDate; 134 } 135 136 public void setDepartureTime(String departureTime) { 137 this.departureTime = departureTime; 138 } 139 140 public void setPrice(float price) { 141 this.price = price; 142 } 143 144 public void setTravelClass(String travelClass) { 145 this.travelClass = travelClass; 146 } 147 148 public void setHeadCount(int headCount) { 149 this.headCount = headCount; 150 } 151 152 public String toXML(String poId) throws XMLException{ 154 155 String transportPO = null; 156 try{ 157 158 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 160 docBuilderFactory.setNamespaceAware(true); 161 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 162 Document doc = docBuilder.newDocument(); 163 Element transportElem = doc.createElement("Transportation"); 164 doc.appendChild(transportElem); 165 Element elem = doc.createElement("OPCPoId"); 166 elem.appendChild(doc.createTextNode(poId)); 167 transportElem.appendChild(elem); 168 elem = doc.createElement("TransportationId"); 169 elem.appendChild(doc.createTextNode(transportationId)); 170 transportElem.appendChild(elem); 171 elem = doc.createElement("DepartureDate"); 172 elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(departureDate.getTime()))); 173 transportElem.appendChild(elem); 174 elem = doc.createElement("HeadCount"); 175 elem.appendChild(doc.createTextNode(Integer.toString(headCount))); 176 transportElem.appendChild(elem); 177 178 ByteArrayOutputStream baStream = new ByteArrayOutputStream(); 180 Result res = new StreamResult(baStream); 181 TransformerFactory transFactory = TransformerFactory.newInstance(); 182 Transformer transformer = transFactory.newTransformer(); 183 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 184 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 185 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 186 transformer.transform(new DOMSource(doc), res); 187 transportPO = baStream.toString("UTF-8"); 188 189 } catch(Exception exe){ 190 throw new XMLException(exe); 191 } 192 return transportPO; 193 } 194 } 195 | Popular Tags |