jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ModuleEntryFactory.java
changeset 39934 9c84ee88dd3a
parent 39933 c0dd0f198453
parent 39922 e613affb88d1
child 39935 6016bd47edc9
equal deleted inserted replaced
39933:c0dd0f198453 39934:9c84ee88dd3a
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package jdk.tools.jlink.internal;
       
    26 
       
    27 import java.nio.file.Path;
       
    28 import java.util.Objects;
       
    29 import jdk.tools.jlink.plugin.ModuleEntry;
       
    30 
       
    31 public final class ModuleEntryFactory {
       
    32     private ModuleEntryFactory() {}
       
    33 
       
    34     public static ModuleEntry create(String path,
       
    35             ModuleEntry.Type type, byte[] content) {
       
    36         return new ByteArrayModuleEntry(moduleFrom(path), path, type, content);
       
    37     }
       
    38 
       
    39     public static ModuleEntry create(String path,
       
    40             ModuleEntry.Type type, Path file) {
       
    41         return new PathModuleEntry(moduleFrom(path), path, type, file);
       
    42     }
       
    43 
       
    44     public static ModuleEntry create(ModuleEntry original, byte[] content) {
       
    45         return new ByteArrayModuleEntry(original.getModule(),
       
    46                 original.getPath(), original.getType(), content);
       
    47     }
       
    48 
       
    49     public static ModuleEntry create(ModuleEntry original, Path file) {
       
    50         return new PathModuleEntry(original.getModule(),
       
    51                 original.getPath(), original.getType(), file);
       
    52     }
       
    53 
       
    54     static String moduleFrom(String path) {
       
    55         Objects.requireNonNull(path);
       
    56         if (path.isEmpty() || path.charAt(0) != '/') {
       
    57             throw new IllegalArgumentException(path + " must start with /");
       
    58         }
       
    59         int idx = path.indexOf('/', 1);
       
    60         if (idx == -1) {
       
    61             throw new IllegalArgumentException("/ missing after module: " + path);
       
    62         }
       
    63         return path.substring(1, idx);
       
    64     }
       
    65 
       
    66     static String packageFrom(String path) {
       
    67         Objects.requireNonNull(path);
       
    68         int idx = path.lastIndexOf('/');
       
    69         if (idx == -1) {
       
    70             throw new IllegalArgumentException("/ missing from path: " + path);
       
    71         }
       
    72         if (path.startsWith("/")) {
       
    73             int jdx = path.indexOf('/', 1);
       
    74             if (jdx == -1) {
       
    75                 throw new IllegalArgumentException("/ missing after module: " + path);
       
    76             }
       
    77             return path.substring(jdx + 1, idx);
       
    78         } else {
       
    79             return path.substring(0, idx);
       
    80         }
       
    81     }
       
    82 }