KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > xslt > sdk > XsltEngine


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id$
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.engine.xslt.sdk;
23
24 import java.net.URI JavaDoc;
25
26 import javax.jbi.JBIException;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
39
40 public class XsltEngine extends AbstractServiceEngine {
41
42     private Xslt xslt;
43
44     private final static String JavaDoc XSL = "xsl";
45
46     @Override JavaDoc
47     protected boolean onExchange(MessageExchangeWrapper exchange,
48             Extensions extensions) throws HandlingException {
49
50         // Get Operation name
51
String JavaDoc opName = exchange.getOperationName();
52
53         // Get Message Exchange Pattern
54
URI JavaDoc pattern = exchange.getExchangePattern();
55
56         // Get in Message content
57
String JavaDoc stringContent = exchange.getInMessageContent();
58
59         if ("transform".equalsIgnoreCase(opName)
60                 && MEPConstants.IN_OUT_PATTERN.value().equals(pattern)) {
61
62             String JavaDoc suRootPath = ((SimpleServiceUnitManager) getServiceUnitManager())
63                     .getSUInstallRootForActivatedEp(exchange.getEndpoint());
64
65             String JavaDoc 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 JavaDoc xslPath = suRootPath + xslRelativePath;
75
76             String JavaDoc outContent = null;
77             try {
78                 outContent = xslt.transform(stringContent, xslPath);
79             } catch (SAXException JavaDoc e) {
80                 throw new HandlingException("Error transforming message", e);
81             } catch (ParserConfigurationException JavaDoc e) {
82                 throw new HandlingException("Error transforming message", e);
83             } catch (TransformerException JavaDoc 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 JavaDoc
100     public void start() throws JBIException {
101         super.start();
102         xslt = new XsltImpl();
103     }
104
105     @Override JavaDoc
106     public void stop() throws JBIException {
107         super.stop();
108         xslt = null;
109     }
110
111 }
112
Popular Tags