    001/*
    002 *  Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017package org.apache.commons.compress.harmony.pack200;
    018
    019import java.nio.file.FileSystems;
    020import java.util.ArrayList;
    021import java.util.HashMap;
    022import java.util.List;
    023import java.util.Map;
    024import java.util.Map.Entry;
    025
    026import org.objectweb.asm.Attribute;
    027
    028/**
    029 * Manages the various options available for pack200.
    030 */
    031public class PackingOptions {
    032
    033    private static final Attribute[] EMPTY_ATTRIBUTE_ARRAY = {};
    034    public static final long SEGMENT_LIMIT = 1_000_000L;
    035    public static final String STRIP = "strip";
    036    public static final String ERROR = "error";
    037    public static final String PASS = "pass";
    038    public static final String KEEP = "keep";
    039
    040    // All options are initially set to their defaults
    041    private boolean gzip = true;
    042    private boolean stripDebug;
    043    private boolean keepFileOrder = true;
    044    private long segmentLimit = SEGMENT_LIMIT;
    045    private int effort = 5;
    046    private String deflateHint = KEEP;
    047    private String modificationTime = KEEP;
    048    private final List<String> passFiles = new ArrayList<>();
    049    private String unknownAttributeAction = PASS;
    050    private final Map<String, String> classAttributeActions = new HashMap<>();
    051    private final Map<String, String> fieldAttributeActions = new HashMap<>();
    052    private final Map<String, String> methodAttributeActions = new HashMap<>();
    053    private final Map<String, String> codeAttributeActions = new HashMap<>();
    054    private boolean verbose;
    055    private String logFile;
    056
    057    private Attribute[] unknownAttributeTypes;
    058
    059    public void addClassAttributeAction(final String attributeName, final String action) {
    060        classAttributeActions.put(attributeName, action);
    061    }
    062
    063    public void addCodeAttributeAction(final String attributeName, final String action) {
    064        codeAttributeActions.put(attributeName, action);
    065    }
    066
    067    public void addFieldAttributeAction(final String attributeName, final String action) {
    068        fieldAttributeActions.put(attributeName, action);
    069    }
    070
    071    public void addMethodAttributeAction(final String attributeName, final String action) {
    072        methodAttributeActions.put(attributeName, action);
    073    }
    074
    075    private void addOrUpdateAttributeActions(final List<Attribute> prototypes, final Map<String, String> attributeActions, final int tag) {
    076        if (attributeActions != null && attributeActions.size() > 0) {
    077            NewAttribute newAttribute;
    078            for (final Entry<String, String> entry : attributeActions.entrySet()) {
    079                final String name = entry.getKey();
    080                final String action = entry.getValue();
    081                boolean prototypeExists = false;
    082                for (final Object prototype : prototypes) {
    083                    newAttribute = (NewAttribute) prototype;
    084                    if (newAttribute.type.equals(name)) {
    085                        // if the attribute exists, update its context
    086                        newAttribute.addContext(tag);
    087                        prototypeExists = true;
    088                        break;
    089                    }
    090                }
    091                // if no attribute is found, add a new attribute
    092                if (!prototypeExists) {
    093                    if (ERROR.equals(action)) {
    094                        newAttribute = new NewAttribute.ErrorAttribute(name, tag);
    095                    } else if (STRIP.equals(action)) {
    096                        newAttribute = new NewAttribute.StripAttribute(name, tag);
    097                    } else if (PASS.equals(action)) {
    098                        newAttribute = new NewAttribute.PassAttribute(name, tag);
    099                    } else {
    100                        newAttribute = new NewAttribute(name, action, tag);
    101                    }
    102                    prototypes.add(newAttribute);
    103                }
    104            }
    105        }
    106    }
    107
    108    /**
    109     * Tell the compressor to pass the file with the given name, or if the name is a directory name all files under that directory will be passed.
    110     *
    111     * @param passFileName the file name
    112     */
    113    public void addPassFile(final String passFileName) {
    114        String fileSeparator = FileSystems.getDefault().getSeparator();
    115        if (fileSeparator.equals("\\")) {
    116            // Need to escape backslashes for replaceAll(), which uses regex
    117            fileSeparator += "\\";
    118        }
    119        passFiles.add(passFileName.replaceAll(fileSeparator, "/"));
    120    }
    121
    122    public String getDeflateHint() {
    123        return deflateHint;
    124    }
    125
    126    public int getEffort() {
    127        return effort;
    128    }
    129
    130    public String getLogFile() {
    131        return logFile;
    132    }
    133
    134    public String getModificationTime() {
    135        return modificationTime;
    136    }
    137
    138    private String getOrDefault(final Map<String, String> map, final String type, final String defaultValue) {
    139        return map == null ? defaultValue : map.getOrDefault(type, defaultValue);
    140    }
    141
    142    public long getSegmentLimit() {
    143        return segmentLimit;
    144    }
    145
    146    public String getUnknownAttributeAction() {
    147        return unknownAttributeAction;
    148    }
    149
    150    public Attribute[] getUnknownAttributePrototypes() {
    151        if (unknownAttributeTypes == null) {
    152            final List<Attribute> prototypes = new ArrayList<>();
    153            addOrUpdateAttributeActions(prototypes, classAttributeActions, AttributeDefinitionBands.CONTEXT_CLASS);
    154            addOrUpdateAttributeActions(prototypes, methodAttributeActions, AttributeDefinitionBands.CONTEXT_METHOD);
    155            addOrUpdateAttributeActions(prototypes, fieldAttributeActions, AttributeDefinitionBands.CONTEXT_FIELD);
    156            addOrUpdateAttributeActions(prototypes, codeAttributeActions, AttributeDefinitionBands.CONTEXT_CODE);
    157            unknownAttributeTypes = prototypes.toArray(EMPTY_ATTRIBUTE_ARRAY);
    158        }
    159        return unknownAttributeTypes;
    160    }
    161
    162    public String getUnknownClassAttributeAction(final String type) {
    163        return getOrDefault(classAttributeActions, type, unknownAttributeAction);
    164    }
    165
    166    public String getUnknownCodeAttributeAction(final String type) {
    167        return getOrDefault(codeAttributeActions, type, unknownAttributeAction);
    168    }
    169
    170    public String getUnknownFieldAttributeAction(final String type) {
    171        return getOrDefault(fieldAttributeActions, type, unknownAttributeAction);
    172    }
    173
    174    public String getUnknownMethodAttributeAction(final String type) {
    175        return getOrDefault(methodAttributeActions, type, unknownAttributeAction);
    176    }
    177
    178    public boolean isGzip() {
    179        return gzip;
    180    }
    181
    182    public boolean isKeepDeflateHint() {
    183        return KEEP.equals(deflateHint);
    184    }
    185
    186    public boolean isKeepFileOrder() {
    187        return keepFileOrder;
    188    }
    189
    190    public boolean isPassFile(final String passFileName) {
    191        for (String pass : passFiles) {
    192            if (passFileName.equals(pass)) {
    193                return true;
    194            }
    195            if (!pass.endsWith(".class")) { // a whole directory is
    196                // passed
    197                if (!pass.endsWith("/")) {
    198                    // Make sure we don't get any false positives (e.g.
    199                    // exclude "org/apache/harmony/pack" should not match
    200                    // files under "org/apache/harmony/pack200/")
    201                    pass += "/";
    202                }
    203                return passFileName.startsWith(pass);
    204            }
    205        }
    206        return false;
    207    }
    208
    209    public boolean isStripDebug() {
    210        return stripDebug;
    211    }
    212
    213    public boolean isVerbose() {
    214        return verbose;
    215    }
    216
    217    public void removePassFile(final String passFileName) {
    218        passFiles.remove(passFileName);
    219    }
    220
    221    public void setDeflateHint(final String deflateHint) {
    222        if (!KEEP.equals(deflateHint) && !"true".equals(deflateHint) && !"false".equals(deflateHint)) {
    223            throw new IllegalArgumentException("Bad argument: -H " + deflateHint + " ? deflate hint should be either true, false or keep (default)");
    224        }
    225        this.deflateHint = deflateHint;
    226    }
    227
    228    /**
    229     * Sets the compression effort level (0-9, equivalent to -E command line option)
    230     *
    231     * @param effort the compression effort level, 0-9.
    232     */
    233    public void setEffort(final int effort) {
    234        this.effort = effort;
    235    }
    236
    237    public void setGzip(final boolean gzip) {
    238        this.gzip = gzip;
    239    }
    240
    241    public void setKeepFileOrder(final boolean keepFileOrder) {
    242        this.keepFileOrder = keepFileOrder;
    243    }
    244
    245    public void setLogFile(final String logFile) {
    246        this.logFile = logFile;
    247    }
    248
    249    public void setModificationTime(final String modificationTime) {
    250        if (!KEEP.equals(modificationTime) && !"latest".equals(modificationTime)) {
    251            throw new IllegalArgumentException("Bad argument: -m " + modificationTime + " ? transmit modtimes should be either latest or keep (default)");
    252        }
    253        this.modificationTime = modificationTime;
    254    }
    255
    256    public void setQuiet(final boolean quiet) {
    257        this.verbose = !quiet;
    258    }
    259
    260    /**
    261     * Sets the segment limit (equivalent to -S command line option)
    262     *
    263     * @param segmentLimit   the limit in bytes
    264     */
    265    public void setSegmentLimit(final long segmentLimit) {
    266        this.segmentLimit = segmentLimit;
    267    }
    268
    269    /**
    270     * Sets strip debug attributes. If true, all debug attributes (i.e. LineNumberTable, SourceFile, LocalVariableTable and LocalVariableTypeTable attributes)
    271     * are stripped when reading the input class files and not included in the output archive.
    272     *
    273     * @param stripDebug If true, all debug attributes.
    274     */
    275    public void setStripDebug(final boolean stripDebug) {
    276        this.stripDebug = stripDebug;
    277    }
    278
    279    /**
    280     * Sets the compressor behavior when an unknown attribute is encountered.
    281     *
    282     * @param unknownAttributeAction   the action to perform
    283     */
    284    public void setUnknownAttributeAction(final String unknownAttributeAction) {
    285        this.unknownAttributeAction = unknownAttributeAction;
    286        if (!PASS.equals(unknownAttributeAction) && !ERROR.equals(unknownAttributeAction) && !STRIP.equals(unknownAttributeAction)) {
    287            throw new IllegalArgumentException("Incorrect option for -U, " + unknownAttributeAction);
    288        }
    289    }
    290
    291    public void setVerbose(final boolean verbose) {
    292        this.verbose = verbose;
    293    }
    294
    295}