CLI: getOptionValue always return 'null' - java

public class converter {
public static void main(String [] args) {
Options opt = new Options();
opt.addOption("I", "in", false, "Eingabeformat (2,8,10,16)");
opt.addOption("O", "out", false, "Ausgabeformat (2,8,10,16)");
opt.addOption("V", "value", true, "Zu konvertierende Zahl");
CommandLineParser parser = new DefaultParser();
String value = "0";
String in = "0";
String out = "0";
int inInt = 0;
int outInt = 0;
try {
CommandLine cl = parser.parse(opt, args);
if (cl.hasOption("I")) {
in = cl.getOptionValue("I");
System.out.println(in);
} else if (cl.hasOption("in")) {
in = cl.getOptionValue("in");
inInt = Integer.parseInt(in);
}
if (cl.hasOption("O")) {
out = cl.getOptionValue("O");
outInt = Integer.parseInt(out);
} else if (cl.hasOption("out")) {
out = cl.getOptionValue("out");
outInt = Integer.parseInt(out);
}
if (cl.hasOption("V")) {
value = cl.getOptionValue("V");
} else if (cl.hasOption("value")) {
value = cl.getOptionValue("value");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
Hello, for my classes I have to learn to work with CLI and it looks ok for now. My problem is: the variable 'in' always return null after using cl.getOptionValue("I") on it. Can someone help?

I assume you expect the args I, O and V to have option values, otherwise you would not try to parse them. But you only specify that for the V option by setting the third argument in the call of addOption to true. You should set them all to true if you want to specify option values:
opt.addOption("I", "in", true, "Eingabeformat (2,8,10,16)");
opt.addOption("O", "out", true, "Ausgabeformat (2,8,10,16)");
opt.addOption("V", "value", true, "Zu konvertierende Zahl");
btw you should name your class Converterwith an uppercase 'C', that's Java convention.

public class converter {
public static void main(String [] args) {
Options opt = new Options();
opt.addOption("I", "in", true, "Eingabeformat (2,8,10,16)"); // Bei false gibts spaeter null
opt.addOption("O", "out", true, "Ausgabeformat (2,8,10,16)"); // Bei false gibt spaeter null
opt.addOption("V", "value", true, "Zu konvertierende Zahl");
CommandLineParser parser = new DefaultParser();
String value = "0";
String in, out;
int inInt = 10;
int outInt = 10;
try {
CommandLine cl = parser.parse(opt, args);
if (cl.hasOption("I")) {
in = cl.getOptionValue("I");
inInt = Integer.parseInt(in);
} else if (cl.hasOption("in")) {
in = cl.getOptionValue("in");
inInt = Integer.parseInt(in);
}
if (cl.hasOption("O")) {
out = cl.getOptionValue("O");
outInt = Integer.parseInt(out);
} else if (cl.hasOption("out")) {
out = cl.getOptionValue("out");
outInt = Integer.parseInt(out);
}
if (cl.hasOption("V")) {
value = cl.getOptionValue("V");
System.out.println(convert(value, inInt, outInt));
} else if (cl.hasOption("value")) {
value = cl.getOptionValue("value");
System.out.println(convert(value, inInt, outInt));
} else {
System.out.println("Keinen Wert eingegeben. Erneut Versuchen!");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
public static String convert(String input, int srcRadix, int dstRadix) {
int value = Integer.parseInt(input, srcRadix);
return Integer.toString(value, dstRadix);
}
}

Related

sheet.getDataValidations() returns an empty list when a cell is validated by a sequence on another sheet

I have a workbook with two sheets:
Sheet1
Sheet2
And there is a sequence on Sheet2 at the range of A1 to A5:
aa
bb
cc
dd
ee
And in Sheet1, the cell A1 is validated by the sequence in Sheet2.
Excel screenshot
However, sheet.getDataValidations() returns an empty list for this case.
Did I miss something?
public static void main(String[] args) throws Exception {
String filePath = "/Users/fujiexiang/ExcelWorkbook.xlsx";
Workbook workbook = WorkbookFactory.create(new FileInputStream(filePath));
Sheet sheet = workbook.getSheetAt(0);
System.out.println("" + dataValidations + " " + dataValidations.size());
}
"[] 0" was printed out.
Apache POI bases on Office Open XML published for Excel 2007. And Excel 2007 had not supported data validation list constraint coming directly from another worksheet. There had must be created a named range for the data validation list constraint. Now current Excel versions support data validation list constraint coming directly from another worksheet but of course not backwards compatible. That's why apache poi also cannot read those constraints as it only reads CTDataValidations which are from Office Open XML published for Excel 2007.
In the XML the difference looks like so in /xl/worksheets/sheet1.xml:
Excel 2007:
<dataValidation type="list" allowBlank="1" showInputMessage="1" showErrorMessage="1" sqref="A1">
<formula1>Sheet2_A1_A5</formula1>
</dataValidation>
There "Sheet2_A1_A5" is a named range in the workbook that points to Sheet2!A1:A5.
Excel 365:
<x14:dataValidation type="list" allowBlank="1" showInputMessage="1" showErrorMessage="1">
<x14:formula1>
<xm:f>Sheet2!A1:A5</xm:f>
</x14:formula1>
<xm:sqref>A1</xm:sqref>
</x14:dataValidation>
There "Sheet2!A1:A5" is a direct reference to the other worksheet.
As you see, the new x14:dataValidation is in a separate name space. This is not covered by apache poi until now.
What one could do is using low level XML parsing methods to get the new XSSFX14DataValidations additional to the XSSFDataValidations. The following example shows a working draft for how to do this.
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlCursor;
import javax.xml.namespace.QName;
class ReadExcelDataValidaton {
static java.util.List<XmlObject> getX14DataValidations(XSSFSheet sheet) {
java.util.List<XmlObject> x14DataValidations = new java.util.ArrayList<XmlObject>();
XmlCursor cursor = sheet.getCTWorksheet().newCursor();
cursor.selectPath(
"declare namespace x14='http://schemas.microsoft.com/office/spreadsheetml/2009/9/main' .//x14:dataValidation");
while(cursor.hasNextSelection()) {
cursor.toNextSelection();
XmlObject obj = cursor.getObject();
x14DataValidations.add(obj);
}
return x14DataValidations;
}
static void addXSSFX14DataValidations(XSSFSheet sheet, java.util.List<DataValidation> dataValidations) {
java.util.List<XmlObject> x14DataValidations = getX14DataValidations(sheet);
for (XmlObject x14DataValidation : x14DataValidations) {
XSSFX14DataValidation xssfX14DataValidation = new XSSFX14DataValidation(x14DataValidation);
dataValidations.add(xssfX14DataValidation);
}
}
static java.util.List<DataValidation> getDataValidations(Sheet sheet) {
#SuppressWarnings("unchecked")
java.util.List<DataValidation> dataValidations = (java.util.List<DataValidation>)sheet.getDataValidations();
if (sheet instanceof XSSFSheet) {
addXSSFX14DataValidations((XSSFSheet)sheet, dataValidations);
}
return dataValidations;
}
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("./Excel.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
java.util.List<DataValidation> dataValidations = getDataValidations(sheet);
for (DataValidation dataValidation : dataValidations) {
System.out.println(dataValidation);
System.out.println(dataValidation.getValidationConstraint().getFormula1());
}
workbook.close();
}
}
class XSSFX14DataValidation implements DataValidation {
private DataValidationConstraint validationConstraint;
private int errorStyle;
private boolean emptyCellAllowed;
private boolean suppressDropDownArrow;
private boolean showPromptBox;
private boolean showErrorBox;
private String promptBoxTitle;
private String promptBoxText;
private String errorBoxTitle;
private String errorBoxText;
private CellRangeAddressList regions;
public XSSFX14DataValidation(XmlObject x14DataValidation) {
String type = "";
XmlObject typeAttribute = x14DataValidation.selectAttribute(new QName("type"));
if (typeAttribute != null) type = typeAttribute.newCursor().getTextValue();
Integer validationType = DataValidationConstraint.ValidationType.ANY;
if ("CUSTOM".equalsIgnoreCase(type)) {
validationType = DataValidationConstraint.ValidationType.FORMULA;
} else if ("DATE".equalsIgnoreCase(type)) {
validationType = DataValidationConstraint.ValidationType.DATE;
} else if ("DECIMAL".equalsIgnoreCase(type)) {
validationType = DataValidationConstraint.ValidationType.DECIMAL;
} else if ("LIST".equalsIgnoreCase(type)) {
validationType = DataValidationConstraint.ValidationType.LIST;
} else if ("NONE".equalsIgnoreCase(type)) {
validationType = DataValidationConstraint.ValidationType.ANY;
} else if ("TEXT_LENGTH".equalsIgnoreCase(type)) {
validationType = DataValidationConstraint.ValidationType.TEXT_LENGTH;
} else if ("TIME".equalsIgnoreCase(type)) {
validationType = DataValidationConstraint.ValidationType.TIME;
} else if ("WHOLE".equalsIgnoreCase(type)) {
validationType = DataValidationConstraint.ValidationType.INTEGER;
}
String operator = "";
XmlObject operatorAttribute = x14DataValidation.selectAttribute(new QName("operator"));
if (operatorAttribute != null) operator = operatorAttribute.newCursor().getTextValue();
Integer operatorType = DataValidationConstraint.OperatorType.IGNORED;
if ("BETWEEN".equalsIgnoreCase(operator)) {
operatorType = DataValidationConstraint.OperatorType.BETWEEN;
} else if ("NOT_BETWEEN".equalsIgnoreCase(operator)) {
operatorType = DataValidationConstraint.OperatorType.NOT_BETWEEN;
} else if ("EQUAL".equalsIgnoreCase(operator)) {
operatorType = DataValidationConstraint.OperatorType.EQUAL;
} else if ("NOT_EQUAL".equalsIgnoreCase(operator)) {
operatorType = DataValidationConstraint.OperatorType.NOT_EQUAL;
} else if ("GREATER_THAN".equalsIgnoreCase(operator)) {
operatorType = DataValidationConstraint.OperatorType.GREATER_THAN;
} else if ("GREATER_OR_EQUAL".equalsIgnoreCase(operator)) {
operatorType = DataValidationConstraint.OperatorType.GREATER_OR_EQUAL;
} else if ("LESS_THAN".equalsIgnoreCase(operator)) {
operatorType = DataValidationConstraint.OperatorType.LESS_THAN;
} else if ("LESS_OR_EQUAL".equalsIgnoreCase(operator)) {
operatorType = DataValidationConstraint.OperatorType.LESS_OR_EQUAL;
}
String formula1 = null;
XmlObject[] xmlObjects = x14DataValidation.selectChildren(
new QName("http://schemas.microsoft.com/office/spreadsheetml/2009/9/main", "formula1"));
if (xmlObjects.length > 0) {
XmlObject formula1Element = xmlObjects[0];
formula1 = formula1Element.newCursor().getTextValue();
}
String formula2 = null;
xmlObjects = x14DataValidation.selectChildren(
new QName("http://schemas.microsoft.com/office/spreadsheetml/2009/9/main", "formula2"));
if (xmlObjects.length > 0) {
XmlObject formula2Element = xmlObjects[0];
formula2 = formula2Element.newCursor().getTextValue();
}
this.validationConstraint = new XSSFDataValidationConstraint(validationType, operatorType, formula1, formula2);
this.regions = new CellRangeAddressList();
String sqref = "";
xmlObjects = x14DataValidation.selectChildren(
new QName("http://schemas.microsoft.com/office/excel/2006/main", "sqref"));
if (xmlObjects.length > 0) {
XmlObject sqrefElement = xmlObjects[0];
sqref = sqrefElement.newCursor().getTextValue();
}
String [] refs = sqref.split(" ");
for (String ref : refs) {
CellRangeAddress address = CellRangeAddress.valueOf(ref);
this.regions.addCellRangeAddress(address);
}
String allowBlank = "";
XmlObject allowBlankAttribute = x14DataValidation.selectAttribute(new QName("allowBlank"));
if (allowBlankAttribute != null) allowBlank = allowBlankAttribute.newCursor().getTextValue();
this.emptyCellAllowed = ("1".equals(allowBlank) || "TRUE".equalsIgnoreCase(allowBlank));
String showInputMessage = "";
XmlObject showInputMessageAttribute = x14DataValidation.selectAttribute(new QName("showInputMessage"));
if (showInputMessageAttribute != null) showInputMessage = showInputMessageAttribute.newCursor().getTextValue();
this.showPromptBox = ("1".equals(showInputMessage) || "TRUE".equalsIgnoreCase(showInputMessage));
String showErrorMessage = "";
XmlObject showErrorMessageAttribute = x14DataValidation.selectAttribute(new QName("showErrorMessage"));
if (showErrorMessageAttribute != null) showErrorMessage = showErrorMessageAttribute.newCursor().getTextValue();
this.showErrorBox = ("1".equals(showErrorMessage) || "TRUE".equalsIgnoreCase(showErrorMessage));
//TODO: complete
}
public DataValidationConstraint getValidationConstraint() {
return this.validationConstraint;
}
public void setErrorStyle(int errorStyle) {
this.errorStyle = errorStyle;
}
public int getErrorStyle() {
return this.errorStyle;
}
public void setEmptyCellAllowed(boolean allowed) {
this.emptyCellAllowed = allowed;
}
public boolean getEmptyCellAllowed() {
return this.emptyCellAllowed;
}
public void setSuppressDropDownArrow(boolean suppress) {
this.suppressDropDownArrow = suppress;
}
public boolean getSuppressDropDownArrow() {
return this.suppressDropDownArrow;
}
public void setShowPromptBox(boolean show) {
this.showPromptBox = show;
}
public boolean getShowPromptBox() {
return this.showPromptBox;
}
public void setShowErrorBox(boolean show) {
this.showErrorBox = show;
}
public boolean getShowErrorBox() {
return this.showErrorBox;
}
public void createPromptBox(String title, String text) {
this.promptBoxTitle = title;
this.promptBoxText = text;
}
public String getPromptBoxTitle() {
return this.promptBoxTitle;
}
public String getPromptBoxText() {
return this.promptBoxText;
}
public void createErrorBox(String title, String text) {
this.errorBoxTitle = title;
this.errorBoxText = text;
}
public String getErrorBoxTitle() {
return this.errorBoxTitle;
}
public String getErrorBoxText() {
return this.errorBoxText;
}
public CellRangeAddressList getRegions() {
return this.regions;
}
}

How to save an instance of a JavaFX javafx.scene.shape.Path to a file

I am trying to save a javafx.scene.shape.Path to a file (at least its elements) however since Path is non-serializable and its PathElement as well it has proven very difficult.
Could someone inform me of a way to either convert the object to a String (preferred), JSON or something else?
Here are all the ways I have tried saving the object:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javafx.scene.shape.Path;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
class Test {
public static void main(String[] args) {
GsonBuilder builder = new GsonBuilder();
Path path;
Gson gson = builder.create();
{
Rectangle rectangle = new Rectangle(100, 100);
Polygon polygon = new Polygon(0, 0, 50, 50, 0, 50);
path = (Path) Shape.subtract(rectangle, polygon);
}
try {
String completePathObject = gson.toJson(path);
System.out.println(completePathObject);
} catch (IllegalArgumentException e) {
e.printStackTrace();
// java.lang.IllegalArgumentException: class com.sun.javafx.util.WeakReferenceQueue$ListEntry declares multiple JSON fields named next
}
try {
String pathObjectElements = gson.toJson(path.getElements());
System.out.println(pathObjectElements);
} catch (IllegalArgumentException e) {
e.printStackTrace();
// java.lang.IllegalArgumentException: class com.sun.javafx.util.WeakReferenceQueue$ListEntry declares multiple JSON fields named next
}
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.set"))) {
objectOutputStream.writeObject(path);
} catch (IOException e) {
e.printStackTrace();
// java.io.NotSerializableException: javafx.scene.shape.Path
}
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.set"))) {
objectOutputStream.writeObject(path.getElements());
} catch (IOException e) {
e.printStackTrace();
// java.io.NotSerializableException: javafx.scene.shape.Path$2
}
}
}
Nodes contain a lot of properties you'd need to convert to a form that can be written to a file.
Since your last attempt indicates you'll be satisfied with writing the path elements to the file, you could convert the PathElements to parts of a SVG path and also implement logic for parsing the elements PathElements from a svg path string.
The following doesn't accept all possible SVG paths and may accept some invalid paths:
public class SVGConverter {
private enum PathElementType {
ARC('a', ArcTo.class, ArcTo::new,
ArcTo::radiusXProperty,
ArcTo::radiusYProperty,
ArcTo::XAxisRotationProperty,
ArcTo::largeArcFlagProperty,
ArcTo::sweepFlagProperty,
ArcTo::xProperty,
ArcTo::yProperty),
CLOSE_PATH('z', ClosePath.class, ClosePath::new),
CUBIC_CURVE('c', CubicCurveTo.class, CubicCurveTo::new,
CubicCurveTo::controlX1Property,
CubicCurveTo::controlY1Property,
CubicCurveTo::controlX2Property,
CubicCurveTo::controlY2Property,
CubicCurveTo::xProperty,
CubicCurveTo::yProperty),
H_LINE_TO('h', HLineTo.class, HLineTo::new,
HLineTo::xProperty),
LINE_TO('l', LineTo.class, LineTo::new,
LineTo::xProperty, LineTo::yProperty),
MOVE_TO('m', MoveTo.class, MoveTo::new,
MoveTo::xProperty, MoveTo::yProperty),
QUAD_CURVE_TO('q', QuadCurveTo.class, QuadCurveTo::new,
QuadCurveTo::controlXProperty, QuadCurveTo::controlYProperty,
QuadCurveTo::xProperty, QuadCurveTo::yProperty),
V_LINE_TO('v', VLineTo.class, VLineTo::new,
VLineTo::yProperty);
private final char letter;
private final String typeName;
private final Supplier<? extends PathElement> factory;
private final Function[] propertyGetters;
<T extends PathElement> PathElementType(char letter, Class<T> type, Supplier<T> factory, Function<T, ? extends Property<?>>... propertyGetters) {
this.letter = letter;
this.typeName = type.getName();
this.factory = factory;
this.propertyGetters = propertyGetters;
}
}
private final Map<String, PathElementType> ELEMENT_TYPES_BY_TYPE;
private final Map<Character, PathElementType> ELEMENT_TYPES_BY_LETTER;
public SVGConverter() {
ELEMENT_TYPES_BY_LETTER = new HashMap<>();
ELEMENT_TYPES_BY_TYPE = new HashMap<>();
for (PathElementType et : PathElementType.values()) {
ELEMENT_TYPES_BY_LETTER.put(et.letter, et);
ELEMENT_TYPES_BY_TYPE.put(et.typeName, et);
}
}
public String pathToSvg(Path path) {
StringBuilder sb = new StringBuilder();
for (PathElement element : path.getElements()) {
PathElementType elementType = ELEMENT_TYPES_BY_TYPE.get(element.getClass().getName());
if (elementType == null) {
throw new IllegalArgumentException("Unknown PathElement type: " + element.getClass().getName());
}
// specify path element type
char c = elementType.letter;
if (element.isAbsolute()) {
c = Character.toUpperCase(c);
}
sb.append(c);
// write property values
for (Function f : elementType.propertyGetters) {
Property property = (Property) f.apply(element);
sb.append((property instanceof BooleanProperty)
// special treatment for booleans to convert true/false to 1/0
? (((BooleanProperty) property).get() ? "1" : "0")
: property.getValue().toString()).append(' ');
}
}
// trim, if necessary
int lastIndex = sb.length() - 1;
if (lastIndex >= 0 && sb.charAt(lastIndex) == ' ') {
sb.deleteCharAt(lastIndex);
}
return sb.toString();
}
private static final String NUMBER_PATTERN_STRING = "[+-]?\\d*\\.\\d*(?:[eE][+-]?\\d+)?";
private static final Pattern NUMBER_PATTERN = Pattern.compile("(?<![\\d.+-])(" + NUMBER_PATTERN_STRING + ')');
private static final Pattern SVG_PATTERN = Pattern.compile("([aAcChHlLvmMqQVzZ])((?:\\s*" + NUMBER_PATTERN_STRING + "(?:[\\s,]+" + NUMBER_PATTERN_STRING + ")*)?)");
// parses doubles from number sequence
private static double[] getNumberMatches(Matcher m, int count) {
double[] result = new double[count];
for (int i = 0; i < count; i++) {
if (!m.find()) {
throw new IllegalArgumentException("missing numbers");
}
result[i] = Double.parseDouble(m.group(1));
}
if (m.find()) {
throw new IllegalArgumentException("too many numbers");
}
return result;
}
public Path svgToPath(String svg) {
Path path = new Path();
Matcher matcher = SVG_PATTERN.matcher(svg);
while (matcher.find()) {
// find out path element type
char c = matcher.group(1).charAt(0);
PathElementType elementType = ELEMENT_TYPES_BY_LETTER.get(Character.toLowerCase(c));
if (elementType == null) {
throw new IllegalArgumentException("Unknown path type " + c);
}
PathElement element = (PathElement) elementType.factory.get();
element.setAbsolute(Character.isUpperCase(c));
// retrieve parameters
if (elementType.propertyGetters.length > 0) {
Matcher numberMatcher = NUMBER_PATTERN.matcher(matcher.group(2));
double[] numbers = getNumberMatches(numberMatcher, elementType.propertyGetters.length);
for (int i = 0; i < elementType.propertyGetters.length; i++) {
Property property = (Property) elementType.propertyGetters[i].apply(element);
property.setValue((property instanceof BooleanProperty)
? (numbers[i] == 1) // convert to boolean (true iff 1.0)
: numbers[i]);
}
}
path.getElements().add(element);
}
return path;
}
}
Note: This does not restore any kind of bindings that may have existed before converting to string of course.

Java, Refactoring case

I was given exercise that I need to refactor several java projects.
Only those 2 left which I truly don't have an idea how to refactor.
csv.writer
public class CsvWriter {
public CsvWriter() {
}
public void write(String[][] lines) {
for (int i = 0; i < lines.length; i++)
writeLine(lines[i]);
}
private void writeLine(String[] fields) {
if (fields.length == 0)
System.out.println();
else {
writeField(fields[0]);
for (int i = 1; i < fields.length; i++) {
System.out.print(",");
writeField(fields[i]);
}
System.out.println();
}
}
private void writeField(String field) {
if (field.indexOf(',') != -1 || field.indexOf('\"') != -1)
writeQuoted(field);
else
System.out.print(field);
}
private void writeQuoted(String field) {
System.out.print('\"');
for (int i = 0; i < field.length(); i++) {
char c = field.charAt(i);
if (c == '\"')
System.out.print("\"\"");
else
System.out.print(c);
}
System.out.print('\"');
}
}
csv.writertest
public class CsvWriterTest {
#Test
public void testWriter() {
CsvWriter writer = new CsvWriter();
String[][] lines = new String[][] {
new String[] {},
new String[] { "only one field" },
new String[] { "two", "fields" },
new String[] { "", "contents", "several words included" },
new String[] { ",", "embedded , commas, included",
"trailing comma," },
new String[] { "\"", "embedded \" quotes",
"multiple \"\"\" quotes\"\"" },
new String[] { "mixed commas, and \"quotes\"", "simple field" } };
// Expected:
// -- (empty line)
// only one field
// two,fields
// ,contents,several words included
// ",","embedded , commas, included","trailing comma,"
// """","embedded "" quotes","multiple """""" quotes"""""
// "mixed commas, and ""quotes""",simple field
writer.write(lines);
}
}
test
public class Configuration {
public int interval;
public int duration;
public int departure;
public void load(Properties props) throws ConfigurationException {
String valueString;
int value;
valueString = props.getProperty("interval");
if (valueString == null) {
throw new ConfigurationException("monitor interval");
}
value = Integer.parseInt(valueString);
if (value <= 0) {
throw new ConfigurationException("monitor interval > 0");
}
interval = value;
valueString = props.getProperty("duration");
if (valueString == null) {
throw new ConfigurationException("duration");
}
value = Integer.parseInt(valueString);
if (value <= 0) {
throw new ConfigurationException("duration > 0");
}
if ((value % interval) != 0) {
throw new ConfigurationException("duration % interval");
}
duration = value;
valueString = props.getProperty("departure");
if (valueString == null) {
throw new ConfigurationException("departure offset");
}
value = Integer.parseInt(valueString);
if (value <= 0) {
throw new ConfigurationException("departure > 0");
}
if ((value % interval) != 0) {
throw new ConfigurationException("departure % interval");
}
departure = value;
}
}
public class ConfigurationException extends Exception {
private static final long serialVersionUID = 1L;
public ConfigurationException() {
super();
}
public ConfigurationException(String arg0) {
super(arg0);
}
public ConfigurationException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
public ConfigurationException(Throwable arg0) {
super(arg0);
}
}
configuration.test
public class ConfigurationTest{
#Test
public void testGoodInput() throws IOException {
String data = "interval = 10\nduration = 100\ndeparture = 200\n";
Properties input = loadInput(data);
Configuration props = new Configuration();
try {
props.load(input);
} catch (ConfigurationException e) {
assertTrue(false);
return;
}
assertEquals(props.interval, 10);
assertEquals(props.duration, 100);
assertEquals(props.departure, 200);
}
#Test
public void testNegativeValues() throws IOException {
processBadInput("interval = -10\nduration = 100\ndeparture = 200\n");
processBadInput("interval = 10\nduration = -100\ndeparture = 200\n");
processBadInput("interval = 10\nduration = 100\ndeparture = -200\n");
}
#Test
public void testInvalidDuration() throws IOException {
processBadInput("interval = 10\nduration = 99\ndeparture = 200\n");
}
#Test
public void testInvalidDeparture() throws IOException {
processBadInput("interval = 10\nduration = 100\ndeparture = 199\n");
}
#Test
private void processBadInput(String data) throws IOException {
Properties input = loadInput(data);
boolean failed = false;
Configuration props = new Configuration();
try {
props.load(input);
} catch (ConfigurationException e) {
failed = true;
}
assertTrue(failed);
}
#Test
private Properties loadInput(String data) throws IOException {
InputStream is = new StringBufferInputStream(data);
Properties input = new Properties();
input.load(is);
is.close();
return input;
}
}
Ok, here some advice regarding the code.
CsvWriter
The bad thing is that you print everything to System.out. It will be hard to test without mocks. Instead I suggest you to add field PrintStream which defines where all data will go.
import java.io.PrintStream;
public class CsvWriter {
private final PrintStream printStream;
public CsvWriter() {
this.printStream = System.out;
}
public CsvWriter(PrintStream printStream) {
this.printStream = printStream;
}
...
You then write everything to this stream. This refactoring easy since you use replace function(Ctrl+R in IDEA). Here is the example how you do it.
private void writeField(String field) {
if (field.indexOf(',') != -1 || field.indexOf('\"') != -1)
writeQuoted(field);
else
printStream.print(field);
}
Others stuff seems ok in this class.
CsvWriterTest
First thing first you don't check all logic in a single method. Make small methods with different kind of tests. It's ok to keep your current test though. Sometimes it's useful to check most of the logic in a complex scenario.
Also pay attention to the names of the methods. Check this
Obviously you test doesn't check the results. That's why we need this functionality with PrintStream. We can build a PrintStream on top of the instance of ByteArrayOutputStream. We then construct a string and check if the content is valid. Here is how you can easily check what was written
public class CsvWriterTest {
private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
private PrintStream printStream = new PrintStream(byteArrayOutputStream);
#Test
public void testWriter() {
CsvWriter writer = new CsvWriter(printStream);
... old logic here ...
writer.write(lines);
String result = new String(byteArrayOutputStream.toByteArray());
Assert.assertTrue(result.contains("two,fields"));
Configuration
Make fields private
Make messages more concise
ConfigurationException
Seems good about serialVersionUID. This thing is needed for serialization/deserialization.
ConfigurationTest
Do not use assertTrue(false/failed); Use Assert.fail(String) with some message which is understandable.
Tip: if you don't have much experience and need to refactor code like this, you may want to read some chapters of Effective Java 2nd edition by Joshua Bloch. The book is not so big so you can read it in a week and it has some rules how to write clean and understandable code.

parse CSV using BaneUtilBean

I am trying to parse a csv and map the fields to a POJO class. However I can see that the mapping is not achieved correctly.
I am trying to map the header from a POJO file to the csv.
public class CarCSVFileInputBean {
private long Id;
private String shortName;
private String Name;
private String Type;
private String Environment;
//getter and setters
}
Can someone please take a look at my code:
public class carCSVUtil {
private static Log log = LogFactory.getLog(carCSVUtil.class);
private static final List<String> fileHeaderFields = new ArrayList<String>();
private static final String UTF8CHARSET = "UTF-8";
static {
for (Field f : carCSVFileInputBean.class.getDeclaredFields()) {
fileHeaderFields.add(f.getName());
}
}
public static List<carCSVFileInputBean> getCSVInputList(InputStream inputStream) {
CSVReader reader = null;
List<carCSVFileInputBean> csvList = null;
carCSVFileInputBean inputRecord = null;
String[] header = null;
String[] row = null;
try {
reader = new CSVReader(new InputStreamReader(inputStream, UTF8CHARSET));
csvList = new ArrayList<carCSVFileInputBean>();
header = reader.readNext();
boolean isEmptyLine = true;
while ((row = reader.readNext()) != null) {
isEmptyLine = true;
if (!(row.length == 1 && StringUtils.isBlank(row[0]))) { // not an empty line, not even containing ','
inputRecord = new carCSVFileInputBean();
isEmptyLine = populateFields(inputRecord, header, row);
if (!isEmptyLine)
csvList.add(inputRecord);
}
}
} catch (IOException e) {
log.debug("IOException while accessing carCSVFileInputBean: " + e);
return null;
} catch (IllegalAccessException e) {
log.debug("IllegalAccessException while accessing carCSVFileInputBean: " + e);
return null;
} catch (InvocationTargetException e) {
log.debug("InvocationTargetException while copying carCSVFileInputBean properties: " + e);
return null;
} catch (Exception e) {
log.debug("Exception while parsing CSV file: " + e);
return null;
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException ioe) {}
}
return csvList;
}
protected static boolean populateFields(carCSVFileInputBean inputRecord, String[] header, String[] row) throws IllegalAccessException, InvocationTargetException {
boolean isEmptyLine = true;
for (int i = 0; i < row.length; i++) {
String val = row[i];
if (!StringUtils.isBlank(val)) {
BeanUtilsBean.getInstance().copyProperty(inputRecord, header[i], val);
isEmptyLine = false;
}
}
return isEmptyLine;
}
}
I found the solution - the headers in the csv file are expected to begin with a lowercase.

c# Marshal structure equivalent of java

I would like to create that object same for java. Is it possible to create it?
How it works :
you can find more informaiton how I used it.
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Common.Models;
using System.Text;
namespace Common.Utilities.Helpers
{
public partial class CommareaHelper
{
public static T StringToObject<T>(string buffer)
{
IntPtr pBuf = IntPtr.Zero;
try
{
pBuf = Marshal.StringToBSTR(buffer);
return (T)Marshal.PtrToStructure(pBuf, typeof(T));
}
catch
{
throw;
}
finally
{
pBuf = IntPtr.Zero;
}
}
public static string ObjectToString(Object conversionObject)
{
int size = 0;
IntPtr pBuf = IntPtr.Zero;
try
{
size = Marshal.SizeOf(conversionObject);
pBuf = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(conversionObject, pBuf, false);
return Marshal.PtrToStringAuto(pBuf, size).Substring(0, size/2);
}
catch
{
throw;
}
finally
{
Marshal.FreeHGlobal(pBuf);
}
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class Comarea
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
private string status;
public string Status
{
get
{
return new string(status).Trim();
}
set
{
status = value.ToFixedCharArray(1, true);
}
}
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
private string operationName;
public string OperationName
{
get
{
return new string(operationName).Trim();
}
set
{
operationName = value.ToFixedCharArray(5, true);
}
}
}
I can fill any object using single line of string and opposite of that operation
string commareaStr = "0TR231";
Commarea commarea = CommareaHelper.StringToObject<Commarea>(commareaStr);

Categories