To previous commit

This commit is contained in:
Georgy Litvinov 2020-03-02 20:36:00 +01:00
parent 4e8245c735
commit 5eaa8910b4

View file

@ -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<String, HashMap<String, Set<String>>> sectionsMetadata;
public Metadata() {
sectionsMetadata = new HashMap<String, HashMap<String,Set<String>>>();
}
public void read(String metadataFilePath) {
File file = new File(metadataFilePath);
if (!file.exists()) {
return;
}
readCSVFile(metadataFilePath);
}
private void readCSVFile(String filePath) {
Map<String, String> 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<String, String> lineMap) {
Set<String> keys = lineMap.keySet();
if (!keys.contains(SECTION)) {
processDocumentMetadata(lineMap);
} else {
String section = lineMap.get(SECTION);
processSectionMetadata(lineMap,section);
}
}
private void processDocumentMetadata(Map<String, String> lineMap) {
processSectionMetadata(lineMap, "");
}
private void processSectionMetadata(Map<String, String> lineMap, String sectionNum) {
HashMap<String, Set<String>> sectionMetadata = getSectionMetadataContainer(sectionNum);
Set<String> names = lineMap.keySet();
for (String name : names) {
Set<String> values = getValueSet(sectionMetadata,name.trim());
String curValue = lineMap.get(name);
if (!curValue.trim().isEmpty()) {
values.add(curValue.trim());
}
}
}
private Set<String> getValueSet(HashMap<String, Set<String>> sectionMetadata, String name) {
if (!sectionMetadata.containsKey(name)) {
Set<String> values = new HashSet<String>();
sectionMetadata.put(name, values);
}
return sectionMetadata.get(name);
}
private HashMap<String, Set<String>> getSectionMetadataContainer(String sectionNum) {
if (!sectionsMetadata.containsKey(sectionNum)) {
HashMap<String,Set<String>> sectionMetadata = new HashMap<String, Set<String>>();
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<String, Set<String>> metadata = sectionsMetadata.get(sequenceNumber);
if (metadata != null) {
part.setMetadata(metadata);
}
}
}