Search in sources :

Example 1 with ParsingNestedException

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

the class CommandlineModuleTypeProvider method importModuleTypes.

/**
 * This method is responsible for importing a set of ModuleTypes from a specified file or URL resource.
 *
 * @param parserType is relevant to the format that you need for conversion of the ModuleTypes in text.
 * @param url a specified URL for import.
 * @throws IOException when I/O operation has failed or has been interrupted.
 * @throws ParsingException when parsing of the text fails for some reasons.
 * @see AutomationCommandsPluggable#importModuleTypes(String, URL)
 */
public Set<ModuleType> importModuleTypes(String parserType, URL url) throws IOException, ParsingException {
    Parser<ModuleType> parser = parsers.get(parserType);
    if (parser != null) {
        InputStream is = url.openStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        InputStreamReader inputStreamReader = new InputStreamReader(bis);
        try {
            return importData(url, parser, inputStreamReader);
        } finally {
            inputStreamReader.close();
        }
    } else {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.MODULE_TYPE, null, new Exception("Parser " + parserType + " not available")));
    }
}
Also used : ModuleType(org.eclipse.smarthome.automation.type.ModuleType) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) IOException(java.io.IOException)

Example 2 with ParsingNestedException

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

the class RuleGSONParser method parse.

@Override
public Set<Rule> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {
        Set<Rule> rules = new HashSet<>();
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                rules.addAll(gson.fromJson(jr, new TypeToken<List<Rule>>() {
                }.getType()));
            } else {
                Rule rule = gson.fromJson(jr, Rule.class);
                rules.add(rule);
            }
            return rules;
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.RULE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}
Also used : ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) List(java.util.List) Rule(org.eclipse.smarthome.automation.Rule) IOException(java.io.IOException) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 3 with ParsingNestedException

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

the class TemplateGSONParser method parse.

@Override
public Set<Template> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            Set<Template> templates = new HashSet<>();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                templates.addAll(gson.fromJson(jr, new TypeToken<List<RuleTemplate>>() {
                }.getType()));
            } else {
                Template template = gson.fromJson(jr, RuleTemplate.class);
                templates.add(template);
            }
            return templates;
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}
Also used : ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) List(java.util.List) IOException(java.io.IOException) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) IOException(java.io.IOException) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) Template(org.eclipse.smarthome.automation.template.Template) HashSet(java.util.HashSet)

Example 4 with ParsingNestedException

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

the class ModuleTypeGSONParser method parse.

@Override
public Set<ModuleType> parse(InputStreamReader reader) throws ParsingException {
    try {
        ModuleTypeParsingContainer mtContainer = gson.fromJson(reader, ModuleTypeParsingContainer.class);
        Set<ModuleType> result = new HashSet<ModuleType>();
        addAll(result, mtContainer.triggers);
        addAll(result, mtContainer.conditions);
        addAll(result, mtContainer.actions);
        return result;
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.MODULE_TYPE, null, e));
    }
}
Also used : ModuleType(org.eclipse.smarthome.automation.type.ModuleType) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) HashSet(java.util.HashSet)

Example 5 with ParsingNestedException

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

the class CommandlineModuleTypeProvider method importData.

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

Aggregations

ParsingException (org.eclipse.smarthome.automation.parser.ParsingException)6 ParsingNestedException (org.eclipse.smarthome.automation.parser.ParsingNestedException)6 IOException (java.io.IOException)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 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RuleTemplate (org.eclipse.smarthome.automation.template.RuleTemplate)2 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Rule (org.eclipse.smarthome.automation.Rule)1 Template (org.eclipse.smarthome.automation.template.Template)1