Search in sources :

Example 1 with TextParser

use of com.opensymphony.xwork2.util.TextParser in project struts by apache.

the class TextParseUtil method translateVariablesCollection.

/**
 * Resolves given expression on given ValueStack. If found element is a
 * collection each element will be converted to String. If just a single
 * object is found it is converted to String and wrapped in a collection.
 *
 * @param openChars open character array
 * @param expression expression string
 * @param stack value stack
 * @param evaluator value evaluator
 * @param excludeEmptyElements Whether empty elements shall be excluded.
 * @param maxLoopCount max loop count
 * @return converted objects
 */
public static Collection<String> translateVariablesCollection(char[] openChars, String expression, final ValueStack stack, boolean excludeEmptyElements, final ParsedValueEvaluator evaluator, int maxLoopCount) {
    ParsedValueEvaluator ognlEval = new ParsedValueEvaluator() {

        public Object evaluate(String parsedValue) {
            // no asType !!!
            return stack.findValue(parsedValue);
        }
    };
    ActionContext actionContext = stack.getActionContext();
    TextParser parser = actionContext.getContainer().getInstance(TextParser.class);
    Object result = parser.evaluate(openChars, expression, ognlEval, maxLoopCount);
    Collection<String> resultCol;
    if (result instanceof Collection) {
        @SuppressWarnings("unchecked") Collection<Object> casted = (Collection<Object>) result;
        resultCol = new ArrayList<>();
        XWorkConverter conv = actionContext.getContainer().getInstance(XWorkConverter.class);
        for (Object element : casted) {
            String stringElement = (String) conv.convertValue(actionContext.getContextMap(), element, String.class);
            if (shallBeIncluded(stringElement, excludeEmptyElements)) {
                if (evaluator != null) {
                    stringElement = evaluator.evaluate(stringElement).toString();
                }
                resultCol.add(stringElement);
            }
        }
    } else {
        resultCol = new ArrayList<>();
        String resultStr = translateVariables(expression, stack, evaluator);
        if (shallBeIncluded(resultStr, excludeEmptyElements)) {
            resultCol.add(resultStr);
        }
    }
    return resultCol;
}
Also used : XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 2 with TextParser

use of com.opensymphony.xwork2.util.TextParser in project struts by apache.

the class AbstractTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    scriptingAttrs.put("onclick", "onclick_");
    scriptingAttrs.put("ondblclick", "ondblclick_");
    scriptingAttrs.put("onmousedown", "onmousedown_");
    scriptingAttrs.put("onmouseup", "onmouseup_");
    scriptingAttrs.put("onmouseover", "onmouseover_");
    scriptingAttrs.put("onmousemove", "onmousemove_");
    scriptingAttrs.put("onmouseout", "onmouseout_");
    scriptingAttrs.put("onfocus", "onfocus_");
    scriptingAttrs.put("onblur", "onblur_");
    scriptingAttrs.put("onkeypress", "onkeypress_");
    scriptingAttrs.put("onkeydown", "onkeydown_");
    scriptingAttrs.put("onkeyup", "onkeyup_");
    scriptingAttrs.put("onselect", "onselect_");
    scriptingAttrs.put("onchange", "onchange_");
    commonAttrs.put("accesskey", "accesskey_");
    dynamicAttrs.put("data-remote", "data-remote_");
    dynamicAttrs.put("data-label", "data-label_");
    theme = new SimpleTheme();
    writer = new StringWriter();
    map = new HashMap<>();
    template = createMock(Template.class);
    stack = createNiceMock(ValueStack.class);
    setUpStack();
    stackContext = new HashMap<>();
    context = new TemplateRenderingContext(template, writer, stack, map, null);
    stackContext.put(Component.COMPONENT_STACK, new Stack<>());
    ActionContext actionContext = ActionContext.of(stackContext).bind();
    request = createNiceMock(HttpServletRequest.class);
    expect(request.getContextPath()).andReturn("/some/path").anyTimes();
    response = createNiceMock(HttpServletResponse.class);
    expect(stack.getActionContext()).andReturn(actionContext).anyTimes();
    expect(stack.getContext()).andReturn(stackContext).anyTimes();
    Container container = createNiceMock(Container.class);
    XWorkConverter converter = new ConverterEx();
    expect(container.getInstance(XWorkConverter.class)).andReturn(converter).anyTimes();
    TextParser parser = new OgnlTextParser();
    expect(container.getInstance(TextParser.class)).andReturn(parser).anyTimes();
    replay(request);
    replay(stack);
    replay(container);
    actionContext.withContainer(container).withServletRequest(request);
}
Also used : OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) ValueStack(com.opensymphony.xwork2.util.ValueStack) TextParser(com.opensymphony.xwork2.util.TextParser) OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) HttpServletResponse(javax.servlet.http.HttpServletResponse) ActionContext(com.opensymphony.xwork2.ActionContext) Template(org.apache.struts2.components.template.Template) HttpServletRequest(javax.servlet.http.HttpServletRequest) Container(com.opensymphony.xwork2.inject.Container) StringWriter(java.io.StringWriter) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter) TemplateRenderingContext(org.apache.struts2.components.template.TemplateRenderingContext)

