/* * 2012-3 Red Hat Inc. and/or its affiliates and other contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.overlord.rtgov.quickstarts.demos.orders; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamSource; import org.switchyard.annotations.Transformer; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import java.io.StringReader; /** * This class represents the transformers for the order mgmt app. * */ public class Transformers { // Element names in XML document private static final String ORDER_ID = "orderId"; private static final String ITEM_ID = "itemId"; private static final String QUANTITY = "quantity"; private static final String CUSTOMER = "customer"; private static final String AMOUNT = "amount"; /** * Transform from a DOM element to an Order instance. * <p/> * No need to specify the "to" type because Order is a concrete type. * @param from Order element. * @return Order instance. */ @Transformer(from = "{urn:switchyard-quickstart-demo:orders:1.0}submitOrder") public Order transformOrder(Element from) { Order order = new Order(); order.setOrderId(getElementValue(from, ORDER_ID)); order.setItemId(getElementValue(from, ITEM_ID)); order.setQuantity(Integer.valueOf(getElementValue(from, QUANTITY))); order.setCustomer(getElementValue(from, CUSTOMER)); return order; } /** * Transform from an OrderAck to an Element. * <p/> * No need to specify the "from" type because OrderAck is a concrete type. * @param orderAck OrderAck. * @return Order response element. */ @Transformer(to = "{urn:switchyard-quickstart-demo:orders:1.0}submitOrderResponse") public Element transformOrderAck(OrderAck orderAck) { StringBuffer ackXml = new StringBuffer() .append("<orders:submitOrderResponse xmlns:orders=\"urn:switchyard-quickstart-demo:orders:1.0\">") .append("<orderAck>") .append("<orderId>" + orderAck.getOrderId() + "</orderId>") .append("<accepted>" + orderAck.isAccepted() + "</accepted>") .append("<status>" + orderAck.getStatus() + "</status>") .append("<customer>" + orderAck.getCustomer() + "</customer>") .append("<total>" + orderAck.getTotal() + "</total>") .append("</orderAck>") .append("</orders:submitOrderResponse>"); return toElement(ackXml.toString()); } /** * Transform from a DOM element to a Payment instance. * <p/> * No need to specify the "to" type because Payment is a concrete type. * @param from Payment element. * @return Payment instance. */ @Transformer(from = "{urn:switchyard-quickstart-demo:orders:1.0}makePayment") public Payment transformPayment(Element from) { Payment payment = new Payment(); payment.setCustomer(getElementValue(from, CUSTOMER)); payment.setAmount(Double.valueOf(getElementValue(from, AMOUNT))); return payment; } /** * Transform from a Receipt to an Element. * <p/> * No need to specify the "from" type because OrderAck is a concrete type. * @param receipt The receipt. * @return Order response element. */ @Transformer(to = "{urn:switchyard-quickstart-demo:orders:1.0}makePaymentResponse") public Element transformReceipt(Receipt receipt) { StringBuffer ackXml = new StringBuffer() .append("<orders:makePaymentResponse xmlns:orders=\"urn:switchyard-quickstart-demo:orders:1.0\">") .append("<receipt>") .append("<customer>" + receipt.getCustomer() + "</customer>") .append("<amount>" + receipt.getAmount() + "</amount>") .append("</receipt>") .append("</orders:makePaymentResponse>"); return toElement(ackXml.toString()); } /** * Returns the text value of a child node of parent. */ private String getElementValue(Element parent, String elementName) { String value = null; NodeList nodes = parent.getElementsByTagName(elementName); if (nodes.getLength() > 0) { value = nodes.item(0).getChildNodes().item(0).getNodeValue(); } return value; } private Element toElement(String xml) { DOMResult dom = new DOMResult(); try { TransformerFactory.newInstance().newTransformer().transform( new StreamSource(new StringReader(xml)), dom); } catch (Exception ex) { ex.printStackTrace(); } return ((Document)dom.getNode()).getDocumentElement(); } }