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