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