All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redhat.ceylon.common.tool.StandardArgumentParsers Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package com.redhat.ceylon.common.tool;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.regex.Pattern;

public class StandardArgumentParsers {

    public static final ArgumentParser CHAR_SEQUENCE_PARSER = new ArgumentParser() {
        @Override
        public String parse(String argument, Tool tool) {
            return argument != null ? argument : null;
        }
    };
    
    public static final ArgumentParser BOOLEAN_PARSER = new ArgumentParser() {
        @Override
        public Boolean parse(String argument, Tool tool) {
            return argument.matches("1|yes|true");
        }
    };
    
    public static final ArgumentParser INTEGER_PARSER = new ArgumentParser() {
        @Override
        public Integer parse(String argument, Tool tool) {
            return Integer.valueOf(argument);
        }
    };
    
    public static final ArgumentParser URI_PARSER = new ArgumentParser() {
        @Override
        public URI parse(String argument, Tool tool) {
            try {
                return new URI(argument);
            } catch (URISyntaxException e) {
                try {
                    return new URI(argument.replace('\\', '/'));
                } catch (URISyntaxException e2) {
                    File f = new File(argument);
                    return f.toURI();
                }
            }
        }
    };

    public static class PathArgumentParser implements ArgumentParser> {
        @Override
        public List parse(String argument, Tool tool) {
            String[] dirs = argument.split(Pattern.quote(File.pathSeparator));
            ArrayList result = new ArrayList(dirs.length); 
            for (String dir : dirs) {
                result.add(new File(dir));
            }
            return result;
        }
    };
    public static final PathArgumentParser PATH_PARSER = new PathArgumentParser();

    public static class ConstructorArgumentParser implements ArgumentParser {

        private Constructor ctor;

        public ConstructorArgumentParser(Class clazz) {
            try {
                this.ctor = clazz.getConstructor(String.class);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public T parse(String argument, Tool tool) {
            try {
                return ctor.newInstance(argument);
            } catch (InvocationTargetException e) {
                Throwable cause = e.getCause();
                if (cause instanceof RuntimeException) {
                    throw (RuntimeException)cause;
                } else if (cause instanceof Error) {
                    throw (Error)cause;
                } else {
                    throw new RuntimeException(e);
                }
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (IllegalArgumentException e) {
                throw new RuntimeException(e);
            }
        }
        
    }
    
    public abstract static class EnumParserBase> implements EnumerableParser {

        protected final boolean denormalize;
        protected final Class enumClass;
        
        public EnumParserBase(Class enumClass, boolean denormalize) {
            this.enumClass = enumClass;
            this.denormalize = denormalize;
        }
        
        protected String denormalize(String argument) {
            argument = argument.replace('-', '_');
            for (String nm : possibilities()) {
                if (nm.equalsIgnoreCase(argument)) {
                    argument = nm;
                }
            }
            return argument;
        }

        protected E valueOf(String name) {
            return EnumUtil.valueOf(enumClass, name);
        }
        
        @Override
        public Iterable possibilities() {
            return EnumUtil.possibilities(enumClass);
        }
    }
    
    public static class EnumArgumentParser> extends EnumParserBase {

        public EnumArgumentParser(Class enumClass, boolean denormalize) {
            super(enumClass, denormalize);
        }

        @Override
        public E parse(String argument, Tool tool) {
            if (denormalize) {
                argument = denormalize(argument);
            }
            return valueOf(argument);
        }
    }
    
    public static class EnumArgumentsParser> extends EnumParserBase, E> {

        public EnumArgumentsParser(Class enumClass, boolean denormalize) {
            super(enumClass, denormalize);
        }

        @Override
        public List parse(String argument, Tool tool) {
            if (!argument.isEmpty()) {
                String[] elems = argument.split(",");
                ArrayList result = new ArrayList(elems.length);
                for (String elem : elems) {
                    elem = elem.trim();
                    if (denormalize) {
                        elem = denormalize(elem);
                    }
                    result.add(valueOf(elem));
                }
                return result;
            } else {
                return new ArrayList<>(EnumSet.allOf(enumClass));
            }
        }
    }
    
    public static ArgumentParser forClass(Class setterType, ToolLoader toolLoader, boolean isSimpleType) {
        if (CharSequence.class.isAssignableFrom(setterType)) {
            return CHAR_SEQUENCE_PARSER;
        } else if (Integer.class.isAssignableFrom(setterType)
                || Integer.TYPE.isAssignableFrom(setterType)) {
            return INTEGER_PARSER;
        } else if (Boolean.class.isAssignableFrom(setterType)
                || Boolean.TYPE.isAssignableFrom(setterType)) {
            return BOOLEAN_PARSER;
        } else if (File.class.isAssignableFrom(setterType)) {
            return new ConstructorArgumentParser<>(File.class);
        } else if (URI.class.isAssignableFrom(setterType)) {
            return URI_PARSER;
        } else if (URL.class.isAssignableFrom(setterType)) {
            return new ConstructorArgumentParser<>(URL.class);
        } else if (Enum.class.isAssignableFrom(setterType)) {
            if (isSimpleType) {
                return new EnumArgumentParser(setterType, true);
            } else {
                return new EnumArgumentsParser(setterType, true);
            }
        } else if (ToolModel.class.isAssignableFrom(setterType)) {
            return new ToolModelArgumentParser(toolLoader);
        } /*else if (Tool.class.isAssignableFrom(setterType)) {
            return new ToolArgumentParser(toolLoader);
        }*/
        return null;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy