Parser

package de.borissov;


/**
 * @author: Sergej Borissov
 * @title:  Projekt Parser
 * @version:  Prototyp Beta Version: 1.0.0.0
 *
 * 
 */

public class Main extends Application  {

    public static void main(String[] args) {
        launch(args);
    }

    DataSourceConst datenQuelle = new DataSourceConst();
    LineChartFactory lineTest = new LineChartFactory();
    ScatterChartFactory scattChart = new ScatterChartFactory();
    TextFactory textTest = new TextFactory();
    DirectoryScanner verzeichnisDurchsuchen = new DirectoryScanner();
    FastCSVWriter fastCSVWriter = new FastCSVWriter();
    FastCSVReader fastCSVReader = new FastCSVReader();
    FileSize verzeichnisGroeße = new FileSize();
    FileAuflistung fileAuflistung = new FileAuflistung();
    Buttons buttons = new Buttons();
    Calculator calculator = new Calculator();
    CheckFormat checkFormat = new CheckFormat();
    ListFactory listFactory = new ListFactory();
    Reader reader = new Reader();
    @Override
    public void start(Stage primaryStage) throws Exception {

        VBox vBox = new VBox(lineTest.buildLineChart(),
                scattChart.buildScatterChart(),
                textTest.getFastCSVReader(),
                textTest.getfastCSVWriter(),
                textTest.getAllFiles(),
                textTest.getDirectorySize(),
                buttons.getFastCsvReader(),
                buttons.getCSVWriter(),
                buttons.getApacheCSVReader(),
                buttons.getButtonKonvertJSONtoXML(),
                buttons.getExitButtonn());



        vBox.setStyle("-fx-background-color: #9FB6CD;");
        Scene scene = new Scene(vBox,1200,900);

        primaryStage.setScene(scene);


        System.out.println("Verzeichnisgröße: " + calculator.verzGröße + " MB");
        System.out.println(fileAuflistung.getFilesDirectory());
        System.out.println("Directory Scanner hat: " + verzeichnisDurchsuchen.outputFiles());
        listFactory.createList();
        reader.readData();

        primaryStage.show();

    }
    
}


package de.borissov.logicLayer;

import de.borissov.dataLayer.DataSourceConst;

import java.util.regex.Pattern;

/**
 * mit regulären Ausdrücken in die Datei
 * nach Mustern suchen
 */

public class Regex {

    DataSourceConst datenQuelle = new DataSourceConst();

    public void gebeRegex(){

        System.out.println("Pattern : ");
        System.out.println(Pattern.matches("'.*'", "'Am Anfang war das Zeichen'"));
    }

    public void gebeRegexAus(){


        System.out.println(
                datenQuelle.XML_TEXT_LANG.replaceAll("<[^>]+>", " ")
            
        );

    }

}

package de.borissov.logicLayer;

import org.json.JSONObject;
import org.json.XML;

public class JsonToXml {


    public long determinate(){
        long startZeit = System.nanoTime();
        String json_data = "{\"student\":[{\"name\":\"Debu Paul\", \"age\":\"27\"},"
                + "{\"name\":\"Subhomoy Dey\", \"age\":\"28\"}]}";

        JSONObject object = new JSONObject(json_data);

        String xml_data = XML.toString(object);
        System.out.println(xml_data);
        long endZeit = System.nanoTime();
        long laufGesamt = endZeit - startZeit;
        return laufGesamt;
    }

}
public class JsonToCsv {


    public static void main(String[] args) throws IOException {




        JsonNode jsonTree = new ObjectMapper().readTree(new File("src/main/resources/jsonDatas/something.json"));

        CsvSchema.Builder csvSchemaBuilder = CsvSchema.builder();
        JsonNode firstObject = jsonTree.elements().next();
        firstObject.fieldNames().forEachRemaining(fieldName -> {
            csvSchemaBuilder.addColumn(fieldName);
        });
        CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();

        CsvMapper csvMapper = new CsvMapper();
        csvMapper.writerFor(JsonNode.class)
                .with(csvSchema)
                .writeValue(new File("src/main/resources/geschriebeneDaten/geschriebenes.csv"), jsonTree);


    }
}

package de.borissov.logicLayer;


import de.borissov.dataLayer.DataSourceConst;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

public class FileSize {

    DataSourceConst datenQuelle = new DataSourceConst();

    File path = datenQuelle.SCHREIBTISCH_FILE;
    public long getDirectorySize(Path path) {

        long size = 0;

        try (Stream<Path> walk = Files.walk(path)) {

            size = walk

                    .filter(Files::isRegularFile)
                    .mapToLong(p -> getFileSize(p))
                    .sum();
        } catch (IOException e) {
            System.out.printf("IO errors %s", e);
        }
        return size;
    }


