Révision 805
tmp/org.txm.searchengine.cqp.core/src/org/txm/importer/cwb/BuildCwbEncodeArgsFromCQP.java (revision 805) | ||
---|---|---|
69 | 69 |
int nWordProps = 0; |
70 | 70 |
|
71 | 71 |
/** |
72 |
* pattern to find structure attributes |
|
73 |
*/ |
|
74 |
public static final Pattern p = Pattern.compile("(([^= ]+)=\"([^\"]+)\")"); |
|
75 |
|
|
76 |
/** |
|
72 | 77 |
* process !!!. |
73 | 78 |
* |
74 |
* @param cqpfile the xmlfile |
|
75 |
* @param wordstag the wordstag |
|
79 |
* @param cqpfile the input file in CQP format |
|
76 | 80 |
* @return true if successful |
77 | 81 |
* @throws XMLStreamException the xML stream exception |
78 | 82 |
* @throws FactoryConfigurationError the factory configuration error |
... | ... | |
90 | 94 |
String line = reader.readLine(); |
91 | 95 |
String localname; |
92 | 96 |
|
93 |
|
|
94 |
Pattern p = Pattern.compile("(([^= ]+)=\"([^\"]+)\")"); |
|
95 | 97 |
//for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { |
96 | 98 |
for(; line != null ; line = reader.readLine()) { |
97 | 99 |
|
tmp/org.txm.rcp/src/main/java/org/txm/rcp/utils/GSERunner.java (revision 805) | ||
---|---|---|
1 | 1 |
package org.txm.rcp.utils; |
2 | 2 |
|
3 |
import groovy.lang.Binding; |
|
4 |
import groovy.util.GroovyScriptEngine; |
|
5 |
import groovy.util.ResourceException; |
|
6 |
import groovy.util.ScriptException; |
|
7 |
|
|
8 |
import java.io.File; |
|
9 |
import java.io.FilenameFilter; |
|
3 | 10 |
import java.io.IOException; |
11 |
import java.net.MalformedURLException; |
|
12 |
import java.net.URL; |
|
4 | 13 |
import java.util.HashMap; |
5 | 14 |
import java.util.HashSet; |
6 | 15 |
import java.util.Map; |
7 | 16 |
|
17 |
import org.codehaus.groovy.control.CompilerConfiguration; |
|
18 |
import org.codehaus.groovy.control.customizers.ImportCustomizer; |
|
8 | 19 |
import org.eclipse.core.internal.runtime.InternalPlatform; |
9 |
import org.eclipse.core.runtime.FileLocator; |
|
10 | 20 |
import org.osgi.framework.Bundle; |
11 | 21 |
import org.osgi.framework.BundleContext; |
12 | 22 |
import org.osgi.framework.wiring.BundleWiring; |
23 |
import org.txm.Toolbox; |
|
13 | 24 |
import org.txm.rcp.TXMClassLoader; |
14 | 25 |
import org.txm.utils.logger.Log; |
15 | 26 |
|
16 |
import groovy.lang.Binding; |
|
17 |
import groovy.util.GroovyScriptEngine; |
|
18 |
import groovy.util.ResourceException; |
|
19 |
import groovy.util.ScriptException; |
|
20 |
|
|
21 | 27 |
/** |
22 | 28 |
* gse wrapper for TXM scripts and macros |
23 | 29 |
* |
... | ... | |
30 | 36 |
super(urls, getTXMClassLoader()); |
31 | 37 |
} |
32 | 38 |
|
39 |
protected static GSERunner defaultGSE; |
|
40 |
protected static String defaultScriptRootDir = ""; |
|
41 |
|
|
42 |
public static void clearDefaultGSE() { |
|
43 |
defaultGSE = null; |
|
44 |
defaultScriptRootDir = ""; |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* Build a GSERunner with default paths : user, macro, import and library path |
|
49 |
* |
|
50 |
* @param scriptRootDir if null uses Toolbox home directory |
|
51 |
* @param script |
|
52 |
* @return |
|
53 |
*/ |
|
54 |
public static GSERunner buildDefaultGSE(String scriptRootDir, File script) { |
|
55 |
if (defaultScriptRootDir.equals(scriptRootDir) && defaultGSE != null) { |
|
56 |
try { |
|
57 |
defaultGSE.getGroovyClassLoader().addURL(new URL("file://"+script.getParent())); |
|
58 |
} catch (MalformedURLException e) { |
|
59 |
// TODO Auto-generated catch block |
|
60 |
e.printStackTrace(); |
|
61 |
} |
|
62 |
return defaultGSE; |
|
63 |
} |
|
64 |
|
|
65 |
if (scriptRootDir == null) { |
|
66 |
scriptRootDir = new File(Toolbox.getTXMHOMEPATH(), "scripts").getAbsolutePath(); |
|
67 |
} |
|
68 |
String[] roots = new String[] { |
|
69 |
scriptRootDir+"/user/", //$NON-NLS-1$ |
|
70 |
scriptRootDir+"/macro/", //$NON-NLS-1$ |
|
71 |
scriptRootDir+"/import/", //$NON-NLS-1$ |
|
72 |
script.getParent(), |
|
73 |
}; |
|
74 |
|
|
75 |
try { |
|
76 |
defaultScriptRootDir = scriptRootDir; |
|
77 |
defaultGSE = new GSERunner(roots); |
|
78 |
} catch (IOException e) { |
|
79 |
e.printStackTrace(); |
|
80 |
return null; |
|
81 |
} |
|
82 |
|
|
83 |
ImportCustomizer imports = new ImportCustomizer(); |
|
84 |
imports.addStarImports("org.txm.rcp.utils", "org.txm.utils", "org.txm", "org.kohsuke.args4j", "org.txm.rcp.swt.widget.parameters"); |
|
85 |
imports.addImports("groovy.transform.Field"); |
|
86 |
CompilerConfiguration configuration = new CompilerConfiguration(); |
|
87 |
configuration.addCompilationCustomizers(imports); |
|
88 |
|
|
89 |
defaultGSE.setConfig(configuration); |
|
90 |
|
|
91 |
File jardir = new File(scriptRootDir,"lib"); //$NON-NLS-1$ |
|
92 |
if (jardir.exists() && jardir.isDirectory()) { |
|
93 |
for (File f: jardir.listFiles(new FilenameFilter() { |
|
94 |
@Override |
|
95 |
public boolean accept(File dir, String name) { |
|
96 |
return name.endsWith(".jar"); //$NON-NLS-1$ |
|
97 |
} |
|
98 |
})) |
|
99 |
{ |
|
100 |
try { |
|
101 |
defaultGSE.getGroovyClassLoader().addURL(f.toURI().toURL()); |
|
102 |
} catch (MalformedURLException e) { |
|
103 |
// TODO Auto-generated catch block |
|
104 |
e.printStackTrace(); |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
defaultGSE.getGroovyClassLoader().addClasspath(scriptRootDir); // for the .class files if any |
|
109 |
|
|
110 |
return defaultGSE; |
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* |
|
115 |
* @return a class loader containing all bundles activated in TXM |
|
116 |
*/ |
|
33 | 117 |
protected static ClassLoader getTXMClassLoader() { |
34 | 118 |
HashSet<ClassLoader> loaders = new HashSet<ClassLoader>(); |
35 | 119 |
BundleContext bundleContext = InternalPlatform.getDefault().getBundleContext(); |
... | ... | |
71 | 155 |
|
72 | 156 |
/** |
73 | 157 |
* Convenience method exactly the same as calling |
74 |
* run(class, ["args":args])
|
|
158 |
* run(clazz, ["args":args])
|
|
75 | 159 |
* |
76 | 160 |
* @param clazz |
77 | 161 |
* @param args |
... | ... | |
88 | 172 |
|
89 | 173 |
/** |
90 | 174 |
* Convenience method exactly the same as calling |
91 |
* run(class, ["args":["var":"val, ...]])
|
|
175 |
* run(clazz, ["args":[:]])
|
|
92 | 176 |
* |
93 | 177 |
* @param clazz |
94 | 178 |
* @param args |
... | ... | |
102 | 186 |
arrangedMap.put("args", new HashMap<String, Object>()); |
103 | 187 |
return run(clazz.getCanonicalName().replace(".", "/")+".groovy", new Binding(arrangedMap)); |
104 | 188 |
} |
105 |
|
|
106 |
/** |
|
107 |
* Creates a new GSERunner and adds to it the class loaders of all installed bundles of the platform. |
|
108 |
* @return a new GSERunner |
|
109 |
*/ |
|
110 |
public static GSERunner createGSERunner(String[] urls) { |
|
111 |
|
|
112 |
GSERunner gse = null; |
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
return gse; |
|
117 |
} |
|
118 |
|
|
119 | 189 |
} |
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteGroovyScript.java (revision 805) | ||
---|---|---|
38 | 38 |
import java.util.Iterator; |
39 | 39 |
import java.util.List; |
40 | 40 |
|
41 |
import org.codehaus.groovy.control.CompilerConfiguration; |
|
42 |
import org.codehaus.groovy.control.customizers.ImportCustomizer; |
|
41 | 43 |
import org.eclipse.core.commands.AbstractHandler; |
42 | 44 |
import org.eclipse.core.commands.ExecutionEvent; |
43 | 45 |
import org.eclipse.core.commands.ExecutionException; |
... | ... | |
218 | 220 |
final String relativepath = path.substring(dir.length()+1); |
219 | 221 |
|
220 | 222 |
//if (gse == null) { |
221 |
gse = buildGSE(scriptsRootDirectories, scriptfile);
|
|
223 |
gse = GSERunner.buildDefaultGSE(scriptsRootDirectories, scriptfile);
|
|
222 | 224 |
if (gse == null) { |
223 | 225 |
System.out.println("Could not create the Groovy script engine. Aborting."); |
224 | 226 |
return; |
... | ... | |
344 | 346 |
* {org.txm.rcp.utils.Logger.printStackTrace(e);} |
345 | 347 |
*/ |
346 | 348 |
} |
347 |
|
|
348 |
public static GSERunner buildGSE(String scriptRootDir, File script) { |
|
349 |
|
|
350 |
GSERunner gse; |
|
351 |
|
|
352 |
String[] roots = new String[] { |
|
353 |
scriptRootDir+"/user/", //$NON-NLS-1$ |
|
354 |
scriptRootDir+"/macro/", //$NON-NLS-1$ |
|
355 |
scriptRootDir+"/import/", //$NON-NLS-1$ |
|
356 |
script.getParent(), |
|
357 |
}; |
|
358 |
|
|
359 |
// HashMap<String, Bundle> bundleWithActivators = new HashMap<String, Bundle>(); |
|
360 |
// HashSet<ClassLoader> loaders = new HashSet<ClassLoader>(); |
|
361 |
// BundleContext bundleContext = InternalPlatform.getDefault().getBundleContext(); |
|
362 |
// for (Bundle b : bundleContext.getBundles()) { |
|
363 |
// String acName = b.getHeaders().get("Bundle-Activator"); |
|
364 |
// //System.out.println(b.getSymbolicName()+":"+b.getHeaders()); |
|
365 |
// if (acName != null) { |
|
366 |
// if (!bundleWithActivators.containsKey(b.getSymbolicName())) { |
|
367 |
// bundleWithActivators.put(b.getSymbolicName(),b); |
|
368 |
// } else { |
|
369 |
// Bundle b2 = bundleWithActivators.get(b.getSymbolicName()); |
|
370 |
// if (b2.getVersion().compareTo(b.getVersion()) < 0) { |
|
371 |
// bundleWithActivators.put(b.getSymbolicName(),b); |
|
372 |
// } |
|
373 |
// } |
|
374 |
// } |
|
375 |
// } |
|
376 |
// |
|
377 |
// for (Bundle b : bundleWithActivators.values()) { |
|
378 |
// try { |
|
379 |
// String acName = b.getHeaders().get("Bundle-Activator"); |
|
380 |
// Class<?> ac = b.loadClass(acName); |
|
381 |
// loaders.add(ac.getClassLoader()); |
|
382 |
// } catch (Throwable e) { |
|
383 |
// System.out.println("Bundle Activator error: "+e); |
|
384 |
// } |
|
385 |
// } |
|
386 |
// Log.info("Initialized TXMClassLoader with "+loaders.size()+ " bundles."); |
|
387 |
// TXMClassLoader scl = new TXMClassLoader(loaders); |
|
388 |
|
|
389 |
try { |
|
390 |
gse = new GSERunner(roots); |
|
391 |
} catch (IOException e) { |
|
392 |
e.printStackTrace(); |
|
393 |
return null; |
|
394 |
} |
|
395 |
|
|
396 |
File jardir = new File(scriptRootDir,"lib"); //$NON-NLS-1$ |
|
397 |
if (jardir.exists() && jardir.isDirectory()) { |
|
398 |
for (File f: jardir.listFiles(new FilenameFilter() { |
|
399 |
@Override |
|
400 |
public boolean accept(File dir, String name) { |
|
401 |
return name.endsWith(".jar"); //$NON-NLS-1$ |
|
402 |
} |
|
403 |
})) |
|
404 |
{ |
|
405 |
try { |
|
406 |
gse.getGroovyClassLoader().addURL(f.toURI().toURL()); |
|
407 |
} catch (MalformedURLException e) { |
|
408 |
// TODO Auto-generated catch block |
|
409 |
e.printStackTrace(); |
|
410 |
} |
|
411 |
} |
|
412 |
} |
|
413 |
gse.getGroovyClassLoader().addClasspath(scriptRootDir); // for the .class files if any |
|
414 |
|
|
415 |
return gse; |
|
416 |
} |
|
417 | 349 |
} |
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/ExecuteImportScript.java (revision 805) | ||
---|---|---|
221 | 221 |
monitor.worked(5); |
222 | 222 |
|
223 | 223 |
// if (ExecuteImportScript.gse == null) |
224 |
ExecuteImportScript.gse = ExecuteGroovyScript.buildGSE(scriptRootDir, script);
|
|
224 |
ExecuteImportScript.gse = GSERunner.buildDefaultGSE(scriptRootDir, script);
|
|
225 | 225 |
|
226 | 226 |
|
227 | 227 |
Binding binding = new Binding(); |
Formats disponibles : Unified diff