1 30 package com.genimen.djeneric.tools.modeler.util; 31 32 import java.io.BufferedReader ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.InputStreamReader ; 36 import java.util.HashMap ; 37 import java.util.StringTokenizer ; 38 import java.util.Vector ; 39 40 import com.genimen.djeneric.language.Messages; 41 import com.genimen.djeneric.repository.DjDomain; 42 import com.genimen.djeneric.repository.DjExtent; 43 import com.genimen.djeneric.repository.DjPersistenceManager; 44 import com.genimen.djeneric.repository.DjProperty; 45 import com.genimen.djeneric.repository.DjPropertyType; 46 import com.genimen.djeneric.repository.DjRelation; 47 import com.genimen.djeneric.repository.exceptions.CatalogException; 48 49 public class ModelChecker 50 { 51 public final static String indent = " "; 52 public final static String RESERVED_WORDS_FILE = "reservedWords.properties"; 53 public final static String ASCII_CHARSET = "US-ASCII"; 54 HashMap _reservedWords; 55 56 private String _checkReport = null; 57 58 public ModelChecker() throws IOException 59 { 60 _reservedWords = loadReservedWords(); 61 } 62 63 public boolean check(DjPersistenceManager mgr) 64 { 65 boolean allOk = true; 66 DjExtent[] extents = mgr.getExtentsSorted(); 67 StringBuffer sb = new StringBuffer (); 68 69 try 70 { 71 mgr.validateModel(true); 72 } 73 catch (Throwable t) 74 { 75 sb.append(t.getMessage() + "\n"); 76 allOk = false; 77 } 78 79 for (int i = 0; i < extents.length; i++) 80 { 81 sb.append("\n" + Messages.getString("ModelChecker.Extent", extents[i].getName()) + "\n"); 82 boolean ok = true; 83 84 ok &= checkExtent(mgr, extents[i], sb); 85 ok &= checkDomains(extents[i], sb); 86 ok &= checkRelprops(extents[i], sb); 87 ok &= checkRelations(extents[i], sb); 88 89 allOk &= ok; 90 91 if (ok) sb.append(Messages.getString("global.Ok") + "\n"); 92 } 93 _checkReport = sb.toString().trim(); 94 return allOk; 95 } 96 97 public String getReport() 98 { 99 return _checkReport; 100 } 101 102 protected HashMap loadReservedWords() throws IOException 103 { 104 InputStream inStream = ModelChecker.class.getResourceAsStream(RESERVED_WORDS_FILE); 105 HashMap result = new HashMap (); 106 if (inStream == null) 107 { 108 throw new IOException (Messages.getString("ModelChecker.CouldNotLoad", RESERVED_WORDS_FILE)); 109 } 110 BufferedReader reader = new BufferedReader (new InputStreamReader (inStream, ASCII_CHARSET)); 111 String line = null; 112 while ((line = reader.readLine()) != null) 113 { 114 line = line.trim().toLowerCase(); 115 StringTokenizer tok = new StringTokenizer (line, " ,\t\n\r"); 116 while (tok.hasMoreElements()) 117 { 118 String s = tok.nextToken(); 119 result.put(s, s); 120 } 121 } 122 reader.close(); 123 return result; 124 } 125 126 protected boolean isReservedWord(String word) 127 { 128 return _reservedWords.containsKey(word.toLowerCase()); 129 } 130 131 protected boolean checkExtent(DjPersistenceManager mgr, DjExtent extent, StringBuffer sb) 132 { 133 boolean allOk = true; 134 allOk &= checkExtentDescriptor(mgr, extent, sb); 135 if (isReservedWord(extent.getName())) 136 { 137 allOk = false; 138 sb.append(indent + Messages.getString("ModelChecker.ReservedName", extent.getName()) + "\n"); 139 } 140 141 if (isReservedWord(extent.getAlias())) 142 { 143 allOk = false; 144 sb.append(indent + Messages.getString("ModelChecker.ReservedAlias", extent.getAlias()) + "\n"); 145 } 146 DjProperty[] props = extent.getProperties(); 147 for (int i = 0; i < props.length; i++) 148 { 149 if (isReservedWord(props[i].getName())) 150 { 151 allOk = false; 152 sb.append(indent + Messages.getString("ModelChecker.ReservedWord", props[i].getName()) + "\n"); 153 } 154 if (isReservedWord(props[i].getAlias())) 155 { 156 allOk = false; 157 sb.append(indent 158 + Messages.getString("ModelChecker.ReservedPropAlias", props[i].getName(), props[i].getAlias()) 159 + "\n"); 160 } 161 } 162 163 Vector had = new Vector (); 164 DjRelation[] rels = extent.getDetailRelations(); 165 for (int i = 0; i < rels.length; i++) 166 { 167 for (int j = 0; j < rels.length; j++) 168 { 169 if (rels[i] != rels[j]) 170 { 171 if (rels[i].getName().equals(rels[j].getName()) && !had.contains(rels[j].getName())) 172 { 173 had.add(rels[j].getName()); 174 String msg = Messages.getString("ModelChecker.RelnameNotUniuqe", rels[i].getName()); 175 if (!rels[i].getMasterExtent().getName().equals(rels[j].getMasterExtent().getName())) 176 { 177 msg += " " 178 + Messages.getString("ModelChecker._MastersAre", rels[i].getMasterExtent().getName(), rels[j] 179 .getMasterExtent().getName()); 180 } 181 allOk = false; 182 sb.append(indent + msg + "\n"); 183 } 184 } 185 } 186 } 187 188 return allOk; 189 } 190 191 protected boolean checkExtentDescriptor(DjPersistenceManager mgr, DjExtent extent, StringBuffer sb) 192 { 193 boolean allOk = true; 194 try 195 { 196 extent.validateDescriptorExpression(); 197 } 198 catch (CatalogException onx) 199 { 200 sb.append(indent + Messages.getString("ModelChecker.DescriptorInvalid", onx.getMessage()) + "\n"); 201 return false; 202 } 203 return allOk; 204 } 205 206 protected boolean checkDomains(DjExtent extent, StringBuffer sb) 207 { 208 boolean allOk = true; 209 DjProperty[] props = extent.getPropertiesSorted(); 210 for (int i = 0; i < props.length; i++) 211 { 212 DjPropertyType tp = props[i].getType(); 213 if (tp.getLength() == 0 && tp.getTypeCode() != DjDomain.DATE_TYPE) 214 { 215 sb.append(indent + Messages.getString("ModelChecker.Property", props[i].getName(), tp.getTypeName())); 216 if (!tp.getNativeType().equals(tp.getTypeName())) sb.append("(" + tp.getNativeType() + ")"); 217 sb.append(Messages.getString("ModelChecker.CheckDomainDef") + "\n"); 218 allOk = false; 219 } 220 } 221 return allOk; 222 } 223 224 protected boolean checkRelprops(DjExtent extent, StringBuffer sb) 225 { 226 boolean allOk = true; 227 DjProperty[] props = extent.getPropertiesSorted(); 228 for (int i = 0; i < props.length; i++) 229 { 230 if (props[i].getMapping().startsWith(DjPersistenceManager.MAPPING_REL) 231 && extent.getMasterRelationByPropertyName(props[i].getName()) == null) 232 { 233 sb.append(indent 234 + Messages.getString("ModelChecker.RelNotMapped", props[i].getName(), 235 DjPersistenceManager.MAPPING_REL)); 236 allOk = false; 237 } 238 } 239 return allOk; 240 } 241 242 protected boolean checkRelations(DjExtent extent, StringBuffer sb) 243 { 244 boolean allOk = true; 245 DjRelation[] rels = extent.getMasterRelations(); 246 for (int i = 0; i < rels.length; i++) 247 { 248 try 249 { 250 rels[i].validateRestriction(); 251 252 } 253 catch (CatalogException ce) 254 { 255 sb.append(indent + Messages.getString("global.Restriction", rels[i].getName(), ce.getMessage())); 256 allOk = false; 257 } 258 } 259 return allOk; 260 } 261 } | Popular Tags |