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.logging.Level;
020: import java.util.logging.Logger;
021:
022: /**
023: * A class that offers a basic readGfEdit method with a lot of
024: * hot spots where subclasses can plug in
025: * @author daniels
026: *
027: */
028: abstract class AbstractProber {
029: /**
030: * reference to the editor whose readRefinementMenu method is used
031: */
032: protected final GfCapsule gfCapsule;
033: protected static Logger logger = Logger
034: .getLogger(AbstractProber.class.getName());
035:
036: /**
037: * A constructor which sets some fields
038: * @param gfCapsule The encapsulation of GF
039: */
040: public AbstractProber(GfCapsule gfCapsule) {
041: this .gfCapsule = gfCapsule;
042: }
043:
044: /**
045: * reads the hmsg part
046: * @param readresult the first line
047: * @return the first line of the next XML child.
048: * if no hmsg is present @see readresult is returned.
049: */
050: protected String readHmsg(String readresult) {
051: if (readresult.equals("<hmsg>")) {
052: gfCapsule.skipChild("<hmsg>");
053: try {
054: String next = gfCapsule.fromProc.readLine();
055: if (logger.isLoggable(Level.FINER)) {
056: logger.finer("2 " + next);
057: }
058: return next;
059: } catch (IOException e) {
060: System.err
061: .println("Could not read from external process:\n"
062: + e);
063: return "";
064: }
065: } else {
066: return readresult;
067: }
068: }
069:
070: /**
071: * reads the linearization subtree.
072: * The first line should be already read
073: * @param readresult the first line with the opening tag
074: */
075: protected void readLinearizations(String readresult) {
076: gfCapsule.skipChild("<linearizations>");
077: }
078:
079: /**
080: * Reads the tree child of the XML from beginning to end
081: */
082: protected void readTree() {
083: gfCapsule.skipChild("<tree>");
084: }
085:
086: /**
087: * Reads the message child of the XML from beginning to end
088: */
089: protected void readMessage() {
090: gfCapsule.skipChild("<message>");
091: }
092:
093: /**
094: * Reads the menu child of the XML from beginning to end
095: */
096: protected void readMenu() {
097: gfCapsule.skipChild("<menu>");
098: }
099:
100: /**
101: * reads the output from GF starting with >gfedit<
102: * and last reads >/gfedit<.
103: */
104: protected void readGfedit() {
105: try {
106: String next = "";
107: //read <gfedit>
108: String readresult = gfCapsule.fromProc.readLine();
109: if (logger.isLoggable(Level.FINER)) {
110: logger.finer("1 " + next);
111: }
112: //read either <hsmg> or <lineatization>
113: readresult = gfCapsule.fromProc.readLine();
114: if (logger.isLoggable(Level.FINER)) {
115: logger.finer("1 " + next);
116: }
117:
118: Hmsg hmsg = gfCapsule.readHmsg(readresult);
119: next = hmsg.lastline;
120:
121: //in case there comes sth. unexpected before <linearizations>
122: //usually the while body is never entered
123: // %%%
124: while ((next != null)
125: && ((next.length() == 0) || (!next.trim().equals(
126: "<linearizations>")))) {
127: next = gfCapsule.fromProc.readLine();
128: if (next != null) {
129: if (logger.isLoggable(Level.FINER)) {
130: logger.finer("1 " + next);
131: }
132: } else {
133: System.exit(0);
134: }
135: }
136: readLinearizations(next);
137: readTree();
138: readMessage();
139: readMenu();
140:
141: for (int i = 0; i < 3 && !next.equals(""); i++) {
142: next = gfCapsule.fromProc.readLine();
143: if (logger.isLoggable(Level.FINER)) {
144: logger.finer("1 " + next);
145: }
146: }
147:
148: } catch (IOException e) {
149: System.err
150: .println("Could not read from external process:\n"
151: + e);
152: }
153:
154: }
155:
156: /**
157: * send a command to GF
158: * @param text the command, exactly the string that is going to be sent
159: */
160: protected void send(String text) {
161: if (logger.isLoggable(Level.FINE)) {
162: logger.fine("## send: '" + text + "'");
163: }
164: gfCapsule.realSend(text);
165: }
166:
167: /**
168: * Just reads the complete output of a GF run and ignores it.
169: */
170: protected void readAndIgnore() {
171: try {
172: StringBuffer debugCollector = new StringBuffer();
173: String readresult = gfCapsule.fromProc.readLine();
174: debugCollector.append(readresult).append('\n');
175: if (logger.isLoggable(Level.FINER))
176: logger.finer("14 " + readresult);
177: while (readresult.indexOf("</gfedit>") == -1) {
178: readresult = gfCapsule.fromProc.readLine();
179: debugCollector.append(readresult).append('\n');
180: if (logger.isLoggable(Level.FINER))
181: logger.finer("14 " + readresult);
182: }
183: //read trailing newline:
184: readresult = gfCapsule.fromProc.readLine();
185: debugCollector.append(readresult).append('\n');
186: if (logger.isLoggable(Level.FINER))
187: logger.finer("14 " + readresult);
188:
189: } catch (IOException e) {
190: System.err
191: .println("Could not read from external process:\n"
192: + e);
193: }
194: }
195: }
|