1 22 package org.objectweb.petals.engine.xslt.sdk; 23 24 import java.net.URI ; 25 26 import javax.jbi.JBIException; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.transform.TransformerException ; 29 30 import org.objectweb.petals.component.common.HandlingException; 31 import org.objectweb.petals.component.common.MEPConstants; 32 import org.objectweb.petals.component.common.se.AbstractServiceEngine; 33 import org.objectweb.petals.component.common.su.SimpleServiceUnitManager; 34 import org.objectweb.petals.component.common.util.MessageExchangeWrapper; 35 import org.objectweb.petals.component.common.util.PetalsExtensionsUtil; 36 import org.objectweb.petals.tools.jbicommon.descriptor.Extensions; 37 import org.objectweb.petals.tools.jbicommon.util.StringHelper; 38 import org.xml.sax.SAXException ; 39 40 public class XsltEngine extends AbstractServiceEngine { 41 42 private Xslt xslt; 43 44 private final static String XSL = "xsl"; 45 46 @Override 47 protected boolean onExchange(MessageExchangeWrapper exchange, 48 Extensions extensions) throws HandlingException { 49 50 String opName = exchange.getOperationName(); 52 53 URI pattern = exchange.getExchangePattern(); 55 56 String stringContent = exchange.getInMessageContent(); 58 59 if ("transform".equalsIgnoreCase(opName) 60 && MEPConstants.IN_OUT_PATTERN.value().equals(pattern)) { 61 62 String suRootPath = ((SimpleServiceUnitManager) getServiceUnitManager()) 63 .getSUInstallRootForActivatedEp(exchange.getEndpoint()); 64 65 String xslRelativePath = PetalsExtensionsUtil 66 .extractValueFromKeyValueExtension(extensions, XSL); 67 68 if (StringHelper.isNullOrEmpty(xslRelativePath)) { 69 throw new HandlingException( 70 "You must specify the relative path of " 71 + "your xsl file into your provides nodes"); 72 } 73 74 String xslPath = suRootPath + xslRelativePath; 75 76 String outContent = null; 77 try { 78 outContent = xslt.transform(stringContent, xslPath); 79 } catch (SAXException e) { 80 throw new HandlingException("Error transforming message", e); 81 } catch (ParserConfigurationException e) { 82 throw new HandlingException("Error transforming message", e); 83 } catch (TransformerException e) { 84 throw new HandlingException("Error transforming message", e); 85 } 86 87 exchange.setOutMessageContent(outContent); 88 89 } else { 90 throw new HandlingException("Operation not allowed (" 91 + exchange.getOperation().getLocalPart() 92 + ") with the following pattern (" 93 + exchange.getPattern().toString() + ")"); 94 } 95 96 return true; 97 } 98 99 @Override 100 public void start() throws JBIException { 101 super.start(); 102 xslt = new XsltImpl(); 103 } 104 105 @Override 106 public void stop() throws JBIException { 107 super.stop(); 108 xslt = null; 109 } 110 111 } 112 | Popular Tags |