Search in sources :

Example 6 with TransformationException

use of org.eclipse.smarthome.core.transform.TransformationException in project smarthome by eclipse.

the class Transformation method transform.

/**
 * Applies a transformation of a given type with some function to a value.
 *
 * @param type the transformation type, e.g. REGEX or MAP
 * @param function the function to call, this value depends on the transformation type
 * @param value the value to apply the transformation to
 * @return the transformed value or the original one, if there was no service registered for the
 *         given type or a transformation exception occurred.
 */
public static String transform(String type, String function, String value) {
    String result;
    TransformationService service = TransformationHelper.getTransformationService(TransformationActivator.getContext(), type);
    Logger logger = LoggerFactory.getLogger(Transformation.class);
    if (service != null) {
        try {
            result = service.transform(function, value);
        } catch (TransformationException e) {
            logger.error("Error executing the transformation '{}': {}", type, e.getMessage());
            result = value;
        }
    } else {
        logger.warn("No transformation service '{}' could be found.", type);
        result = value;
    }
    return result;
}
Also used : TransformationException(org.eclipse.smarthome.core.transform.TransformationException) TransformationService(org.eclipse.smarthome.core.transform.TransformationService) Logger(org.slf4j.Logger)

Example 7 with TransformationException

use of org.eclipse.smarthome.core.transform.TransformationException in project smarthome by eclipse.

the class XsltTransformationService method transform.

/**
 * Transforms the input <code>source</code> by XSLT.
 *
 * The method expects the transformation rule to be read from a file which
 * is stored under the 'configurations/transform' folder. To organize the
 * various transformations one should use subfolders.
 *
 * @param filename
 *            the name of the file which contains the XSLT transformation rule.
 *            The name may contain subfoldernames as well
 * @param source
 *            the input to transform
 */
@Override
public String transform(String filename, String source) throws TransformationException {
    if (filename == null || source == null) {
        throw new TransformationException("the given parameters 'filename' and 'source' must not be null");
    }
    Source xsl = null;
    try {
        String path = ConfigConstants.getConfigFolder() + File.separator + TransformationService.TRANSFORM_FOLDER_NAME + File.separator + filename;
        xsl = new StreamSource(new File(path));
    } catch (Exception e) {
        String message = "opening file '" + filename + "' throws exception";
        logger.error("{}", message, e);
        throw new TransformationException(message, e);
    }
    logger.debug("about to transform '{}' by the function '{}'", source, xsl);
    StringReader xml = new StringReader(source);
    StringWriter out = new StringWriter();
    Transformer transformer;
    try {
        transformer = TransformerFactory.newInstance().newTransformer(xsl);
        transformer.transform(new StreamSource(xml), new StreamResult(out));
    } catch (Exception e) {
        logger.error("transformation throws exception", e);
        throw new TransformationException("transformation throws exception", e);
    }
    logger.debug("transformation resulted in '{}'", out.toString());
    return out.toString();
}
Also used : TransformationException(org.eclipse.smarthome.core.transform.TransformationException) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) File(java.io.File) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformationException(org.eclipse.smarthome.core.transform.TransformationException)

Example 8 with TransformationException

use of org.eclipse.smarthome.core.transform.TransformationException in project smarthome by eclipse.

the class RegExTransformationService method transform.

@Override
public String transform(String regExpression, String source) throws TransformationException {
    if (regExpression == null || source == null) {
        throw new TransformationException("the given parameters 'regex' and 'source' must not be null");
    }
    logger.debug("about to transform '{}' by the function '{}'", source, regExpression);
    String result = "";
    Matcher substMatcher = SUBSTR_PATTERN.matcher(regExpression);
    if (substMatcher.matches()) {
        logger.debug("Using substitution form of regex transformation");
        String regex = substMatcher.group(1);
        String substitution = substMatcher.group(2);
        String options = substMatcher.group(3);
        if (options.equals("g")) {
            result = source.trim().replaceAll(regex, substitution);
        } else {
            result = source.trim().replaceFirst(regex, substitution);
        }
        if (result != null) {
            return result;
        }
    }
    Matcher matcher = Pattern.compile("^" + regExpression + "$", Pattern.DOTALL).matcher(source.trim());
    if (!matcher.matches()) {
        logger.debug("the given regex '^{}$' doesn't match the given content '{}' -> couldn't compute transformation", regExpression, source);
        return null;
    }
    matcher.reset();
    while (matcher.find()) {
        if (matcher.groupCount() == 0) {
            logger.info("the given regular expression '^{}$' doesn't contain a group. No content will be extracted and returned!", regExpression);
            continue;
        }
        result = matcher.group(1);
        if (matcher.groupCount() > 1) {
            logger.debug("the given regular expression '^{}$' contains more than one group. Only the first group will be returned!", regExpression);
        }
    }
    return result;
}
Also used : TransformationException(org.eclipse.smarthome.core.transform.TransformationException) Matcher(java.util.regex.Matcher)

Aggregations

TransformationException (org.eclipse.smarthome.core.transform.TransformationException)8 Matcher (java.util.regex.Matcher)3 StringReader (java.io.StringReader)2 TransformationService (org.eclipse.smarthome.core.transform.TransformationService)2 Logger (org.slf4j.Logger)2 InvalidJsonException (com.jayway.jsonpath.InvalidJsonException)1 InvalidPathException (com.jayway.jsonpath.InvalidPathException)1 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 BigDecimal (java.math.BigDecimal)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 ScriptEngine (javax.script.ScriptEngine)1