From 00e1144c98ccf42b00bf89ff5a660be2c41990a0 Mon Sep 17 00:00:00 2001 From: salni84 Date: Tue, 20 Oct 2020 17:16:07 +0200 Subject: [PATCH 1/3] cleancode1 --- .../java/services/MeasurementValueReader.java | 143 ++++++------------ 1 file changed, 45 insertions(+), 98 deletions(-) diff --git a/src/main/java/services/MeasurementValueReader.java b/src/main/java/services/MeasurementValueReader.java index cb80a00..4deae80 100644 --- a/src/main/java/services/MeasurementValueReader.java +++ b/src/main/java/services/MeasurementValueReader.java @@ -39,51 +39,49 @@ public class MeasurementValueReader { /** - * Dient als Weiche um die, gemaess Prokoll des Devices, passende Methode aufzurufen. + * Dient als Weiche um die, gemaess Protokoll des Devices, passende Methode aufzurufen. * * @param deviceName * @return - * @throws ReadWriteException */ - public static MeasurementValueXml getActualValue(String deviceName)throws ReadWriteException{ - - String protocol = DeviceMapperJson.getMeasurementValueProtocol(deviceName); - switch (protocol){ - - case "xml-1": - return getActualValueFromXml(deviceName); - case "txt-1": - return getActualValueFromTxt(deviceName); - case "xml-2": - return getActualValueFromXmlSax(deviceName); - default: - throw new ReadWriteException("Not possible to read protocol-type: " + protocol); + public static MeasurementValueXml getActualValue(String deviceName) { + + try { + String protocol = DeviceMapperJson.getMeasurementValueProtocol(deviceName); + assert protocol != null; + switch (protocol){ + + case "xml-1": + return getActualValueFromXml(deviceName); + case "txt-1": + return getActualValueFromTxt(deviceName); + case "xml-2": + return getActualValueFromXmlSax(deviceName); + default: + throw new ReadWriteException("Not possible to read protocol-type: " + protocol); + } + + }catch (ParserConfigurationException | SAXException | IOException e) { + throw new ReadWriteException("Fehler beim Lesen von " /*+ path*/ + "\n" + e.getMessage()); + } catch (ParseException e) {throw new ReadWriteException("Fehler xy"); } + } /** - * Gibt einen Messwert mit Zeitstempel in Form einer Instanz von MeasurementValueXml zurueck, - * wenn das Protokoll xml-1 konfiguriert wurde. - * - * Wirft im Fehlerfall eine ReadWriteException - * + * @param deviceName * @return - * @throws ReadWriteException */ - public static MeasurementValueXml getActualValueFromXml(String deviceName)throws ReadWriteException{ + private static MeasurementValueXml getActualValueFromXml(String deviceName) throws ReadWriteException, ParserConfigurationException, SAXException, IOException{ MeasurementValueXml measurementValue = new MeasurementValueXml(); - // Pfad der Messwerte-Files gemaess deviceName aus der Konfiguration lesen und ein File erstellen String path = DeviceMapperJson.getMeasurementValuePath(deviceName); InputStream xmlFile; - if(path != null){ - xmlFile = MeasurementValueReader.class.getResourceAsStream(path); - }else{ - throw new ReadWriteException("Path for DataSource from " + deviceName + " not found in configuration"); - } + assert path != null; + xmlFile = MeasurementValueReader.class.getResourceAsStream(path); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); - try { + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(xmlFile); doc.getDocumentElement().normalize(); @@ -94,9 +92,7 @@ public static MeasurementValueXml getActualValueFromXml(String deviceName)throws if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; String deviceId = eElement.getElementsByTagName("Id").item(0).getTextContent(); - //Suchbegriff mit dem Wert des aktuellen Elements vergleichen if(deviceId.equals(deviceName)){ - // Messwert erstellen aus Xml-File measurementValue.setId(deviceId); measurementValue.setValue(eElement.getElementsByTagName("Value").item(0).getTextContent()); measurementValue.setTime(new File(path).lastModified()); @@ -104,99 +100,58 @@ public static MeasurementValueXml getActualValueFromXml(String deviceName)throws } } } - } catch (ParserConfigurationException e) { - throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage()); - } catch (SAXException e) { - throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage()); - } catch (IOException e) { - throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage()); - } - if(measurementValue == null){ - throw new ReadWriteException("Der Name " + deviceName + " wurde in " + path +" nicht gefunden"); - } return measurementValue; } /** - * Gibt einen Messwert mit Zeitstempel in Form einer Instanz von MeasurementValueXml zurueck, - * wenn das Protokoll xml-2 konfiguriert wurde. - * - * Wirft im Fehlerfall eine ReadWriteException - * * @param deviceName * @return - * @throws ReadWriteException */ - public static MeasurementValueXml getActualValueFromXmlSax(String deviceName)throws ReadWriteException { - // Pfad der Messwerte-Files gemaess deviceName aus der Konfiguration lesen und ein File erstellen + private static MeasurementValueXml getActualValueFromXmlSax(String deviceName) throws ReadWriteException, ParserConfigurationException, SAXException, IOException { + String path = DeviceMapperJson.getMeasurementValuePath(deviceName); long time = 0; MeasurementValueXml actualValue = null; SaxHandler handler = new SaxHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); ArrayList measurementValues = (ArrayList) handler.getMeasurementValues(); - try { SAXParser saxParser = factory.newSAXParser(); + assert path != null; saxParser.parse( new SequenceInputStream( Collections.enumeration(Arrays.asList( - new InputStream[] { - new ByteArrayInputStream("".getBytes()), - new FileInputStream(path), - new ByteArrayInputStream("".getBytes()), - })) + new ByteArrayInputStream("".getBytes()), + new FileInputStream(path), + new ByteArrayInputStream("".getBytes()))) ), handler); - } catch (ParserConfigurationException e) { - throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage()); - } catch (SAXException e) { - throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage()); - } catch (FileNotFoundException e) { - throw new ReadWriteException("Datei konnte nicht gefunden werden " + path + "\n" + e.getMessage()); - } catch (IOException e) { - throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage()); - } - // Aktuellster Messwert zuweisen for (MeasurementValueXml value : measurementValues){ if(value.getTime() > time){ actualValue = value; time = value.getTime(); } } - if(actualValue == null){ - throw new ReadWriteException("Der Name " + deviceName + " wurde in " + path +" nicht gefunden"); - } - //Bei Abbruch des Bluetooth-Signals der Messuhr wird NaN (Not_A_Number) als Messwert eingelesen - if(actualValue.getValue().equals("NaN")){ - throw new ReadWriteException("Kein gültiger Messwert in " + path +" für " + deviceName + " gefunden"); - } return actualValue; } /** - * Gibt einen Messwert mit Zeitstempel in Form einer Instanz von MeasurementValueXml zurueck, - * wenn das Protokoll txt-1 konfiguriert wurde. - * - * Wirft im Fehlerfall eine ReadWriteException - * * @param deviceName * @return - * @throws ReadWriteException */ - public static MeasurementValueXml getActualValueFromTxt(String deviceName)throws ReadWriteException{ - FileInputStream in = null; - BufferedReader br = null; + private static MeasurementValueXml getActualValueFromTxt(String deviceName) throws ReadWriteException, ParseException, IOException { + + FileInputStream in; + BufferedReader br; String strLine = null; - String line = null; - String lastLine = null; + String line; + String lastLine; MeasurementValueXml measurementValue = new MeasurementValueXml(); - // Pfad der Messwerte-Files gemaess deviceName aus der Konfiguration lesen und ein File erstellen String path = DeviceMapperJson.getMeasurementValuePath(deviceName); - try { + assert path != null; in = new FileInputStream(path); br = new BufferedReader(new InputStreamReader(in)); @@ -209,12 +164,7 @@ public static MeasurementValueXml getActualValueFromTxt(String deviceName)throws System.out.println(lastLine); in.close(); - } catch (FileNotFoundException ex) { - throw new ReadWriteException(ex.getMessage()); - } catch (IOException ex) { - throw new ReadWriteException(ex.getMessage()); - } - + assert lastLine != null; String[] tokens = lastLine.split(";"); measurementValue.setId(tokens[0]); measurementValue.setValue(tokens[1]); @@ -222,16 +172,13 @@ public static MeasurementValueXml getActualValueFromTxt(String deviceName)throws String pattern = "dd.MM.yyyy HH:mm:ss"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); - Date date = null; + Date date; - try { - date = simpleDateFormat.parse(tokens[2]); - } catch (ParseException e) { - e.printStackTrace(); - } + date = simpleDateFormat.parse(tokens[2]); + + assert date != null; measurementValue.setTime(date.getTime()); return measurementValue; } - } From 4b2c60b6bb10642953d241779384c3d18fa40e66 Mon Sep 17 00:00:00 2001 From: salni84 Date: Tue, 20 Oct 2020 17:58:28 +0200 Subject: [PATCH 2/3] cleancode2 --- .../java/services/MeasurementValueReader.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/services/MeasurementValueReader.java b/src/main/java/services/MeasurementValueReader.java index 4deae80..db4044b 100644 --- a/src/main/java/services/MeasurementValueReader.java +++ b/src/main/java/services/MeasurementValueReader.java @@ -48,6 +48,7 @@ public static MeasurementValueXml getActualValue(String deviceName) { try { String protocol = DeviceMapperJson.getMeasurementValueProtocol(deviceName); + assert protocol != null; switch (protocol){ @@ -61,11 +62,13 @@ public static MeasurementValueXml getActualValue(String deviceName) { throw new ReadWriteException("Not possible to read protocol-type: " + protocol); } - }catch (ParserConfigurationException | SAXException | IOException e) { + }catch (ParserConfigurationException | IOException | SAXException e) { throw new ReadWriteException("Fehler beim Lesen von " /*+ path*/ + "\n" + e.getMessage()); - } catch (ParseException e) {throw new ReadWriteException("Fehler xy"); + } catch (ParseException e) { + throw new ReadWriteException("Der Name " + deviceName + " wurde nicht gefunden"); + }catch (NumberFormatException e) { + throw new ReadWriteException("Kein gültiger Messwert für " + deviceName + " gefunden"); } - } /** @@ -76,9 +79,8 @@ public static MeasurementValueXml getActualValue(String deviceName) { private static MeasurementValueXml getActualValueFromXml(String deviceName) throws ReadWriteException, ParserConfigurationException, SAXException, IOException{ MeasurementValueXml measurementValue = new MeasurementValueXml(); String path = DeviceMapperJson.getMeasurementValuePath(deviceName); - InputStream xmlFile; assert path != null; - xmlFile = MeasurementValueReader.class.getResourceAsStream(path); + InputStream xmlFile = MeasurementValueReader.class.getResourceAsStream(path); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); @@ -143,23 +145,20 @@ private static MeasurementValueXml getActualValueFromXmlSax(String deviceName) t private static MeasurementValueXml getActualValueFromTxt(String deviceName) throws ReadWriteException, ParseException, IOException { - FileInputStream in; - BufferedReader br; String strLine = null; String line; - String lastLine; MeasurementValueXml measurementValue = new MeasurementValueXml(); String path = DeviceMapperJson.getMeasurementValuePath(deviceName); assert path != null; - in = new FileInputStream(path); - br = new BufferedReader(new InputStreamReader(in)); + FileInputStream in = new FileInputStream(path); + BufferedReader br = new BufferedReader(new InputStreamReader(in)); while ((line = br.readLine()) != null) { strLine = line; } - lastLine = strLine; + String lastLine = strLine; System.out.println(lastLine); in.close(); @@ -171,10 +170,8 @@ private static MeasurementValueXml getActualValueFromTxt(String deviceName) thro tokens[2].split(" "); String pattern = "dd.MM.yyyy HH:mm:ss"; - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); - Date date; - date = simpleDateFormat.parse(tokens[2]); + Date date = new SimpleDateFormat(pattern).parse(tokens[2]); assert date != null; measurementValue.setTime(date.getTime()); From 018159d4f741a3f40f83af72e5809ecdbcc86938 Mon Sep 17 00:00:00 2001 From: salni84 Date: Tue, 20 Oct 2020 18:00:43 +0200 Subject: [PATCH 3/3] cleancode3 --- .../java/services/MeasurementValueReader.java | 41 +------------------ 1 file changed, 2 insertions(+), 39 deletions(-) diff --git a/src/main/java/services/MeasurementValueReader.java b/src/main/java/services/MeasurementValueReader.java index db4044b..9790fe4 100644 --- a/src/main/java/services/MeasurementValueReader.java +++ b/src/main/java/services/MeasurementValueReader.java @@ -7,20 +7,8 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.SequenceInputStream; +import javax.xml.parsers.*; +import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -29,21 +17,9 @@ import java.util.Date; -/** - * Die Klasse dient als Schnittstelle zur Datenhaltung der Messwerte. - * Je nach konfiguriertem Protokoll wird die entsprechende Methode aufgerufen. - * - * Created by Nett on 04.06.2017. - */ public class MeasurementValueReader { - /** - * Dient als Weiche um die, gemaess Protokoll des Devices, passende Methode aufzurufen. - * - * @param deviceName - * @return - */ public static MeasurementValueXml getActualValue(String deviceName) { try { @@ -71,11 +47,7 @@ public static MeasurementValueXml getActualValue(String deviceName) { } } - /** - * @param deviceName - * @return - */ private static MeasurementValueXml getActualValueFromXml(String deviceName) throws ReadWriteException, ParserConfigurationException, SAXException, IOException{ MeasurementValueXml measurementValue = new MeasurementValueXml(); String path = DeviceMapperJson.getMeasurementValuePath(deviceName); @@ -105,10 +77,6 @@ private static MeasurementValueXml getActualValueFromXml(String deviceName) thro return measurementValue; } - /** - * @param deviceName - * @return - */ private static MeasurementValueXml getActualValueFromXmlSax(String deviceName) throws ReadWriteException, ParserConfigurationException, SAXException, IOException { @@ -138,11 +106,6 @@ private static MeasurementValueXml getActualValueFromXmlSax(String deviceName) t } - /** - * @param deviceName - * @return - */ - private static MeasurementValueXml getActualValueFromTxt(String deviceName) throws ReadWriteException, ParseException, IOException { String strLine = null;