Monday March 19, 2007 Experiments with the JSR-223 compatible Quercus PHP engine
There's a lot of hype for all sorts of Java scripting engines at the moment. For myself I've made some experiments with groovy/grails. Today I've found the PHP Quercus engine from Caucho.com, which is licensed under GPL and I also tried to use it as a JSR-223 scripting engine. As the goal of the test, I want to generate some text from a PHP file, which prints out some PHP variables ($x, $y) in a PHP double quoted string.
Therefore I opened the downloadable *.war file from the Quercus 3.1 snapshot (alpha) with my ZIP tool and extracted:
in the /lib classpath of a newly created quercus.script.test Eclipse project.
Additionally I added a servlet-api.jar from a tomcat 6.x installation to the project's classpath.
The Quercus standard JSR-223 script engine is implemented in the package com.caucho.quercus.script.
Note: if you're not using JDK 1.6, but only JDK 1.5, you'll also need the javax.scripting.* JSR 223 (see link at the end of the blog entry). I also didn't install necessary additional *.jar libraries for Quercus MailModule and UnicodeModule classes.
The Test.java file below now creates the following output
hello world Hello - World Hasta la vista Baby
Test.java file (updated: it now reflects the comments from baennaeck)
package info.bliki.quercus.script.test;
import java.io.FileReader;
import java.io.StringWriter;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Test {
public static void main(String[] args) {
ScriptEngineManager scriptManager = new ScriptEngineManager();
Object php2javaResult = null;
ScriptEngine phpEngine = scriptManager.getEngineByExtension("php");
ScriptContext context = phpEngine.getContext();
try {
context.setWriter(new StringWriter());
php2javaResult = phpEngine.eval("<?php echo \"hello world\"; ?>",
context);
StringWriter writer = (StringWriter) context.getWriter();
// show output from PHP script in console:
System.out.println(writer.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
try {
context.setWriter(new StringWriter());
phpEngine.put("x", "Hello");
phpEngine.put("y", "World");
php2javaResult = phpEngine.eval("<?php echo \"$x - $y\"; ?>", context);
StringWriter writer = (StringWriter) context.getWriter();
System.out.println(writer.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
try {// file test
context.setWriter(new StringWriter());
//redefine variable in ENGINE_SCOPE x,y:
phpEngine.put("x", "Hasta la vista");
phpEngine.put("y", "Baby");
// the following file contains the line: <?php echo "$x $y"; ?>
php2javaResult = phpEngine
.eval(new FileReader(
"C:\\temp\\echo_test.php"), context);
StringWriter writer = (StringWriter) context.getWriter();
System.out.println(writer.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
BTW: the Quercus phpinfo() implementation gives the following output:
<h1>Quercus</h1><pre>PHP Version => 5.2.0 System => Windows XP 5.1 x86 Build Date => 20061220T1222 Configure Command => n/a Server API => CGI Virtual Directory Support => disabled Configuration File (php.ini) Path => WEB-INF/php.ini PHP API => 20031224 PHP Extension => 20041030 Debug Build => no Thread Safety => enabled Registered PHP Streams => php, file, http, https </pre>
Related links:
Eclipse Grails plugin (very early alpha)
I created a Grails plugin for the Eclipse Java IDE.
You can download it here (source code included):
Install the *.jar file in your eclipse/plugins directory and restart Eclipse with the -clean option. Set the GRAILS_HOME classpath variable in your Java->Build Path->Classpath Variables preferences menu.
I simply wrapped the calls to the Grails Ant targets with some Eclipse external tool calls (see the org.eclipse.ui.externaltools plugin for more information).
Do a "right mouse click" on a Grails project node in the Eclipse Package Explorer and choose the menu Grails and the appropriate target you would like to run. The script should show its output in the Eclipse console.
Wikipedia Java API - parser now based on htmlcleaner project's API
I just committed some sources to SVN for my Wikipedia Java API project. In the first version of this API, I've rendered the wiki text directly into a java.lang.StringBuffer. In the new version I'm using the org.htmlcleaner DOM API as a base to parse the text into an internal HTML tree node structure. This HTML tree is then rendered into a HTML string.
In the future it should be easier to convert the internal HTML tree into other formats like plain text, PDF or even TeX.
One promising new project I've found for converting texts into PDF, is Ulrich Fuchs JTypeSet library. Ulrich has used this library as a base for PDF creation in his german Wikipedia clone: Wikiweise
Description of the JTypeSet project from the sourceforge.net summary page:
Type setting engine in Java. Uses an algorithm similar to Tex to layout text and images on a page and create a pdf output, supports multiple columns and images, which can be placed at arbitrary positions, spawm columns etc.
Guillaume Laforge announced the Groovy 1.0 release on his blog:
Groovy 1.0 can be downloaded here:
Posted by axelclk ( Jan 03 2007, 07:48:49 AM CET ) Permalink Comments [4]How to integrate jsMath into JAMWiki
A mini guide for integrating jsMath into JAMWiki.
In the following I assume that your JAMWiki Application-URI is /wiki
Store the jsMath files and fonts in the directory:
wiki/static/jsMath
Modify the file:
wiki/WEB-INF/jsp/top.jsp
...
<script src="../static/jsMath/jsMath.js"></script>
<style type="text/css">#jsMath_Warning {display: none} </style>
</head>
<body>
Modify the file:
wiki/WEB-INF/jsp/close-document.jsp
... <SCRIPT> jsMath.Process() </SCRIPT> </body> </html>
Note: the wiki syntax renderer in JAMWiki has to be enabled so that the math wikitag
<math>sin(x)</math>
will be transformed to the following html:
<DIV CLASS="math">sin(x)</DIV>
All DIV formulas that have the class math are now rendered by jsMath.
See also:
Posted by axelclk ( Dec 17 2006, 02:47:30 PM CET ) Permalink Comments [4]How to integrate a GWT module in a new JAMWiki Special: page
Note: this document is work in progress and not complete at the moment
| Contents |
|---|
My example GWT module is the MathEclipse AJAX calculator called org.matheclipse.gwt.Calc which is separately developed in the GWT hosted mode and should now be included into a new JAMWiki Special:Calc page.
In the main GWT EntryPoint in the method org.matheclipse.gwt.client.Calc#onModuleLoad() we can distinguish between non-hosted mode and hosted mode with the GWT.isScript() method. In the onModuleLoad() method we also create the main calculator GUI panel and insert it into a prepared "slot1" in calc.jsp (see below).
public void onModuleLoad() {
CALC_SERVICE = (CalcServiceAsync) GWT.create(CalcService.class);
ServiceDefTarget target = (ServiceDefTarget) CALC_SERVICE;
if (GWT.isScript()) {
String url = GWT.getModuleBaseURL();
url += "calc";
target.setServiceEntryPoint(url);
} else {
target.setServiceEntryPoint("/calc");
}
...
// Create a panel for the GUI and insert it into a prepared "slot1" in cals.jsp
RootPanel slot1 = RootPanel.get("slot1");
slot1.add(panel);
...
protected ModelAndView handleJAMWikiRequest(HttpServletRequest request, HttpServletResponse response, ModelAndView next, WikiPageInfo pageInfo) throws Exception {
pageInfo.setPageTitle(new WikiMessage("calc.title"));
pageInfo.setAction(WikiPageInfo.ACTION_CALC);
pageInfo.setSpecial(true);
return next;
}
calc.title=AJAX online calculator
...
public static final int ACTION_CALC = 999;
...
public boolean getActionCalc() {
return (this.action == ACTION_CALC);
}
<c:when test="${pageInfo.actionCalc}">
<jsp:include page="calc.jsp" flush="true" />
</c:when>
Note:In the following I assume that your JAMWiki Application-URI is /wiki
In a separated GWT project I compiled all GWT files with the GWTCompiler from Java to JavaScript into the \www\org.matheclipse.gwt.Calc directory. These files must now been copied into a JAMWiki subdirectory.
For a better organization all compiled (static) GWT files in this example go into subdirectory:
/wiki/static
and the main gwt.js is stored in
/wiki/static/gwt.js
/www/org.matheclipse.gwt.Calc
directory into the directory
/wiki/static/org.matheclipse.gwt.Calc
<meta name='gwt:module' content='/wiki/static/org.matheclipse.gwt.Calc=org.matheclipse.gwt.Calc'>
<%@ page errorPage="/WEB-INF/jsp/error.jsp"
contentType="text/html; charset=utf-8"
%>
<%@ include file="page-init.jsp" %>
<-- <GWT CSS styles follow in this section -->
<style type="text/css">
...
</style>
<script language='javascript' src='/wiki/static/gwt.js'></script>
<iframe id='__gwt_historyFrame' style='width:0;height:0;border:0'></iframe>
<div id="loadingWait">Loading...</div>
<table align="left" verticalalign="top">
<tr><td id="slot1"></td></tr>
</table>
...
<servlet>
<servlet-name>calculator</servlet-name>
<servlet-class>org.matheclipse.gwt.server.CalcServiceImpl</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
<servlet-mapping>
<servlet-name>calculator</servlet-name>
<url-pattern>/static/org.matheclipse.gwt.Calc/calc</url-pattern>
</servlet-mapping>
Eclipse Wikipedia Editor plugin 2.0.6 released
I released the Eclipse Wikipedia Editor plugin version 2.0.6.
You can download it here:
Usage:
Changes:
*.wp in the package explorer, the browser views the rendered HTML output from the corresponding file in the /wpbin subdirectory.
MathEclipse Parser API 0.0.4 released
The Math Parser API is used in the Math Eclipse Plugin for parsing math expressions. The expression parser is driven by an operator table as described in this Wikipedia article:
Download
The latest API is available as meparser.jar in this source code download:
Usage
This is a wiki page describing the Math Parser API
Related Links
Google MathEclipse news and discussion group:
MathEclipse homepage:
Example
This is a JUnit example for parsing a math string expression into an abstract syntax tree node (ASTNode):
public void testParser1() {
try {
Parser p = new Parser();
ASTNode obj = p.parseExpression("Integrate[Sin[x]^2+3*x^4, x]");
assertEquals(obj.toString(),
"Integrate[Plus[Power[Sin[x], 2], Times[3, Power[x, 4]]], x]");
} catch (Exception e) {
e.printStackTrace();
}
}
Posted by axelclk
( Oct 08 2006, 01:48:45 PM CEST )
Permalink
Comments [3]
Bliki Wikipedia Syntax rendering engine 2.0.5 released
The Bliki engine API supports the rendering of a Wikipedia article into HTML.
Download:
Description of the API usage:
The download contains the following parts
info.bliki.wiki/bliki.jar is a precompiled binary for the API
BlikiParser.java and JAMWikiModel.java parser files for JAMWiki.org (see the demo page at: http://www.bliki.info.jamwiki )
A simple wiki to html fragment looks like this:
public static void main(String[] args)
{
WikiModel wikiModel =
new WikiModel("${image}", "${title}");
String htmlStr = wikiModel.render(" [[Hello World]] wiki tag");
System.out.print(htmlStr);
}
Posted by axelclk
( Oct 08 2006, 12:18:03 PM CEST )
Permalink
Comments [5]
Eclipse Wikipedia Editor plugin version 2.0.5 released
I released version 2.0.5 of the Eclipse Wikipedia Editor plugin.
You can download the plugin here:
You can view the installation description here:
The Wikipedia Plugin contains a Wikipedia Syntax Editor with the following features:
Running JAMWiki with own parser
I've setup a JAMWiki demo instance
which uses the syntax parser from my Eclipse Wikipedia plugin project
Posted by axelclk ( Sep 14 2006, 08:32:03 PM CEST ) Permalink Comments [4]JAMWiki - some Wikipedia Syntax JUnit tests
To test the Wikipedia syntax parser in the JAMWiki project I created some simple JUnit tests.
Some test results:
ParserInput.MODE_SAVE mode to create a user signature rendering test
testPreformatedText1 the opening <pre> block was never closed
Here is the implementation:
At first a general support class for JAMWiki JUnit tests:
package org.jamwiki.parser;
import java.util.Locale;
import junit.framework.TestCase;
import org.jamwiki.model.WikiUser;
import org.jamwiki.utils.Utilities;
/**
* Support class for defining JUnit Wiki synatx tests.
*
*/
public class JAMWikiTestSupport extends TestCase
{
protected ParserInput parserInput;
public JAMWikiTestSupport(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
parserInput = new ParserInput();
parserInput.setContext("context/path");
parserInput.setLocale(Locale.ENGLISH);
WikiUser testUser = new WikiUser();
testUser.setFirstName("Mr.");
testUser.setLastName("Tester");
testUser.setDisplayName("displayName");
testUser.setLogin("dummy");
parserInput.setWikiUser(testUser);
parserInput.setUserIpAddress("127.0.0.1");
parserInput.setVirtualWiki("en");
parserInput.setAllowSectionEdit(true);
parserInput.setMode(ParserInput.MODE_SAVE);
}
protected void check(String rawWikiText, String expectedHTMLResult)
{
check(rawWikiText, expectedHTMLResult, "Test Topic");
}
protected void check(String rawWikiText, String expectedHTMLResult, String topic)
{
parserInput.setTopicName(topic);
try {
ParserOutput parserOutput = Utilities.parse(parserInput, rawWikiText, topic);
assertEquals(expectedHTMLResult, parserOutput.getContent());
} catch (Exception e) {
e.printStackTrace();
}
}
}
This class defines some simple JUnit tests, to see what HTML output the JAMWiki parser generates:
package org.jamwiki.parser;
/**
* Class for defining some simple Wiki Syntax tests.
*
*/
public class SimpleParserTests extends JAMWikiTestSupport
{
public static final String TABLE_TEST = "{| width=\"100%\" cellspacing=\"3\" cellpadding=\"6\" \n"
+ "|- valign=\"top\" \n"
+ "| width=\"40%\" bgcolor=\"#FFF4F4\" style=\"border: solid 1px #ffc9c9; padding:1em;\" cellpadding=\"0\" cellspacing=\"0\" | \n"
+ "{| cellspacing=\"0\" cellpadding=\"0\" \n" + "| bgcolor=\"#FFF4F4\" | \n"
+ "\'\'\'jamwiki.org\'\'\' is dedicated to develop a beautiful wiki\n" + "\n" + "|} \n"
+ "| width=\"60%\" bgcolor=\"#f0f0ff\" style=\"border: 1px solid #C6C9FF; padding: 1em;\" | \n"
+ "{| cellspacing=\"0\" cellpadding=\"6\" \n" + "| bgcolor=\"#f0f0ff\" |\n" + "\n" + "|}\n" + "|}\n" + "";
public SimpleParserTests(String name)
{
super(name);
}
public void testParagraph1()
{
check("This is a simple paragraph.", "<p>This is a simple paragraph.\n" + "</p>");
}
public void testBoldItalic()
{
check("'''Hello''' ''world'' '''''HelloWorld'''''", "<p><b>Hello</b> <i>world</i> <b><i>HelloWorld</b></i>\n" + "</p>");
}
public void testHead()
{
check(
"== head 1 ==\nsome text \n=== head 2 ===",
"<div style=\"font-size:90%;float:right;margin-left:5px;\">[<a href=\"context/path/en/Special:Edit?topic=Test_Topic&section=1\">Edit</a>]</div><a name=\"head_1\"></a><h2> head 1 </h2>\n"
+ "<p>some text \n"
+ "</p><div style=\"font-size:90%;float:right;margin-left:5px;\">[<a href=\"context/path/en/Special:Edit?topic=Test_Topic&section=2\">Edit</a>]</div><a name=\"head_2\"></a><h3> head 2 </h3>\n"
+ "");
}
public void testList()
{
check("* Item 1\n" + "* Item 2", "<ul><li> Item 1\n" + "</li><li> Item 2\n" + "</li></ul>");
}
public void testTable()
{
check(
TABLE_TEST,
"<table width=\"100%\" cellspacing=\"3\" cellpadding=\"6\"><tr valign=\"top\"><td width=\"40%\" bgcolor=\"#FFF4F4\" style=\"border: solid 1px #ffc9c9; padding:1em;\" cellpadding=\"0\" cellspacing=\"0\"> \n"
+ "<table cellspacing=\"0\" cellpadding=\"0\"><td bgcolor=\"#FFF4F4\"> \n"
+ "<p><b>jamwiki.org</b> is dedicated to develop a beautiful wiki</p>\n"
+ "\n"
+ "</td></tr></table>\n"
+ " \n"
+ "</td><td width=\"60%\" bgcolor=\"#f0f0ff\" style=\"border: 1px solid #C6C9FF; padding: 1em;\"> \n"
+ "<table cellspacing=\"0\" cellpadding=\"6\"><td bgcolor=\"#f0f0ff\">\n"
+ "\n"
+ "</td></tr></table>\n"
+ "\n" + "</td></tr></table>\n" + "<p><br /></p>");
}
public void testSignature()
{
check("-- ~~~", "<p>-- [[User:dummy|displayName]]\n" + "</p>");
}
public void testPreformatedText1()
{
// this seems to be wrong:
check(" < > & \" \' test ", "<pre>< > & " ' test \n");
}
public void testPreformatedText2()
{
// this seems to be wrong:
check("<pre> < > & \" ' test </pre>", "<pre> < > & " ' test </pre>\n");
}
public void testNoWiki()
{
// this seems to be wrong:
check("<nowiki>< > & \" ' test </nowiki>", "< > & " ' test \n");
}
}
Posted by axelclk
( Aug 24 2006, 07:10:50 PM CEST )
Permalink
Comments [0]
Seems that more and more people try implementing Wikipedia functionalities in Java
JAMWiki is the newest and greatest project:
Posted by axelclk ( Aug 19 2006, 05:47:48 PM CEST ) Permalink Comments [5]Debugging JavaScript errors in GWT generated code for Firefox
The first version of my MathEclipse calculator had an error in Firefox 1.5. The application run's fine under Internet Explorer 6.0.
The error messages was:
uncaught exception: com.google.gwt.core.client.JavaScriptException: JavaScript TypeError exception: seb has no properties
As a first attempt to solve this bug I installed Firebug.
With the feature Spy on XMLHttpRequest traffic I could see, that the error occured before the RPC XMLHttpRequest was send.
In the next step I set the option -style pretty in the Calc-compile.cmd script,
so that the GWT compiler generates readable JavaScript code.
Now I could see in the Firebug debugger, that in the function _$__evaluate a serialization call for the RPC requests produces an error in the function: _$writeObject(_stream, _function).
The variable _function contained the value "eval".
After renaming the function from "eval" to "$eval" the script runs perfectly under Firefox.
This bug seems to be a problem with the "eval" keyword under Firefox as explained here:
Don't rely on IndexOutOfBoundsException in GWT
The IndexOutOfBoundsException works good in the hosted GWT mode, but the generated JavaScript code doesn't ( at least for char[] arrays).
The following pattern throws an IndexOutOfBoundsException in the hosted mode, if currentPosition isn't valid. But in the JavaScript code generated by GWT source[currentPosition++], only results in an undefined character.
public String filter(String input) {
char[] source = input.toCharArray();
int currentPosition = 0;
...
...
try {
while (true) {
currentChar = source[currentPosition++];
...
// do something here
...
}
} catch (IndexOutOfBoundsException e) {
}
Posted by axelclk
( Aug 13 2006, 11:56:14 AM CEST )
Permalink
Comments [5]