00001 package edu.ksu.cis.bandera.specification.pattern.datastructure;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import java.io.*;
00036 import edu.ksu.cis.bandera.specification.pattern.exception.*;
00037 import edu.ksu.cis.bandera.specification.ast.*;
00038 import java.util.*;
00039
00040 public class Pattern {
00041 private String filename;
00042 private String name;
00043 private String scope;
00044 private String format;
00045 private String compareFormat;
00046 private String url = "";
00047 private String description = "";
00048 private TreeSet parameters = new TreeSet();
00049 private Hashtable mappings = new Hashtable();
00050 private Hashtable misc = new Hashtable();
00051 private boolean hasChanged = false;
00052 private Vector exceptions = new Vector();
00053
00054
00055
00056
00057
00058 public void addMapping(String name, String pattern) {
00059 mappings.put(name, pattern);
00060 }
00061
00062
00063
00064
00065
00066 public void addMiscData(String key, String value) {
00067 misc.put(key, value);
00068 }
00069
00070
00071
00072
00073 public void addParameter(String id) throws PatternException {
00074 if (parameters.contains(id))
00075 throw new PatternException("Parameter '" + id
00076 + "' has already been declared in pattern with name '" + name
00077 + "' and scope '" + scope + "'");
00078 parameters.add(id);
00079 }
00080
00081
00082
00083
00084
00085 public String expandFormat(Hashtable parameters) {
00086 StringBuffer buffer = new StringBuffer();
00087 for (StringTokenizer tokenizer =
00088 new StringTokenizer(format, "{}", true); tokenizer.hasMoreTokens();) {
00089 String token = tokenizer.nextToken();
00090 if ("{".equals(token)) {
00091 String param = (String) tokenizer.nextToken();
00092 String arg = (String) parameters.get(param.trim());
00093 if (arg != null) {
00094 buffer.append("{" + arg + "}");
00095 } else {
00096 buffer.append("{" + param + "}");
00097 }
00098 tokenizer.nextToken();
00099 } else {
00100 buffer.append(token);
00101 }
00102 }
00103
00104 return buffer.toString();
00105 }
00106
00107
00108
00109
00110
00111
00112 public String expandMapping(String mapping, Hashtable parameters) throws MappingException {
00113 if (mappings.get(mapping) == null)
00114 throw new MappingException("Mapping for " + mapping + " is not declared");
00115
00116 StringBuffer buffer = new StringBuffer();
00117 for (StringTokenizer tokenizer =
00118 new StringTokenizer((String) mappings.get(mapping), "{}", true); tokenizer.hasMoreTokens();) {
00119 String token = tokenizer.nextToken();
00120 if ("{".equals(token)) {
00121 String param = (String) tokenizer.nextToken();
00122 String arg = (String) parameters.get(param.trim());
00123 if (arg != null) {
00124 buffer.append(arg);
00125 } else {
00126 buffer.append("{" + param + "}");
00127 }
00128 tokenizer.nextToken();
00129 } else {
00130 buffer.append(token);
00131 }
00132 }
00133
00134 return buffer.toString();
00135 }
00136
00137
00138
00139
00140 public java.lang.String getCompareFormat() {
00141 return compareFormat;
00142 }
00143
00144
00145
00146
00147 public java.lang.String getDescription() {
00148 return description;
00149 }
00150
00151
00152
00153
00154 public java.util.Vector getExceptions() {
00155 return exceptions;
00156 }
00157
00158
00159
00160
00161 public java.lang.String getFilename() {
00162 return filename;
00163 }
00164
00165
00166
00167
00168 public java.lang.String getFormat() {
00169 return format;
00170 }
00171
00172
00173
00174
00175
00176 public String getMapping(String key) {
00177 return (String) mappings.get(key);
00178 }
00179
00180
00181
00182
00183 public java.lang.String getName() {
00184 return name;
00185 }
00186
00187
00188
00189
00190 public java.util.Vector getParameters() {
00191 return new Vector(parameters);
00192 }
00193
00194
00195
00196
00197 public Vector getParametersOccurenceOrder() {
00198 Vector v = new Vector();
00199 for (StringTokenizer tokenizer =
00200 new StringTokenizer(format, "{}", true); tokenizer.hasMoreTokens();) {
00201 String token = tokenizer.nextToken();
00202 if ("{".equals(token)) {
00203 String param = (String) tokenizer.nextToken();
00204 v.add(param);
00205 tokenizer.nextToken();
00206 }
00207 }
00208 return v;
00209 }
00210
00211
00212
00213
00214
00215 public String getPattern(String mapping) throws MappingException {
00216 if (mappings.get(mapping) == null)
00217 throw new MappingException("Mapping for " + mapping + " is not declared");
00218 return (String) mappings.get(mapping);
00219 }
00220
00221
00222
00223
00224 public java.lang.String getScope() {
00225 return scope;
00226 }
00227
00228
00229
00230
00231 public java.lang.String getURL() {
00232 return url;
00233 }
00234
00235
00236
00237
00238
00239 public boolean hasMapping(String mapping) {
00240 return mappings.get(mapping) != null;
00241 }
00242
00243
00244
00245
00246 public boolean isHasChanged() {
00247 return hasChanged;
00248 }
00249
00250
00251
00252
00253 public boolean isValid() {
00254 return exceptions.size() == 0;
00255 }
00256
00257
00258
00259
00260
00261 private boolean isValid(String pattern) {
00262 Vector v = new Vector();
00263 for (StringTokenizer tokenizer = new StringTokenizer(pattern, "{}", true);
00264 tokenizer.hasMoreTokens();) {
00265 String token = tokenizer.nextToken();
00266 if ("{".equals(token)) {
00267 String id = tokenizer.nextToken();
00268 if (!v.contains(id)) v.add(id);
00269 tokenizer.nextToken();
00270 }
00271 }
00272 if (parameters.size() != v.size()) return false;
00273
00274 for (Enumeration e = v.elements(); e.hasMoreElements();) {
00275 if (!parameters.contains(e.nextElement()))
00276 return false;
00277 }
00278 return true;
00279 }
00280
00281
00282
00283
00284 public void setDescription(java.lang.String newDescription) {
00285 description = newDescription;
00286 }
00287
00288
00289
00290
00291 public void setExceptions(java.util.Vector newExceptions) {
00292 exceptions = newExceptions;
00293 }
00294
00295
00296
00297
00298 public void setFilename(java.lang.String newFilename) {
00299 filename = newFilename;
00300 }
00301
00302
00303
00304
00305 public void setFormat(String newFormat) throws PatternException {
00306 format = newFormat;
00307 try {
00308 compareFormat = Reformatter.format(format);
00309 } catch (Exception e) {
00310 throw new PatternException(e.getMessage());
00311 }
00312 }
00313
00314
00315
00316
00317 public void setHasChanged(boolean newHasChanged) {
00318 hasChanged = newHasChanged;
00319 }
00320
00321
00322
00323
00324 public void setName(java.lang.String newName) {
00325 name = newName;
00326 }
00327
00328
00329
00330
00331 public void setParameters(java.util.Vector newParameters) {
00332 parameters = new TreeSet(newParameters);
00333 }
00334
00335
00336
00337
00338 public void setScope(java.lang.String newScope) {
00339 scope = newScope;
00340 }
00341
00342
00343
00344
00345 public void setURL(java.lang.String newURL) {
00346 url = newURL;
00347 }
00348
00349
00350
00351 public void validate() {
00352 if (name == null) exceptions.add("Bad name");
00353 if (scope == null) exceptions.add("Bad scope");
00354 if (format == null) exceptions.add("Bad format");
00355 if (!isValid(format)) exceptions.add("Bad parameter(s) in format");
00356 for (Enumeration e = mappings.keys(); e.hasMoreElements();) {
00357 String key = (String) e.nextElement();
00358 if(!isValid((String) mappings.get(key))) exceptions.add("Bad parameter(s) in " + key + " mapping");
00359 }
00360 }
00361 }