From 5eaa8910b4d4c9300e7c8b285d995eb9839c9ec6 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Mon, 2 Mar 2020 20:36:00 +0100 Subject: [PATCH] To previous commit --- src/main/java/writer2latex/rdf/Metadata.java | 124 +++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/main/java/writer2latex/rdf/Metadata.java diff --git a/src/main/java/writer2latex/rdf/Metadata.java b/src/main/java/writer2latex/rdf/Metadata.java new file mode 100644 index 0000000..80c0792 --- /dev/null +++ b/src/main/java/writer2latex/rdf/Metadata.java @@ -0,0 +1,124 @@ +package writer2latex.rdf; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import com.opencsv.CSVReader; +import com.opencsv.CSVReaderHeaderAware; +import com.opencsv.exceptions.CsvValidationException; + +public class Metadata { + private static final String SUBTITLE = "subtitle"; + private static final String FILENAME = "Filename"; + private static final String SECTION = "Section"; + private HashMap>> sectionsMetadata; + + public Metadata() { + sectionsMetadata = new HashMap>>(); + } + + public void read(String metadataFilePath) { + File file = new File(metadataFilePath); + if (!file.exists()) { + return; + } + readCSVFile(metadataFilePath); + } + + private void readCSVFile(String filePath) { + Map lineMap; + CSVReaderHeaderAware csvReader = null; + int i = 0; + try { + FileReader reader = new FileReader(filePath); + csvReader = new CSVReaderHeaderAware(reader); + while (i >= 0) { + lineMap = csvReader.readMap(); + if (lineMap != null) { + processCSVLine(lineMap); + } else { + break; + } + i++; + } + + } catch (CsvValidationException e) { + System.out.println("CSV Validation failed. CSV file is broken"); + e.printStackTrace(); + } catch (IOException e) { + System.out.println("I/O Exception happened. Oops."); + System.out.println(i); + e.printStackTrace(); + } finally { + try { + csvReader.close(); + } catch (IOException e) { + System.out.println("I/O Exception happened. Sorry."); + e.printStackTrace(); + } + } + } + + private void processCSVLine(Map lineMap) { + Set keys = lineMap.keySet(); + if (!keys.contains(SECTION)) { + processDocumentMetadata(lineMap); + } else { + String section = lineMap.get(SECTION); + processSectionMetadata(lineMap,section); + } + } + + private void processDocumentMetadata(Map lineMap) { + processSectionMetadata(lineMap, ""); + } + + private void processSectionMetadata(Map lineMap, String sectionNum) { + HashMap> sectionMetadata = getSectionMetadataContainer(sectionNum); + Set names = lineMap.keySet(); + for (String name : names) { + Set values = getValueSet(sectionMetadata,name.trim()); + String curValue = lineMap.get(name); + if (!curValue.trim().isEmpty()) { + values.add(curValue.trim()); + } + } + } + + private Set getValueSet(HashMap> sectionMetadata, String name) { + if (!sectionMetadata.containsKey(name)) { + Set values = new HashSet(); + sectionMetadata.put(name, values); + } + return sectionMetadata.get(name); + } + + private HashMap> getSectionMetadataContainer(String sectionNum) { + if (!sectionsMetadata.containsKey(sectionNum)) { + HashMap> sectionMetadata = new HashMap>(); + sectionsMetadata.putIfAbsent(sectionNum, sectionMetadata); + } + return sectionsMetadata.get(sectionNum); + } + + public void apply(DocumentPart part) { + String sequenceNumber = part.getSequentionalNumber(); + if (sequenceNumber == null) { + return; + } + //root part doesn't have number in csv + if (sequenceNumber.equals("0")) { + sequenceNumber = ""; + } + HashMap> metadata = sectionsMetadata.get(sequenceNumber); + if (metadata != null) { + part.setMetadata(metadata); + } + } + +}