00001 package edu.ksu.cis.bandera.specification.pattern;
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.analysis.*;
00037 import edu.ksu.cis.bandera.specification.pattern.exception.*;
00038 import edu.ksu.cis.bandera.specification.pattern.lexer.*;
00039 import edu.ksu.cis.bandera.specification.pattern.node.*;
00040 import edu.ksu.cis.bandera.specification.pattern.parser.*;
00041 import edu.ksu.cis.bandera.jjjc.util.*;
00042 import edu.ksu.cis.bandera.specification.pattern.datastructure.*;
00043 import java.util.*;
00044 public class PatternSaverLoader extends DepthFirstAdapter {
00045 private static Hashtable patternTable;
00046 private static Hashtable formatPatternTable = new Hashtable();
00047 private java.util.Vector exceptions;
00048 private static String currentFilename;
00049 private Pattern currentPattern;
00050 private StringBuffer buffer;
00051
00052 static {
00053 try {
00054 reset();
00055 } catch (Exception e) {
00056 }
00057 }
00058
00059
00060
00061
00062 public void caseAIdIds(AIdIds node) {
00063 try {
00064 currentPattern.addParameter(node.getId().toString().trim());
00065 } catch (Exception e) {
00066 exceptions.add(e);
00067 }
00068 }
00069
00070
00071
00072
00073 public void caseAIdsIds(AIdsIds node) {
00074 node.getIds().apply(this);
00075 try {
00076 currentPattern.addParameter(node.getId().toString().trim());
00077 } catch (Exception e) {
00078 exceptions.add(e);
00079 }
00080 }
00081
00082
00083
00084
00085 public void caseAParamResource(AParamResource node) {
00086 if ("parameters".equals(node.getId().toString().trim())) {
00087 if (node.getIds() != null) {
00088 node.getIds().apply(this);
00089 }
00090 } else {
00091 exceptions.add(new PatternException("Unsupported resource type: " + node.getId()));
00092 }
00093 }
00094
00095
00096
00097
00098 public void caseAPattern(APattern node) {
00099 exceptions = new Vector();
00100 currentPattern = new Pattern();
00101 currentPattern.setExceptions(exceptions);
00102 super.caseAPattern(node);
00103
00104 currentPattern.setFilename(currentFilename);
00105
00106 if (patternTable.get(currentPattern.getName()) == null) {
00107 Hashtable table = new Hashtable();
00108 patternTable.put(currentPattern.getName(), table);
00109 }
00110
00111 currentPattern.validate();
00112
00113 Hashtable table = (Hashtable) patternTable.get(currentPattern.getName());
00114 formatPatternTable.put(currentPattern.getCompareFormat(), currentPattern);
00115 if (table.put(currentPattern.getScope(), currentPattern) != null) {
00116 exceptions.add(new PatternException("Pattern with name '" + currentPattern.getName()
00117 + "' and scope '" + currentPattern.getScope() + "' has already been declared"));
00118 }
00119 }
00120
00121
00122
00123
00124 public void caseAStringResource(AStringResource node) {
00125 buffer = new StringBuffer();
00126
00127 String id = node.getId().toString().trim();
00128 if ("name".equals(id)) {
00129 node.getStrings().apply(this);
00130 currentPattern.setName(buffer.toString());
00131 } else if ("scope".equals(id)) {
00132 node.getStrings().apply(this);
00133 currentPattern.setScope(buffer.toString());
00134 } else if ("format".equals(id)) {
00135 node.getStrings().apply(this);
00136 try {
00137 currentPattern.setFormat(buffer.toString());
00138 } catch (PatternException e) {
00139 exceptions.add(e);
00140 }
00141 } else if ("url".equals(id)) {
00142 node.getStrings().apply(this);
00143 currentPattern.setURL(buffer.toString());
00144 } else if ("description".equals(id)) {
00145 node.getStrings().apply(this);
00146 currentPattern.setDescription(buffer.toString());
00147 } else if ("ltl".equals(id)) {
00148 node.getStrings().apply(this);
00149 currentPattern.addMapping("ltl", buffer.toString());
00150 } else if ("ctl".equals(id)) {
00151 node.getStrings().apply(this);
00152 currentPattern.addMapping("ctl", buffer.toString());
00153 } else {
00154 node.getStrings().apply(this);
00155 currentPattern.addMiscData(id, buffer.toString());
00156 }
00157 }
00158
00159
00160
00161
00162 public void caseAStringsStrings(AStringsStrings node) {
00163 node.getStrings().apply(this);
00164 buffer.append(Util.decodeString(node.getStringLiteral().toString().trim()));
00165 }
00166
00167
00168
00169
00170 public void caseAStringStrings(AStringStrings node) {
00171 buffer.append(Util.decodeString(node.getStringLiteral().toString().trim()));
00172 }
00173
00174
00175
00176
00177
00178
00179 public static Pattern getPattern(String name, String scope) {
00180 Hashtable table = (Hashtable) patternTable.get(name.trim());
00181 if (table == null) return null;
00182
00183 return (Pattern) table.get(scope.trim());
00184 }
00185
00186
00187
00188
00189 public static java.util.Hashtable getPatternTable() {
00190 return patternTable;
00191 }
00192
00193
00194
00195
00196
00197 public static Pattern getPatternWithFormat(String format) {
00198 return (Pattern) formatPatternTable.get(format);
00199 }
00200
00201
00202
00203
00204 public static void load(String filename) throws PatternException {
00205 try {
00206 currentFilename = filename;
00207 PatternSaverLoader loader = new PatternSaverLoader();
00208 FileReader fr = new FileReader(currentFilename);
00209 new Parser(new Lexer(new PushbackReader(fr))).parse().apply(loader);
00210 fr.close();
00211 } catch (Exception e) {
00212 throw new PatternException("Exceptions were raised when extracting patterns:" + e.getMessage());
00213 }
00214 }
00215
00216
00217
00218
00219 public static void main(String[] args) throws Exception {
00220 reset();
00221 for (int i = 0; i < args.length; i++) {
00222 load(args[i]);
00223 }
00224 Hashtable t = getPatternTable();
00225 System.out.println(t);
00226 }
00227
00228
00229
00230
00231 public static void removePattern(Pattern pattern) {
00232 Hashtable table = (Hashtable) patternTable.get(pattern.getName());
00233 if (table != null) {
00234 table.remove(pattern.getScope());
00235 }
00236 }
00237
00238
00239
00240 public static void reset() throws PatternException {
00241 patternTable = new Hashtable();
00242 PatternSaverLoader loader = new PatternSaverLoader();
00243 try {
00244 currentFilename = "bandera.pattern";
00245 new Parser(new Lexer(new PushbackReader(new InputStreamReader(PatternSaverLoader.class.getResourceAsStream(currentFilename))))).parse().apply(loader);
00246 } catch (Exception ex) {
00247 throw new PatternException("Exceptions were raised when loading patterns: " + ex.getMessage());
00248 }
00249 }
00250
00251
00252
00253
00254 public static void setPatternTable(java.util.Hashtable newPatternTable) {
00255 patternTable = newPatternTable;
00256 }
00257 }