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;
}
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();
}
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;
}
Aggregations