        private long getFileSize(Path path){

        try {
            return Files.size(path);
        } catch (IOException e){
            System.out.printf("Failed to get SIze of %s%n%s", path, e);
            return  0L;
        }

    }
}

package de.borissov.logicLayer;

import de.borissov.dataLayer.DataSourceConst;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

public class FileAuflistung {

    DataSourceConst datenQuelle = new DataSourceConst();
    private int anzahlCSVFormate = 0;
    private int anzahljsonFormate = 0;
    private int anzahlXmlFormate = 0;
    private long fileGroeße;

    public void getAllFiles(){

        try {

            File[] files = datenQuelle.SCHREIBTISCH_FILE.listFiles();

            System.out.println("Files are:");
            System.out.println("-----------------");

            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getName());
            }
        }  catch (Exception e) {
            System.err.println(e.getMessage());
        }

    }

    public String getFilesDirectory(){

        String output = "";

        File file = new File("/home/oem/Desktop/ProjektParser/");

        if (file.isDirectory()) {

            File[] files = file.listFiles();
            for (File f : files) {
                try {
                    output += f.getCanonicalPath() + "\n";
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println(output);
        return output;
    }


    public int getAllCSVDatas(){

        File[] files = datenQuelle.SCHREIBTISCH_FILE.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith("csv");
            }

        });
        for(File f : files){

            if(f.getName().endsWith("csv")){
                anzahlCSVFormate++;
            }

        }

        System.out.println("Anzahl CSV Daten: " + anzahlCSVFormate);
        return anzahlCSVFormate;
    }
    public int getJSONDatas(){

        File[] files = datenQuelle.SCHREIBTISCH_FILE.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith("json");
            }

        });
        for(File f : files){

            if(f.getName().endsWith("json")){
                anzahljsonFormate++;
            }

        }

        System.out.println("Anzahl JSON: " + anzahljsonFormate);
        return anzahljsonFormate;
    }

    public int getXMLDatas(){

        File[] files = datenQuelle.SCHREIBTISCH_FILE.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith("xml");
            }

        });
        for(File f : files){

            if(f.getName().endsWith("xml")){
                anzahlXmlFormate++;
            }

        }

        System.out.println("Anzahl XML: " + anzahlXmlFormate);
        return anzahlXmlFormate;
    }

    public double getDataVolume(){

        File[] files = datenQuelle.SCHREIBTISCH_FILE.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith("csv");
            }

        });

        for(File f : files){
            System.out.println(f.getName());
            //Dateigröße
            System.out.println("Dateivolumen CSV: " + f.length());

        }
        return files.length;
    }

    public double getDataVolumeJSON(){

        File[] files = datenQuelle.SCHREIBTISCH_FILE.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith("json");
            }

        });

        for(File f : files){
            System.out.println(f.getName());
            //Dateigröße
            System.out.println("Dateivolumen Json: " + f.length());

        }
        return files.length;
    }

    public double getDataVolumeXML(){

        File[] files = datenQuelle.SCHREIBTISCH_FILE.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith("xml");
            }

        });

        for(File f : files){
            System.out.println(f.getName());
            //Dateigröße
            System.out.println("Dateivolumen XML: " + f.length());

        }
        return files.length;
    }


}


package de.borissov.logicLayer;



@SuppressWarnings("PMD.SystemPrintln")
public class FastCSVWriter {

    public String writeFastCSV() {
        final StringWriter sw = new StringWriter();

        CsvWriter.builder()
                .fieldSeparator(';')
                .quoteCharacter('\'')
                .quoteStrategy(QuoteStrategy.ALWAYS)
                .lineDelimiter(LineDelimiter.LF)
                .build(sw)
                .writeRow("kopfzeile1", "kopfzeile2")
                .writeRow("wert1", "wert2");

        System.out.println("Advanced CSV:");
        System.out.println(sw);
        return String.valueOf(sw);
    }


    public long determinate(){
        long startZeit = System.nanoTime();
        final StringWriter sw = new StringWriter();

        CsvWriter.builder()
                .fieldSeparator(';')
                .quoteCharacter('\'')
                .quoteStrategy(QuoteStrategy.ALWAYS)
                .lineDelimiter(LineDelimiter.LF)
                .build(sw)
                .writeRow("kopfzeile1", "kopfzeile2")
                .writeRow("wert1", "wert2");

        System.out.println("Advanced CSV:");
        System.out.println(sw);
        long endZeit = System.nanoTime();
        long laufGesamt = endZeit - startZeit;
        return laufGesamt;

    }

}

