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.portal.tools.sourceformatter;
016    
017    import com.liferay.portal.kernel.util.StringUtil;
018    import com.liferay.portal.kernel.util.UniqueList;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    /**
024     * @author Hugo Huijser
025     */
026    public class SourceFormatter {
027    
028            public static void main(String[] args) {
029                    try {
030                            new SourceFormatter(false, false);
031                    }
032                    catch (Exception e) {
033                            e.printStackTrace();
034                    }
035            }
036    
037            public SourceFormatter(
038                            final boolean useProperties, final boolean throwException)
039                    throws Exception {
040    
041                    Thread thread1 = new Thread () {
042    
043                            @Override
044                            public void run() {
045                                    try {
046                                            List<SourceProcessor> sourceProcessors =
047                                                    new ArrayList<SourceProcessor>();
048    
049                                            sourceProcessors.add(
050                                                    FTLSourceProcessor.class.newInstance());
051                                            sourceProcessors.add(
052                                                    JavaSourceProcessor.class.newInstance());
053                                            sourceProcessors.add(JSSourceProcessor.class.newInstance());
054                                            sourceProcessors.add(
055                                                    PropertiesSourceProcessor.class.newInstance());
056                                            sourceProcessors.add(SHSourceProcessor.class.newInstance());
057                                            sourceProcessors.add(
058                                                    SQLSourceProcessor.class.newInstance());
059                                            sourceProcessors.add(
060                                                    TLDSourceProcessor.class.newInstance());
061                                            sourceProcessors.add(
062                                                    XMLSourceProcessor.class.newInstance());
063    
064                                            for (SourceProcessor sourceProcessor : sourceProcessors) {
065                                                    sourceProcessor.format(useProperties, throwException);
066    
067                                                    _errorMessages.addAll(
068                                                            sourceProcessor.getErrorMessages());
069                                            }
070                                    }
071                                    catch (Exception e) {
072                                            e.printStackTrace();
073                                    }
074                            }
075    
076                    };
077    
078                    Thread thread2 = new Thread () {
079    
080                            @Override
081                            public void run() {
082                                    try {
083                                            SourceProcessor sourceProcessor =
084                                                    JSPSourceProcessor.class.newInstance();
085    
086                                            sourceProcessor.format(useProperties, throwException);
087    
088                                            _errorMessages.addAll(sourceProcessor.getErrorMessages());
089                                    }
090                                    catch (Exception e) {
091                                            e.printStackTrace();
092                                    }
093                            }
094    
095                    };
096    
097                    thread1.start();
098                    thread2.start();
099    
100                    thread1.join();
101                    thread2.join();
102    
103                    if (throwException && !_errorMessages.isEmpty()) {
104                            System.out.println(StringUtil.merge(_errorMessages, "\n"));
105                    }
106            }
107    
108            private static List<String> _errorMessages = new UniqueList<String>();
109    
110    }