Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Pattern.java

00001 package edu.ksu.cis.bandera.specification.pattern.datastructure;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 2000   Robby (robby@cis.ksu.edu)                    *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
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  * @param name java.lang.String
00056  * @param pattern java.lang.String
00057  */
00058 public void addMapping(String name, String pattern)  {
00059     mappings.put(name, pattern);
00060 }
00061 /**
00062  * 
00063  * @param key java.lang.String
00064  * @param value java.lang.String
00065  */
00066 public void addMiscData(String key, String value) {
00067     misc.put(key, value);
00068 }
00069 /**
00070  * 
00071  * @param id java.lang.String
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  * @return java.lang.String
00083  * @param parameters java.util.Hashtable
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  * @return java.lang.String
00109  * @param mapping java.lang.String
00110  * @param parameters java.util.Hashtable
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  * @return java.lang.String
00139  */
00140 public java.lang.String getCompareFormat() {
00141     return compareFormat;
00142 }
00143 /**
00144  * 
00145  * @return java.lang.String
00146  */
00147 public java.lang.String getDescription() {
00148     return description;
00149 }
00150 /**
00151  * 
00152  * @return java.util.Vector
00153  */
00154 public java.util.Vector getExceptions() {
00155     return exceptions;
00156 }
00157 /**
00158  * 
00159  * @return java.lang.String
00160  */
00161 public java.lang.String getFilename() {
00162     return filename;
00163 }
00164 /**
00165  * 
00166  * @return java.lang.String
00167  */
00168 public java.lang.String getFormat() {
00169     return format;
00170 }
00171 /**
00172  * 
00173  * @return java.lang.String
00174  * @param key java.lang.String
00175  */
00176 public String getMapping(String key) {
00177     return (String) mappings.get(key);
00178 }
00179 /**
00180  * 
00181  * @return java.lang.String
00182  */
00183 public java.lang.String getName() {
00184     return name;
00185 }
00186 /**
00187  * 
00188  * @return java.util.Vector
00189  */
00190 public java.util.Vector getParameters() {
00191     return new Vector(parameters);
00192 }
00193 /**
00194  * 
00195  * @return java.util.Vector
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  * @return java.lang.String
00213  * @param mapping java.lang.String
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  * @return java.lang.String
00223  */
00224 public java.lang.String getScope() {
00225     return scope;
00226 }
00227 /**
00228  * 
00229  * @return java.lang.String
00230  */
00231 public java.lang.String getURL() {
00232     return url;
00233 }
00234 /**
00235  * 
00236  * @return boolean
00237  * @param mapping java.lang.String
00238  */
00239 public boolean hasMapping(String mapping) {
00240     return mappings.get(mapping) != null;
00241 }
00242 /**
00243  * 
00244  * @return boolean
00245  */
00246 public boolean isHasChanged() {
00247     return hasChanged;
00248 }
00249 /**
00250  * 
00251  * @return boolean
00252  */
00253 public boolean isValid() {
00254     return exceptions.size() == 0;
00255 }
00256 /**
00257  * 
00258  * @return boolean
00259  * @param pattern java.lang.String
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  * @param newDescription java.lang.String
00283  */
00284 public void setDescription(java.lang.String newDescription) {
00285     description = newDescription;
00286 }
00287 /**
00288  * 
00289  * @param newExceptions java.util.Vector
00290  */
00291 public void setExceptions(java.util.Vector newExceptions) {
00292     exceptions = newExceptions;
00293 }
00294 /**
00295  * 
00296  * @param newFilename java.lang.String
00297  */
00298 public void setFilename(java.lang.String newFilename) {
00299     filename = newFilename;
00300 }
00301 /**
00302  * 
00303  * @param newFormat java.lang.String
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  * @param newHasChanged boolean
00316  */
00317 public void setHasChanged(boolean newHasChanged) {
00318     hasChanged = newHasChanged;
00319 }
00320 /**
00321  * 
00322  * @param newName java.lang.String
00323  */
00324 public void setName(java.lang.String newName) {
00325     name = newName;
00326 }
00327 /**
00328  * 
00329  * @param newParameters java.util.Vector
00330  */
00331 public void setParameters(java.util.Vector newParameters) {
00332     parameters = new TreeSet(newParameters);
00333 }
00334 /**
00335  * 
00336  * @param newScope java.lang.String
00337  */
00338 public void setScope(java.lang.String newScope) {
00339     scope = newScope;
00340 }
00341 /**
00342  * 
00343  * @param newURL java.lang.String
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 }

Generated at Thu Feb 7 06:51:40 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001