KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > util > TemplateSetManager


1 package com.genimen.djeneric.tools.generator.util;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.StringTokenizer JavaDoc;
6
7 import com.genimen.djeneric.util.DjFileFilter;
8
9 public class TemplateSetManager
10 {
11   ArrayList JavaDoc _knownTemplateSets = new ArrayList JavaDoc();
12
13   public final static String JavaDoc TEMPLATE_SET_EXTENSION = "dts";
14   public final static String JavaDoc TEMPLATE_EXTENSION = "tpl";
15
16   public TemplateSetManager()
17   {
18   }
19
20   public void rescanTemplateSets(String JavaDoc paths)
21   {
22     StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(paths, ";");
23     _knownTemplateSets = new ArrayList JavaDoc();
24
25     while (tok.hasMoreElements())
26     {
27       String JavaDoc path = tok.nextToken();
28       scanPath(path, _knownTemplateSets);
29     }
30   }
31
32   public TemplateSet[] getTemplateSets()
33   {
34     return (TemplateSet[]) _knownTemplateSets.toArray(new TemplateSet[0]);
35   }
36
37   public Template[] getTemplates(String JavaDoc[] templateSetNames)
38   {
39     ArrayList JavaDoc result = new ArrayList JavaDoc();
40
41     for (int t = 0; t < templateSetNames.length; t++)
42       for (int i = 0; i < _knownTemplateSets.size(); i++)
43       {
44         TemplateSet ts = (TemplateSet) _knownTemplateSets.get(i);
45
46         if (ts.getName().equalsIgnoreCase(templateSetNames[t]))
47         {
48           Template[] subset = ts.getTemplates();
49           for (int s = 0; s < subset.length; s++)
50             result.add(subset[s]);
51         }
52       }
53     return (Template[]) result.toArray(new Template[0]);
54   }
55
56   public String JavaDoc[] getTemplatesWithErrors()
57   {
58     ArrayList JavaDoc result = new ArrayList JavaDoc();
59
60     for (int i = 0; i < _knownTemplateSets.size(); i++)
61     {
62       TemplateSet ts = (TemplateSet) _knownTemplateSets.get(i);
63       String JavaDoc[] subset = ts.getTemplatesWithErrors();
64       for (int s = 0; s < subset.length; s++)
65         result.add(subset[s]);
66     }
67     return (String JavaDoc[]) result.toArray(new String JavaDoc[0]);
68   }
69
70   public String JavaDoc getLoadLog()
71   {
72     StringBuffer JavaDoc log = new StringBuffer JavaDoc();
73
74     for (int i = 0; i < _knownTemplateSets.size(); i++)
75     {
76       TemplateSet ts = (TemplateSet) _knownTemplateSets.get(i);
77       log.append(ts.getLoadLog());
78     }
79     return log.toString().trim();
80   }
81
82   private void scanPath(String JavaDoc path, ArrayList JavaDoc known)
83   {
84     File JavaDoc file = new File JavaDoc(path);
85     if (!file.isDirectory()) return;
86
87     File JavaDoc[] sets = file.listFiles(new DjFileFilter(TEMPLATE_SET_EXTENSION));
88     for (int i = 0; i < sets.length; i++)
89     {
90       if (sets[i].isDirectory()) scanPath(sets[i].getAbsolutePath(), known);
91       else known.add(new TemplateSet(sets[i].getAbsolutePath()));
92     }
93   }
94
95 }
Popular Tags