package de.borissov.logicLayer;



public class FastCSVReader {

    DataSourceConst datenQuelle = new DataSourceConst();

    /**
     *
     * @return: gibt die gelesene Datei aus
     */
    public  String readFastCSV() {

        final String data = "name;vorname;ort;plz;stadt";
        final String parsedData = CsvReader.builder()
                .fieldSeparator(';')
                .quoteCharacter('\'')
                .commentStrategy(CommentStrategy.SKIP)
                .commentCharacter('#')
                .skipEmptyRows(true)
                .errorOnDifferentFieldCount(false)
                .build(data)
                .stream()
                .map(csvRow -> csvRow.getFields().toString())
                .collect(Collectors.joining(" || "));

        System.out.println("Geparst wurde folgendes: " + parsedData);
        return parsedData;
    }

    /**
     * Messung der Laufzeit mit System.nano()
     * @return:  gibt den Laufzeitwert aus
     */
    public long determinate(){
        long startZeit = System.nanoTime();
        final File data = new File(String.valueOf(DataSourceConst.CSV_BIG));
        final String parsedData = CsvReader.builder()
                .fieldSeparator(';')
                .quoteCharacter('\'')
                .commentStrategy(CommentStrategy.SKIP)
                .commentCharacter('#')
                .skipEmptyRows(true)
                .errorOnDifferentFieldCount(false)
                .build(String.valueOf(data))
                .stream()
                .map(csvRow -> csvRow.getFields().toString())
                .collect(Collectors.joining(" || "));

        System.out.println("Geparst wurde folgendes: " + parsedData);
        long endZeit = System.nanoTime();
        long laufGesamt = endZeit - startZeit;
        return laufGesamt;
    }

}


package de.borissov.logicLayer;


public class DirectoryScanner {


    /**
     *
     * @return
     */

    public String outputFiles(){
        String output = "";

        File file = new File("/home/oem/h/ProjektParser");

        if(file.isDirectory()){
            File[] files = file.listFiles();
            for(File f : files){
                try{
                    output += f.getCanonicalPath() + "\n";
                } catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
        System.out.println(output);

        return output;
    }

    private static ArrayList<File> getPaths(File file, ArrayList<File> list) {
        if (file == null || list == null || !file.isDirectory())
            return null;
        File[] fileArr = file.listFiles();
        for (File f : fileArr) {
            if (f.isDirectory()) {
                getPaths(f, list);
            }
            list.add(f);
        }
        return list;
    }

}
public class CsvToJson {

    private String item;
    private int quantity;
    private BigDecimal unitPrice;


    public static void main(String[] args) throws IOException {

        CsvSchema orderLineSchema = CsvSchema.emptySchema().withHeader();
        CsvMapper csvMapper = new CsvMapper();
        MappingIterator<CsvToJson> orderLines = csvMapper.readerFor(CsvToJson.class)
                .with(orderLineSchema)
                .readValues(new File("src/main/resources/csvDatas/something.csv"));

        new ObjectMapper()
                .configure(SerializationFeature.INDENT_OUTPUT, true)
                .writeValue(new File("src/main/resources/geschriebeneDaten/vonCSVzuJSon.json"), orderLines.readAll());

    }
}

package de.borissov.logicLayer;


import de.borissov.dataLayer.DataSourceConst;

public class CheckFormat {



    int anzahlCsv = 0;
    int anzahlXml = 0;
    int anzahlJson = 0;

    DataSourceConst datenQuelle = new DataSourceConst();


    public int checkCsv(){
        for(int i = 1; i < datenQuelle.dataWarehoue.length; i++) {

            if (datenQuelle.dataWarehoue[i].contains(";")) {
                anzahlCsv++;
            }

        }
        return anzahlCsv / 2;
    }

    public int checkXML(){

        for(int i = 1; i < datenQuelle.dataWarehoue.length; i++) {

            if (datenQuelle.dataWarehoue[i].contains("<")) {

                anzahlXml++;
            }

        }
        return anzahlXml / 2;

    }

    public int checkJson(){

        for(int i = 1; i < datenQuelle.dataWarehoue.length; i++) {

            if (datenQuelle.dataWarehoue[i].contains("[")) {

                anzahlJson++;
            }

        }
        return anzahlJson / 2;
    }

    public int checkFormatUnkown(){

        // gesamtAnzahlderOrdnerinhalte -  anzahlFormateGesamt
        return 0;
    }


}


package de.borissov.logicLayer;

import de.borissov.dataLayer.DataSourceConst;

public class Calculator {

    FileSize fileSize = new FileSize();
    DataSourceConst dataSource = new DataSourceConst();

    public double  verzGröße = fileSize.getDirectorySize
            (DataSourceConst.SCHREIBTISCHFILE_2.toPath()) / 1000000;

    /**
     *
     * @return
     */
    public String getFileSize(){
        return   "Verzeichnisgröße: "   + verzGröße + " MB";
    }

}


package de.borissov.logicLayer;

import de.borissov.praesentationLayer.TextFactory;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.text.Text;


public class Buttons  {

    FastCSVReader fastCSVReader = new FastCSVReader();
    FastCSVWriter fastCSVWriter = new FastCSVWriter();
    ApacheCsvReader apacheCsvReader = new ApacheCsvReader();
    JsonToXml jsonToXml = new JsonToXml();


    /**
     * mit lambda eine Funktion auslösen!
     * @return
     */
    public Button getFastCsvReader(){
        Button buttonFastCsvReader = new Button("Read FastCSV");
        buttonFastCsvReader.setMinSize(150,50);
        buttonFastCsvReader.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Fast CSV Reader Speed:  " + fastCSVReader.determinate());

            }
        });

