Package org.openhab.core.transform

Examples of org.openhab.core.transform.TransformationException


   * @{inheritDoc
   */
  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 = source;
View Full Code Here


   *
   */
  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");
  }
 

  String result = "not found";
  String basename = FilenameUtils.getBaseName(filename);
  String extension = FilenameUtils.getExtension(filename);
  String locale = Locale.getDefault().getLanguage();
  String basePath = ConfigDispatcher.getConfigFolder() + File.separator + TransformationActivator.TRANSFORM_FOLDER_NAME + File.separator;
  String path = basePath + filename;
  // eg : /home/sysadmin/projects/openhab/distribution/openhabhome/configurations/transform/test.scale
  String alternatePath = basePath + basename + "_" + locale + "." + extension;
  // eg : /home/sysadmin/projects/openhab/distribution/openhabhome/configurations/transform/test-en.scale

  File f = new File(alternatePath);
  if (f.exists()) {
    path = alternatePath;
  }
  logger.debug("Using scale file '{}'",path);

  try{
    double value = Double.parseDouble(source);
    FileInputStream fstream = new FileInputStream(path);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    String strLine;
    while ((strLine = br.readLine()) != null) {
      Matcher matcher = limits_pattern.matcher(strLine);
      if (matcher.matches() && (matcher.groupCount()==5)) {
        double minLimit = Double.parseDouble(matcher.group(2));
        double maxLimit = Double.parseDouble(matcher.group(3));

        // a bit of a trick to include/exclude limits of the segment
        if (matcher.group(1).equals(']'))
          minLimit = minLimit - 0.0000000001;
        if (matcher.group(1).equals('['))
          minLimit = minLimit + 0.0000000001;

        if ((minLimit < value) && (value < maxLimit)) {
          result = matcher.group(5);
          break;
        }

      } else {
        logger.warn("Line '{}' does not match scale pattern in the file '{}'.",strLine,path );
      }

    }

    in.close();
  } catch (NumberFormatException e){
    logger.warn("Scale transform can only work with numeric values, '{}' is not ", source);
  } catch (IOException e) { 
    throw new TransformationException("An error occured while scaling value ", e);
  }

  return result;
}
View Full Code Here

   *            the input to transform
   */
  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");
    }

    logger.debug("about to transform '{}' by the Java Script '{}'", source, filename);

    Reader reader;

    try {
      String path = ConfigDispatcher.getConfigFolder()
        + File.separator + TransformationActivator.TRANSFORM_FOLDER_NAME
        + File.separator + filename;
      reader = new InputStreamReader(new FileInputStream(path));
    } catch (FileNotFoundException e) {
      throw new TransformationException("An error occured while loading script.", e);
    }

    ScriptEngineManager manager = new ScriptEngineManager();
   
    ScriptEngine engine = manager.getEngineByName("javascript");
    engine.put("input", source);

    Object result = null;

    long startTime = System.currentTimeMillis();

    try {
      result = engine.eval(reader);
    } catch (ScriptException e) {
      throw new TransformationException("An error occured while executing script.", e);
    } finally {
      IOUtils.closeQuietly(reader);
    }

    logger.trace("JavaScript execution elapsed {} ms", System.currentTimeMillis() - startTime);
View Full Code Here

   *            the input to transform
   */
  public String transform(String commandLine, String source) throws TransformationException {

    if (commandLine == null || source == null) {
      throw new TransformationException(
          "the given parameters 'commandLine' and 'source' must not be null");
    }

    logger.debug("about to transform '{}' by the commanline '{}'", source, commandLine);

View Full Code Here

   *
   */
  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");
    }
   
    String basename = FilenameUtils.getBaseName(filename);
    String extension = FilenameUtils.getExtension(filename);
    String locale = Locale.getDefault().getLanguage();
    String basePath = ConfigDispatcher.getConfigFolder() + File.separator + TransformationActivator.TRANSFORM_FOLDER_NAME + File.separator;
   
    String path = basePath + filename;
    // eg : /home/sysadmin/projects/openhab/distribution/openhabhome/configurations/transform/test.map
    String alternatePath = basePath + basename + "_" + locale + "." + extension;
    // eg : /home/sysadmin/projects/openhab/distribution/openhabhome/configurations/transform/test-en.map
   
    File f = new File(alternatePath);
    if (f.exists()) {
      path = alternatePath;           
    }
    logger.debug("Transformation file found '{}'",path);
         
    Reader reader = null;
    try {           
      Properties properties = new Properties();
      reader = new FileReader(path);
      properties.load(reader);
      String target = properties.getProperty(source);
      if (target!=null) {
        logger.debug("transformation resulted in '{}'", target);
        return target;
      } else {
        logger.warn("Could not find a mapping for '{}' in the file '{}'.", source, filename);
        return "";
      }
    } catch (IOException e) {
      String message = "opening file '" + filename + "' throws exception";
      logger.error(message, e);
      throw new TransformationException(message, e);
    } finally {
      IOUtils.closeQuietly(reader);
    }
  }
View Full Code Here

   *
   */
  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 = ConfigDispatcher.getConfigFolder() + File.separator + TransformationActivator.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();
View Full Code Here

   * @{inheritDoc
   */
  public String transform(String xpathExpression, String source) throws TransformationException {

    if (xpathExpression == null || source == null) {
      throw new TransformationException("the given parameters 'xpath' and 'source' must not be null");
    }

    logger.debug("about to transform '{}' by the function '{}'", source, xpathExpression);

    StringReader stringReader = null;

    try {
      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
      domFactory.setNamespaceAware(true);
      domFactory.setValidating(false);
      DocumentBuilder builder = domFactory.newDocumentBuilder();

      stringReader = new StringReader(source);
      InputSource inputSource = new InputSource(stringReader);
      inputSource.setEncoding("UTF-8");

      Document doc = builder.parse(inputSource);

      XPath xpath = XPathFactory.newInstance().newXPath();
      XPathExpression expr = xpath.compile(xpathExpression);

      String transformationResult = (String) expr.evaluate(doc, XPathConstants.STRING);

      logger.debug("transformation resulted in '{}'", transformationResult);

      return transformationResult;
    } catch (Exception e) {
      throw new TransformationException("transformation throws exceptions", e);
    } finally {
      if (stringReader != null) {
        stringReader.close();
      }
    }
View Full Code Here

   * @{inheritDoc
   */
  public String transform(String JSonPathExpression, String source) throws TransformationException {

    if (JSonPathExpression == null || source == null) {
      throw new TransformationException("the given parameters 'JSonPath' and 'source' must not be null");
    }

    logger.debug("about to transform '{}' by the function '{}'", source, JSonPathExpression);

    try {
      String transformationResult = JsonPath.read(source, JSonPathExpression);
      logger.debug("transformation resulted in '{}'", transformationResult);
      return transformationResult;
    } catch(InvalidPathException e) {
      throw new TransformationException("An error occured while transforming JSON expression.", e);
    }

  }
View Full Code Here

TOP

Related Classes of org.openhab.core.transform.TransformationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.