Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / scripts / jsdoc-validator / src / org / chromium / devtools / jsdoc / JsDocValidator.java
1 /**
2  * Validator for Closure-based JSDoc.
3  */
4
5 package org.chromium.devtools.jsdoc;
6
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.SortedSet;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.Future;
17
18 public class JsDocValidator {
19
20     private void run(String[] args) {
21         int threadCount = Math.min(args.length, Runtime.getRuntime().availableProcessors());
22         ExecutorService executor = Executors.newFixedThreadPool(threadCount);
23         try {
24             runWithExecutor(args, executor);
25         } finally {
26             executor.shutdown();
27         }
28     }
29
30     private void runWithExecutor(String[] args, ExecutorService executor) {
31         List<Future<ValidatorContext>> futures = new ArrayList<>(args.length);
32         Map<Future<ValidatorContext>, String> fileNamesByFuture = new HashMap<>();
33         for (String fileName : args) {
34             Future<ValidatorContext> future = executor.submit(new FileCheckerCallable(fileName));
35             fileNamesByFuture.put(future, fileName);
36             futures.add(future);
37         }
38
39         List<ValidatorContext> contexts = new ArrayList<>(args.length);
40         for (Future<ValidatorContext> future : futures) {
41             try {
42                 ValidatorContext context = future.get();
43                 if (context != null) {
44                     contexts.add(context);
45                 }
46             } catch (InterruptedException | ExecutionException e) {
47                 System.err.println(fileNamesByFuture.get(future) + ": ERROR - " + e.getMessage());
48             }
49         }
50
51         int entryCount = 0;
52         for (ValidatorContext context : contexts) {
53             entryCount += context.getValidationResult().size();
54         }
55         List<LogEntry> entries = new ArrayList<>(entryCount);
56         for (ValidatorContext context : contexts) {
57             SortedSet<ValidatorContext.MessageRecord> records = context.getValidationResult();
58             for (ValidatorContext.MessageRecord record : records) {
59                 entries.add(new LogEntry(context.scriptFileName, record));
60             }
61         }
62         Collections.sort(entries);
63         for (LogEntry entry : entries) {
64             System.err.println(entry.record.text);
65         }
66         if (!entries.isEmpty())
67             System.err.println("Total errors: " + entries.size());
68     }
69
70     public static void main(String[] args) {
71         new JsDocValidator().run(args);
72     }
73
74     private static class LogEntry implements Comparable<LogEntry> {
75         private final String fileName;
76         private final ValidatorContext.MessageRecord record;
77
78         LogEntry(String fileName, ValidatorContext.MessageRecord record) {
79             this.fileName = fileName;
80             this.record = record;
81         }
82
83         @Override
84         public int compareTo(LogEntry other) {
85             int result = fileName.compareTo(other.fileName);
86             if (result != 0) {
87                 return result;
88             }
89             return Integer.compare(record.position, other.record.position);
90         }
91
92         @Override
93         public int hashCode() {
94             return 17 + fileName.hashCode() * 3 + this.record.hashCode() * 5;
95         }
96
97         @Override
98         public boolean equals(Object obj) {
99             if (this == obj) {
100                 return true;
101             }
102             if (obj == null) {
103                 return false;
104             }
105             if (getClass() != obj.getClass()) {
106                 return false;
107             }
108             LogEntry other = (LogEntry) obj;
109             if (fileName != other.fileName
110                     && (fileName != null && !fileName.equals(other.fileName))) {
111                 return false;
112             }
113
114             if (record == other.record) {
115                 return true;
116             }
117             if (record != null) {
118                 if (other.record == null) {
119                     return false;
120                 }
121                 return record.position == other.record.position;
122             }
123             return false;
124         }
125     }
126 }