src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImageResourcesTree.java
author goetz
Tue, 16 Jan 2018 08:48:34 +0100
changeset 48543 7067fe4e054e
parent 47216 71c04702a3d5
permissions -rw-r--r--
8189102: All tools should support -?, -h and --help Reviewed-by: kvn, jjg, weijun, alanb, rfield, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.tools.jlink.internal;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.DataOutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.TreeMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.TreeSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * A class to build a sorted tree of Resource paths as a tree of ImageLocation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
// XXX Public only due to the JImageTask / JImageTask code duplication
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
public final class ImageResourcesTree {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
    public static boolean isTreeInfoResource(String path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
        return path.startsWith("/packages") || path.startsWith("/modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
     * Path item tree node.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    private static class Node {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        private final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        private final Map<String, Node> children = new TreeMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        private final Node parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        private ImageLocationWriter loc;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        private Node(String name, Node parent) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
            this.name = name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
            this.parent = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
            if (parent != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
                parent.children.put(name, this);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        public String getPath() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            if (parent == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                return "/";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            return buildPath(this);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        public String getName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        public Node getChildren(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            Node item = children.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            return item;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        private static String buildPath(Node item) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            if (item == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            String path = buildPath(item.parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            if (path == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                return item.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                return path + "/" + item.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    private static final class ResourceNode extends Node {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        public ResourceNode(String name, Node parent) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            super(name, parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    private static class PackageNode extends Node {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
         * A reference to a package. Empty packages can be located inside one or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
         * more modules. A package with classes exist in only one module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        final static class PackageReference {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            private final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            private final boolean isEmpty;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            PackageReference(String name, boolean isEmpty) {
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 36511
diff changeset
   116
                this.name = Objects.requireNonNull(name);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                this.isEmpty = isEmpty;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                return name + "[empty:" + isEmpty + "]";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        private final Map<String, PackageReference> references = new TreeMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        PackageNode(String name, Node parent) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            super(name, parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        private void addReference(String name, boolean isEmpty) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            PackageReference ref = references.get(name);
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 36511
diff changeset
   134
            if (ref == null || ref.isEmpty) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                references.put(name, new PackageReference(name, isEmpty));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        private void validate() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            boolean exists = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            for (PackageReference ref : references.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                if (!ref.isEmpty) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                    if (exists) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                        throw new RuntimeException("Multiple modules to contain package "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                                + getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                        exists = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
     * Tree of nodes.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
    private static final class Tree {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        private final Map<String, Node> directAccess = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        private final List<String> paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        private final Node root;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        private Node modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        private Node packages;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        private Tree(List<String> paths) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
            this.paths = paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
            root = new Node("", null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            buildTree();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        private void buildTree() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            modules = new Node("modules", root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            directAccess.put(modules.getPath(), modules);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            Map<String, Set<String>> moduleToPackage = new TreeMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            Map<String, Set<String>> packageToModule = new TreeMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            for (String p : paths) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                if (!p.startsWith("/")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                String[] split = p.split("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
                // minimum length is 3 items: /<mod>/<pkg>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                if (split.length < 3) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                    System.err.println("Resources tree, invalid data structure, "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                            + "skipping " + p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
                Node current = modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
                String module = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                for (int i = 0; i < split.length; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                    // When a non terminal node is marked as being a resource, something is wrong.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                    // It has been observed some badly created jar file to contain
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
                    // invalid directory entry marled as not directory (see 8131762)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
                    if (current instanceof ResourceNode) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
                        System.err.println("Resources tree, invalid data structure, "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
                                + "skipping " + p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                        continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                    String s = split[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                    if (!s.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                        // First item, this is the module, simply add a new node to the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                        // tree.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
                        if (module == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                            module = s;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
                        Node n = current.children.get(s);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                        if (n == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                            if (i == split.length - 1) { // Leaf
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                                n = new ResourceNode(s, current);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                                String pkg = toPackageName(n.parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                                //System.err.println("Adding a resource node. pkg " + pkg + ", name " + s);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                                if (pkg != null && !pkg.startsWith("META-INF")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                                    Set<String> pkgs = moduleToPackage.get(module);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                                    if (pkgs == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                                        pkgs = new TreeSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
                                        moduleToPackage.put(module, pkgs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
                                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
                                    pkgs.add(pkg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
                            } else { // put only sub trees, no leaf
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
                                n = new Node(s, current);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
                                directAccess.put(n.getPath(), n);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
                                String pkg = toPackageName(n);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
                                if (pkg != null && !pkg.startsWith("META-INF")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                                    Set<String> mods = packageToModule.get(pkg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
                                    if (mods == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
                                        mods = new TreeSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                                        packageToModule.put(pkg, mods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
                                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
                                    mods.add(module);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                        current = n;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            packages = new Node("packages", root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            directAccess.put(packages.getPath(), packages);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            // The subset of package nodes that have some content.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
            // These packages exist only in a single module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            for (Map.Entry<String, Set<String>> entry : moduleToPackage.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
                for (String pkg : entry.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
                    PackageNode pkgNode = new PackageNode(pkg, packages);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                    pkgNode.addReference(entry.getKey(), false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
                    directAccess.put(pkgNode.getPath(), pkgNode);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
            // All packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
            for (Map.Entry<String, Set<String>> entry : packageToModule.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                // Do we already have a package node?
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                PackageNode pkgNode = (PackageNode) packages.getChildren(entry.getKey());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                if (pkgNode == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                    pkgNode = new PackageNode(entry.getKey(), packages);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                for (String module : entry.getValue()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
                    pkgNode.addReference(module, true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
                directAccess.put(pkgNode.getPath(), pkgNode);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
            // Validate that the packages are well formed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
            for (Node n : packages.children.values()) {
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 36511
diff changeset
   265
                ((PackageNode)n).validate();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
        public String toResourceName(Node node) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
            if (!node.children.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
                throw new RuntimeException("Node is not a resource");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
            return removeRadical(node);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
        public String getModule(Node node) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
            if (node.parent == null || node.getName().equals("modules")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                    || node.getName().startsWith("packages")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
            String path = removeRadical(node);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
            // "/xxx/...";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
            path = path.substring(1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            int i = path.indexOf("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            if (i == -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
                return path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                return path.substring(0, i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
        public String toPackageName(Node node) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
            if (node.parent == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            String path = removeRadical(node.getPath(), "/modules/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            String module = getModule(node);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            if (path.equals(module)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
            String pkg = removeRadical(path, module + "/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
            return pkg.replaceAll("/", ".");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        public String removeRadical(Node node) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            return removeRadical(node.getPath(), "/modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
        private String removeRadical(String path, String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
            if (!(path.length() < str.length())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
                path = path.substring(str.length());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
            return path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
        public Node getRoot() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            return root;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
        public Map<String, Node> getMap() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
            return directAccess;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
    private static final class LocationsAdder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        private long offset;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
        private final List<byte[]> content = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
        private final BasicImageWriter writer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
        private final Tree tree;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
        LocationsAdder(Tree tree, long offset, BasicImageWriter writer) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
            this.tree = tree;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
            this.offset = offset;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
            this.writer = writer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            addLocations(tree.getRoot());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
        private int addLocations(Node current) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
            if (current instanceof PackageNode) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
                PackageNode pkgNode = (PackageNode) current;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
                int size = pkgNode.references.size() * 8;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
                writer.addLocation(current.getPath(), offset, 0, size);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
                offset += size;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
                int[] ret = new int[current.children.size()];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
                int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
                for (java.util.Map.Entry<String, Node> entry : current.children.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
                    ret[i] = addLocations(entry.getValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
                    i += 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
                if (current != tree.getRoot() && !(current instanceof ResourceNode)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
                    int size = ret.length * 4;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
                    writer.addLocation(current.getPath(), offset, 0, size);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
                    offset += size;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
            return 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
        private List<byte[]> computeContent() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
            // Map used to associate Tree item with locations offset.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
            Map<String, ImageLocationWriter> outLocations = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
            for (ImageLocationWriter wr : writer.getLocations()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
                outLocations.put(wr.getFullName(), wr);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
            // Attach location to node
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
            for (Map.Entry<String, ImageLocationWriter> entry : outLocations.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
                Node item = tree.getMap().get(entry.getKey());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
                if (item != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
                    item.loc = entry.getValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
            computeContent(tree.getRoot(), outLocations);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
            return content;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
        private int computeContent(Node current, Map<String, ImageLocationWriter> outLocations) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
            if (current instanceof PackageNode) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
                // /packages/<pkg name>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
                PackageNode pkgNode = (PackageNode) current;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
                int size = pkgNode.references.size() * 8;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
                ByteBuffer buff = ByteBuffer.allocate(size);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                buff.order(writer.getByteOrder());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
                for (PackageNode.PackageReference mod : pkgNode.references.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
                    buff.putInt(mod.isEmpty ? 1 : 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
                    buff.putInt(writer.addString(mod.name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
                byte[] arr = buff.array();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                content.add(arr);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
                current.loc = outLocations.get(current.getPath());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                int[] ret = new int[current.children.size()];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                for (java.util.Map.Entry<String, Node> entry : current.children.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
                    ret[i] = computeContent(entry.getValue(), outLocations);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
                    i += 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
                if (ret.length > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                    int size = ret.length * 4;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
                    ByteBuffer buff = ByteBuffer.allocate(size);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                    buff.order(writer.getByteOrder());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
                    for (int val : ret) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
                        buff.putInt(val);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
                    byte[] arr = buff.array();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
                    content.add(arr);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
                    if (current instanceof ResourceNode) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
                        // A resource location, remove "/modules"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
                        String s = tree.toResourceName(current);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
                        current.loc = outLocations.get(s);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
                        // empty "/packages" or empty "/modules" paths
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
                        current.loc = outLocations.get(current.getPath());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
                if (current.loc == null && current != tree.getRoot()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
                    System.err.println("Invalid path in metadata, skipping " + current.getPath());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
            return current.loc == null ? 0 : current.loc.getLocationOffset();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
    private final List<String> paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
    private final LocationsAdder adder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
    public ImageResourcesTree(long offset, BasicImageWriter writer, List<String> paths) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
        this.paths = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
        this.paths.addAll(paths);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
        Collections.sort(this.paths);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
        Tree tree = new Tree(this.paths);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
        adder = new LocationsAdder(tree, offset, writer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
    public void addContent(DataOutputStream out) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        List<byte[]> content = adder.computeContent();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
        for (byte[] c : content) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            out.write(c, 0, c.length);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
}