1 |
|
---|
2 | // License: GPL. For details, see LICENSE file.
|
---|
3 | package org.openstreetmap.josm;
|
---|
4 |
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.OutputStreamWriter;
|
---|
7 | import java.io.FileWriter;
|
---|
8 | import java.io.PrintWriter;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Script to extract declared help topics from JOSM source files.
|
---|
12 | *
|
---|
13 | * Help topics are declared with the method ht(...). Example: ht("/Action/New")
|
---|
14 | *
|
---|
15 | * Run with
|
---|
16 | * groovy org.openstreetmap.josm.HelpTopicExtractor [options] fileOrDirs
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class HelpTopicExtractor {
|
---|
20 |
|
---|
21 | private boolean recursive = false
|
---|
22 | private List<String> files = []
|
---|
23 | private String outputFile = null
|
---|
24 | private PrintWriter outputWriter = null
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * extracts declared help topics from file and writes them to the output stream
|
---|
28 | *
|
---|
29 | * @param file the file
|
---|
30 | */
|
---|
31 | def extractHelpTopics(File file) {
|
---|
32 | def packageName;
|
---|
33 | def linenr = 0
|
---|
34 | file.eachLine {line ->
|
---|
35 | linenr++
|
---|
36 | def m = line =~ /^\s*package\s+(\S+)/
|
---|
37 | if (m.matches()) {
|
---|
38 | packageName = m[0][1].replace(";", "")
|
---|
39 | }
|
---|
40 |
|
---|
41 | m = line =~ /.*[^a-zA-Z]ht\s*\(\s*"([^"]+)"\s*\).*/
|
---|
42 | if (m.matches()) {
|
---|
43 | def topic = m[0][1]
|
---|
44 | outputWriter.println "${file.getPath()}\t${file.getName()}\t${linenr}\t${packageName}\t${topic}"
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * display help information
|
---|
51 | */
|
---|
52 | def showHelp() {
|
---|
53 | println "groovy org.openstreetmap.josm.HelpTopicExtractor [options] fileOrDirs"
|
---|
54 | println "Options:"
|
---|
55 | println " --help|-h show help information"
|
---|
56 | println " --recurive|-r recursively extracts help topics from java files in the specified directories"
|
---|
57 | println " -output-file|-o file write to output file"
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * processes a file or directory. If recursive processing is enabled and file is a directory,
|
---|
62 | * recursively processes child directories.
|
---|
63 | *
|
---|
64 | * @param file the file
|
---|
65 | */
|
---|
66 | def process(File file) {
|
---|
67 | if (file.isFile()) {
|
---|
68 | extractHelpTopics(file)
|
---|
69 | } else if (file.isDirectory()) {
|
---|
70 | file.listFiles().grep(~/.*\.java$/).each {
|
---|
71 | File child ->
|
---|
72 | extractHelpTopics(child)
|
---|
73 | }
|
---|
74 | if (recursive) {
|
---|
75 | file.listFiles().grep { File child ->
|
---|
76 | child.isDirectory() && child.getName() != "." && child.getName() != ".."
|
---|
77 | }.each { File child ->
|
---|
78 | process(child)
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | def run(String[] args) {
|
---|
85 | def i = 0
|
---|
86 | while(i<args.length) {
|
---|
87 | def arg = args[i]
|
---|
88 | if (arg == "-h" || arg == "--help") {
|
---|
89 | showHelp()
|
---|
90 | System.exit(0)
|
---|
91 | } else if (arg == "-r" || arg == "--recursive") {
|
---|
92 | recursive = true
|
---|
93 | i++;
|
---|
94 | } else if (arg == "-o" || arg == "--output-file") {
|
---|
95 | i++
|
---|
96 | if (i >= args.length) {
|
---|
97 | System.err.println "Missing argument for option '${args[i-1]}'"
|
---|
98 | System.exit(1)
|
---|
99 | }
|
---|
100 | outputFile = args[i]
|
---|
101 | outputWriter = new PrintWriter(new FileWriter(outputFile))
|
---|
102 | i++
|
---|
103 | } else {
|
---|
104 | files.add(arg)
|
---|
105 | i++
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|
109 | if (outputFile == null) {
|
---|
110 | outputWriter = new PrintWriter(new OutputStreamWriter(System.out))
|
---|
111 | }
|
---|
112 | files.each {file -> process(new File(file)) }
|
---|
113 | outputWriter.flush()
|
---|
114 | if (outputFile != null) {
|
---|
115 | outputWriter.close()
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public static void main(String[] args) {
|
---|
120 | new HelpTopicExtractor().run(args)
|
---|
121 | }
|
---|
122 | }
|
---|