author | vasya |
Mon, 14 Dec 2015 20:18:19 +0100 | |
changeset 34752 | 9c262a013456 |
parent 30846 | 2b3f379840f0 |
permissions | -rw-r--r-- |
25874 | 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 |
*/ |
|
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
30846
diff
changeset
|
25 |
|
25874 | 26 |
package com.sun.tools.jdeps; |
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.util.HashSet; |
|
31 |
import java.util.Set; |
|
32 |
import javax.xml.namespace.QName; |
|
33 |
import javax.xml.stream.XMLEventReader; |
|
34 |
import javax.xml.stream.XMLInputFactory; |
|
35 |
import javax.xml.stream.XMLStreamException; |
|
36 |
import javax.xml.stream.events.Attribute; |
|
37 |
import javax.xml.stream.events.XMLEvent; |
|
27579 | 38 |
import com.sun.tools.jdeps.ClassFileReader.ModuleClassReader; |
39 |
import com.sun.tools.jdeps.PlatformClassPath.ImageHelper; |
|
25874 | 40 |
|
27579 | 41 |
final class ModulesXmlReader { |
42 |
public static Set<Module> load(ImageHelper helper,InputStream in) |
|
25874 | 43 |
throws IOException |
44 |
{ |
|
45 |
try { |
|
27579 | 46 |
ModulesXmlReader reader = new ModulesXmlReader(helper); |
25874 | 47 |
return reader.load(in); |
48 |
} catch (XMLStreamException e) { |
|
49 |
throw new RuntimeException(e); |
|
50 |
} |
|
51 |
} |
|
52 |
||
53 |
private static final String MODULES = "modules"; |
|
54 |
private static final String MODULE = "module"; |
|
55 |
private static final String NAME = "name"; |
|
56 |
private static final String DEPEND = "depend"; |
|
57 |
private static final String EXPORT = "export"; |
|
58 |
private static final String TO = "to"; |
|
59 |
private static final QName REEXPORTS = new QName("re-exports"); |
|
27579 | 60 |
private final ImageHelper helper; |
61 |
ModulesXmlReader(ImageHelper helper) { |
|
62 |
this.helper = helper; |
|
63 |
} |
|
64 |
||
25874 | 65 |
public Set<Module> load(InputStream in) throws XMLStreamException, IOException { |
66 |
Set<Module> modules = new HashSet<>(); |
|
67 |
if (in == null) { |
|
26276
0dca2378aa34
8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents:
25874
diff
changeset
|
68 |
throw new RuntimeException("jdeps-modules.xml doesn't exist"); |
25874 | 69 |
} |
70 |
XMLInputFactory factory = XMLInputFactory.newInstance(); |
|
71 |
XMLEventReader reader = factory.createXMLEventReader(in, "UTF-8"); |
|
72 |
Module.Builder mb = null; |
|
73 |
String modulename = null; |
|
74 |
String exportedPackage = null; |
|
75 |
Set<String> permits = new HashSet<>(); |
|
76 |
while (reader.hasNext()) { |
|
77 |
XMLEvent event = reader.nextEvent(); |
|
78 |
if (event.isStartElement()) { |
|
79 |
String startTag = event.asStartElement().getName().getLocalPart(); |
|
80 |
switch (startTag) { |
|
81 |
case MODULES: |
|
82 |
break; |
|
83 |
case MODULE: |
|
84 |
if (mb != null) { |
|
85 |
throw new RuntimeException("end tag for module is missing"); |
|
86 |
} |
|
87 |
modulename = getNextTag(reader, NAME); |
|
88 |
mb = new Module.Builder(); |
|
89 |
mb.name(modulename); |
|
90 |
break; |
|
91 |
case NAME: |
|
92 |
throw new RuntimeException(event.toString()); |
|
93 |
case DEPEND: |
|
94 |
boolean reexports = false; |
|
95 |
Attribute attr = event.asStartElement().getAttributeByName(REEXPORTS); |
|
96 |
if (attr != null) { |
|
97 |
String value = attr.getValue(); |
|
98 |
if (value.equals("true") || value.equals("false")) { |
|
99 |
reexports = Boolean.parseBoolean(value); |
|
100 |
} else { |
|
101 |
throw new RuntimeException("unexpected attribute " + attr.toString()); |
|
102 |
} |
|
103 |
} |
|
104 |
mb.require(getData(reader), reexports); |
|
105 |
break; |
|
106 |
case EXPORT: |
|
107 |
exportedPackage = getNextTag(reader, NAME); |
|
108 |
break; |
|
109 |
case TO: |
|
110 |
permits.add(getData(reader)); |
|
111 |
break; |
|
112 |
default: |
|
113 |
throw new RuntimeException("invalid element: " + event); |
|
114 |
} |
|
115 |
} else if (event.isEndElement()) { |
|
116 |
String endTag = event.asEndElement().getName().getLocalPart(); |
|
117 |
switch (endTag) { |
|
118 |
case MODULE: |
|
27579 | 119 |
ModuleClassReader cfr = helper.getModuleClassReader(modulename); |
25874 | 120 |
mb.classes(cfr); |
27579 | 121 |
mb.packages(cfr.packages()); |
25874 | 122 |
modules.add(mb.build()); |
123 |
mb = null; |
|
124 |
break; |
|
125 |
case EXPORT: |
|
126 |
if (exportedPackage == null) { |
|
127 |
throw new RuntimeException("export's name is missing"); |
|
128 |
} |
|
129 |
mb.export(exportedPackage, permits); |
|
130 |
exportedPackage = null; |
|
131 |
permits.clear(); |
|
132 |
break; |
|
133 |
default: |
|
134 |
} |
|
135 |
} else if (event.isCharacters()) { |
|
136 |
String s = event.asCharacters().getData(); |
|
137 |
if (!s.trim().isEmpty()) { |
|
138 |
throw new RuntimeException("export-to is malformed"); |
|
139 |
} |
|
140 |
} |
|
141 |
} |
|
142 |
return modules; |
|
143 |
} |
|
144 |
private String getData(XMLEventReader reader) throws XMLStreamException { |
|
145 |
XMLEvent e = reader.nextEvent(); |
|
146 |
if (e.isCharacters()) { |
|
147 |
return e.asCharacters().getData(); |
|
148 |
} |
|
149 |
throw new RuntimeException(e.toString()); |
|
150 |
} |
|
151 |
||
152 |
private String getNextTag(XMLEventReader reader, String tag) throws XMLStreamException { |
|
153 |
XMLEvent e = reader.nextTag(); |
|
154 |
if (e.isStartElement()) { |
|
155 |
String t = e.asStartElement().getName().getLocalPart(); |
|
156 |
if (!tag.equals(t)) { |
|
157 |
throw new RuntimeException(e + " expected: " + tag); |
|
158 |
} |
|
159 |
return getData(reader); |
|
160 |
} |
|
161 |
throw new RuntimeException("export-to name is missing:" + e); |
|
162 |
} |
|
163 |
} |