001    /**
002     * Copyright (c) 2000-present 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.Tuple;
019    
020    import java.util.ArrayList;
021    import java.util.LinkedHashSet;
022    import java.util.List;
023    import java.util.Set;
024    import java.util.concurrent.atomic.AtomicReference;
025    
026    /**
027     * @author Hugo Huijser
028     */
029    public class SourceFormatter {
030    
031            public static void main(String[] args) {
032                    try {
033                            SourceFormatter sourceFormatter = SourceFormatterUtil.create(
034                                    false, false, true, true);
035    
036                            sourceFormatter.format();
037                    }
038                    catch (Throwable t) {
039                            t.printStackTrace();
040                    }
041            }
042    
043            public SourceFormatter(
044                            boolean useProperties, boolean throwException, boolean printErrors,
045                            boolean autoFix)
046                    throws Exception {
047    
048                    _useProperties = useProperties;
049                    _throwException = throwException;
050                    _printErrors = printErrors;
051                    _autoFix = autoFix;
052            }
053    
054            public void format() throws Throwable {
055                    final AtomicReference<Throwable> exceptionReference1 =
056                            new AtomicReference<Throwable>();
057    
058                    Thread thread1 = new Thread () {
059    
060                            @Override
061                            public void run() {
062                                    try {
063                                            List<SourceProcessor> sourceProcessors =
064                                                    new ArrayList<SourceProcessor>();
065    
066                                            sourceProcessors.add(
067                                                    CSSSourceProcessor.class.newInstance());
068                                            sourceProcessors.add(
069                                                    FTLSourceProcessor.class.newInstance());
070                                            sourceProcessors.add(
071                                                    JSPSourceProcessor.class.newInstance());
072                                            sourceProcessors.add(JSSourceProcessor.class.newInstance());
073                                            sourceProcessors.add(
074                                                    PropertiesSourceProcessor.class.newInstance());
075                                            sourceProcessors.add(SHSourceProcessor.class.newInstance());
076                                            sourceProcessors.add(
077                                                    SQLSourceProcessor.class.newInstance());
078                                            sourceProcessors.add(
079                                                    TLDSourceProcessor.class.newInstance());
080    
081                                            for (SourceProcessor sourceProcessor : sourceProcessors) {
082                                                    _runSourceProcessor(sourceProcessor);
083                                            }
084                                    }
085                                    catch (Throwable t) {
086                                            t.printStackTrace();
087    
088                                            exceptionReference1.set(t);
089                                    }
090                            }
091    
092                    };
093    
094                    final AtomicReference<Throwable> exceptionReference2 =
095                            new AtomicReference<Throwable>();
096    
097                    Thread thread2 = new Thread () {
098    
099                            @Override
100                            public void run() {
101                                    try {
102                                            List<SourceProcessor> sourceProcessors =
103                                                    new ArrayList<SourceProcessor>();
104    
105                                            sourceProcessors.add(
106                                                    JavaSourceProcessor.class.newInstance());
107                                            sourceProcessors.add(
108                                                    XMLSourceProcessor.class.newInstance());
109    
110                                            for (SourceProcessor sourceProcessor : sourceProcessors) {
111                                                    _runSourceProcessor(sourceProcessor);
112                                            }
113                                    }
114                                    catch (Throwable t) {
115                                            t.printStackTrace();
116    
117                                            exceptionReference2.set(t);
118                                    }
119                            }
120    
121                    };
122    
123                    thread1.start();
124                    thread2.start();
125    
126                    thread1.join();
127                    thread2.join();
128    
129                    Throwable throwable1 = exceptionReference1.get();
130                    Throwable throwable2 = exceptionReference2.get();
131    
132                    if (throwable1 != null) {
133                            if (throwable2 != null) {
134                                    throwable1.addSuppressed(throwable2);
135                            }
136    
137                            throw throwable1;
138                    }
139                    else if (throwable2 != null) {
140                            throw throwable2;
141                    }
142    
143                    if (_throwException) {
144                            if (!_errorMessages.isEmpty()) {
145                                    throw new Exception(StringUtil.merge(_errorMessages, "\n"));
146                            }
147    
148                            if (_firstSourceMismatchException != null) {
149                                    throw _firstSourceMismatchException;
150                            }
151                    }
152            }
153    
154            public Tuple format(String fileName) throws Exception {
155                    SourceProcessor sourceProcessor = null;
156    
157                    if (fileName.endsWith(".testjava")) {
158                            sourceProcessor = JavaSourceProcessor.class.newInstance();
159                    }
160                    else if (fileName.endsWith(".testsql")) {
161                            sourceProcessor = SQLSourceProcessor.class.newInstance();
162                    }
163                    else if (fileName.endsWith(".testtld")) {
164                            sourceProcessor = TLDSourceProcessor.class.newInstance();
165                    }
166                    else if (fileName.endsWith(".testxml")) {
167                            sourceProcessor = XMLSourceProcessor.class.newInstance();
168                    }
169    
170                    if (sourceProcessor == null) {
171                            return null;
172                    }
173    
174                    String newContent = sourceProcessor.format(
175                            fileName, _useProperties, _printErrors, _autoFix);
176    
177                    return new Tuple(newContent, sourceProcessor.getErrorMessages());
178            }
179    
180            private void _runSourceProcessor(SourceProcessor sourceProcessor)
181                    throws Exception {
182    
183                    sourceProcessor.format(_useProperties, _printErrors, _autoFix);
184    
185                    _errorMessages.addAll(sourceProcessor.getErrorMessages());
186    
187                    if (_firstSourceMismatchException == null) {
188                            _firstSourceMismatchException =
189                                    sourceProcessor.getFirstSourceMismatchException();
190                    }
191            }
192    
193            private final boolean _autoFix;
194            private final Set<String> _errorMessages = new LinkedHashSet<String>();
195            private SourceMismatchException _firstSourceMismatchException;
196            private final boolean _printErrors;
197            private final boolean _throwException;
198            private final boolean _useProperties;
199    
200    }