/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package de.innovationgate.wgpublisher.expressions.tmlscript;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import de.innovationgate.ext.org.mozilla.javascript.Context;
import de.innovationgate.ext.org.mozilla.javascript.EvaluatorException;
import de.innovationgate.ext.org.mozilla.javascript.Function;
import de.innovationgate.ext.org.mozilla.javascript.ImporterTopLevel;
import de.innovationgate.ext.org.mozilla.javascript.JavaScriptException;
import de.innovationgate.ext.org.mozilla.javascript.NativeJavaClass;
import de.innovationgate.ext.org.mozilla.javascript.NativeObject;
import de.innovationgate.ext.org.mozilla.javascript.Scriptable;
import de.innovationgate.ext.org.mozilla.javascript.WrappedException;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.webgate.api.WGDatabase;
import de.innovationgate.webgate.api.WGException;
import de.innovationgate.wga.common.CodeCompletion;
import de.innovationgate.wgpublisher.WGACore;
import de.innovationgate.wgpublisher.expressions.tmlscript.VarArgParser.Arguments;
import de.innovationgate.wgpublisher.expressions.tmlscript.wgaglobal.Design;
import de.innovationgate.wgpublisher.expressions.tmlscript.wgaglobal.Master;
import de.innovationgate.wgpublisher.expressions.tmlscript.wgaglobal.WGAGlobal;
import de.innovationgate.wgpublisher.expressions.tmlscript.wgaglobal.Xml;
import de.innovationgate.wgpublisher.webtml.utils.TMLContext;
@CodeCompletion(delegate=RhinoScopeCC.class)
public class RhinoScope extends ImporterTopLevel {
private static final String WGAGLOBAL_NAME = "WGA";
private static VarArgParser _localLabelVarargs;
static {
_localLabelVarargs = VarArgParser.create("localLabel")
.add("currentObject", NativeObject.class, true)
.add("container", String.class, true)
.add("file", String.class, true)
.add("key", String.class, false)
.add("params", List.class, true)
.setCompatibleMode(true)
.pack();
}
private boolean _inited = false;
private WGAGlobal _wgaGlobal = new WGAGlobal();
public RhinoScope() {
super();
String[] functions = { "javaObject", "scriptObject", "parseXML", "xpath", "xpathList", "format", "sortList", "deleteDoubles", "deleteDoublets", "callAction", "synchronizedFunction", "loadObjectDefinition", "createObject",
"logException", "getLookupKeys", "localLabel", "localDB", "registerGlobal", "serializeObject", "deserializeObject", "runMasterFunction", "runMasterMethod"};
defineFunctionProperties(functions, RhinoScope.class, DONTENUM);
}
public static Scriptable javaObject(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
return WGAGlobal.javaObject(cx, thisObj, args, funObj);
}
public static Scriptable synchronizedFunction(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
return WGAGlobal.synchronizedFunction(cx, thisObj, args, funObj);
}
public static Scriptable scriptObject(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
return WGAGlobal.scriptObject(cx, thisObj, args, funObj);
}
/**
* @see de.innovationgate.ext.org.mozilla.javascript.Scriptable#getClassName()
*/
public String getClassName() {
return this.getClass().getName();
}
public static Document parseXML(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
try {
String xml = String.valueOf(args[0]).trim();
return Xml.parse(xml);
}
catch (DocumentException e) {
throw new WrappedException(e);
}
}
public static Object xpath(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
try {
Object object = args[0];
String xpath = args[1].toString();
return Context.javaToJS(Xml.xpath(object, xpath), thisObj);
}
catch (DocumentException e) {
return null;
}
}
public static Object xpathList(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
try {
Object object = args[0];
String xpath = args[1].toString();
return Context.javaToJS(Xml.xpathList(object, xpath), thisObj);
}
catch (DocumentException e) {
return null;
}
}
public static String format(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException {
return WGAGlobal.format(cx, thisObj, args, funObj);
}
public static List sortList(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException {
return WGAGlobal.sortList(cx, thisObj, args, funObj);
}
public static List deleteDoubles(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException {
return WGAGlobal.deleteDoublets(cx, thisObj, args, funObj);
}
public static List deleteDoublets(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException {
return WGAGlobal.deleteDoublets(cx, thisObj, args, funObj);
}
public static Object callAction(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException {
return WGAGlobal.callAction(cx, thisObj, args, funObj);
}
public static Function loadObjectDefinition(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException {
return WGAGlobal.loadObjectDefinition(cx, thisObj, args, funObj);
}
public synchronized void init(Context cx, WGACore wgaCore) {
if (!_inited) {
initStandardObjects(cx, false);
_wgaGlobal.setParentScope(this);
_wgaGlobal.put("Core", _wgaGlobal, wgaCore);
_wgaGlobal.put("Utils", _wgaGlobal, new NativeJavaClass(_wgaGlobal, WGUtils.class));
_inited = true;
_wgaGlobal.sealObject();
sealObject();
}
}
/**
* @return Returns the inited.
*/
public boolean isInited() {
return _inited;
}
public static Scriptable createObject(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException {
return WGAGlobal.createObject(cx, thisObj, args, funObj);
}
public static void logException(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
WGAGlobal.logException(cx, thisObj, args, funObj);
}
public static List getLookupKeys(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException {
return WGAGlobal.getLookupKeys(cx, thisObj, args, funObj);
}
public static WGDatabase localDB(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException, WGException {
Design design;
if (args.length == 0) {
design = Design.getCurrent();
}
else if (args[0] instanceof NativeObject) {
design = new Design((NativeObject) args[0]);
}
else {
throw new EvaluatorException("Global function localDB() needs either no or a custom TMLScript object as parameter");
}
return Design.db(cx, design, args, funObj);
}
public static String localLabel(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException, WGException {
TMLContext context = WGAGlobal.fetchInitialContext(cx);
Arguments parsedArgs = _localLabelVarargs.parse(args);
WGDatabase localDB = localDB(cx, thisObj, new Object[] {parsedArgs.get("currentObject")}, funObj);
return context.label(localDB, (String) parsedArgs.get("container"), (String) parsedArgs.get("file"), (String) parsedArgs.get("key"), (List) parsedArgs.get("params"));
}
public static void registerGlobal(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws JavaScriptException, WGException {
Design.registerGlobal(cx, Design.getCurrent(), args, funObj);
}
public static String serializeObject(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
return WGAGlobal.serializeObject(cx, thisObj, args, funObj);
}
public static Object deserializeObject(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
return WGAGlobal.deserializeObject(cx, thisObj, args, funObj);
}
public Object get(String name, Scriptable start) {
// Return the shared WGA Global
if (WGAGLOBAL_NAME.equals(name)) {
return _wgaGlobal;
}
// Return scope objects
Object result = super.get(name, start);
// Unknown references in TMLScript return null instead of throwing a reference error
if (result == Scriptable.NOT_FOUND) {
return null;
}
else {
return result;
}
}
public static Object runMasterFunction(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
return Master.runFunction(cx, thisObj, args, funObj);
}
public static Object runMasterMethod(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
return Master.runMethod(cx, thisObj, args, funObj);
}
@Override
public boolean has(String name, Scriptable start) {
if (WGAGLOBAL_NAME.equals(name)) {
return true;
}
return super.has(name, start);
}
public WGAGlobal getWgaGlobal() {
return _wgaGlobal;
}
}