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