        return buttonFastCsvReader;
    }

    /**
     *
     * @return
     */

    public Button getCSVWriter(){
        Button buttonCsvWriter = new Button("Write FastCSV");
        buttonCsvWriter.setMinSize(150,50);
        buttonCsvWriter.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event)
            {

                System.out.println(" CSV Write speed: " + fastCSVWriter.determinate());
            }
        });
        return buttonCsvWriter;
    }

    public Button getApacheCSVReader(){
        Button buttonjsonreader = new Button("Read ApacheCSV");
        buttonjsonreader.setMinSize(150,50);

        buttonjsonreader.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Apache CSV Reader Speed:  " + apacheCsvReader.determinate());
            }
        });

        return buttonjsonreader;
    }
    public Button getButtonKonvertJSONtoXML(){
        Button buttonJsonToXml = new Button("Konvert Json To XML");
        buttonJsonToXml.setMinSize(150,50);

        buttonJsonToXml.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Konvert JSSON to XML Speed:  " + jsonToXml.determinate());

            }
        });

        return buttonJsonToXml;
    }

    public Button getExitButtonn(){
        Button exitButton = new Button("Exit");
        exitButton.setMinSize(150, 50);

        exitButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.exit(1);
            }
        });

        return exitButton;
    }

}


package de.borissov.logicLayer;

