001: //Copyright (c) Hans-Joachim Daniels 2005
002: //
003: //This program is free software; you can redistribute it and/or modify
004: //it under the terms of the GNU General Public License as published by
005: //the Free Software Foundation; either version 2 of the License, or
006: //(at your option) any later version.
007: //
008: //This program is distributed in the hope that it will be useful,
009: //but WITHOUT ANY WARRANTY; without even the implied warranty of
010: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: //GNU General Public License for more details.
012: //
013: //You can either finde the file LICENSE or LICENSE.TXT in the source
014: //distribution or in the .jar file of this application
015:
016: package de.uka.ilkd.key.ocl.gf;
017:
018: import java.io.IOException;
019: import java.util.Hashtable;
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022:
023: /**
024: * @author daniels
025: * asks GF to print all available printnames, parses that list and generates
026: * the suiting Printname objects.
027: */
028: public class PrintnameLoader extends AbstractProber {
029: private final static Logger nogger = Logger
030: .getLogger(Printname.class.getName());
031: /**
032: * The PrintnameManager on which the read Printnames
033: * will be registered with their fun name.
034: */
035: private final PrintnameManager printnameManager;
036: /**
037: * Here, the funs with their types get stored
038: */
039: private final Hashtable funTypes = new Hashtable();
040: /**
041: * if the Printnames should have their type appended to their display names
042: */
043: private final boolean loadTypes;
044:
045: /**
046: * an initializing constructor, does nothing except setting fields
047: * @param gfCapsule the read/write encapsulation of GF
048: * @param pm The PrintnameManager on which the read Printnames
049: * will be registered with their fun name.
050: * @param withTypes true iff the Printnames should have their type
051: * appended to their display names
052: */
053: public PrintnameLoader(GfCapsule gfCapsule, PrintnameManager pm,
054: boolean withTypes) {
055: super (gfCapsule);
056: this .printnameManager = pm;
057: this .loadTypes = withTypes;
058: }
059:
060: /**
061: * Reads the tree child of the XML from beginning to end.
062: * Sets autocompleted to false, if the focus position is open.
063: */
064: protected void readMessage() {
065: try {
066: String result = gfCapsule.fromProc.readLine();
067: if (nogger.isLoggable(Level.FINER)) {
068: nogger.finer("1 " + result);
069: }
070: //first read line is <message>, but this one gets filtered out in the next line
071: while (result.indexOf("/message") == -1) {
072: result = result.trim();
073: if (result.startsWith("printname fun ")) {
074: //unescape backslashes. Probably there are more
075: result = Linearization.unescapeTextFromGF(result);
076: this .printnameManager.addNewPrintnameLine(result,
077: this .funTypes);
078: }
079:
080: result = gfCapsule.fromProc.readLine();
081: if (nogger.isLoggable(Level.FINER)) {
082: nogger.finer("1 " + result);
083: }
084: }
085: if (nogger.isLoggable(Level.FINER)) {
086: nogger.finer("finished loading printnames");
087: }
088: } catch (IOException e) {
089: System.err.println(e.getMessage());
090: e.printStackTrace();
091: }
092:
093: }
094:
095: /**
096: * asks GF to print a list of all available printnames and
097: * calls the registered PrintnameManager to register those.
098: * @param lang The module for which the grammars should be printed.
099: * If lang is "" or null, the last read grammar module is used.
100: */
101: protected void readPrintnames(String lang) {
102: //before, we want the types to be read.
103: if (this .loadTypes) {
104: TypesLoader tl = new TypesLoader(gfCapsule, this .funTypes);
105: tl.readTypes();
106: }
107: //prints the printnames of the last loaded grammar,
108: String sendString = "gf pg -printer=printnames";
109: if (lang != null && !("".equals(lang))) {
110: sendString = sendString + " -lang=" + lang;
111: }
112: nogger.fine("collecting printnames :" + sendString);
113: send(sendString);
114: readGfedit();
115: }
116:
117: }
|