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