gvsig-scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.lib / org.gvsig.scripting.lib.impl / src / main / java / org / gvsig / scripting / impl / MethodList.java @ 212
History | View | Annotate | Download (4.47 KB)
1 |
package org.gvsig.scripting.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.net.URL; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Collections; |
7 |
import java.util.Comparator; |
8 |
import java.util.HashMap; |
9 |
import java.util.Iterator; |
10 |
import java.util.List; |
11 |
import java.util.Map; |
12 |
import java.util.Set; |
13 |
import java.util.regex.Matcher; |
14 |
import java.util.regex.Pattern; |
15 |
|
16 |
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpClass; |
17 |
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpMethod; |
18 |
|
19 |
public class MethodList extends ArrayList<ScriptingHelpMethod> { |
20 |
|
21 |
/**
|
22 |
*
|
23 |
*/
|
24 |
private static final long serialVersionUID = 2097121200002178537L; |
25 |
|
26 |
public MethodList() {
|
27 |
} |
28 |
|
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=\"[^\"]*\" />"); |
35 |
|
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); |
45 |
|
46 |
Matcher classMatcher = classPattern.matcher(methodExtract);
|
47 |
while(classMatcher.find()){
|
48 |
String[] classnode = (methodExtract.substring(classMatcher.start(), classMatcher.end())).split("\""); |
49 |
|
50 |
if(classnode.length>3){ |
51 |
ClassNode cn = new ClassNode(classnode[1],classnode[3]); |
52 |
mn.add(cn); |
53 |
} |
54 |
|
55 |
} |
56 |
this.add(mn);
|
57 |
|
58 |
} |
59 |
} |
60 |
|
61 |
} |
62 |
} |
63 |
|
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;
|
86 |
} |
87 |
|
88 |
public void printMethodList(Map<String, MethodNode> map) { |
89 |
Set<String> keys = map.keySet(); |
90 |
Iterator<String> it = keys.iterator(); |
91 |
while(it.hasNext()){
|
92 |
MethodNode mn = map.get(it.next()); |
93 |
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) { |
103 |
|
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 |
} |
141 |
|
142 |
public Iterator<ScriptingHelpClass> iterator() { |
143 |
return this.classes.iterator(); |
144 |
} |
145 |
|
146 |
} |
147 |
|
148 |
private static class ClassNode implements ScriptingHelpClass{ |
149 |
String className;
|
150 |
String classUrl;
|
151 |
|
152 |
ClassNode(String name, String url){ |
153 |
this.className = name;
|
154 |
this.classUrl = url;
|
155 |
} |
156 |
|
157 |
public String getName(){ |
158 |
return this.className; |
159 |
} |
160 |
|
161 |
public String getUrl(){ |
162 |
return this.classUrl; |
163 |
} |
164 |
} |
165 |
|
166 |
public ScriptingHelpClass createHelpClass(String name, String target) { |
167 |
return new ClassNode(name, target); |
168 |
} |
169 |
} |