001
014
015 package com.liferay.portal.tools.sourceformatter;
016
017 import com.liferay.portal.kernel.util.ReleaseInfo;
018 import com.liferay.portal.kernel.util.StringUtil;
019 import com.liferay.portal.kernel.util.Tuple;
020 import com.liferay.portal.kernel.util.UniqueList;
021
022 import java.util.ArrayList;
023 import java.util.List;
024
025
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 _setVersion();
053 }
054
055 public void format() throws Exception {
056 Thread thread1 = new Thread () {
057
058 @Override
059 public void run() {
060 try {
061 List<SourceProcessor> sourceProcessors =
062 new ArrayList<SourceProcessor>();
063
064 sourceProcessors.add(
065 CSSSourceProcessor.class.newInstance());
066 sourceProcessors.add(
067 FTLSourceProcessor.class.newInstance());
068 sourceProcessors.add(
069 JavaSourceProcessor.class.newInstance());
070 sourceProcessors.add(JSSourceProcessor.class.newInstance());
071 sourceProcessors.add(
072 PropertiesSourceProcessor.class.newInstance());
073 sourceProcessors.add(SHSourceProcessor.class.newInstance());
074 sourceProcessors.add(
075 SQLSourceProcessor.class.newInstance());
076 sourceProcessors.add(
077 TLDSourceProcessor.class.newInstance());
078 sourceProcessors.add(
079 XMLSourceProcessor.class.newInstance());
080
081 for (SourceProcessor sourceProcessor : sourceProcessors) {
082 sourceProcessor.format(
083 _useProperties, _printErrors, _autoFix,
084 _mainReleaseVersion);
085
086 _errorMessages.addAll(
087 sourceProcessor.getErrorMessages());
088 }
089 }
090 catch (Exception e) {
091 e.printStackTrace();
092 }
093 }
094
095 };
096
097 Thread thread2 = new Thread () {
098
099 @Override
100 public void run() {
101 try {
102 SourceProcessor sourceProcessor =
103 JSPSourceProcessor.class.newInstance();
104
105 sourceProcessor.format(
106 _useProperties, _printErrors, _autoFix,
107 _mainReleaseVersion);
108
109 _errorMessages.addAll(sourceProcessor.getErrorMessages());
110 }
111 catch (Exception e) {
112 e.printStackTrace();
113 }
114 }
115
116 };
117
118 thread1.start();
119 thread2.start();
120
121 thread1.join();
122 thread2.join();
123
124 if (_throwException && !_errorMessages.isEmpty()) {
125 throw new Exception(StringUtil.merge(_errorMessages, "\n"));
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
136 if (sourceProcessor == null) {
137 return null;
138 }
139
140 String newContent = sourceProcessor.format(
141 fileName, _useProperties, _printErrors, _autoFix,
142 _mainReleaseVersion);
143
144 return new Tuple(newContent, sourceProcessor.getErrorMessages());
145 }
146
147 public String getMainReleaseVersion() {
148 return _mainReleaseVersion;
149 }
150
151 private void _setVersion() throws Exception {
152 String releaseInfoVersion = ReleaseInfo.getVersion();
153
154 if (releaseInfoVersion.startsWith("6.1")) {
155 _mainReleaseVersion =
156 BaseSourceProcessor.MAIN_RELEASE_VERSION_6_1_0;
157 }
158 else if (releaseInfoVersion.startsWith("6.2")) {
159 _mainReleaseVersion =
160 BaseSourceProcessor.MAIN_RELEASE_VERSION_6_2_0;
161 }
162 else {
163 throw new Exception(
164 "Invalid release information: " + ReleaseInfo.getVersion());
165 }
166 }
167
168 private static boolean _autoFix;
169 private static List<String> _errorMessages = new UniqueList<String>();
170 private static String _mainReleaseVersion;
171 private static boolean _printErrors;
172 private static boolean _throwException;
173 private static boolean _useProperties;
174
175 }