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 CSSSourceProcessor.class.newInstance());
062 sourceProcessors.add(
063 FTLSourceProcessor.class.newInstance());
064 sourceProcessors.add(
065 JavaSourceProcessor.class.newInstance());
066 sourceProcessors.add(JSSourceProcessor.class.newInstance());
067 sourceProcessors.add(
068 PropertiesSourceProcessor.class.newInstance());
069 sourceProcessors.add(SHSourceProcessor.class.newInstance());
070 sourceProcessors.add(
071 SQLSourceProcessor.class.newInstance());
072 sourceProcessors.add(
073 TLDSourceProcessor.class.newInstance());
074 sourceProcessors.add(
075 XMLSourceProcessor.class.newInstance());
076
077 for (SourceProcessor sourceProcessor : sourceProcessors) {
078 sourceProcessor.format(
079 _useProperties, _printErrors, _autoFix);
080
081 _errorMessages.addAll(
082 sourceProcessor.getErrorMessages());
083 }
084 }
085 catch (Exception e) {
086 e.printStackTrace();
087 }
088 }
089
090 };
091
092 Thread thread2 = new Thread () {
093
094 @Override
095 public void run() {
096 try {
097 SourceProcessor sourceProcessor =
098 JSPSourceProcessor.class.newInstance();
099
100 sourceProcessor.format(
101 _useProperties, _printErrors, _autoFix);
102
103 _errorMessages.addAll(sourceProcessor.getErrorMessages());
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 && !_errorMessages.isEmpty()) {
119 throw new Exception(StringUtil.merge(_errorMessages, "\n"));
120 }
121 }
122
123 public String[] format(String fileName) throws Exception {
124 SourceProcessor sourceProcessor = null;
125
126 if (fileName.endsWith(".testjava")) {
127 sourceProcessor = JavaSourceProcessor.class.newInstance();
128 }
129
130 if (sourceProcessor == null) {
131 return null;
132 }
133
134 String newContent = sourceProcessor.format(
135 fileName, _useProperties, _printErrors, _autoFix);
136
137 List<String> errorMessages = sourceProcessor.getErrorMessages();
138
139 if (errorMessages.isEmpty()) {
140 return new String[] {newContent, null};
141 }
142 else {
143 return new String[] {newContent, errorMessages.get(0)};
144 }
145 }
146
147 private static boolean _autoFix;
148 private static List<String> _errorMessages = new UniqueList<String>();
149 private static boolean _printErrors;
150 private static boolean _throwException;
151 private static boolean _useProperties;
152
153 }