001/*
002 * Copyright (c) 2004-2010, Kohsuke Kawaguchi
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
006 * that the following conditions are met:
007 *
008 *     * Redistributions of source code must retain the above copyright notice, this list of
009 *       conditions and the following disclaimer.
010 *     * Redistributions in binary form must reproduce the above copyright notice, this list of
011 *       conditions and the following disclaimer in the documentation and/or other materials
012 *       provided with the distribution.
013 *
014 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
015 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
016 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
017 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
018 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
019 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
020 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
021 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022 */
023
024package org.kohsuke.stapler.jelly;
025
026import org.apache.commons.jelly.JellyTagException;
027import org.apache.commons.jelly.XMLOutput;
028import org.apache.commons.jelly.LocationAware;
029import org.jvnet.localizer.LocaleProvider;
030import org.jvnet.maven.jellydoc.annotation.Required;
031import org.xml.sax.SAXException;
032
033import java.util.ArrayList;
034import java.util.List;
035
036/**
037 * Format message from a resource, but by using a nested children as arguments, instead of just using expressions.
038 * 
039 * @author Kohsuke Kawaguchi
040 */
041public class StructuredMessageFormatTag extends AbstractStaplerTag implements LocationAware {
042    private final List<Object> arguments = new ArrayList<Object>();
043
044    private String key;
045    private ResourceBundle rb;
046
047    @Required
048    public void setKey(String resourceKey) {
049        this.key = resourceKey;
050    }
051
052    public void addArgument(Object o) {
053        this.arguments.add(o);
054    }
055
056    public void doTag(XMLOutput output) throws JellyTagException {
057        try {
058            arguments.clear();
059            invokeBody(output);
060
061            output.write(rb.format(LocaleProvider.getLocale(), key,arguments.toArray()));
062        } catch (SAXException e) {
063            throw new JellyTagException("could not write the XMLOutput",e);
064        } finally {
065            arguments.clear(); // don't keep heavy objects in memory for too long
066        }
067    }
068
069    public int getLineNumber() {
070        return -1;
071    }
072
073    public void setLineNumber(int lineNumber) {
074    }
075
076    public int getColumnNumber() {
077        return -1;
078    }
079
080    public void setColumnNumber(int columnNumber) {
081    }
082
083    public String getFileName() {
084        return null;
085    }
086
087    public void setFileName(String fileName) {
088        rb = ResourceBundle.load(fileName);
089    }
090
091    public String getElementName() {
092        return null;
093    }
094
095    public void setElementName(String elementName) {
096    }
097}