KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.genimen.djeneric.tools.generator.util;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5
6 import com.genimen.djeneric.util.DjFileFilter;
7
8 public class TemplateSet
9 {
10   String JavaDoc _location;
11   ArrayList JavaDoc _allTemplatesWithErrors = new ArrayList JavaDoc();
12   String JavaDoc _loadLog = "";
13
14   public TemplateSet(String JavaDoc location)
15   {
16     _location = location;
17   }
18
19   public void clear()
20   {
21     _loadLog = "";
22     _allTemplatesWithErrors = new ArrayList JavaDoc();
23     _loadLog = "";
24   }
25
26   public Template[] getTemplatesFor(String JavaDoc objectType)
27   {
28     Template[] tpl = getTemplates();
29     ArrayList JavaDoc result = new ArrayList JavaDoc();
30     for (int i = 0; i < tpl.length; i++)
31     {
32       if (tpl[i].isValid() && tpl[i].isRootBased() && objectType.equals(tpl[i].getObjectType()))
33       {
34         result.add(tpl[i]);
35       }
36     }
37     return (Template[]) result.toArray(new Template[0]);
38   }
39
40   public Template[] getGlobalTemplates()
41   {
42     Template[] tpl = getTemplates();
43     ArrayList JavaDoc result = new ArrayList JavaDoc();
44     for (int i = 0; i < tpl.length; i++)
45     {
46       if (tpl[i].isValid() && !tpl[i].isRootBased())
47       {
48         result.add(tpl[i]);
49       }
50     }
51     return (Template[]) result.toArray(new Template[0]);
52   }
53
54   public Template[] getTemplates()
55   {
56     StringBuffer JavaDoc loadLog = new StringBuffer JavaDoc(1000);
57     File JavaDoc rootDir = new File JavaDoc(getDirectory());
58     File JavaDoc[] files = rootDir.listFiles(new DjFileFilter(TemplateSetManager.TEMPLATE_EXTENSION));
59
60     if (files == null)
61     {
62       System.err.println("Template " + getName() + " has an invalid location: " + getDirectory());
63       return new Template[0];
64     }
65
66     ArrayList JavaDoc result = new ArrayList JavaDoc();
67     _allTemplatesWithErrors = new ArrayList JavaDoc();
68
69     for (int i = 0; i < files.length; i++)
70     {
71       if (files[i].isDirectory()) continue;
72
73       result.add(new Template(this, files[i].getAbsolutePath()));
74     }
75
76     for (int i = 0; i < result.size(); i++)
77     {
78       Template t = (Template) result.get(i);
79       if (!t.isValid())
80       {
81         _allTemplatesWithErrors.add(t);
82         loadLog.append(t.getLoadLog());
83       }
84     }
85
86     _loadLog = loadLog.toString();
87     return (Template[]) result.toArray(new Template[0]);
88   }
89
90   public String JavaDoc toString()
91   {
92     return _location;
93   }
94
95   public String JavaDoc getFileName()
96   {
97     int idx = _location.lastIndexOf("/");
98     int idx2 = _location.lastIndexOf("\\");
99     if (idx2 > idx) idx = idx2;
100
101     return _location.substring(idx + 1);
102   }
103
104   public String JavaDoc getName()
105   {
106     String JavaDoc name = getFileName();
107     int idx = name.lastIndexOf(".");
108     if (idx != -1) name = name.substring(0, idx);
109     return name;
110   }
111
112   public String JavaDoc getDirectory()
113   {
114     int idx = _location.lastIndexOf("/");
115     int idx2 = _location.lastIndexOf("\\");
116     if (idx2 > idx) idx = idx2;
117
118     return _location.substring(0, idx);
119   }
120
121   public String JavaDoc[] getTemplatesWithErrors()
122   {
123     return (String JavaDoc[]) _allTemplatesWithErrors.toArray(new String JavaDoc[0]);
124   }
125
126   public String JavaDoc getLoadLog()
127   {
128     return _loadLog;
129   }
130
131 }
Popular Tags