use of org.eclipse.smarthome.automation.parser.ParsingException 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")));
}
}
use of org.eclipse.smarthome.automation.parser.ParsingException 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();
}
use of org.eclipse.smarthome.automation.parser.ParsingException 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();
}
use of org.eclipse.smarthome.automation.parser.ParsingException 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));
}
}
use of org.eclipse.smarthome.automation.parser.ParsingException 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;
}
Aggregations