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.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.io.IOException;
023    
024    import org.apache.tools.ant.BuildEvent;
025    import org.apache.tools.ant.BuildLogger;
026    import org.apache.tools.ant.DefaultLogger;
027    import org.apache.tools.ant.Project;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class AntUtil {
033    
034            public static Project getProject() {
035                    Project project = new Project();
036    
037                    BuildLogger buildLogger = new DefaultLogger() {
038    
039                            @Override
040                            public void messageLogged(BuildEvent buildEvent) {
041                                    int priority = buildEvent.getPriority();
042    
043                                    if (priority > msgOutputLevel) {
044                                            return;
045                                    }
046    
047                                    StringBundler sb = new StringBundler();
048    
049                                    try {
050                                            boolean first = true;
051    
052                                            UnsyncBufferedReader unsyncBufferedReader =
053                                                    new UnsyncBufferedReader(
054                                                            new UnsyncStringReader(buildEvent.getMessage()));
055    
056                                            String line = unsyncBufferedReader.readLine();
057    
058                                            while (line != null) {
059                                                    if (!first) {
060                                                            sb.append(StringPool.OS_EOL);
061                                                    }
062    
063                                                    first = false;
064    
065                                                    sb.append(StringPool.DOUBLE_SPACE);
066                                                    sb.append(line);
067    
068                                                    line = unsyncBufferedReader.readLine();
069                                            }
070                                    }
071                                    catch (IOException ioe) {
072                                    }
073    
074                                    String message = sb.toString();
075    
076                                    if (priority != Project.MSG_ERR) {
077                                            printMessage(message, out, priority);
078                                    }
079                                    else {
080                                            printMessage(message, err, priority);
081                                    }
082    
083                                    log(message);
084                            }
085    
086                    };
087    
088                    buildLogger.setErrorPrintStream(System.err);
089                    buildLogger.setMessageOutputLevel(Project.MSG_INFO);
090                    buildLogger.setOutputPrintStream(System.out);
091    
092                    project.addBuildListener(buildLogger);
093    
094                    return project;
095            }
096    
097    }