use of javax.script.ScriptContext in project xwiki-platform by xwiki.
the class PropertyClass method displayCustom.
public void displayCustom(StringBuffer buffer, String fieldName, String prefix, String type, BaseObject object, final XWikiContext context) throws XWikiException {
String content = "";
try {
ScriptContext scontext = Utils.getComponent(ScriptContextManager.class).getCurrentScriptContext();
scontext.setAttribute("name", fieldName, ScriptContext.ENGINE_SCOPE);
scontext.setAttribute("prefix", prefix, ScriptContext.ENGINE_SCOPE);
// The PropertyClass instance can be used to access meta properties in the custom displayer (e.g.
// dateFormat, multiSelect). It can be obtained from the XClass of the given object but only if the property
// has been added to the XClass. We need to have it in the Velocity context for the use case when an XClass
// property needs to be previewed before being added to the XClass.
scontext.setAttribute("field", new com.xpn.xwiki.api.PropertyClass(this, context), ScriptContext.ENGINE_SCOPE);
scontext.setAttribute("object", new com.xpn.xwiki.api.Object(object, context), ScriptContext.ENGINE_SCOPE);
scontext.setAttribute("type", type, ScriptContext.ENGINE_SCOPE);
BaseProperty prop = (BaseProperty) object.safeget(fieldName);
if (prop != null) {
scontext.setAttribute("value", prop.getValue(), ScriptContext.ENGINE_SCOPE);
} else {
// The $value property can exist in the velocity context, we overwrite it to make sure we don't get a
// wrong value in the displayer when the property does not exist yet.
scontext.setAttribute("value", null, ScriptContext.ENGINE_SCOPE);
}
String customDisplayer = getCachedDefaultCustomDisplayer(context);
if (StringUtils.isNotEmpty(customDisplayer)) {
if (customDisplayer.equals(CLASS_DISPLAYER_IDENTIFIER)) {
final String rawContent = getCustomDisplay();
XWikiDocument classDocument = context.getWiki().getDocument(getObject().getDocumentReference(), context);
final String classSyntax = classDocument.getSyntax().toIdString();
// Using author reference since the document content is not relevant in this case.
DocumentReference authorReference = classDocument.getAuthorReference();
// Make sure we render the custom displayer with the rights of the user who wrote it (i.e. class
// document author).
content = renderContentInContext(rawContent, classSyntax, authorReference, context);
} else if (customDisplayer.startsWith(DOCUMENT_DISPLAYER_IDENTIFIER_PREFIX)) {
XWikiDocument displayerDoc = context.getWiki().getDocument(StringUtils.substringAfter(customDisplayer, DOCUMENT_DISPLAYER_IDENTIFIER_PREFIX), context);
final String rawContent = displayerDoc.getContent();
final String displayerDocSyntax = displayerDoc.getSyntax().toIdString();
DocumentReference authorReference = displayerDoc.getContentAuthorReference();
// Make sure we render the custom displayer with the rights of the user who wrote it (i.e. displayer
// document content author).
content = renderContentInContext(rawContent, displayerDocSyntax, authorReference, context);
} else if (customDisplayer.startsWith(TEMPLATE_DISPLAYER_IDENTIFIER_PREFIX)) {
content = context.getWiki().evaluateTemplate(StringUtils.substringAfter(customDisplayer, TEMPLATE_DISPLAYER_IDENTIFIER_PREFIX), context);
}
}
} catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_CLASSES, XWikiException.ERROR_XWIKI_CLASSES_CANNOT_PREPARE_CUSTOM_DISPLAY, "Exception while preparing the custom display of " + fieldName, e, null);
}
buffer.append(content);
}
use of javax.script.ScriptContext in project xwiki-platform by xwiki.
the class StubVelocityManager method evaluate.
@Override
public boolean evaluate(Writer out, String templateName, Reader source) throws XWikiVelocityException {
// Get up to date Velocity context
VelocityContext velocityContext = getVelocityContext();
// Execute Velocity context
boolean result = getVelocityEngine().evaluate(velocityContext, out, templateName, source);
// Update current script context with potentially modified Velocity context
ScriptContext scontext = this.scriptContextManager.getCurrentScriptContext();
for (Object vkey : velocityContext.getKeys()) {
if (vkey instanceof String) {
String svkey = (String) vkey;
// context is a reserved binding in JSR223 specification
if (!"context".equals(svkey)) {
scontext.setAttribute(svkey, velocityContext.get(svkey), ScriptContext.ENGINE_SCOPE);
}
}
}
return result;
}
use of javax.script.ScriptContext in project jmeter-plugins by undera.
the class WebDriverSamplerTest method shouldHaveExpectedInstanceVariablesOnScriptContext.
@Test
public void shouldHaveExpectedInstanceVariablesOnScriptContext() {
sampler.setName("name");
sampler.setParameters("p1 p2 p3");
final SampleResult sampleResult = new SampleResult();
final ScriptEngine scriptEngine = sampler.createScriptEngineWith(sampleResult);
final ScriptContext scriptContext = scriptEngine.getContext();
final WebDriverScriptable scriptable = (WebDriverScriptable) scriptContext.getAttribute("WDS");
assertThat(scriptable.getLog(), is(instanceOf(Logger.class)));
assertThat(scriptable.getName(), is(sampler.getName()));
assertThat(scriptable.getParameters(), is(sampler.getParameters()));
assertThat(scriptable.getArgs(), is(new String[] { "p1", "p2", "p3" }));
assertThat(scriptable.getBrowser(), is(instanceOf(WebDriver.class)));
assertThat(scriptable.getSampleResult(), is(sampleResult));
}
use of javax.script.ScriptContext in project teiid by teiid.
the class TeiidScriptEngine method compile.
@Override
public CompiledScript compile(String script) throws ScriptException {
final String[] parts = splitter.split(script);
final int[] indexes = new int[parts.length];
for (int i = 1; i < parts.length; i++) {
String string = parts[i];
for (int j = 0; j < string.length(); j++) {
if (!Character.isJavaIdentifierPart(string.charAt(j))) {
throw new ScriptException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30431, string, string.charAt(j)));
}
}
try {
indexes[i] = Integer.parseInt(string);
} catch (NumberFormatException e) {
indexes[i] = -1;
}
}
return new CompiledScript() {
@Override
public ScriptEngine getEngine() {
return TeiidScriptEngine.this;
}
@Override
public Object eval(ScriptContext sc) throws ScriptException {
if (sc == null) {
throw new NullPointerException();
}
Object obj = null;
if (parts.length > 0) {
obj = sc.getAttribute(parts[0]);
}
for (int i = 1; i < parts.length; i++) {
if (obj == null) {
return null;
}
String part = parts[i];
Map<String, Method> methodMap = getMethodMap(obj.getClass());
Method m = methodMap.get(part);
if (m == null) {
int index = indexes[i];
if (index > 0) {
// assume it's a list/array
if (obj instanceof List) {
try {
obj = ((List<?>) obj).get(index - 1);
} catch (IndexOutOfBoundsException e) {
obj = null;
}
continue;
}
try {
obj = FunctionMethods.array_get(obj, index);
continue;
} catch (FunctionExecutionException e) {
throw new ScriptException(e);
} catch (SQLException e) {
throw new ScriptException(e);
}
}
throw new ScriptException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31111, part, obj.getClass()));
}
try {
obj = m.invoke(obj);
} catch (IllegalAccessException e) {
throw new ScriptException(e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw new ScriptException((Exception) e.getCause());
}
throw new ScriptException(e);
}
}
return obj;
}
};
}
use of javax.script.ScriptContext in project ofbiz-framework by apache.
the class ScriptUtil method createScriptContext.
/**
* Returns a <code>ScriptContext</code> that contains the members of <code>context</code>.
* <p>If a <code>CompiledScript</code> instance is to be shared by multiple threads, then
* each thread must create its own <code>ScriptContext</code> and pass it to the
* <code>CompiledScript</code> eval method.</p>
*
* @param context
* @return
*/
public static ScriptContext createScriptContext(Map<String, Object> context) {
Assert.notNull("context", context);
Map<String, Object> localContext = new HashMap<>(context);
localContext.put(WIDGET_CONTEXT_KEY, context);
localContext.put("context", context);
ScriptContext scriptContext = new SimpleScriptContext();
ScriptHelper helper = createScriptHelper(scriptContext);
if (helper != null) {
localContext.put(SCRIPT_HELPER_KEY, helper);
}
Bindings bindings = new SimpleBindings(localContext);
scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
return scriptContext;
}
Aggregations