author | shurailine |
Tue, 21 Feb 2017 15:38:07 -0800 | |
changeset 44015 | 27aa35eb727c |
parent 38838 | 3d6ea76b35d6 |
permissions | -rw-r--r-- |
33919 | 1 |
/* |
37641
6949903ba85a
8151777: Add "@index" tag to the sampleapi generator
ksrini
parents:
35426
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33919 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
package sampleapi; |
|
24 |
||
44015 | 25 |
import com.sun.source.util.JavacTask; |
26 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
27 |
import com.sun.tools.javac.api.JavacTool; |
|
28 |
import com.sun.tools.javac.util.Context; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.UncheckedIOException; |
|
32 |
import java.nio.file.Files; |
|
33919 | 33 |
import java.nio.file.Path; |
44015 | 34 |
import java.nio.file.Paths; |
35 |
import java.util.ArrayList; |
|
36 |
import java.util.List; |
|
37 |
import java.util.Optional; |
|
38 |
import static java.util.stream.Collectors.toList; |
|
39 |
import javax.xml.parsers.DocumentBuilder; |
|
40 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
41 |
import javax.xml.parsers.ParserConfigurationException; |
|
42 |
import org.w3c.dom.Document; |
|
43 |
import org.w3c.dom.Element; |
|
44 |
import org.w3c.dom.NodeList; |
|
45 |
import org.xml.sax.SAXException; |
|
46 |
import sampleapi.generator.ModuleGenerator; |
|
33919 | 47 |
|
48 |
import sampleapi.generator.PackageGenerator; |
|
49 |
||
50 |
public class SampleApi { |
|
51 |
||
44015 | 52 |
private final Context ctx; |
53 |
private final List<ModuleGenerator> modules = new ArrayList<>(); |
|
54 |
||
55 |
public SampleApi() { |
|
56 |
JavacTool jt = JavacTool.create(); |
|
57 |
JavacTask task = jt.getTask(null, null, null, null, null, null); |
|
58 |
ctx = ((JavacTaskImpl) task).getContext(); |
|
59 |
} |
|
33919 | 60 |
|
44015 | 61 |
public static SampleApi load(Path resDir) |
62 |
throws ParserConfigurationException, IOException, SAXException { |
|
63 |
SampleApi result = new SampleApi(); |
|
64 |
System.out.println("Loading resources from " + resDir); |
|
65 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
66 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
|
67 |
Files.list(resDir) |
|
68 |
.peek(f -> System.out.println(f.getFileName())) |
|
69 |
.filter(f -> f.getFileName().toString().endsWith(".xml")) |
|
70 |
.peek(f -> System.out.println(f.getFileName())) |
|
71 |
.forEach(resFile -> { |
|
72 |
try (InputStream is = Files.newInputStream(resFile)) { |
|
73 |
Document document = builder.parse(is); |
|
74 |
NodeList moduleElements = document.getElementsByTagName("module"); |
|
75 |
for (int i = 0; i < moduleElements.getLength(); i++) { |
|
76 |
result.modules.add(ModuleGenerator |
|
77 |
.load((Element) moduleElements.item(i))); |
|
78 |
} |
|
79 |
} catch (IOException ex) { |
|
80 |
throw new UncheckedIOException(ex); |
|
81 |
} catch (SAXException ex) { |
|
82 |
throw new RuntimeException(ex); |
|
83 |
} |
|
84 |
}); |
|
85 |
return result; |
|
86 |
} |
|
87 |
||
88 |
public Context getContext() { |
|
89 |
return ctx; |
|
90 |
} |
|
91 |
||
92 |
public List<ModuleGenerator> getModules() { |
|
93 |
return modules; |
|
33919 | 94 |
} |
95 |
||
44015 | 96 |
|
97 |
public void generate(Path outDir) { |
|
98 |
//resolveIDs(modules); |
|
99 |
modules.forEach(m -> { |
|
100 |
try { |
|
101 |
m.generate(outDir, this); |
|
102 |
} catch (IOException ex) { |
|
103 |
throw new UncheckedIOException(ex); |
|
104 |
} |
|
105 |
}); |
|
33919 | 106 |
} |
107 |
||
44015 | 108 |
public void generate(String dir) |
109 |
throws ParserConfigurationException, IOException, SAXException { |
|
110 |
generate(Paths.get(dir)); |
|
111 |
} |
|
112 |
||
113 |
public ModuleGenerator moduleById(String id) { |
|
114 |
String real_id = getId(id); |
|
115 |
return modules.stream() |
|
116 |
.filter(m -> m.id.equals(real_id)) |
|
117 |
.findAny().orElseThrow(() -> new IllegalStateException("No module with id: " + real_id)); |
|
33919 | 118 |
} |
119 |
||
44015 | 120 |
public PackageGenerator packageById(String id) { |
121 |
String real_id = getId(id); |
|
122 |
return modules.stream() |
|
123 |
.flatMap(m -> m.packages.stream()) |
|
124 |
.filter(p -> p.id.equals(real_id)).findAny() |
|
125 |
.orElseThrow(() -> new IllegalStateException("No package with id: " + real_id)); |
|
126 |
} |
|
127 |
||
128 |
public String classById(String id) { |
|
129 |
String real_id = getId(id); |
|
130 |
return modules.stream() |
|
131 |
.flatMap(m -> m.packages.stream()) |
|
132 |
.peek(p -> System.out.println(p.packageName + " " + p.idBases.size())) |
|
133 |
.flatMap(p -> p.idBases.entrySet().stream() |
|
134 |
.filter(e -> e.getKey().equals(real_id)) |
|
135 |
.map(e -> p.packageName + "." + e.getValue().name.toString()) |
|
136 |
.peek(System.out::println)) |
|
137 |
.findAny().orElseThrow(() -> new IllegalStateException("No class with id: " + id)); |
|
138 |
} |
|
139 |
||
140 |
public boolean isId(String name) { |
|
141 |
return name.startsWith("$"); |
|
142 |
} |
|
143 |
||
144 |
public boolean isIdEqual(String name, String id) { |
|
145 |
return isId(name) && getId(name).equals(id); |
|
146 |
} |
|
147 |
||
148 |
public String getId(String name) { |
|
149 |
if(!isId(name)) { |
|
150 |
throw new IllegalStateException("Not an id: " + name); |
|
33919 | 151 |
} |
44015 | 152 |
return name.substring(1); |
33919 | 153 |
} |
154 |
} |