java - String manipulation for VML Path -
hi trying parse vml path value using java string manipulation. want retreive commands in path moveto, lineto, curveto, rlineto (other commands) , corresponding x , y coordinates/parameters.
here example data parse, each command has own x,y coordinates.
1. m1,1 l1,200,200,200,200,1 xe 2. m, l1,200,200,200,200,1 xe
can suggest algorithm or code on retreiving commands , parameters each command? example in number 1.
command = moveto 'm' command parameters = (x=1,y=1).
ref: http://www.w3.org/tr/note-vml#_toc416858391
this weird tried using stringtokenizer
stringtokenizer tokenizer = new stringtokenizer(path);
a friend suggested using stringtokenizer , did near goal, gave me following data. maybe can utilize stringtokenizer suit needs.
m1,1 l1,200,200,200,200,1 xe
for #1, here ideal output. (pseudocode)
string command_type = "m" list<string, string> parameters = add("1", "1") string command_type = "l" list<string, string> parameters = add("1", "200") add("200", "200") add("200", "1") string command_type = "x" (can have no parameter ) string command_type = "e" (can have no parameter )
for #2, here ideal output. (pseudocode)
string command_type = "m" list<string, string> parameters = add("0", "0") // because x , y parameters not specified need force them 0,0 string command_type = "l" list<string, string> parameters = add("1", "200") add("200", "200") add("200", "1") string command_type = "x" (can have no parameter ) string command_type = "e" (can have no parameter )
a spec important here based on sample input , output have guessed:
letter -> comma separated parameters -> letter -> comma separated parameters
i have noted commands not separated spaces. e.g. have xe 2 separate commands. means in sample spaces have no meaning , can ignored.
i note commands single letters. (otherwise xe wouldn’t come 2 commands)
also parameters must come in pairs , must numbers. see no negative numbers in sample assume these possible. assume integers , not decimals.
so based on assumed spec can come possible solution have through , work out doing.
package ic.ac.uk.relationshipvisualiser.app; import java.util.arraylist; import java.util.list; public class tmpnofxtest { private static class coord { int x = 0; int y = 0; public coord(int p_x,int p_y) { x=p_x;y=p_y; } public int getx() {return x;} public int gety() {return y;} } private static class command { string command = ""; list<coord> param_list = new arraylist<coord>(); public command(string p_command) { command = p_command; } private string parseoneparam(string p_inp) { if (p_inp.equals("")) return ""; if (isletter(p_inp.substring(0,1))) return p_inp; int firstchar = 0; (int c=0;c<p_inp.length();c++) { if (firstchar==0) { if (isletter(p_inp.substring(c,c+1))) { firstchar = c; } } } string parms = p_inp.substring(0,firstchar); if (parms.length()==0) return p_inp.substring(firstchar); int x = 0; int y = 0; int p = 0; string tmp = ""; while ((p<parms.length()) && (!parms.substring(p,p+1).equals(","))) { tmp = tmp + parms.substring(p,p+1); p++; } p++; if (tmp.length()>0) { x = integer.parseint(tmp); } tmp = ""; while ((p<parms.length()) && (!parms.substring(p,p+1).equals(","))) { tmp = tmp + parms.substring(p,p+1); p++; } if (p_inp.substring(p,p+1)==",") p++; if (tmp.length()>0) { y = integer.parseint(tmp); } param_list.add(new coord(x,y)); return p_inp.substring(p); } public string parseparams(string p_inp) { if (p_inp.equals("")) return ""; while (!isletter(p_inp)) { p_inp = parseoneparam(p_inp); } return p_inp; } public string tostring() { string ret = ""; ret = "string command_type = \"" + command + "\""; if (param_list.size()==0) return ret + " (can have no parameter )"; (int c=0;c<param_list.size();c++) { if (c>0) ret += "\n "; ret += " list<string, string> parameters = add(\"" + param_list.get(c).getx() + "\", \"" + param_list.get(c).gety() + "\")"; } return ret; } } private static boolean isletter(string p_inp) { return p_inp.substring(0,1).matches("\\p{l}"); } private static string parsesinglecommand(string p_inp, list<command> p_cmds) throws exception { //read single command off incoming string , pass remaining input string cmd = p_inp.substring(0,1); if (!isletter(p_inp)) throw new exception("error command starts non letter (" + cmd + ")"); p_inp = p_inp.substring(1); command c = new command(cmd); p_inp = c.parseparams(p_inp); p_cmds.add(c); return p_inp; } private static list<command> parse(string p_inp) throws exception { list<command> r = new arraylist<command>(); //spaces don't matter , want make case-insensitive minimise errors p_inp = p_inp.tolowercase(); p_inp = p_inp.replace(" ", ""); while (p_inp.length()>0) { p_inp = parsesinglecommand(p_inp,r); } return r; } public static void main(string[] args) { system.out.println("start tmptest"); list<string> tests = new arraylist<string>(); tests.add("m1,1 l1,200,200,200,200,1 xe"); tests.add("m, l1,200,200,200,200,1 xe"); (int c=0;c<tests.size();c++) { system.out.println("running test case " + c + " (" + tests.get(c) + ")"); try { list<command> pr = parse(tests.get(c)); (int d=0;d<pr.size();d++) { system.out.println(pr.get(d).tostring()); } } catch (exception e) { e.printstacktrace(); return; } }; system.out.println("end tmptest"); } }
Comments
Post a Comment