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

org.gradle.internal.Transformers Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2012 the original author or authors.
 *
 * 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 org.gradle.internal;

import org.gradle.api.Action;
import org.gradle.api.Transformer;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.regex.Pattern;

/**
 * Utility transformers.
 */
public abstract class Transformers {

    /**
     * Creates a transformer that simply type casts the input to the given output type.
     *
     * @param outputType The type to cast the input to
     * @param  The type of the transformed object
     * @param  The type of the object to be transformed
     * @return A transformer that simply type casts the input to the given output type.
     */
    public static  Transformer cast(Class outputType) {
        return new CastingTransformer(outputType);
    }

    //just returns the original object
    public static  Transformer noOpTransformer() {
        return new Transformer() {
            @Override
            public T transform(T original) {
                return original;
            }
        };
    }

    private static class CastingTransformer implements Transformer {
        final Class outputType;

        public CastingTransformer(Class outputType) {
            this.outputType = outputType;
        }

        @Override
        public O transform(I input) {
            return Cast.cast(outputType, input);
        }
    }

    public static  Transformer asString() {
        return new ToStringTransformer();
    }

    private static class ToStringTransformer implements Transformer {
        @Override
        public String transform(T original) {
            return original == null ? null : original.toString();
        }
    }

    /**
     * Transforms strings which may have spaces and which may have already been escaped with
     * quotes into safe command-line arguments.
     */
    public static Transformer asSafeCommandLineArgument() {
        return new CommandLineArgumentTransformer();
    }

    private static class CommandLineArgumentTransformer implements Transformer {
        private static final Pattern SINGLE_QUOTED = Pattern.compile("^'.*'$");
        private static final Pattern DOUBLE_QUOTED = Pattern.compile("^\".*\"$");
        private static final Pattern A_SINGLE_QUOTE =  Pattern.compile("'");

        @Override
        public String transform(String input) {
            if (SINGLE_QUOTED.matcher(input).matches() || DOUBLE_QUOTED.matcher(input).matches() || !input.contains(" ")) {
                return input;
            } else {
                return wrapWithSingleQuotes(input);
            }
        }

        private String wrapWithSingleQuotes(String input) {
            return String.format("'%1$s'", escapeSingleQuotes(input));
        }

        private String escapeSingleQuotes(String input) {
            return A_SINGLE_QUOTE.matcher(input).replaceAll("\\\\'");
        }
    }

    /**
     * A getClass() transformer.
     *
     * @param  The type of the object
     * @return A getClass() transformer.
     */
    public static  Transformer, T> type() {
        return new Transformer, T>() {
            @Override
            public Class transform(T original) {
                @SuppressWarnings("unchecked")
                Class aClass = (Class) original.getClass();
                return aClass;
            }
        };
    }

    public static  Transformer toTransformer(final Factory factory) {
        return new Transformer() {
            @Override
            public R transform(Object original) {
                return factory.create();
            }
        };
    }

    public static  Transformer toTransformer(final Action action) {
        return new Transformer() {
            @Override
            public R transform(I original) {
                action.execute(original);
                return null;
            }
        };
    }

    /**
     * Converts a URL to a URI
     */
    public static Transformer toURL() {
        return new Transformer() {
            @Override
            public URL transform(URI original) {
                try {
                    return original.toURL();
                } catch (MalformedURLException e) {
                    throw UncheckedException.throwAsUncheckedException(e);
                }
            }
        };
    }

    /**
     * Always returns the given argument.
     */
    public static  Transformer constant(final T t) {
        return new Transformer() {
            @Override
            public T transform(I original) {
                return t;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy