mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
the grammars are now recognized by extension i.e. .pgf is grammar just like .php is a PHP script
This commit is contained in:
@@ -34,19 +34,12 @@ main = do stderrToFile logFile
|
||||
|
||||
cgiMain :: Cache PGF -> CGI CGIResult
|
||||
cgiMain cache =
|
||||
do path <- pathInfo
|
||||
jsonp <- serveResource cache $ filter (not . null) $ splitBy (=='/') path
|
||||
do path <- getVarWithDefault "SCRIPT_FILENAME" ""
|
||||
pgf <- liftIO $ readCache cache path
|
||||
command <- liftM (maybe "grammar" (urlDecodeUnicode . UTF8.decodeString)) (getInput "command")
|
||||
jsonp <- pgfMain pgf command
|
||||
outputJSONP jsonp
|
||||
|
||||
serveResource :: Cache PGF -> [String] -> CGI JSValue
|
||||
serveResource cache resource =
|
||||
case resource of
|
||||
[] -> liftIO doListGrammars
|
||||
[file] -> serveResource cache [file,"grammar"]
|
||||
[file,command] -> do pgf <- liftIO $ readCache cache $ cleanFilePath file
|
||||
pgfMain pgf command
|
||||
_ -> throwCGIError 400 "Unknown resource" ["Unknown resource: " ++ show resource]
|
||||
|
||||
pgfMain :: PGF -> String -> CGI JSValue
|
||||
pgfMain pgf command =
|
||||
case command of
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
<!-- Inherit the core Web Toolkit stuff. -->
|
||||
<inherits name="com.google.gwt.user.User" />
|
||||
<inherits name="com.google.gwt.xml.XML" />
|
||||
|
||||
<inherits name="se.chalmers.cs.gf.gwt.PGF" />
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
<!-- Inherit the core Web Toolkit stuff. -->
|
||||
<inherits name="com.google.gwt.user.User" />
|
||||
<inherits name="com.google.gwt.xml.XML" />
|
||||
|
||||
<inherits name="se.chalmers.cs.gf.gwt.PGF" />
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import com.google.gwt.user.client.ui.Widget;
|
||||
|
||||
public class FridgeApp implements EntryPoint {
|
||||
|
||||
protected static final String pgfBaseURL = "/pgf";
|
||||
protected static final String pgfBaseURL = "/grammars";
|
||||
|
||||
protected PGFWrapper pgf;
|
||||
|
||||
@@ -274,7 +274,7 @@ public class FridgeApp implements EntryPoint {
|
||||
public void onModuleLoad() {
|
||||
statusPopup = new StatusPopup();
|
||||
|
||||
pgf = new PGFWrapper(new PGF(pgfBaseURL));
|
||||
pgf = new PGFWrapper(pgfBaseURL);
|
||||
RootPanel.get().add(createUI());
|
||||
pgf.addSettingsListener(new MySettingsListener());
|
||||
History.addHistoryListener(new MyHistoryListener());
|
||||
|
||||
@@ -9,34 +9,13 @@ import java.util.ArrayList;
|
||||
|
||||
public class PGF {
|
||||
|
||||
private String baseURL;
|
||||
|
||||
public PGF (String baseURL) {
|
||||
this.baseURL = baseURL;
|
||||
}
|
||||
|
||||
/* List grammars */
|
||||
|
||||
public JSONRequest listGrammars (final GrammarNamesCallback callback) {
|
||||
return JSONRequestBuilder.sendRequest(baseURL + "/", null, callback);
|
||||
}
|
||||
|
||||
public interface GrammarNamesCallback extends JSONCallback<GrammarNames> { }
|
||||
|
||||
public static class GrammarNames extends IterableJsArray<GrammarName> {
|
||||
protected GrammarNames() { }
|
||||
public PGF () {
|
||||
}
|
||||
|
||||
public static class GrammarName extends JavaScriptObject {
|
||||
protected GrammarName() { }
|
||||
|
||||
public final native String getName() /*-{ return this.name; }-*/;
|
||||
}
|
||||
|
||||
/* Grammar */
|
||||
|
||||
public JSONRequest grammar (String pgfName, final GrammarCallback callback) {
|
||||
return sendGrammarRequest(pgfName, "grammar", null, callback);
|
||||
public JSONRequest grammar (String pgfURL, final GrammarCallback callback) {
|
||||
return sendGrammarRequest(pgfURL, "grammar", new ArrayList<Arg>(), callback);
|
||||
}
|
||||
|
||||
public interface GrammarCallback extends JSONCallback<Grammar> { }
|
||||
@@ -61,14 +40,14 @@ public class PGF {
|
||||
|
||||
/* Translation */
|
||||
|
||||
public JSONRequest translate (String pgfName, String input, String fromLang, String cat, String toLang,
|
||||
public JSONRequest translate (String pgfURL, String input, String fromLang, String cat, String toLang,
|
||||
final TranslateCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
args.add(new Arg("from", fromLang));
|
||||
args.add(new Arg("cat", cat));
|
||||
args.add(new Arg("to", toLang));
|
||||
return sendGrammarRequest(pgfName, "translate", args, callback);
|
||||
return sendGrammarRequest(pgfURL, "translate", args, callback);
|
||||
}
|
||||
|
||||
public interface TranslateCallback extends JSONCallback<Translations> { }
|
||||
@@ -92,7 +71,7 @@ public class PGF {
|
||||
* @param limit The number of suggestions to get.
|
||||
* If -1 is passed, all available suggestions are retrieved.
|
||||
*/
|
||||
public JSONRequest complete (String pgfName, String input, String fromLang, String cat, int limit, final CompleteCallback callback) {
|
||||
public JSONRequest complete (String pgfURL, String input, String fromLang, String cat, int limit, final CompleteCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
args.add(new Arg("from", fromLang));
|
||||
@@ -100,7 +79,7 @@ public class PGF {
|
||||
if (limit > 0) {
|
||||
args.add(new Arg("limit", limit));
|
||||
}
|
||||
return sendGrammarRequest(pgfName, "complete", args, callback);
|
||||
return sendGrammarRequest(pgfURL, "complete", args, callback);
|
||||
}
|
||||
|
||||
public interface CompleteCallback extends JSONCallback<Completions> { }
|
||||
@@ -118,12 +97,12 @@ public class PGF {
|
||||
|
||||
/* Parsing */
|
||||
|
||||
public JSONRequest parse (String pgfName, String input, String fromLang, String cat, final ParseCallback callback) {
|
||||
public JSONRequest parse (String pgfURL, String input, String fromLang, String cat, final ParseCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
args.add(new Arg("from", fromLang));
|
||||
args.add(new Arg("cat", cat));
|
||||
return sendGrammarRequest(pgfName, "parse", args, callback);
|
||||
return sendGrammarRequest(pgfURL, "parse", args, callback);
|
||||
}
|
||||
|
||||
public interface ParseCallback extends JSONCallback<ParseResults> { }
|
||||
@@ -141,8 +120,9 @@ public class PGF {
|
||||
|
||||
/* Common */
|
||||
|
||||
public <T extends JavaScriptObject> JSONRequest sendGrammarRequest(String pgfName, String resource, List<Arg> args, final JSONCallback<T> callback) {
|
||||
return JSONRequestBuilder.sendRequest(baseURL + "/" + pgfName + "/" + resource, args, callback);
|
||||
public <T extends JavaScriptObject> JSONRequest sendGrammarRequest(String pgfURL, String resource, List<Arg> args, final JSONCallback<T> callback) {
|
||||
args.add(new Arg("command", resource));
|
||||
return JSONRequestBuilder.sendRequest(pgfURL, args, callback);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,10 +5,13 @@ import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.gwt.http.client.*;
|
||||
import com.google.gwt.xml.client.*;
|
||||
|
||||
public class PGFWrapper {
|
||||
|
||||
private String baseURL;
|
||||
|
||||
private PGF pgf;
|
||||
|
||||
private String pgfName = null;
|
||||
@@ -36,28 +39,61 @@ public class PGFWrapper {
|
||||
private List<SettingsListener> listeners = new LinkedList<SettingsListener>();
|
||||
|
||||
|
||||
public PGFWrapper(PGF pgf) {
|
||||
this.pgf = pgf;
|
||||
|
||||
public PGFWrapper(String baseURL) {
|
||||
this.baseURL = baseURL;
|
||||
this.pgf = new PGF();
|
||||
}
|
||||
|
||||
public void updateAvailableGrammars() {
|
||||
pgf.listGrammars(new PGF.GrammarNamesCallback() {
|
||||
public void onResult(PGF.GrammarNames grammarNames) {
|
||||
grammars = new ArrayList<String>();
|
||||
for (PGF.GrammarName grammarName : grammarNames.iterable()) {
|
||||
grammars.add(grammarName.getName());
|
||||
String url = baseURL+"/grammars.xml";
|
||||
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
|
||||
try
|
||||
{
|
||||
Request request = builder.sendRequest(null, new RequestCallback() {
|
||||
public void onResponseReceived(Request request, Response response)
|
||||
{
|
||||
if (200 == response.getStatusCode())
|
||||
{
|
||||
grammars = new ArrayList<String>();
|
||||
try
|
||||
{
|
||||
Document grammarsDoc = XMLParser.parse(response.getText());
|
||||
|
||||
NodeList grammarsList = grammarsDoc.getElementsByTagName("grammar");
|
||||
for (int i = 0; i < grammarsList.getLength(); i++)
|
||||
{
|
||||
Node grammarNode = grammarsList.item(i);
|
||||
grammars.add(((Element)grammarNode).getAttribute("name"));
|
||||
}
|
||||
}
|
||||
catch (DOMException e)
|
||||
{
|
||||
fireSettingsError("Could not parse XML document.", e);
|
||||
}
|
||||
fireAvailableGrammarsChanged();
|
||||
}
|
||||
else
|
||||
{
|
||||
fireSettingsError("Error getting grammar list", null);
|
||||
}
|
||||
}
|
||||
fireAvailableGrammarsChanged();
|
||||
}
|
||||
public void onError (Throwable e) {
|
||||
fireSettingsError("Error getting grammar list", e);
|
||||
}
|
||||
});
|
||||
|
||||
public void onError(Request request, Throwable e)
|
||||
{
|
||||
fireSettingsError("Error getting grammar list", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (RequestException e)
|
||||
{
|
||||
fireSettingsError("Couldn't connect to server", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateSelectedGrammar () {
|
||||
clearCachedInfo();
|
||||
pgf.grammar(pgfName, new PGF.GrammarCallback() {
|
||||
pgf.grammar(baseURL+"/"+pgfName, new PGF.GrammarCallback() {
|
||||
public void onResult(PGF.Grammar grammar) {
|
||||
userLanguage = grammar.getUserLanguage();
|
||||
languages = new LinkedHashMap<String,PGF.Language>();
|
||||
@@ -83,15 +119,15 @@ public class PGFWrapper {
|
||||
//
|
||||
|
||||
public JSONRequest translate (String input, final PGF.TranslateCallback callback) {
|
||||
return pgf.translate(pgfName, input, inputLanguage, cat, outputLanguage, callback);
|
||||
return pgf.translate(baseURL+"/"+pgfName, input, inputLanguage, cat, outputLanguage, callback);
|
||||
}
|
||||
|
||||
public JSONRequest complete (String input, int limit, final PGF.CompleteCallback callback) {
|
||||
return pgf.complete(pgfName, input, inputLanguage, cat, limit, callback);
|
||||
return pgf.complete(baseURL+"/"+pgfName, input, inputLanguage, cat, limit, callback);
|
||||
}
|
||||
|
||||
public JSONRequest parse (String input, final PGF.ParseCallback callback) {
|
||||
return pgf.parse(pgfName, input, inputLanguage, cat, callback);
|
||||
return pgf.parse(baseURL+"/"+pgfName, input, inputLanguage, cat, callback);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@@ -14,7 +14,7 @@ import com.google.gwt.user.client.ui.Widget;
|
||||
|
||||
public class TranslateApp implements EntryPoint {
|
||||
|
||||
protected static final String pgfBaseURL = "/pgf";
|
||||
protected static final String pgfBaseURL = "/grammars";
|
||||
|
||||
protected PGFWrapper pgf;
|
||||
|
||||
@@ -197,7 +197,7 @@ public class TranslateApp implements EntryPoint {
|
||||
public void onModuleLoad() {
|
||||
statusPopup = new StatusPopup();
|
||||
|
||||
pgf = new PGFWrapper(new PGF(pgfBaseURL));
|
||||
pgf = new PGFWrapper(pgfBaseURL);
|
||||
RootPanel.get().add(createUI());
|
||||
pgf.addSettingsListener(new MySettingsListener());
|
||||
History.addHistoryListener(new MyHistoryListener());
|
||||
|
||||
@@ -47,7 +47,7 @@ $HTTP["host"] =~ "^(.*)$" {
|
||||
}
|
||||
|
||||
fastcgi.debug = 0
|
||||
fastcgi.server = ( "/pgf" =>
|
||||
fastcgi.server = ( ".pgf" =>
|
||||
((
|
||||
"socket" => basedir + "/" + var.PID + "-pgf.socket",
|
||||
"bin-path" => basedir + "/pgf.fcgi",
|
||||
|
||||
@@ -6,7 +6,7 @@ license: GPL
|
||||
license-file: LICENSE
|
||||
synopsis: FastCGI Server for Grammatical Framework
|
||||
|
||||
executable pgf.fcgi
|
||||
executable pgf-server
|
||||
build-depends: base,
|
||||
old-time,
|
||||
directory,
|
||||
|
||||
Reference in New Issue
Block a user