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 */
25package jdk.tools.jlink.internal.plugins;
26
27import java.util.HashSet;
28import java.util.Map;
29import java.util.Set;
30
31import jdk.tools.jlink.plugin.Plugin;
32import jdk.tools.jlink.plugin.PluginException;
33import jdk.tools.jlink.plugin.ResourcePool;
34import jdk.tools.jlink.plugin.ResourcePoolBuilder;
35import jdk.tools.jlink.plugin.ResourcePoolEntry.Type;
36
37/**
38 *
39 * A plugin to exclude a JMOD section such as man pages or header files
40 */
41public final class ExcludeJmodSectionPlugin implements Plugin {
42
43    public static final String NAME = "exclude-jmod-section";
44    public static final String MAN_PAGES = "man";
45    public static final String INCLUDE_HEADER_FILES = "headers";
46
47    private final Set<Type> filters = new HashSet<>();
48
49    @Override
50    public String getName() {
51        return NAME;
52    }
53
54    @Override
55    public void configure(Map<String, String> config) {
56        String arg = config.get(NAME);
57        if (arg.isEmpty()) {
58            throw new IllegalArgumentException("Section name must be specified");
59        }
60
61        switch (arg) {
62            case MAN_PAGES:
63                filters.add(Type.MAN_PAGE);
64                break;
65            case INCLUDE_HEADER_FILES:
66                filters.add(Type.HEADER_FILE);
67                break;
68            default:
69                throw new IllegalArgumentException("Invalid section name: " + arg);
70        }
71    }
72
73    @Override
74    public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
75        in.transformAndCopy(entry -> {
76            // filter entries whose type corresponds to the specified JMOD section
77            if (filters.contains(entry.type())) {
78                entry = null;
79            }
80            return entry;
81        }, out);
82        return out.build();
83    }
84
85    @Override
86    public Category getType() {
87        return Category.FILTER;
88    }
89
90    @Override
91    public String getDescription() {
92        return PluginsResourceBundle.getDescription(NAME);
93    }
94
95    @Override
96    public boolean hasArguments() {
97        return true;
98    }
99
100    @Override
101    public String getArgumentsDescription() {
102       return PluginsResourceBundle.getArgument(NAME);
103    }
104}
105