001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.ant;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    
019    import java.text.MessageFormat;
020    
021    import org.apache.tools.ant.BuildException;
022    import org.apache.tools.ant.Project;
023    import org.apache.tools.ant.taskdefs.Echo;
024    
025    /**
026     * @author Miguel Pastor
027     */
028    public class FormatTask extends Echo {
029    
030            @Override
031            public void execute() throws BuildException {
032                    if (message == null) {
033                            throw new BuildException("The message attribute is mandatory");
034                    }
035    
036                    Object[] arguments = _arguments.split(_separator);
037    
038                    String value = MessageFormat.format(message, arguments);
039    
040                    if (_property != null) {
041                            Project project = getProject();
042    
043                            project.setProperty(_property, value);
044                    }
045                    else {
046                            setMessage(value);
047    
048                            super.execute();
049                    }
050            }
051    
052            public void setArguments(String arguments) {
053                    _arguments = arguments;
054            }
055    
056            public void setProperty(String property) {
057                    _property = property;
058            }
059    
060            public void setSeparator(String separator) {
061                    _separator = separator;
062            }
063    
064            private String _arguments;
065            private String _property;
066            private String _separator = StringPool.COMMA;
067    
068    }