1 package com.genimen.djeneric.tools.generator.util; 2 3 import java.io.File ; 4 import java.util.ArrayList ; 5 6 import com.genimen.djeneric.util.DjFileFilter; 7 8 public class TemplateSet 9 { 10 String _location; 11 ArrayList _allTemplatesWithErrors = new ArrayList (); 12 String _loadLog = ""; 13 14 public TemplateSet(String location) 15 { 16 _location = location; 17 } 18 19 public void clear() 20 { 21 _loadLog = ""; 22 _allTemplatesWithErrors = new ArrayList (); 23 _loadLog = ""; 24 } 25 26 public Template[] getTemplatesFor(String objectType) 27 { 28 Template[] tpl = getTemplates(); 29 ArrayList result = new ArrayList (); 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 result = new ArrayList (); 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 loadLog = new StringBuffer (1000); 57 File rootDir = new File (getDirectory()); 58 File [] 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 result = new ArrayList (); 67 _allTemplatesWithErrors = new ArrayList (); 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 toString() 91 { 92 return _location; 93 } 94 95 public String 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 getName() 105 { 106 String name = getFileName(); 107 int idx = name.lastIndexOf("."); 108 if (idx != -1) name = name.substring(0, idx); 109 return name; 110 } 111 112 public String 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 [] getTemplatesWithErrors() 122 { 123 return (String []) _allTemplatesWithErrors.toArray(new String [0]); 124 } 125 126 public String getLoadLog() 127 { 128 return _loadLog; 129 } 130 131 } | Popular Tags |