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