Revision 461 org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/MethodList.java
MethodList.java | ||
---|---|---|
3 | 3 |
import java.io.File; |
4 | 4 |
import java.net.URL; |
5 | 5 |
import java.util.ArrayList; |
6 |
import java.util.Collection; |
|
6 | 7 |
import java.util.Collections; |
7 | 8 |
import java.util.Comparator; |
8 | 9 |
import java.util.HashMap; |
10 |
import java.util.HashSet; |
|
9 | 11 |
import java.util.Iterator; |
10 | 12 |
import java.util.List; |
11 | 13 |
import java.util.Map; |
... | ... | |
16 | 18 |
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpClass; |
17 | 19 |
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpMethod; |
18 | 20 |
|
19 |
public class MethodList extends ArrayList<ScriptingHelpMethod> {
|
|
21 |
public class MethodList extends ArrayList<ScriptingHelpMethod> { |
|
20 | 22 |
|
21 |
/**
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
private static final long serialVersionUID = 2097121200002178537L;
|
|
23 |
/**
|
|
24 |
*
|
|
25 |
*/
|
|
26 |
private static final long serialVersionUID = 2097121200002178537L;
|
|
25 | 27 |
|
26 |
public MethodList() { |
|
27 |
} |
|
28 |
private Map<String, ClassNode> allClasses = new HashMap<String, ClassNode>(); |
|
28 | 29 |
|
29 |
public void addMethods(URL indexFileUrl){ |
|
30 |
File f = new File(indexFileUrl.getFile()); |
|
31 |
String indexFile = DefaultScriptingManager.getStringFromFile(f.getPath()); |
|
32 |
Pattern methodPattern = Pattern.compile("<indexitem text=\"[^\"]*\" expand=\"false\">"); |
|
33 |
Pattern endMethodPattern = Pattern.compile("</indexitem>");; |
|
34 |
Pattern classPattern = Pattern.compile("<indexitem text=\"[^\"]*\" target=\"[^\"]*\" />"); |
|
30 |
public MethodList() { |
|
31 |
} |
|
35 | 32 |
|
36 |
Matcher methodMatcher = methodPattern.matcher(indexFile); |
|
37 |
while (methodMatcher.find()){ |
|
38 |
String subIndexFile = indexFile.substring(methodMatcher.start()); |
|
39 |
Matcher endMethodMatcher = endMethodPattern.matcher(subIndexFile); |
|
40 |
if(endMethodMatcher.find()){ |
|
41 |
String methodExtract = subIndexFile.substring(0, endMethodMatcher.start()); |
|
42 |
String[] methodnode = (indexFile.substring(methodMatcher.start(), methodMatcher.end())).split("\""); |
|
43 |
if(methodnode.length>2){ |
|
44 |
MethodNode mn = new MethodNode(methodnode[1],null); |
|
33 |
public void addMethods(URL indexFileUrl) { |
|
34 |
File f = new File(indexFileUrl.getFile()); |
|
35 |
String indexFile = DefaultScriptingManager.getStringFromFile(f.getPath()); |
|
36 |
Pattern methodPattern = Pattern.compile("<indexitem text=\"[^\"]*\" expand=\"false\">"); |
|
37 |
Pattern endMethodPattern = Pattern.compile("</indexitem>");; |
|
38 |
Pattern classPattern = Pattern.compile("<indexitem text=\"[^\"]*\" target=\"[^\"]*\" />"); |
|
45 | 39 |
|
46 |
Matcher classMatcher = classPattern.matcher(methodExtract); |
|
47 |
while(classMatcher.find()){ |
|
48 |
String[] classnode = (methodExtract.substring(classMatcher.start(), classMatcher.end())).split("\""); |
|
40 |
Matcher methodMatcher = methodPattern.matcher(indexFile); |
|
41 |
while (methodMatcher.find()) { |
|
42 |
String subIndexFile = indexFile.substring(methodMatcher.start()); |
|
43 |
Matcher endMethodMatcher = endMethodPattern.matcher(subIndexFile); |
|
44 |
if (endMethodMatcher.find()) { |
|
45 |
String methodExtract = subIndexFile.substring(0, endMethodMatcher.start()); |
|
46 |
String[] methodnode = (indexFile.substring(methodMatcher.start(), methodMatcher.end())).split("\""); |
|
47 |
if (methodnode.length > 2) { |
|
48 |
MethodNode mn = new MethodNode(methodnode[1], null); |
|
49 | 49 |
|
50 |
if(classnode.length>3){ |
|
51 |
ClassNode cn = new ClassNode(classnode[1],classnode[3]); |
|
52 |
mn.add(cn); |
|
53 |
} |
|
50 |
Matcher classMatcher = classPattern.matcher(methodExtract); |
|
51 |
while (classMatcher.find()) { |
|
52 |
String[] classnode = (methodExtract.substring(classMatcher.start(), classMatcher.end())).split("\""); |
|
54 | 53 |
|
55 |
} |
|
56 |
this.add(mn); |
|
54 |
if (classnode.length > 3) { |
|
55 |
ClassNode cn = this.getOrCreateClassInfo(classnode[1], classnode[3]); |
|
56 |
cn.addMethod(mn.getName()); |
|
57 |
mn.add(cn); |
|
58 |
} |
|
57 | 59 |
|
58 |
}
|
|
59 |
}
|
|
60 |
}
|
|
61 |
this.add(mn);
|
|
60 | 62 |
|
61 |
}
|
|
62 |
}
|
|
63 |
}
|
|
64 |
}
|
|
63 | 65 |
|
64 |
public ScriptingHelpClass add(String className, String target, List<String> methods){ |
|
65 |
ClassNode cn = new ClassNode(className,target); |
|
66 |
for (String methodName : methods) { |
|
67 |
boolean isFound=false; |
|
68 |
for (ScriptingHelpMethod mn : this) { |
|
69 |
if(mn.getName().equals(methodName)){ |
|
70 |
((MethodNode) mn).add(cn); |
|
71 |
isFound=true; |
|
72 |
} |
|
73 |
} |
|
74 |
if(!isFound){ |
|
75 |
this.add(new MethodNode(methodName, cn)); |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
Collections.sort(this, new Comparator<ScriptingHelpMethod>() { |
|
80 |
public int compare(ScriptingHelpMethod p1,ScriptingHelpMethod p2) { |
|
81 |
return p1.getName().toLowerCase().compareTo(p2.getName().toLowerCase()); |
|
82 |
} |
|
83 |
}); |
|
84 |
|
|
85 |
return cn; |
|
66 |
} |
|
86 | 67 |
} |
87 | 68 |
|
69 |
private ClassNode getOrCreateClassInfo(String name, String url) { |
|
70 |
ClassNode cn = this.allClasses.get(name); |
|
71 |
if( cn == null ) { |
|
72 |
cn = new ClassNode(name,url); |
|
73 |
this.allClasses.put(name,cn); |
|
74 |
} |
|
75 |
return cn; |
|
76 |
} |
|
77 |
|
|
78 |
public ClassNode getHelpClass(String name) { |
|
79 |
return this.allClasses.get(name); |
|
80 |
} |
|
81 |
|
|
82 |
public List<ScriptingHelpClass> getHelpClasses(){ |
|
83 |
List<ScriptingHelpClass> classes = new ArrayList<ScriptingHelpClass>(this.allClasses.values()); |
|
84 |
Collections.sort(classes); |
|
85 |
return classes; |
|
86 |
} |
|
87 |
|
|
88 |
public ScriptingHelpClass add(String className, String target, List<String> methods) { |
|
89 |
ClassNode cn = this.getOrCreateClassInfo(className, target); |
|
90 |
for (String methodName : methods) { |
|
91 |
boolean isFound = false; |
|
92 |
for (ScriptingHelpMethod mn : this) { |
|
93 |
if (mn.getName().equals(methodName)) { |
|
94 |
((MethodNode) mn).add(cn); |
|
95 |
cn.addMethod(((MethodNode) mn).getName()); |
|
96 |
isFound = true; |
|
97 |
} |
|
98 |
} |
|
99 |
if (!isFound) { |
|
100 |
this.add(new MethodNode(methodName, cn)); |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
Collections.sort(this, new Comparator<ScriptingHelpMethod>() { |
|
105 |
public int compare(ScriptingHelpMethod p1, ScriptingHelpMethod p2) { |
|
106 |
return p1.getName().toLowerCase().compareTo(p2.getName().toLowerCase()); |
|
107 |
} |
|
108 |
}); |
|
109 |
|
|
110 |
return cn; |
|
111 |
} |
|
112 |
|
|
88 | 113 |
public void printMethodList(Map<String, MethodNode> map) { |
89 | 114 |
Set<String> keys = map.keySet(); |
90 | 115 |
Iterator<String> it = keys.iterator(); |
91 |
while(it.hasNext()){
|
|
92 |
MethodNode mn = map.get(it.next());
|
|
116 |
while (it.hasNext()) {
|
|
117 |
MethodNode mn = map.get(it.next());
|
|
93 | 118 |
Iterator<ScriptingHelpClass> i = mn.iterator(); |
94 |
System.out.println(mn); |
|
95 |
while (i.hasNext()){ |
|
96 |
ClassNode cn = (ClassNode)i.next(); |
|
97 |
System.out.println(" ->"+cn.className); |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
public Map<String,ScriptingHelpMethod> findMethods(String text) { |
|
119 |
System.out.println(mn); |
|
120 |
while (i.hasNext()) { |
|
121 |
ClassNode cn = (ClassNode) i.next(); |
|
122 |
System.out.println(" ->" + cn.className); |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
103 | 126 |
|
104 |
Map<String,ScriptingHelpMethod> matches = new HashMap<String,ScriptingHelpMethod>(); |
|
105 |
List<ScriptingHelpMethod> l = this; |
|
106 |
if(text != null ){ |
|
107 |
for (ScriptingHelpMethod mn : l) { |
|
108 |
if(mn.getName().toLowerCase().contains(text.toLowerCase())){ |
|
109 |
matches.put(mn.getName(),mn); |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
return matches; |
|
114 |
} |
|
115 |
|
|
116 |
private static class MethodNode implements ScriptingHelpMethod { |
|
117 |
/** |
|
118 |
* |
|
119 |
*/ |
|
120 |
private static final long serialVersionUID = -7155323542211180518L; |
|
121 |
private final String methodName; |
|
122 |
private final List<ScriptingHelpClass> classes; |
|
123 |
|
|
124 |
|
|
125 |
MethodNode(String name, ScriptingHelpClass c){ |
|
126 |
this.methodName = name; |
|
127 |
this.classes = new ArrayList<ScriptingHelpClass>(); |
|
128 |
if(c!=null){ |
|
129 |
classes.add(c); |
|
130 |
} |
|
131 |
} |
|
132 |
|
|
133 |
MethodNode add(ClassNode c){ |
|
134 |
this.classes.add(c); |
|
135 |
return this; |
|
136 |
} |
|
137 |
|
|
138 |
public String getName(){ |
|
139 |
return this.methodName; |
|
140 |
} |
|
127 |
public Map<String, ScriptingHelpMethod> findMethods(String text) { |
|
141 | 128 |
|
142 |
public Iterator<ScriptingHelpClass> iterator() { |
|
143 |
return this.classes.iterator(); |
|
144 |
} |
|
145 |
public String toString() { |
|
146 |
return getName(); |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
private static class ClassNode implements ScriptingHelpClass{ |
|
151 |
String className; |
|
152 |
String classUrl; |
|
153 |
|
|
154 |
ClassNode(String name, String url){ |
|
155 |
this.className = name; |
|
156 |
this.classUrl = url; |
|
157 |
} |
|
158 |
|
|
159 |
public String getName(){ |
|
160 |
return this.className; |
|
161 |
} |
|
162 |
|
|
163 |
public String getUrl(){ |
|
164 |
return this.classUrl; |
|
165 |
} |
|
166 |
public String toString() { |
|
167 |
return getName(); |
|
168 |
} |
|
169 |
} |
|
129 |
Map<String, ScriptingHelpMethod> matches = new HashMap<String, ScriptingHelpMethod>(); |
|
130 |
List<ScriptingHelpMethod> l = this; |
|
131 |
if (text != null) { |
|
132 |
for (ScriptingHelpMethod mn : l) { |
|
133 |
if (mn.getName().toLowerCase().contains(text.toLowerCase())) { |
|
134 |
matches.put(mn.getName(), mn); |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
return matches; |
|
139 |
} |
|
170 | 140 |
|
171 |
public ScriptingHelpClass createHelpClass(String name, String target) { |
|
172 |
return new ClassNode(name, target); |
|
173 |
} |
|
141 |
private static class MethodNode implements ScriptingHelpMethod { |
|
142 |
|
|
143 |
/** |
|
144 |
* |
|
145 |
*/ |
|
146 |
private static final long serialVersionUID = -7155323542211180518L; |
|
147 |
private final String methodName; |
|
148 |
private final List<ScriptingHelpClass> classes; |
|
149 |
|
|
150 |
MethodNode(String name, ScriptingHelpClass c) { |
|
151 |
this.methodName = name; |
|
152 |
this.classes = new ArrayList<ScriptingHelpClass>(); |
|
153 |
if (c != null) { |
|
154 |
classes.add(c); |
|
155 |
} |
|
156 |
} |
|
157 |
|
|
158 |
MethodNode add(ClassNode c) { |
|
159 |
this.classes.add(c); |
|
160 |
return this; |
|
161 |
} |
|
162 |
|
|
163 |
public String getName() { |
|
164 |
return this.methodName; |
|
165 |
} |
|
166 |
|
|
167 |
public Iterator<ScriptingHelpClass> iterator() { |
|
168 |
return this.classes.iterator(); |
|
169 |
} |
|
170 |
|
|
171 |
public String toString() { |
|
172 |
return getName(); |
|
173 |
} |
|
174 |
} |
|
175 |
|
|
176 |
private static class ClassNode implements ScriptingHelpClass, Comparable { |
|
177 |
|
|
178 |
String className; |
|
179 |
String classUrl; |
|
180 |
Set<String> methods = new HashSet<String>(); |
|
181 |
|
|
182 |
ClassNode(String name, String url) { |
|
183 |
this.className = name; |
|
184 |
this.classUrl = url; |
|
185 |
} |
|
186 |
|
|
187 |
public String getName() { |
|
188 |
return this.className; |
|
189 |
} |
|
190 |
|
|
191 |
public String getUrl() { |
|
192 |
return this.classUrl; |
|
193 |
} |
|
194 |
|
|
195 |
public String toString() { |
|
196 |
return getName(); |
|
197 |
} |
|
198 |
|
|
199 |
public void addMethod(String name) { |
|
200 |
this.methods.add(name); |
|
201 |
} |
|
202 |
|
|
203 |
public List<String> getMethods() { |
|
204 |
List l = new ArrayList(this.methods); |
|
205 |
Collections.sort(l); |
|
206 |
return l; |
|
207 |
} |
|
208 |
|
|
209 |
@Override |
|
210 |
public int compareTo(Object o) { |
|
211 |
return this.className.compareToIgnoreCase( ((ClassNode)o).getName() ); |
|
212 |
} |
|
213 |
} |
|
214 |
|
|
215 |
// public ScriptingHelpClass createHelpClass(String name, String target) { |
|
216 |
// return new ClassNode(name, target); |
|
217 |
// } |
|
174 | 218 |
} |
Also available in: Unified diff