/*******************************************************************************
* 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.webtml;
import de.innovationgate.wgpublisher.expressions.ExpressionEngineFactory;
import de.innovationgate.wgpublisher.expressions.ExpressionResult;
import de.innovationgate.wgpublisher.expressions.tmlscript.RhinoExpressionEngine;
import de.innovationgate.wgpublisher.webtml.utils.TMLException;
public class Option extends Base {
private String name;
private String mode;
private String scope;
private String expression;
private String _default;
private String _defaultexpression;
/**
* @see Base#tmlEndTag()
*/
public void tmlEndTag() throws TMLException {
Object result = this.getResultString();
boolean setMode = false;
String theMode = getMode();
// If mode-Att is set, determine mode by it
if (theMode != null) {
if (theMode.equals("set") || theMode.equals("write")) {
setMode = true;
}
}
// Else determine by the presence of tag content
else {
setMode = !result.equals("") || getExpression() != null;
}
// Get mode
if (!setMode) {
Object value = getOption(this.getName());
if (value == null) {
// Try to find a default value
String defaultValue = getDefault();
String defaultExpression = getDefaultexpression();
if (defaultValue == null && defaultExpression == null) {
return;
}
if (defaultValue != null) {
value = defaultValue;
}
else if (defaultExpression != null) {
value = calculateOptionByExpression(defaultExpression);
}
}
this.setResult(value);
}
// Set mode
else {
this.setResultOutput(false);
BaseTagStatus parent = getStatus().getParentTag();
if (parent == null) {
this.addWarning("Parent tag is no option receptor", true);
return;
}
String expr = getExpression();
if (expr != null) {
result = calculateOptionByExpression(expr);
}
try {
parent.setOption(this.getName(), result, scope);
}
catch (IllegalArgumentException e) {
addWarning(e.getMessage(), true);
}
}
}
private Object calculateOptionByExpression(String expr) throws TMLException {
Object result = null;
RhinoExpressionEngine engine = ExpressionEngineFactory.getTMLScriptEngine();
ExpressionResult exprResult = engine.evaluateExpression(expr, getTMLContext(), RhinoExpressionEngine.TYPE_EXPRESSION, null);
if (!exprResult.isError()) {
result = exprResult.getResult();
return result;
}
else {
throw new TMLException("Error evaluation option expression: " + exprResult.getException().getClass() + exprResult.getException().getMessage(), true);
}
}
/**
* Gets the name
* @return Returns a String
*/
public String getName() {
return this.getTagAttributeValue("name", name, "");
}
/**
* Sets the name
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the mode.
*/
public String getMode() {
return getTagAttributeValue("mode", mode, null);
}
/**
* @param mode The mode to set.
*/
public void setMode(String mode) {
this.mode = mode;
}
public String getScope() {
return getTagAttributeValue("scope", scope, null);
}
public void setScope(String scope) {
this.scope = scope;
}
public String getExpression() {
return getTagAttributeValue("expression", expression, null);
}
public void setExpression(String expression) {
this.expression = expression;
}
public String getDefault() {
return getTagAttributeValue("default", _default, null);
}
public void setDefault(String default1) {
_default = default1;
}
public String getDefaultexpression() {
return getTagAttributeValue("defaultexpression", _defaultexpression, null);
}
public void setDefaultexpression(String defaultexpression) {
_defaultexpression = defaultexpression;
}
}