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 Lodging implements Serializable{ 51 52 protected String lodgingId; 53 protected String name; 54 protected float pricePerNight; 55 protected String location; 56 protected Calendar startDate; 57 protected Calendar endDate; 58 protected int noNights; 59 protected int noRooms; 60 61 public Lodging() {} 63 64 public Lodging(String lodgingId, String name, float pricePerNight, 65 String location, Calendar startDate, Calendar endDate, int noNights, 66 int noRooms) { 67 this.lodgingId = lodgingId; 68 this.name = name; 69 this.pricePerNight = pricePerNight; 70 this.location = location; 71 this.startDate = startDate; 72 this.endDate = endDate; 73 this.noNights = noNights; 74 this.noRooms = noRooms; 75 76 } 77 78 public String getLodgingId() { 80 return lodgingId; 81 } 82 83 public String getName() { 84 return name; 85 } 86 87 public float getPricePerNight() { 88 return pricePerNight; 89 } 90 91 public String getLocation() { 92 return location; 93 } 94 95 public Calendar getStartDate() { 96 return startDate; 97 } 98 99 public Calendar getEndDate() { 100 return endDate; 101 } 102 103 public int getNoNights() { 104 return noNights; 105 } 106 107 public int getNoRooms() { 108 return noRooms; 109 } 110 111 public void setLodgingId(String lodgingId) { 113 this.lodgingId = lodgingId; 114 } 115 116 public void setName(String name) { 117 this.name = name; 118 } 119 120 public void setPricePerNight(float pricePerNight) { 121 this.pricePerNight = pricePerNight; 122 } 123 124 public void setLocation(String location) { 125 this.location = location; 126 } 127 128 public void setStartDate(Calendar startDate) { 129 this.startDate = startDate; 130 } 131 132 public void setEndDate(Calendar endDate) { 133 this.endDate = endDate; 134 } 135 136 public void setNoNights(int noNights) { 137 this.noNights = noNights; 138 } 139 140 public void setNoRooms(int noRooms) { 141 this.noRooms = noRooms; 142 } 143 144 public String toXML(String poId) throws XMLException{ 146 147 String lodgingPO = null; 148 try{ 149 150 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 152 docBuilderFactory.setNamespaceAware(true); 153 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 154 Document doc = docBuilder.newDocument(); 155 Element lodgingElem = doc.createElement("Lodging"); 156 doc.appendChild(lodgingElem); 157 Element elem = doc.createElement("OPCPoId"); 158 elem.appendChild(doc.createTextNode(poId)); 159 lodgingElem.appendChild(elem); 160 elem = doc.createElement("LodgingId"); 161 elem.appendChild(doc.createTextNode(lodgingId)); 162 lodgingElem.appendChild(elem); 163 elem = doc.createElement("StartDate"); 164 elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(startDate.getTime()))); 165 lodgingElem.appendChild(elem); 166 elem = doc.createElement("EndDate"); 167 elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(endDate.getTime()))); 168 lodgingElem.appendChild(elem); 169 elem = doc.createElement("Nights"); 170 elem.appendChild(doc.createTextNode(Integer.toString(noNights))); 171 lodgingElem.appendChild(elem); 172 elem = doc.createElement("Rooms"); 173 elem.appendChild(doc.createTextNode(Integer.toString(noRooms))); 174 lodgingElem.appendChild(elem); 175 176 ByteArrayOutputStream baStream = new ByteArrayOutputStream(); 178 Result res = new StreamResult(baStream); 179 TransformerFactory transFactory = TransformerFactory.newInstance(); 180 Transformer transformer = transFactory.newTransformer(); 181 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 182 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 183 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 184 transformer.transform(new DOMSource(doc), res); 185 lodgingPO = baStream.toString("UTF-8"); 186 187 } catch(Exception exe){ 188 throw new XMLException(exe); 189 } 190 return lodgingPO; 191 } 192 193 } 194 | Popular Tags |