]> git.basschouten.com Git - openhab-addons.git/blob
99c446b544699e6035344070dc6d0e52a12b955c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.transform.xslt.internal;
14
15 import java.io.File;
16 import java.io.StringReader;
17 import java.io.StringWriter;
18
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;
24
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;
33
34 /**
35  * <p>
36  * The implementation of {@link TransformationService} which transforms the input by XSLT.
37  *
38  * @author Thomas.Eichstaedt-Engelen
39  */
40 @NonNullByDefault
41 @Component(property = { "openhab.transform=XSLT" })
42 public class XsltTransformationService implements TransformationService {
43
44     private final Logger logger = LoggerFactory.getLogger(XsltTransformationService.class);
45
46     /**
47      * Transforms the input <code>source</code> by XSLT.
48      *
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.
52      *
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
56      */
57     @Override
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");
61         }
62
63         Source xsl = null;
64
65         try {
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";
71
72             logger.error("{}", message, e);
73             throw new TransformationException(message, e);
74         }
75
76         logger.debug("about to transform '{}' by the function '{}'", source, xsl);
77
78         StringReader xml = new StringReader(source);
79         StringWriter out = new StringWriter();
80
81         Transformer transformer;
82
83         try {
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);
89         }
90
91         logger.debug("transformation resulted in '{}'", out.toString());
92
93         return out.toString();
94     }
95 }