|
| 1 | +package com.cataloguploadrequestv2; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.IOException; |
| 7 | +import java.sql.Timestamp; |
| 8 | +import java.util.Properties; |
| 9 | +import javax.xml.parsers.*; |
| 10 | +import javax.xml.transform.*; |
| 11 | +import javax.xml.transform.dom.DOMSource; |
| 12 | +import javax.xml.transform.stream.StreamResult; |
| 13 | +import org.w3c.dom.DOMImplementation; |
| 14 | +import org.w3c.dom.Document; |
| 15 | +import org.w3c.dom.DocumentType; |
| 16 | +import org.w3c.dom.Element; |
| 17 | + |
| 18 | +public class cXMLBuilder { |
| 19 | + |
| 20 | + public void createcXML() throws ParserConfigurationException, TransformerConfigurationException, TransformerException, FileNotFoundException, IOException { |
| 21 | + |
| 22 | + Properties properties = new Properties(); |
| 23 | + FileInputStream input = new FileInputStream("config.properties"); |
| 24 | + properties.load(input); |
| 25 | + |
| 26 | + String XMLVersion = properties.getProperty("XMLVersion"); |
| 27 | + String XMLLang = properties.getProperty("XMLLang"); |
| 28 | + String operationType = properties.getProperty("operationType"); |
| 29 | + String fromIdentityValue = properties.getProperty("fromIdentityValue"); |
| 30 | + String toIdentityValue = properties.getProperty("toIdentityValue"); |
| 31 | + String domainType = properties.getProperty("domainType"); |
| 32 | + String sharedSecretValue = properties.getProperty("sharedSecretValue"); |
| 33 | + String senderIdentityValue = properties.getProperty("senderIdentityValue"); |
| 34 | + String catalogNameValue = properties.getProperty("catalogNameValue"); |
| 35 | + String catalogDescription = properties.getProperty("catalogDescription"); |
| 36 | + String attachmentURLValue = properties.getProperty("attachmentURLValue"); |
| 37 | + String autoPublishEnabled = properties.getProperty("autoPublishEnabled"); |
| 38 | + String URLPostEnabled = properties.getProperty("URLPostEnabled"); |
| 39 | + String emailAddress = properties.getProperty("emailAddress"); |
| 40 | + String userAgentValue = properties.getProperty("userAgentValue"); |
| 41 | + |
| 42 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 43 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 44 | + Document doc = dBuilder.newDocument(); |
| 45 | + doc.setXmlStandalone(true); |
| 46 | + |
| 47 | + DOMImplementation domImpl = doc.getImplementation(); |
| 48 | + DocumentType docType = domImpl.createDocumentType("doctype", "", "http://xml.cXML.org/schemas/cXML/1.2.033/cXML.dtd"); |
| 49 | + |
| 50 | + // cXML root element |
| 51 | + Element rootElement = doc.createElement("cXML"); |
| 52 | + rootElement.setAttribute("payloadID", createPayload()); |
| 53 | + rootElement.setAttribute("timestamp", createTimeStamp()); |
| 54 | + //rootElement.setAttribute("version", XMLVersion); |
| 55 | + rootElement.setAttribute("version", XMLVersion); |
| 56 | + rootElement.setAttribute("xml:lang", XMLLang); |
| 57 | + doc.appendChild(rootElement); |
| 58 | + |
| 59 | + // Header |
| 60 | + Element header = doc.createElement("Header"); |
| 61 | + rootElement.appendChild(header); |
| 62 | + |
| 63 | + // From Identity |
| 64 | + Element from = doc.createElement("From"); |
| 65 | + header.appendChild(from); |
| 66 | + Element fromIdentity = doc.createElement("Identity"); |
| 67 | + fromIdentity.appendChild(doc.createTextNode(fromIdentityValue)); |
| 68 | + |
| 69 | + Element fromCredential = doc.createElement("Credential"); |
| 70 | + fromCredential.setAttribute("domain", domainType); |
| 71 | + |
| 72 | + fromCredential.appendChild(fromIdentity); |
| 73 | + from.appendChild(fromCredential); |
| 74 | + |
| 75 | + // To Identity |
| 76 | + Element To = doc.createElement("To"); |
| 77 | + header.appendChild(To); |
| 78 | + Element toIdentity = doc.createElement("Identity"); |
| 79 | + toIdentity.appendChild(doc.createTextNode(toIdentityValue)); |
| 80 | + |
| 81 | + Element toCredential = doc.createElement("Credential"); |
| 82 | + toCredential.setAttribute("domain", domainType); |
| 83 | + |
| 84 | + toCredential.appendChild(toIdentity); |
| 85 | + To.appendChild(toCredential); |
| 86 | + |
| 87 | + // Sender |
| 88 | + Element sender = doc.createElement("Sender"); |
| 89 | + header.appendChild(sender); |
| 90 | + Element senderCredential = doc.createElement("Credential"); |
| 91 | + senderCredential.setAttribute("domain", domainType); |
| 92 | + |
| 93 | + Element senderIdentity = doc.createElement("Identity"); |
| 94 | + senderIdentity.appendChild(doc.createTextNode(senderIdentityValue)); |
| 95 | + |
| 96 | + senderCredential.appendChild(senderIdentity); |
| 97 | + |
| 98 | + sender.appendChild(senderCredential); |
| 99 | + |
| 100 | + Element sharedSecret = doc.createElement("SharedSecret"); |
| 101 | + sharedSecret.appendChild(doc.createTextNode(sharedSecretValue)); |
| 102 | + |
| 103 | + senderCredential.appendChild(sharedSecret); |
| 104 | + |
| 105 | + // User Agent |
| 106 | + Element userAgent = doc.createElement("UserAgent"); |
| 107 | + |
| 108 | + sender.appendChild(userAgent); |
| 109 | + userAgent.appendChild(doc.createTextNode(userAgentValue)); |
| 110 | + |
| 111 | + // Request Element |
| 112 | + Element request = doc.createElement("Request"); |
| 113 | + rootElement.appendChild(request); |
| 114 | + |
| 115 | + // CatalogUploadRequest |
| 116 | + Element catalogUploadRequest = doc.createElement("CatalogUploadRequest"); |
| 117 | + catalogUploadRequest.setAttribute("operation", operationType); |
| 118 | + |
| 119 | + request.appendChild(catalogUploadRequest); |
| 120 | + |
| 121 | + // Catalog Name |
| 122 | + Element catalogName = doc.createElement("CatalogName"); |
| 123 | + catalogName.setAttribute("xml:lang", XMLLang); |
| 124 | + catalogName.appendChild(doc.createTextNode(catalogNameValue)); |
| 125 | + |
| 126 | + catalogUploadRequest.appendChild(catalogName); |
| 127 | + |
| 128 | + // Description |
| 129 | + Element description = doc.createElement("Description"); |
| 130 | + description.setAttribute("xml:lang", XMLLang); |
| 131 | + description.appendChild(doc.createTextNode(catalogDescription)); |
| 132 | + |
| 133 | + catalogUploadRequest.appendChild(description); |
| 134 | + |
| 135 | + // Attachment |
| 136 | + Element attachment = doc.createElement("Attachment"); |
| 137 | + |
| 138 | + catalogUploadRequest.appendChild(attachment); |
| 139 | + |
| 140 | + // Attachment URL |
| 141 | + Element attachmentURL = doc.createElement("URL"); |
| 142 | + attachmentURL.appendChild(doc.createTextNode("cid:" + attachmentURLValue)); |
| 143 | + |
| 144 | + attachment.appendChild(attachmentURL); |
| 145 | + |
| 146 | + // Auto Publish |
| 147 | + Element autoPublish = doc.createElement("AutoPublish"); |
| 148 | + autoPublish.setAttribute("enabled", autoPublishEnabled); |
| 149 | + catalogUploadRequest.appendChild(autoPublish); |
| 150 | + |
| 151 | + // Notification |
| 152 | + Element notification = doc.createElement("Notification"); |
| 153 | + catalogUploadRequest.appendChild(notification); |
| 154 | + |
| 155 | + // Email |
| 156 | + Element email = doc.createElement("Email"); |
| 157 | + email.appendChild(doc.createTextNode(emailAddress)); |
| 158 | + notification.appendChild(email); |
| 159 | + |
| 160 | + // Notification URL Post |
| 161 | + Element urlPost = doc.createElement("URLPost"); |
| 162 | + urlPost.setAttribute("enabled", URLPostEnabled); |
| 163 | + |
| 164 | + notification.appendChild(urlPost); |
| 165 | + |
| 166 | + // Output |
| 167 | + TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 168 | + Transformer transformer = transformerFactory.newTransformer(); |
| 169 | + StreamResult consoleResult = new StreamResult(System.out); |
| 170 | + DOMSource source = new DOMSource(doc); |
| 171 | + transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docType.getSystemId()); |
| 172 | + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 173 | + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); |
| 174 | + |
| 175 | + Result output = new StreamResult(new File("files/CatalogUploadRequest.xml")); |
| 176 | + transformer.transform(source, output); |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + public String createPayload() { |
| 181 | + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); |
| 182 | + return String.valueOf(timestamp.getTime()); |
| 183 | + } |
| 184 | + |
| 185 | + public String createTimeStamp() { |
| 186 | + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); |
| 187 | + return timestamp.toString(); |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments