2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.transform.xslt.internal;
16 import java.io.StringReader;
17 import java.io.StringWriter;
19 import javax.xml.transform.Source;
20 import javax.xml.transform.Transformer;
21 import javax.xml.transform.TransformerFactory;
22 import javax.xml.transform.stream.StreamResult;
23 import javax.xml.transform.stream.StreamSource;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.core.OpenHAB;
28 import org.openhab.core.transform.TransformationException;
29 import org.openhab.core.transform.TransformationService;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
36 * The implementation of {@link TransformationService} which transforms the input by XSLT.
38 * @author Thomas.Eichstaedt-Engelen - Initial contribution
41 @Component(property = { "openhab.transform=XSLT" })
42 public class XsltTransformationService implements TransformationService {
44 private final Logger logger = LoggerFactory.getLogger(XsltTransformationService.class);
47 * Transforms the input <code>source</code> by XSLT.
49 * The method expects the transformation rule to be read from a file which
50 * is stored under the 'configurations/transform' folder. To organize the
51 * various transformations one should use subfolders.
53 * @param filename the name of the file which contains the XSLT transformation rule.
54 * The name may contain subfoldernames as well
55 * @param source the input to transform
58 public @Nullable String transform(String filename, String source) throws TransformationException {
59 if (filename == null || source == null) {
60 throw new TransformationException("the given parameters 'filename' and 'source' must not be null");
66 String path = OpenHAB.getConfigFolder() + File.separator + TransformationService.TRANSFORM_FOLDER_NAME
67 + File.separator + filename;
68 xsl = new StreamSource(new File(path));
69 } catch (Exception e) {
70 String message = "opening file '" + filename + "' throws exception";
72 logger.error("{}", message, e);
73 throw new TransformationException(message, e);
76 logger.debug("about to transform '{}' by the function '{}'", source, xsl);
78 StringReader xml = new StringReader(source);
79 StringWriter out = new StringWriter();
81 Transformer transformer;
84 transformer = TransformerFactory.newInstance().newTransformer(xsl);
85 transformer.transform(new StreamSource(xml), new StreamResult(out));
86 } catch (Exception e) {
87 logger.error("transformation throws exception", e);
88 throw new TransformationException("transformation throws exception", e);
91 logger.debug("transformation resulted in '{}'", out.toString());
93 return out.toString();