1 | package com.framsticks.params.types; |
---|
2 | |
---|
3 | import com.framsticks.params.Param; |
---|
4 | import com.framsticks.util.lang.Strings; |
---|
5 | |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.List; |
---|
8 | import java.util.regex.Matcher; |
---|
9 | import java.util.regex.Pattern; |
---|
10 | |
---|
11 | /** |
---|
12 | * @author Piotr Sniegowski |
---|
13 | */ |
---|
14 | public class ProcedureParam extends Param { |
---|
15 | private Param resultType; |
---|
16 | private List<Param> argumentsType = new ArrayList<Param>(); |
---|
17 | |
---|
18 | private static Pattern addressPattern = Pattern.compile("^([^\\(]+)?\\(([^\\)]*)\\)$"); |
---|
19 | |
---|
20 | protected static Param parseType(String type, String name) { |
---|
21 | return Param.build().type(type).name(name).finish(); |
---|
22 | } |
---|
23 | |
---|
24 | public void parseSignature(String signature) throws Exception { |
---|
25 | if (!Strings.notEmpty(signature)) { |
---|
26 | return; |
---|
27 | } |
---|
28 | Matcher matcher = addressPattern.matcher(signature); |
---|
29 | if (!matcher.matches()) { |
---|
30 | throw new Exception("invalid signature"); |
---|
31 | } |
---|
32 | String result = Strings.collapse(matcher.group(1)); |
---|
33 | if (result != null) { |
---|
34 | resultType = parseType(result, null); |
---|
35 | } |
---|
36 | String arguments = matcher.group(2); |
---|
37 | if (!Strings.notEmpty(arguments)) { |
---|
38 | return; |
---|
39 | } |
---|
40 | for (String a : arguments.split(",")) { |
---|
41 | int space = a.indexOf(' '); |
---|
42 | String type; |
---|
43 | String name; |
---|
44 | if (space == -1) { |
---|
45 | type = a; |
---|
46 | name = null; |
---|
47 | } else { |
---|
48 | type = a.substring(0, space); |
---|
49 | name = a.substring(space + 1); |
---|
50 | } |
---|
51 | argumentsType.add(parseType(type, name)); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | @Override |
---|
57 | public Class<?> getStorageType() { |
---|
58 | return Void.class; |
---|
59 | } |
---|
60 | |
---|
61 | public Param getResultType() { |
---|
62 | return resultType; |
---|
63 | } |
---|
64 | |
---|
65 | public List<Param> getArgumentsType() { |
---|
66 | return argumentsType; |
---|
67 | } |
---|
68 | |
---|
69 | @Override |
---|
70 | public String getFramsTypeName() { |
---|
71 | return "p"; |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|