org.harctoolbox.harchardware.misc.ArduinoFlasher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of HarcHardware Show documentation
Show all versions of HarcHardware Show documentation
Helper functions for accessing hardware etc.
/*
Copyright (C) 2019 Bengt Martensson.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/.
*/
package org.harctoolbox.harchardware.misc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.harctoolbox.ircore.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
*/
public class ArduinoFlasher {
private static InputStreamReader openUrlOrFile(String urlOrFilename, String charSetName) throws IOException {
try {
return openURL(urlOrFilename, charSetName);
} catch (MalformedURLException ex) {
return openURL(new File(urlOrFilename), charSetName);
}
}
private static InputStreamReader openURL(String urlOrFilename, String charsetName) throws MalformedURLException, IOException {
URL url = new URL(urlOrFilename);
URLConnection urlConnection = url.openConnection();
try (InputStream inputStream = urlConnection.getInputStream()) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charsetName);
return inputStreamReader;
}
}
private static InputStreamReader openURL(File file, String charsetName) throws FileNotFoundException, UnsupportedEncodingException {
return new InputStreamReader(new FileInputStream(file), charsetName);
}
private static Document mkDoc(String urlOrFile) throws IOException, SAXException {
InputStreamReader reader = openUrlOrFile(urlOrFile, "UTF-8");
return XmlUtils.openXmlReader(reader, /* schema */ null, /* name space aware */ false, /* xinclude aware */ true);
}
private Map aliases;
private Map programs;
private final Document document;
public ArduinoFlasher(String urlOrFile) throws IOException, SAXException {
this(mkDoc(urlOrFile));
}
public ArduinoFlasher(Document doc) {
this.document = doc;
extractAliases();
// ignore firmwareGroup for now
extractPrograms();
}
private void extractAliases() {
NodeList nl = document.getElementsByTagName("alias");
aliases = new HashMap<>(nl.getLength());
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
aliases.put(e.getAttribute("board"), e.getAttribute("alias"));
}
}
private void extractPrograms() {
NodeList nl = document.getElementsByTagName("program");
programs = new HashMap<>(nl.getLength());
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
programs.put(e.getAttribute("name"), new Program(e));
}
}
private static class Variant {
private final String name;
private final Map versions;
private static Map extractVersions(NodeList nodeList) {
Map versions = new HashMap<>(nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
Element e = (Element) nodeList.item(i);
Version version = new Version(e);
versions.put(version.getName(), version);
}
return versions;
}
Variant(String name, Map versions) {
this.name = name;
this.versions = versions;
}
private Variant(Element e) {
this(e.getAttribute("name"), extractVersions(e.getElementsByTagName("version")));
}
String getName() {
return name;
}
}
private static class Version {
private String name;
//private String date;
private List boards;
private Map sums;
private String hexUrl;
private static List extractBoards(Element e) {
NodeList nodeList = e.getElementsByTagName("board");
List boards = new ArrayList<>(nodeList.getLength());
return boards;
}
private static Map extractSums(Element e) {
NodeList nodeList = e.getElementsByTagName("sum");
Map sums = new HashMap<>(nodeList.getLength());
return sums;
}
private static String extractHexUrl(Element e) {
return ((Element) e.getElementsByTagName("hex").item(0)).getTextContent();
}
Version(String name, Listboards, Map sums, String hexUrl) {
this.name = name;
this.boards = boards;
this.sums = sums;
this.hexUrl = hexUrl;
}
private Version(Element e) {
this(e.getAttribute("name"), extractBoards(e), extractSums(e), extractHexUrl(e));
}
private String getName() {
return name;
}
}
private static class Program {
private static HashMap extractVersions(NodeList nodeList) {
HashMap versions = new HashMap<>(nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
Element e = (Element) nodeList.item(i);
Variant variant = new Variant(e);
versions.put(variant.getName(), variant);
}
return versions;
}
private final String name;
private final Map variants;
private Program(String name, Map variants) {
this.name = name;
this.variants = variants;
}
private Program(String name, NodeList nodeList) {
this(name, extractVersions(nodeList));
}
Program(Element e) {
this(e.getAttribute("name"), e.getElementsByTagName("variant"));
}
}
}