
org.kohsuke.stapler.jelly.InternationalizedStringExpression Maven / Gradle / Ivy
Show all versions of stapler-jelly Show documentation
/*******************************************************************************
*
* Copyright (c) 2004-2010 Oracle Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
* Kohsuke Kawaguchi
*
*******************************************************************************/
package org.kohsuke.stapler.jelly;
import org.apache.commons.jelly.expression.Expression;
import org.apache.commons.jelly.expression.ExpressionSupport;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.JellyContext;
import org.jvnet.localizer.LocaleProvider;
import org.kohsuke.stapler.Stapler;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
/**
* Expression of the form "%messageName(arg1,arg2,...)" that represents
* internationalized text.
*
*
* The "(arg1,...)" portion is optional and can be ommitted. Each argument
* is assumed to be a parenthesis-balanced expression and passed to
* {@link JellyClassLoaderTearOff#EXPRESSION_FACTORY} to be parsed.
*
*
* The message resource is loaded from files like "xyz.properties" and
* "xyz_ja.properties" when the expression is placed in "xyz.jelly".
*
*
* @author Kohsuke Kawaguchi
*/
public class InternationalizedStringExpression extends ExpressionSupport {
public final ResourceBundle resourceBundle;
private final Expression[] arguments;
public final String key;
public final String expressionText;
public InternationalizedStringExpression(ResourceBundle resourceBundle, String text) throws JellyException {
this.resourceBundle = resourceBundle;
this.expressionText = text;
if(!text.startsWith("%"))
throw new JellyException(text+" doesn't start with %");
text = text.substring(1);
int idx = text.indexOf('(');
if(idx<0) {
// no arguments
key = text;
arguments = EMPTY_ARGUMENTS;
return;
}
List args = new ArrayList();
key = text.substring(0,idx);
text = text.substring(idx+1); // at this point text="arg,arg)"
while(text.length()>0) {
String token = tokenize(text);
args.add(JellyClassLoaderTearOff.EXPRESSION_FACTORY.createExpression(token));
text = text.substring(token.length()+1);
}
this.arguments = args.toArray(new Expression[args.size()]);
}
public List getArguments() {
return Collections.unmodifiableList(Arrays.asList(arguments));
}
/**
* Takes a string like "arg)" or "arg,arg,...)", then
* find "arg" and returns it.
*
* Note: this code is also copied into idea-stapler-plugin,
* so don't forget to update that when this code changes.
*/
private String tokenize(String text) throws JellyException {
int parenthesis=0;
for(int idx=0;idx