]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/ant/AntRegistryUtil.java
Introduce directory based structure
[gpl/argeo-slc.git] / org.argeo.slc / src / main / java / org / argeo / slc / ant / AntRegistryUtil.java
1 package org.argeo.slc.ant;
2
3 import java.io.File;
4 import java.util.List;
5 import java.util.Vector;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.apache.tools.ant.Project;
10 import org.apache.tools.ant.ProjectHelper;
11
12 import org.argeo.slc.core.structure.StructurePath;
13 import org.argeo.slc.core.structure.StructureRegistry;
14
15 /** Utilities to manipulate the structure registry in SLC Ant. */
16 public class AntRegistryUtil {
17 private static Log log = LogFactory.getLog(AntRegistryUtil.class);
18
19 /** Read a structure registry from an Ant file without executing it. */
20 public static StructureRegistry readRegistry(File antFile) {
21
22 Project p = new Project();
23 p.setUserProperty("ant.file", antFile.getAbsolutePath());
24 p.setBaseDir(antFile.getParentFile());
25 p.init();
26 ProjectHelper helper = new SlcProjectHelper();
27 p.addReference("ant.projectHelper", helper);
28 helper.parse(p, antFile);
29
30 StructureRegistry registry = (StructureRegistry) p
31 .getReference(SlcProjectHelper.REF_STRUCTURE_REGISTRY);
32 registry.setMode(StructureRegistry.READ);
33
34 p.executeTarget(p.getDefaultTarget());
35 return registry;
36 }
37
38 /** Execute only the active paths of teh Ant files. */
39 public static void runActive(File antFile, List<StructurePath> activePaths) {
40
41 Project p = new Project();
42 p.setUserProperty("ant.file", antFile.getAbsolutePath());
43 p.setBaseDir(antFile.getParentFile());
44 p.init();
45 ProjectHelper helper = new SlcProjectHelper();
46 p.addReference("ant.projectHelper", helper);
47 helper.parse(p, antFile);
48
49 StructureRegistry registry = (StructureRegistry) p
50 .getReference(SlcProjectHelper.REF_STRUCTURE_REGISTRY);
51 registry.setMode(StructureRegistry.ACTIVE);
52 registry.setActivePaths(activePaths);
53 p.executeTarget(p.getDefaultTarget());
54 }
55
56 public static void main(String[] args) {
57 File antFile = new File(
58 "C:/dev/workspaces/default/org.argeo.slc/src/test/slc/root/Category1/SubCategory2/build.xml");
59 StructureRegistry registry = AntRegistryUtil.readRegistry(antFile);
60
61 StringBuffer buf = new StringBuffer("");
62
63 int count = 0;
64 List<StructurePath> activePaths = new Vector<StructurePath>();
65 for (StructurePath path : registry.listPaths()) {
66 buf.append(path);
67 if (count != 0 && count % 3 == 0) {
68 // skip
69 } else {
70 activePaths.add(path);
71 buf.append(" <");
72 }
73 buf.append('\n');
74 count++;
75 }
76 log.info(buf);
77
78 runActive(antFile, activePaths);
79
80 }
81 }