import de.borissov.dataLayer.DataSourceConst;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class ApacheCsvReader {

    DataSourceConst datenQuelle = new DataSourceConst();
    
    /**
     * @return
     */

    public long determinate()
    {
        long startZeit = System.nanoTime();
        final File file = new File(String.valueOf(DataSourceConst.CSV_BIG));
        try (final CSVParser p = CSVParser.parse(file, StandardCharsets.UTF_8, CSVFormat.EXCEL)) {
            final List<CSVRecord> resords = p.getRecords();
            for (final CSVRecord csvRecord : resords) {
                for (int i = 0; i < csvRecord.size(); i++) {
                    System.out.print(csvRecord.get(i) + " ");
                }
                System.out.println("");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        long endZeit = System.nanoTime();
        long laufGesamt = endZeit - startZeit;
        return laufGesamt;
    }

}



package de.borissov.dataLayer;



import java.time.LocalDateTime;

public class TemperatureData {

    private int id;
    private LocalDateTime timestamp;
    private double temperature;
    private double humidity;


    public TemperatureData(int id, LocalDateTime timestamp, double temperature, double humidity) {
        this.id = id;
        this.timestamp = timestamp;
        this.temperature = temperature;
        this.humidity = humidity;
    }

    public int getId() {
        return id;
    }

    public LocalDateTime getTimestamp() {
        return timestamp;
    }

    public double getTemperature() {
        return temperature;
    }

    public double getHumidity() {
        return humidity;
    }

    @Override
    public String toString() {
        return "TemperatureData{" +
                "id=" + id +
                ", timestamp=" + timestamp +
                ", temperature=" + temperature +
                ", humidity=" + humidity +
                '}';
    }
}


package de.borissov.dataLayer;


public class Reader {


    public void readData() throws IOException {


        List<String> readAllLines =  Files.readAllLines(Paths.get("/home/oem/h/FormatTests/parser-data.csv"));

        LinkedList<TemperatureData> tdListRead = new LinkedList<>();

        for (String line : readAllLines){
            String[] columns =  line.split(";");

            TemperatureData temperatureData =  new TemperatureData(Integer.parseInt(columns[0]),
                    LocalDateTime.parse(columns[1]),
                    Double.parseDouble(columns[2]),
                    Double.parseDouble(columns[3]));

            tdListRead.add(temperatureData);
        }

        for( TemperatureData td : tdListRead){
            System.out.println(td);
        }

    }
}



package de.borissov.dataLayer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.LinkedList;

public class ListFactory {

    public void createList() throws IOException {

        LinkedList<TemperatureData> temperatureData = new LinkedList<>();
        temperatureData.add(new TemperatureData(1, LocalDateTime.now(), 25.0, 0.5));
        temperatureData.add(new TemperatureData(2, LocalDateTime.now(), 27.0, 0.3));
        temperatureData.add(new TemperatureData(3, LocalDateTime.now(), 29.0, 0.1));
        temperatureData.add(new TemperatureData(4, LocalDateTime.now(), 22.0, 0.3));
        temperatureData.add(new TemperatureData(5, LocalDateTime.now(), 21.0, 0.8));

        LinkedList<String> lines = new LinkedList<>();
        for (TemperatureData td : temperatureData) {
            String line = td.getId() + ";" + td.getTimestamp() + ";" + td.getTemperature() + ";" + td.getHumidity();
            lines.add(line);
        }

        Files.write(Paths.get("/home/oem/h/FormatTests/parser-data.csv"), lines);

    }
}


package de.borissov.dataLayer;

import java.io.File;

public class DataSourceConst {

    public static final String ORDNER_ALLGEMEIN = "/home/oem/Desktop/FormatTests/";
    public static final String BIG_DATEI_CSV = "/home/oem/Desktop/FormatTests/bigDatei.csv";
    public static final String CSV_DATEI_KOMMA = "Name,Vorname,Adresse,Plz,Sadt,Land";
    public static final String CSV_TEXT = "name;vorname;ort;plz;stadt";
    public static final String XML_TEXT = "<Name>Sergej</name><Nachname>Borissov</Nachname>";
    public static final String JSON_TEXT = "[{Name: Walter, Nachname: Michel}]";
    public static final String CSV_MIKRO = "/home/oem/Desktop/FormatTests/mikroDatei.csv";
    public static final String CSV_MIKRO2 = "home/oem/Desktop/FormatTests/mikroDateiName.csv";
    public static final String ZU_SPLITTERNDE_ZEICHEN = "ich;bin;ein;philosoph";
    public static final File SCHREIBTISCH_FILE = new File("/home/oem/Desktop/ProjektParser/");
    public static final File SCHREIBTISCHFILE_2 = new File("/home/oem/Desktop/FormatTests/");
    public static final File CSV_BIG = new File("/home/oem/h/Desktop/FormatTests/bigDatei.csv");

    public static final String[] SPLITTERUNG = CSV_TEXT.split(";");

    public static final String XML_TEXT_LANG = "<Nummer>1331</Nummer>" +
            "<Name>Anselm</Name>" +
            "<Nachname>Kiefer</Nachname>" +
            "<Funktion>Maler</Funktion>" +
            "<Anfang>01.Januar</Anfang>" +
            "<Geburtstag>12.Dezember</Geburtstag>";


    public String[] dataWarehoue = {
            "<Nachname>Kiefer</Nachname>",
            "ich;bin;ein;philosoph" ,
            "[{Name: Walter, Nachname: Michel}",
            "<Name>Sergej</name><Nachname>Borissov</Nachname>",
            "du;ich;wir",
            "<Nachname>Kiefer</Nachname>",
            "ich;bin;ein;philosoph" ,
            "[{Name: Walter, Nachname: Michel}",
            "<Name>Sergej</name><Nachname>Borissov</Nachname>",
            "du;ich;wir",
            "<Nachname>Kiefer</Nachname>",
            "ich;bin;ein;philosoph" ,
            "[{Name: Walter, Nachname: Michel}",
            "<Name>Sergej</name><Nachname>Borissov</Nachname>",
            "du;ich;wir",
            "<Nachname>Kiefer</Nachname>",
            "ich;bin;ein;philosoph" ,
            "[{Name: Walter, Nachname: Michel}",
            "<Name>Sergej</name><Nachname>Borissov</Nachname>",
            "du;ich;wir",
            "[{Name: Walter, Nachname: Michel}"
    };
}