1 /* |
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package com.sun.tools.jdeps; |
|
26 |
|
27 import java.io.IOException; |
|
28 import java.io.InputStream; |
|
29 import java.util.HashSet; |
|
30 import java.util.Set; |
|
31 import javax.xml.namespace.QName; |
|
32 import javax.xml.stream.XMLEventReader; |
|
33 import javax.xml.stream.XMLInputFactory; |
|
34 import javax.xml.stream.XMLStreamException; |
|
35 import javax.xml.stream.events.Attribute; |
|
36 import javax.xml.stream.events.XMLEvent; |
|
37 import com.sun.tools.jdeps.ClassFileReader.ModuleClassReader; |
|
38 import com.sun.tools.jdeps.PlatformClassPath.ImageHelper; |
|
39 |
|
40 final class ModulesXmlReader { |
|
41 public static Set<Module> load(ImageHelper helper,InputStream in) |
|
42 throws IOException |
|
43 { |
|
44 try { |
|
45 ModulesXmlReader reader = new ModulesXmlReader(helper); |
|
46 return reader.load(in); |
|
47 } catch (XMLStreamException e) { |
|
48 throw new RuntimeException(e); |
|
49 } |
|
50 } |
|
51 |
|
52 private static final String MODULES = "modules"; |
|
53 private static final String MODULE = "module"; |
|
54 private static final String NAME = "name"; |
|
55 private static final String DEPEND = "depend"; |
|
56 private static final String EXPORT = "export"; |
|
57 private static final String TO = "to"; |
|
58 private static final QName REEXPORTS = new QName("re-exports"); |
|
59 private final ImageHelper helper; |
|
60 ModulesXmlReader(ImageHelper helper) { |
|
61 this.helper = helper; |
|
62 } |
|
63 |
|
64 public Set<Module> load(InputStream in) throws XMLStreamException, IOException { |
|
65 Set<Module> modules = new HashSet<>(); |
|
66 if (in == null) { |
|
67 throw new RuntimeException("jdeps-modules.xml doesn't exist"); |
|
68 } |
|
69 XMLInputFactory factory = XMLInputFactory.newInstance(); |
|
70 XMLEventReader reader = factory.createXMLEventReader(in, "UTF-8"); |
|
71 Module.Builder mb = null; |
|
72 String modulename = null; |
|
73 String exportedPackage = null; |
|
74 Set<String> permits = new HashSet<>(); |
|
75 while (reader.hasNext()) { |
|
76 XMLEvent event = reader.nextEvent(); |
|
77 if (event.isStartElement()) { |
|
78 String startTag = event.asStartElement().getName().getLocalPart(); |
|
79 switch (startTag) { |
|
80 case MODULES: |
|
81 break; |
|
82 case MODULE: |
|
83 if (mb != null) { |
|
84 throw new RuntimeException("end tag for module is missing"); |
|
85 } |
|
86 modulename = getNextTag(reader, NAME); |
|
87 mb = new Module.Builder(); |
|
88 mb.name(modulename); |
|
89 break; |
|
90 case NAME: |
|
91 throw new RuntimeException(event.toString()); |
|
92 case DEPEND: |
|
93 boolean reexports = false; |
|
94 Attribute attr = event.asStartElement().getAttributeByName(REEXPORTS); |
|
95 if (attr != null) { |
|
96 String value = attr.getValue(); |
|
97 if (value.equals("true") || value.equals("false")) { |
|
98 reexports = Boolean.parseBoolean(value); |
|
99 } else { |
|
100 throw new RuntimeException("unexpected attribute " + attr.toString()); |
|
101 } |
|
102 } |
|
103 mb.require(getData(reader), reexports); |
|
104 break; |
|
105 case EXPORT: |
|
106 exportedPackage = getNextTag(reader, NAME); |
|
107 break; |
|
108 case TO: |
|
109 permits.add(getData(reader)); |
|
110 break; |
|
111 default: |
|
112 throw new RuntimeException("invalid element: " + event); |
|
113 } |
|
114 } else if (event.isEndElement()) { |
|
115 String endTag = event.asEndElement().getName().getLocalPart(); |
|
116 switch (endTag) { |
|
117 case MODULE: |
|
118 ModuleClassReader cfr = helper.getModuleClassReader(modulename); |
|
119 mb.classes(cfr); |
|
120 mb.packages(cfr.packages()); |
|
121 modules.add(mb.build()); |
|
122 mb = null; |
|
123 break; |
|
124 case EXPORT: |
|
125 if (exportedPackage == null) { |
|
126 throw new RuntimeException("export's name is missing"); |
|
127 } |
|
128 mb.export(exportedPackage, permits); |
|
129 exportedPackage = null; |
|
130 permits.clear(); |
|
131 break; |
|
132 default: |
|
133 } |
|
134 } else if (event.isCharacters()) { |
|
135 String s = event.asCharacters().getData(); |
|
136 if (!s.trim().isEmpty()) { |
|
137 throw new RuntimeException("export-to is malformed"); |
|
138 } |
|
139 } |
|
140 } |
|
141 return modules; |
|
142 } |
|
143 private String getData(XMLEventReader reader) throws XMLStreamException { |
|
144 XMLEvent e = reader.nextEvent(); |
|
145 if (e.isCharacters()) { |
|
146 return e.asCharacters().getData(); |
|
147 } |
|
148 throw new RuntimeException(e.toString()); |
|
149 } |
|
150 |
|
151 private String getNextTag(XMLEventReader reader, String tag) throws XMLStreamException { |
|
152 XMLEvent e = reader.nextTag(); |
|
153 if (e.isStartElement()) { |
|
154 String t = e.asStartElement().getName().getLocalPart(); |
|
155 if (!tag.equals(t)) { |
|
156 throw new RuntimeException(e + " expected: " + tag); |
|
157 } |
|
158 return getData(reader); |
|
159 } |
|
160 throw new RuntimeException("export-to name is missing:" + e); |
|
161 } |
|
162 } |
|