Skip to content

Commit

Permalink
Merge branch 'master' into logs_contingencies_xml_ca_db
Browse files Browse the repository at this point in the history
  • Loading branch information
geofjamg committed Nov 15, 2016
2 parents ddedda0 + 5cebcf1 commit 7837dd3
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ public class AsciiTableFormatter extends AbstractTableFormatter {

private final Table table ;

public AsciiTableFormatter(String title, Locale locale, Column... columns) {
this(new OutputStreamWriter(System.out), title, locale, columns);
}

public AsciiTableFormatter(Writer writer, String title, Locale locale, Column... columns) {
super(locale, "inv");
public AsciiTableFormatter(Writer writer, String title, TableFormatterConfig config, Column... columns) {
super(config.getLocale(), config.getInvalidString());
this.writer = writer;
this.title = title;
this.table = new Table(columns.length, BorderStyle.CLASSIC_WIDE);
Expand All @@ -39,6 +35,10 @@ public AsciiTableFormatter(Writer writer, String title, Locale locale, Column...
}
}

public AsciiTableFormatter(String title, Column... columns) {
this(new OutputStreamWriter(System.out), title, TableFormatterConfig.load(), columns);
}

@Override
public TableFormatter writeComment(String comment) throws IOException {
// not supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class AsciiTableFormatterFactory implements TableFormatterFactory {

@Override
public TableFormatter create(Writer writer, String title, Locale locale, Column... columns) {
return new AsciiTableFormatter(writer, title, locale, columns);
public TableFormatter create(Writer writer, String title, TableFormatterConfig config, Column... columns) {
return new AsciiTableFormatter(writer, title, config, columns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,24 @@ public class CsvTableFormatter extends AbstractTableFormatter {

protected int column = 0;

public CsvTableFormatter(Writer writer, String title, Column... columns) {
this(writer, title, Locale.getDefault(), columns);
}
protected final boolean writeTitle;

public CsvTableFormatter(Writer writer, String title, Locale locale, Column... columns) {
this(writer, title, ';', "inv", true, locale, columns);
public CsvTableFormatter(Writer writer, String title, TableFormatterConfig config, Column... columns) {
this(writer, title, config.getCsvSeparator(), config.getInvalidString(), config.getPrintHeader(), config.getPrintTitle(), config.getLocale(), columns);
}

public CsvTableFormatter(Writer writer, String title, char separator, String invalidString, boolean writeHeader, Locale locale, Column... columns) {
this(writer, title, separator, invalidString, writeHeader, true, locale, columns);
}

public CsvTableFormatter(Writer writer, String title, char separator, String invalidString, boolean writeHeader, boolean writeTitle, Locale locale, Column... columns) {
super(locale, invalidString);
this.writer = Objects.requireNonNull(writer);
this.title = Objects.requireNonNull(title);
this.separator = separator;
this.columns = Objects.requireNonNull(columns);
headerDone = !writeHeader;
this.writeTitle = writeTitle;
}

private void writeHeaderIfNotDone() throws IOException {
Expand All @@ -55,7 +58,9 @@ private void writeHeaderIfNotDone() throws IOException {
}

protected void writeHeader() throws IOException {
writer.append(title).append(System.lineSeparator());
if (writeTitle) {
writer.append(title).append(System.lineSeparator());
}
for (int i = 0; i < columns.length; i++) {
writer.append(columns[i].getName());
if (i < columns.length-1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class CsvTableFormatterFactory implements TableFormatterFactory {

@Override
public TableFormatter create(Writer writer, String title, Locale locale, Column... columns) {
return new CsvTableFormatter(writer, title, locale, columns);
public TableFormatter create(Writer writer, String title, TableFormatterConfig config, Column... columns) {
return new CsvTableFormatter(writer, title, config, columns);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.commons.io.table;

import eu.itesla_project.commons.config.ModuleConfig;
import eu.itesla_project.commons.config.PlatformConfig;

import java.util.Locale;

/**
* @author c.biasuzzi@techrain.it
*/
public class TableFormatterConfig {

private static final String CONFIG_MODULE_NAME = "table-formatter";
private static final Locale DEFAULT_LOCALE = Locale.getDefault();
private static final String DEFAULT_LANGUAGE = DEFAULT_LOCALE.getLanguage();
private static final char DEFAULT_CSV_SEPARATOR = ';';
private static final String DEFAULT_INVALID_STRING = "inv";
private static final Boolean DEFAULT_PRINT_HEADER = true;
private static final Boolean DEFAULT_PRINT_TITLE = true;

private final Locale locale;
private final char csvSeparator;
private final String invalidString;
private final Boolean printHeader;
private final Boolean printTitle;

public static TableFormatterConfig load() {
String language = DEFAULT_LANGUAGE;
String separator = DEFAULT_CSV_SEPARATOR + "";
String invalidString = DEFAULT_INVALID_STRING;
Boolean printHeader = DEFAULT_PRINT_HEADER;
Boolean printTitle = DEFAULT_PRINT_TITLE;
if (PlatformConfig.defaultConfig().moduleExists(CONFIG_MODULE_NAME)) {
ModuleConfig config = PlatformConfig.defaultConfig().getModuleConfig(CONFIG_MODULE_NAME);
language = config.getStringProperty("language", DEFAULT_LANGUAGE);
separator = config.getStringProperty("separator", DEFAULT_CSV_SEPARATOR + "");
invalidString = config.getStringProperty("invalid-string", DEFAULT_INVALID_STRING);
printHeader = config.getBooleanProperty("print-header", DEFAULT_PRINT_HEADER);
printTitle = config.getBooleanProperty("print-title", DEFAULT_PRINT_TITLE);
}
Locale locale= Locale.forLanguageTag(language);
return new TableFormatterConfig(locale,separator.charAt(0),invalidString,printHeader,printTitle);
}

public TableFormatterConfig(Locale locale, char csvSeparator, String invalidString, Boolean printHeader, Boolean printTitle) {
this.locale = locale;
this.csvSeparator = csvSeparator;
this.invalidString = invalidString;
this.printHeader = printHeader;
this.printTitle = printTitle;
}

public TableFormatterConfig(Locale locale, String invalidString, Boolean printHeader, Boolean printTitle) {
this(locale, DEFAULT_CSV_SEPARATOR, invalidString, printHeader,printTitle);
}

public TableFormatterConfig(Locale locale, String invalidString) {
this(locale, DEFAULT_CSV_SEPARATOR, invalidString, DEFAULT_PRINT_HEADER, DEFAULT_PRINT_TITLE);
}

public TableFormatterConfig() {
this(DEFAULT_LOCALE, DEFAULT_CSV_SEPARATOR, DEFAULT_INVALID_STRING, DEFAULT_PRINT_HEADER, DEFAULT_PRINT_TITLE);
}

public Locale getLocale() {
return locale;
}

public char getCsvSeparator() {
return csvSeparator;
}

public String getInvalidString() {
return invalidString;
}

public Boolean getPrintHeader() {
return printHeader;
}

public Boolean getPrintTitle() {
return printTitle;
}

@Override
public String toString() {
return getClass().getSimpleName() + " [locale=" + locale +
", csvSeparator=" + csvSeparator +
", invalidString=" + invalidString +
", printHeader=" + printHeader +
", printTitle=" + printTitle +
"]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
*/
public interface TableFormatterFactory {

TableFormatter create(Writer writer, String title, Locale locale, Column... columns);
TableFormatter create(Writer writer, String title, TableFormatterConfig config, Column... columns);

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ private static void write(TableFormatter formatter) throws IOException {
@Test
public void testCsv() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (TableFormatter formatter = new CsvTableFormatter(new OutputStreamWriter(bos), "csv test", Locale.US, COLUMNS)) {
TableFormatterConfig config = new TableFormatterConfig(Locale.US, ';', "inv", true, true);
try (TableFormatter formatter = new CsvTableFormatter(new OutputStreamWriter(bos), "csv test", config, COLUMNS)) {
write(formatter);
}
assertEquals(new String(bos.toByteArray(), StandardCharsets.UTF_8),
Expand All @@ -49,7 +50,8 @@ public void testCsv() throws IOException {
@Test
public void testAcsii() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (TableFormatter formatter = new AsciiTableFormatter(new OutputStreamWriter(bos), "ascii test", Locale.US, COLUMNS)) {
TableFormatterConfig config = new TableFormatterConfig(Locale.US, "inv");
try (TableFormatter formatter = new AsciiTableFormatter(new OutputStreamWriter(bos), "ascii test", config, COLUMNS)) {
write(formatter);
}
assertEquals(new String(bos.toByteArray(), StandardCharsets.UTF_8),
Expand Down
1 change: 1 addition & 0 deletions config/logback-cmdline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-->
<!--Configuration of the command line process log-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
Expand Down
4 changes: 4 additions & 0 deletions config/logback-wp5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-->
<!--Configuration of the online process log-->
<configuration>
<!--appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${java.io.tmpdir}/itesla_wp5.log</file>
Expand Down Expand Up @@ -36,6 +37,9 @@
<logger name="eu.itesla_project.helmflow" level="ERROR" />
<logger name="com.gridquant.helm" level="ERROR" />
<logger name="org.apache" level="ERROR" />
<logger name="org.nocrala.tools.texttablefmt" level="ERROR" />
<logger name="eu.itesla_project.iidm.network.impl.CurrentLimitsAdderImpl" level="ERROR" />
<logger name="com.rte_france.itesla.iidm.export.adn.ADNConverter" level="ERROR" />
<root level="DEBUG">
<!--appender-ref ref="FILE" /-->
<appender-ref ref="STDOUT" />
Expand Down
4 changes: 4 additions & 0 deletions config/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-->
<!--Configuration of the offline process log-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
Expand All @@ -25,6 +26,9 @@
<logger name="eu.itesla_project.ucte.network" level="ERROR" />
<logger name="com.rte_france.itesla.hades2.Hades2Executor" level="DEBUG" />
<logger name="eu.itesla_project.iidm.ddb.eurostag_imp_exp" level="ERROR" />
<logger name="org.nocrala.tools.texttablefmt" level="ERROR" />
<logger name="eu.itesla_project.iidm.network.impl.CurrentLimitsAdderImpl" level="ERROR" />
<logger name="com.rte_france.itesla.iidm.export.adn.ADNConverter" level="ERROR" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import eu.itesla_project.commons.io.table.Column;
import eu.itesla_project.commons.io.table.TableFormatter;
import eu.itesla_project.commons.io.table.TableFormatterConfig;
import eu.itesla_project.commons.io.table.TableFormatterFactory;
import eu.itesla_project.iidm.network.*;
import org.nocrala.tools.texttablefmt.BorderStyle;
Expand Down Expand Up @@ -189,7 +190,7 @@ public static void printPreContingencyViolations(SecurityAnalysisResult result,
Objects.requireNonNull(formatterFactory);
try (TableFormatter formatter = formatterFactory.create(writer,
"Pre-contingency violations",
Locale.getDefault(),
TableFormatterConfig.load(),
new Column("Action"),
new Column("Equipment"),
new Column("Violation type"),
Expand Down Expand Up @@ -237,7 +238,7 @@ public static void printPostContingencyViolations(SecurityAnalysisResult result,
if (result.getPostContingencyResults().size() > 0) {
try (TableFormatter formatter = formatterFactory.create(writer,
"Post-contingency limit violations",
Locale.getDefault(),
TableFormatterConfig.load(),
new Column("Contingency"),
new Column("Status"),
new Column("Action"),
Expand Down

0 comments on commit 7837dd3

Please sign in to comment.