org.eobjects.analyzer.beans.codec.UrlEncoderTransformer.java Source code

Java tutorial

Introduction

Here is the source code for org.eobjects.analyzer.beans.codec.UrlEncoderTransformer.java

Source

/**
 * eobjects.org AnalyzerBeans
 * Copyright (C) 2010 eobjects.org
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.eobjects.analyzer.beans.codec;

import org.eobjects.analyzer.beans.api.Categorized;
import org.eobjects.analyzer.beans.api.Configured;
import org.eobjects.analyzer.beans.api.Description;
import org.eobjects.analyzer.beans.api.OutputColumns;
import org.eobjects.analyzer.beans.api.Transformer;
import org.eobjects.analyzer.beans.api.TransformerBean;
import org.eobjects.analyzer.beans.categories.StringManipulationCategory;
import org.eobjects.analyzer.data.InputColumn;
import org.eobjects.analyzer.data.InputRow;
import org.eobjects.analyzer.data.MockInputColumn;
import org.apache.metamodel.util.HasName;

import com.google.common.escape.Escaper;
import com.google.common.net.UrlEscapers;

@TransformerBean("URL encoder")
@Description("Encodes/escapes a URL or part of a URL")
@Categorized({ StringManipulationCategory.class })
public class UrlEncoderTransformer implements Transformer<String> {

    public static enum TargetFormat implements HasName {
        FORM_PARAMETER("Form parameter"), FRAGMENT("URL fragment"), PATH_SEGMENT("Path segment");

        private final String _name;

        private TargetFormat(String name) {
            _name = name;
        }

        @Override
        public String getName() {
            return _name;
        }
    }

    @Configured
    InputColumn<String> column;

    @Configured
    TargetFormat targetFormat = TargetFormat.FRAGMENT;

    public UrlEncoderTransformer() {
    }

    public UrlEncoderTransformer(MockInputColumn<String> column) {
        this();
        this.column = column;
    }

    @Override
    public OutputColumns getOutputColumns() {
        return new OutputColumns(column.getName() + " (URL encoded)");
    }

    @Override
    public String[] transform(final InputRow inputRow) {
        final String value = inputRow.getValue(column);
        if (value == null) {
            return new String[1];
        }
        final Escaper escaper;
        switch (targetFormat) {
        case FORM_PARAMETER:
            escaper = UrlEscapers.urlFormParameterEscaper();
            break;
        case FRAGMENT:
            escaper = UrlEscapers.urlFragmentEscaper();
            break;
        case PATH_SEGMENT:
            escaper = UrlEscapers.urlPathSegmentEscaper();
            break;
        default:
            throw new UnsupportedOperationException();
        }
        final String escaped = escaper.escape(value);
        return new String[] { escaped };
    }

}