Parse en string til flere variabler med split regex m.m
Jeg har lavet denne kode, den skulle meget gerne forklare hvad jeg prøver at gøre.. problemet er bare at koden ser meget "vild" ud i forhold til det den skal.. og jeg undrede mig om der var en letter måde at gøre dette på, samt måske en god guide til at gøre det.Jeg står og skal gøre dette ved mange forskellige typer strenge, med forskellige formateringer.. og ville gerne lære at gøre det korrekt, så jeg ikke skal ændre for meget.
Ligenu bruger jeg regex, da jeg ikke kunne overkomme og se andre måder at gøre det på, men jeg byder alle former for kodning velkommen :)
CommandUpdate.java
[div]
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommandUpdate {
private List<ParamSingle> parms;
public CommandUpdate(String commandstring) {
parms = generateParms(splitParm(commandstring));
}
public ParamSingle[] getParms(){
return parms.toArray(new ParamSingle[]{});
}
// ****************************************************************//
// Class Tools
// ****************************************************************//
private String[] splitParm(String text) {
String[] textarr = text.split(" ");
List<String> parms = new ArrayList<>();
parms.add(textarr[0]); // UPDATE
parms.add(textarr[1]); // 12.7543.81
for (int i = 2; i < textarr.length; i++) {
if (textarr[i].matches("^([A-Za-z]+)=([\\S ]+)$")) {
parms.add(textarr[i]);
} else {
parms.set(parms.size() - 1, parms.get(parms.size() - 1) + " "
+ textarr[i]);
}
}
return parms.toArray(new String[] {});
}
private List<ParamSingle> generateParms(String[] parmstrings) {
List<ParamSingle> parms = new ArrayList<>();
Pattern pv1 = Pattern.compile("^([A-Za-z]+)=([0-9]+)$");
Pattern pv2 = Pattern.compile("^([\\S]+)=([\\S ]+) \\(([\\S ]+)\\)$");
Pattern pv3 = Pattern.compile("^([A-Za-z]+)=\"([\\S ]+)\" \\[([\\S ]+)\\] \\(([0-9\\.]+)\\)");
Matcher match;
for (String parmstring : parmstrings) {
if ((match = pv1.matcher(parmstring)).find()) {
parms.add(new Param1Value<Integer>(match.group(1), Integer.parseInt(match.group(2))));
} else if ((match = pv2.matcher(parmstring)).find()) {
parms.add(new Param2Value<String,String>(match.group(1), match.group(2),match.group(3)));
} else if ((match = pv3.matcher(parmstring)).find()) {
parms.add(new Param3Value<String,String,Float>(match.group(1), match.group(2),match.group(3),Float.parseFloat(match.group(4))));
}
}
return parms;
}
// ****************************************************************//
// Structure Classes
// ****************************************************************//
public class Param3Value<T, T2, T3> extends Param2Value<T, T2> {
private T3 value3;
public Param3Value(String name, T value, T2 value2, T3 value3) {
super(name, value, value2);
setValue3(value3);
}
public T3 getValue3() {
return this.value3;
}
public void setValue3(T3 value3) {
this.value3 = value3;
}
}
public class Param2Value<T, T2> extends Param1Value<T> {
private T2 value2;
public Param2Value(String name, T value, T2 value2) {
super(name, value);
setValue2(value2);
}
public T2 getValue2() {
return this.value2;
}
public void setValue2(T2 value2) {
this.value2 = value2;
}
}
public class Param1Value<T> extends ParamSingle {
private T value;
public Param1Value(String name, T value) {
super(name);
setValue(value);
}
public T getValue() {
return this.value;
}
public void setValue(T value) {
this.value = value;
}
}
public class ParamSingle {
private String name;
public ParamSingle(String name) {
setName(name);
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}
[/Div]
Test.java
[Div]
public class Test {
final static String[] lines = new String[] {
"UPDATE 12.7543.81 ParmA=\"dk.asterix.org\" [Denmark] (9.1) ParmB=\"de.asterix.org\" [Germany] (1.0) Called=49xxxxxxx (GBH) Calling=45xxxxxxxx (LOA) Internal=0 State=2",
"UPDATE 12.7543.81 ParmB=\"de.asterix.org\" [Germany] (1.0) ParmA=\"dk.asterix.org\" [Denmark] (9.1) Called=49xxxxxxx (GBH) Calling=45xxxxxxxx (LOA) Internal=0 State=2",
"UPDATE 12.7543.81 Internal=0 State=2 ParmB=\"de.asterix.org\" [Germany] (1.0) ParmA=\"dk.asterix.org\" [Denmark] (9.1) Called=49xxxxxxx (GBH) Calling=45xxxxxxxx (LOA)",
"UPDATE 12.7543.81 Internal=0 State=2 ParmB=\"de.asterix.org\" [Germany] (1.0) Calling=90xxxxxx (LOA) ParmA=\"dk.asterix.org\" [Denmark] (9.1) Called=49xxxxxxx (GBH)" };
@SuppressWarnings("unchecked")
public static void main(String[] args) {
CommandUpdate[] cus = new CommandUpdate[] {
new CommandUpdate(lines[0]), new CommandUpdate(lines[1]),
new CommandUpdate(lines[2]), new CommandUpdate(lines[3]) };
for (CommandUpdate cu : cus) {
for (CommandUpdate.ParamSingle ps : cu.getParms()) {
if (ps instanceof CommandUpdate.Param3Value) {
CommandUpdate.Param3Value<String,String,Float> d = (CommandUpdate.Param3Value<String,String,Float>) ps;
System.out.println("Data: " + d.getName() + " Value:"
+ d.getValue() + " Value2:" + d.getValue2()
+ " Value3:" + d.getValue3());
} else if (ps instanceof CommandUpdate.Param2Value) {
CommandUpdate.Param2Value<String,String> d = (CommandUpdate.Param2Value<String, String>) ps;
System.out.println("Data: " + d.getName() + " Value:"
+ d.getValue() + " Value2:" + d.getValue2());
} else if (ps instanceof CommandUpdate.Param1Value) {
CommandUpdate.Param1Value<Integer> d = (CommandUpdate.Param1Value<Integer>) ps;
System.out.println("Data: " + d.getName() + " Value:"
+ d.getValue());
}
}
}
}
}
[/Div]