Search in sources :

Example 6 with ParsingException

use of org.eclipse.smarthome.automation.parser.ParsingException in project smarthome by eclipse.

the class CommandlineTemplateProvider method importData.

@Override
protected Set<RuleTemplate> importData(URL url, Parser<RuleTemplate> parser, InputStreamReader inputStreamReader) throws ParsingException {
    Set<RuleTemplate> providedObjects = parser.parse(inputStreamReader);
    if (providedObjects != null && !providedObjects.isEmpty()) {
        List<String> portfolio = new ArrayList<String>();
        synchronized (providerPortfolio) {
            providerPortfolio.put(url, portfolio);
        }
        List<ParsingNestedException> importDataExceptions = new ArrayList<ParsingNestedException>();
        for (RuleTemplate ruleT : providedObjects) {
            List<ParsingNestedException> exceptions = new ArrayList<ParsingNestedException>();
            String uid = ruleT.getUID();
            checkExistence(uid, exceptions);
            if (exceptions.isEmpty()) {
                portfolio.add(uid);
                synchronized (providedObjectsHolder) {
                    notifyListeners(providedObjectsHolder.put(uid, ruleT), ruleT);
                }
            } else {
                importDataExceptions.addAll(exceptions);
            }
        }
        if (!importDataExceptions.isEmpty()) {
            throw new ParsingException(importDataExceptions);
        }
    }
    return providedObjects;
}
Also used : ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ArrayList(java.util.ArrayList) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate)

Example 7 with ParsingException

use of org.eclipse.smarthome.automation.parser.ParsingException in project smarthome by eclipse.

the class AbstractResourceBundleProvider method parseData.

/**
 * This method is called from {@link #processAutomationProvider(Bundle)} to process the loading of the provided
 * objects.
 *
 * @param vendor is a holder of information about the bundle providing data for import.
 * @param parser the {@link Parser} which is responsible for parsing of a particular format in which the provided
 *            objects are presented
 * @param url the resource which is used for loading the objects.
 * @param bundle is the resource holder
 * @return a set of automation objects - the result of loading.
 */
protected Set<E> parseData(Parser<E> parser, URL url, Bundle bundle) {
    InputStreamReader reader = null;
    InputStream is = null;
    try {
        is = url.openStream();
        reader = new InputStreamReader(is, StandardCharsets.UTF_8);
        return parser.parse(reader);
    } catch (ParsingException e) {
        logger.error("{}", e.getLocalizedMessage(), e);
    } catch (IOException e) {
        logger.error("Can't read from resource of bundle with ID {}", bundle.getBundleId(), e);
        processAutomationProviderUninstalled(bundle);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignore) {
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignore) {
            }
        }
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) IOException(java.io.IOException)

Example 8 with ParsingException

use of org.eclipse.smarthome.automation.parser.ParsingException in project smarthome by eclipse.

the class AbstractFileProvider method importFile.

/**
 * This method is responsible for importing a set of Automation objects from a specified URL resource.
 *
 * @param parserType is relevant to the format that you need for conversion of the Automation objects in text.
 * @param url a specified URL for import.
 */
protected void importFile(String parserType, URL url) {
    Parser<E> parser = parsers.get(parserType);
    if (parser != null) {
        InputStream is = null;
        InputStreamReader inputStreamReader = null;
        try {
            is = url.openStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            inputStreamReader = new InputStreamReader(bis);
            Set<E> providedObjects = parser.parse(inputStreamReader);
            updateProvidedObjectsHolder(url, providedObjects);
        } catch (ParsingException e) {
            logger.debug("{}", e.getMessage(), e);
        } catch (IOException e) {
            logger.debug("{}", e.getMessage(), e);
        } finally {
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    } else {
        synchronized (urls) {
            List<URL> value = urls.get(parserType);
            if (value == null) {
                value = new ArrayList<URL>();
                urls.put(parserType, value);
            }
            value.add(url);
        }
        logger.debug("Parser {} not available", parserType, new Exception());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) IOException(java.io.IOException) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

ParsingException (org.eclipse.smarthome.automation.parser.ParsingException)8 ParsingNestedException (org.eclipse.smarthome.automation.parser.ParsingNestedException)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 HashSet (java.util.HashSet)3 ModuleType (org.eclipse.smarthome.automation.type.ModuleType)3 JsonReader (com.google.gson.stream.JsonReader)2 JsonToken (com.google.gson.stream.JsonToken)2 BufferedInputStream (java.io.BufferedInputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RuleTemplate (org.eclipse.smarthome.automation.template.RuleTemplate)2 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Rule (org.eclipse.smarthome.automation.Rule)1 Template (org.eclipse.smarthome.automation.template.Template)1