Example 3 with TextParser

use of com.opensymphony.xwork2.util.TextParser in project struts by apache.

the class NotURLClassLoader method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    result = new EmbeddedJSPResult();
    request = EasyMock.createNiceMock(HttpServletRequest.class);
    response = new MockHttpServletResponse();
    context = new MockServletContext();
    config = new MockServletConfig(context);
    final Map params = new HashMap();
    HttpSession session = EasyMock.createNiceMock(HttpSession.class);
    EasyMock.replay(session);
    EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
    EasyMock.expect(request.getParameterMap()).andReturn(params).anyTimes();
    EasyMock.expect(request.getParameter("username")).andAnswer(() -> ActionContext.getContext().getParameters().get("username").getValue());
    EasyMock.expect(request.getAttribute("something")).andReturn("somethingelse").anyTimes();
    EasyMock.replay(request);
    // mock value stack
    ValueStack valueStack = EasyMock.createNiceMock(ValueStack.class);
    EasyMock.expect(valueStack.getActionContext()).andReturn(ActionContext.getContext()).anyTimes();
    EasyMock.replay(valueStack);
    // mock converter
    XWorkConverter converter = new DummyConverter();
    DefaultFileManager fileManager = new DefaultFileManager();
    fileManager.setReloadingConfigs(false);
    // mock container
    Container container = EasyMock.createNiceMock(Container.class);
    EasyMock.expect(container.getInstance(XWorkConverter.class)).andReturn(converter).anyTimes();
    TextParser parser = new OgnlTextParser();
    EasyMock.expect(container.getInstance(TextParser.class)).andReturn(parser).anyTimes();
    EasyMock.expect(container.getInstanceNames(FileManager.class)).andReturn(new HashSet<>()).anyTimes();
    EasyMock.expect(container.getInstance(FileManager.class)).andReturn(fileManager).anyTimes();
    UrlHelper urlHelper = new DefaultUrlHelper();
    EasyMock.expect(container.getInstance(UrlHelper.class)).andReturn(urlHelper).anyTimes();
    FileManagerFactory fileManagerFactory = new DummyFileManagerFactory();
    EasyMock.expect(container.getInstance(FileManagerFactory.class)).andReturn(fileManagerFactory).anyTimes();
    EasyMock.replay(container);
    ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).withServletRequest(request).withServletResponse(response).withServletContext(context).withContainer(container).withValueStack(valueStack).bind();
}
Also used : OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) UrlHelper(org.apache.struts2.views.util.UrlHelper) ValueStack(com.opensymphony.xwork2.util.ValueStack) TextParser(com.opensymphony.xwork2.util.TextParser) OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) MockServletConfig(org.springframework.mock.web.MockServletConfig) MockServletContext(org.springframework.mock.web.MockServletContext) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) FileManager(com.opensymphony.xwork2.FileManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Container(com.opensymphony.xwork2.inject.Container) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HashSet(java.util.HashSet)

Aggregations

XWorkConverter (com.opensymphony.xwork2.conversion.impl.XWorkConverter)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 Container (com.opensymphony.xwork2.inject.Container)2 OgnlTextParser (com.opensymphony.xwork2.util.OgnlTextParser)2 TextParser (com.opensymphony.xwork2.util.TextParser)2 ValueStack (com.opensymphony.xwork2.util.ValueStack)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 FileManager (com.opensymphony.xwork2.FileManager)1 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 Template (org.apache.struts2.components.template.Template)1 TemplateRenderingContext (org.apache.struts2.components.template.TemplateRenderingContext)1 DefaultUrlHelper (org.apache.struts2.views.util.DefaultUrlHelper)1 UrlHelper (org.apache.struts2.views.util.UrlHelper)1