Adding source to collect all localisation texts
[gigi.git] / util-testing / org / cacert / gigi / localisation / TaintSource.java
1 package org.cacert.gigi.localisation;
2 import java.util.regex.Matcher;
3 import java.util.regex.Pattern;
4
5 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
6
7 public class TaintSource {
8         private String pack, cls, meth;
9         private int tgt;
10         private TaintSource maskOnly;
11
12         public TaintSource(String pack, String cls, String meth, int tgt) {
13                 this(pack, cls, meth, tgt, null);
14         }
15         public TaintSource(String pack, String cls, String meth, int tgt,
16                         TaintSource maskOnly) {
17                 this.pack = pack;
18                 this.cls = cls;
19                 this.meth = meth;
20                 this.tgt = tgt;
21                 this.maskOnly = maskOnly;
22
23         }
24         public TaintSource(MethodBinding mb) {
25                 pack = new String(mb.declaringClass.qualifiedPackageName());
26                 cls = new String(mb.declaringClass.qualifiedSourceName());
27                 meth = new String(mb.readableName());
28         }
29         @Override
30         public int hashCode() {
31                 final int prime = 31;
32                 int result = 1;
33                 result = prime * result + ((cls == null) ? 0 : cls.hashCode());
34                 result = prime * result + ((meth == null) ? 0 : meth.hashCode());
35                 result = prime * result + ((pack == null) ? 0 : pack.hashCode());
36                 return result;
37         }
38         @Override
39         public String toString() {
40                 StringBuffer res = new StringBuffer();
41                 res.append("new TaintSource(");
42                 res.append("\"" + pack + "\",");
43                 res.append("\"" + cls + "\",");
44                 res.append("\"" + meth + "\",0);");
45                 return res.toString();
46         }
47         public String toConfLine() {
48                 return pack + " " + cls + "." + meth + "," + tgt
49                                 + (maskOnly == null ? "" : "=>" + maskOnly.toConfLine());
50         }
51         @Override
52         public boolean equals(Object obj) {
53                 if (this == obj) {
54                         return true;
55                 }
56                 if (obj == null) {
57                         return false;
58                 }
59                 if (getClass() != obj.getClass()) {
60                         return false;
61                 }
62                 TaintSource other = (TaintSource) obj;
63                 if (cls == null) {
64                         if (other.cls != null) {
65                                 return false;
66                         }
67                 } else if (!cls.equals(other.cls)) {
68                         return false;
69                 }
70                 if (pack == null) {
71                         if (other.pack != null) {
72                                 return false;
73                         }
74                 } else if (!pack.equals(other.pack)) {
75                         return false;
76                 }
77                 if (meth == null) {
78                         if (other.meth != null) {
79                                 return false;
80                         }
81                 } else if (!meth.equals(other.meth)) {
82                         return false;
83                 }
84                 return true;
85         }
86         public static TaintSource parseTaint(String confline) {
87                 // Pattern matches "Taint-lines"
88                 // first part is package name up to space (may not include space or equals sign)
89                 // second part is Class name [with inner class name] (may not include "=" but may include ".")
90                 // third part is method name including params (may not include "=" or ".")
91                 // fourth is index of tainted argument (seperated by "," from the rest)
92                 Pattern p = Pattern
93                                 .compile("^([^= ]*) ([^=]*)\\.([^=.]*\\([^)]*\\)),([0-9]+)");
94                 Matcher m = p.matcher(confline);
95                 if (!m.find()) {
96                         throw new Error(confline);
97                 }
98                 String pack = m.group(1);
99                 String cls = m.group(2);
100                 String meth = m.group(3);
101                 int tgt = Integer.parseInt(m.group(4));
102                 TaintSource mask = null;
103                 if (m.end() != confline.length()) {
104                         String s = confline.substring(m.end(), m.end() + 2);
105                         if (!s.equals("=>")) {
106                                 throw new Error("malformed");
107                         }
108                         mask = parseTaint(confline.substring(m.end() + 2));
109                 }
110                 return new TaintSource(pack, cls, meth, tgt, mask);
111         }
112         public TaintSource getMaskOnly() {
113                 return maskOnly;
114         }
115         public int getTgt() {
116                 return tgt;
117         }
118
119 }