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