/**
 * Copyright 2011-2015 Asakusa Framework Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.asakusafw.bridge.launch;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * An abstract {@link LaunchOption} which accepts properties.
 */
public abstract class AbstractPropertiesOption implements LaunchOption<Map<String, String>> {

    /**
     * The prefix of file path.
     */
    public static final char FILE_PREFIX = '@';

    /**
     * The key-value separator character.
     */
    public static final char KEY_VALUE_SEPARATOR = '=';

    /**
     * The default value for key-only entries.
     */
    public static final String DEFAULT_VALUE = "true"; //$NON-NLS-1$

    private final Map<String, String> properties = new LinkedHashMap<>();

    @Override
    public void accept(String command, String value) throws LaunchConfigurationException {
        if (value.isEmpty()) {
            return;
        }
        if (value.charAt(0) == FILE_PREFIX) {
            File file = new File(value.substring(1));
            properties.putAll(extract(file));
        } else {
            int separtorAt = value.indexOf(KEY_VALUE_SEPARATOR);
            if (separtorAt < 0) {
                properties.put(value, DEFAULT_VALUE);
            } else {
                properties.put(value.substring(0, separtorAt), value.substring(separtorAt + 1));
            }
        }
    }

    /**
     * Extracts properties in the target file.
     * @param file the target file (may not exist)
     * @return the extracted properties
     * @throws LaunchConfigurationException if the option is invalid
     */
    protected abstract Map<String, String> extract(File file) throws LaunchConfigurationException;

    @Override
    public Map<String, String> resolve() throws LaunchConfigurationException {
        return properties;
    }
}

@@ -718,16 +718,48 @@
 til.Map;
+%0Aimport java.util.regex.Pattern;
 %0A%0A/**%0A *
@@ -1010,24 +1010,129 @@
 FIX = '@';%0A%0A
+    /**%0A     * The selective file separator.%0A     */%0A    public static final char FILE_SEPARATOR = '%7C';%0A%0A
     /**%0A    
@@ -1670,95 +1670,372 @@
     
-File file = new File(value.substring(1));%0A            properties.putAll(extract(file));
+for (String s : value.substring(1).split(Pattern.quote(String.valueOf(FILE_SEPARATOR)))) %7B%0A                if (s.isEmpty()) %7B%0A                    continue;%0A                %7D%0A                File file = new File(s);%0A                if (file.exists()) %7B%0A                    properties.putAll(extract(file));%0A                    break;%0A                %7D%0A            %7D
 %0A   

Support `--{engine,hadoop}-conf @file1|file2|...` syntax.