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

PatternSaverLoader.java

00001 package edu.ksu.cis.bandera.specification.pattern;
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.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  * @param node edu.ksu.cis.bandera.pattern.node.AIdIds
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  * @param node edu.ksu.cis.bandera.pattern.node.AIdsIds
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  * @param node edu.ksu.cis.bandera.pattern.node.AParamResource
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  * @param node edu.ksu.cis.bandera.pattern.node.APattern
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  * @param node edu.ksu.cis.bandera.pattern.node.AStringResource
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  * @param node edu.ksu.cis.bandera.pattern.node.AStringsStrings
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  * @param node edu.ksu.cis.bandera.pattern.node.AStringStrings
00169  */
00170 public void caseAStringStrings(AStringStrings node) {
00171     buffer.append(Util.decodeString(node.getStringLiteral().toString().trim()));
00172 }
00173 /**
00174  * 
00175  * @return edu.ksu.cis.bandera.pattern.Pattern
00176  * @param name java.lang.String
00177  * @param scope java.lang.String
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  * @return java.util.Hashtable
00188  */
00189 public static java.util.Hashtable getPatternTable() {
00190     return patternTable;
00191 }
00192 /**
00193  * 
00194  * @return edu.ksu.cis.bandera.pattern.Pattern
00195  * @param format java.lang.String
00196  */
00197 public static Pattern getPatternWithFormat(String format) {
00198     return (Pattern) formatPatternTable.get(format);
00199 }
00200 /**
00201  * 
00202  * @param filename java.lang.String
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  * @param args java.lang.String[]
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  * @param pattern edu.ksu.cis.bandera.pattern.Pattern
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  * @param newPatternTable java.util.Hashtable
00253  */
00254 public static void setPatternTable(java.util.Hashtable newPatternTable) {
00255     patternTable = newPatternTable;
00256 }
00257 }

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