KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > util > ModelChecker


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.modeler.util;
31
32 import java.io.BufferedReader JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.InputStreamReader JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 import java.util.Vector JavaDoc;
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 JavaDoc indent = " ";
52   public final static String JavaDoc RESERVED_WORDS_FILE = "reservedWords.properties";
53   public final static String JavaDoc ASCII_CHARSET = "US-ASCII";
54   HashMap JavaDoc _reservedWords;
55
56   private String JavaDoc _checkReport = null;
57
58   public ModelChecker() throws IOException JavaDoc
59   {
60     _reservedWords = loadReservedWords();
61   }
62
63   public boolean check(DjPersistenceManager mgr)
64   {
65     boolean allOk = true;
66     DjExtent[] extents = mgr.getExtentsSorted();
67     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
68
69     try
70     {
71       mgr.validateModel(true);
72     }
73     catch (Throwable JavaDoc 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 JavaDoc getReport()
98   {
99     return _checkReport;
100   }
101
102   protected HashMap JavaDoc loadReservedWords() throws IOException JavaDoc
103   {
104     InputStream JavaDoc inStream = ModelChecker.class.getResourceAsStream(RESERVED_WORDS_FILE);
105     HashMap JavaDoc result = new HashMap JavaDoc();
106     if (inStream == null)
107     {
108       throw new IOException JavaDoc(Messages.getString("ModelChecker.CouldNotLoad", RESERVED_WORDS_FILE));
109     }
110     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inStream, ASCII_CHARSET));
111     String JavaDoc line = null;
112     while ((line = reader.readLine()) != null)
113     {
114       line = line.trim().toLowerCase();
115       StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(line, " ,\t\n\r");
116       while (tok.hasMoreElements())
117       {
118         String JavaDoc s = tok.nextToken();
119         result.put(s, s);
120       }
121     }
122     reader.close();
123     return result;
124   }
125
126   protected boolean isReservedWord(String JavaDoc word)
127   {
128     return _reservedWords.containsKey(word.toLowerCase());
129   }
130
131   protected boolean checkExtent(DjPersistenceManager mgr, DjExtent extent, StringBuffer JavaDoc 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 JavaDoc had = new Vector JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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