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