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

com.versionone.apiclient.services.ValueStringizer Maven / Gradle / Ivy

Go to download

A library for custom Java development against the VersionOne Development Platform's REST API.

There is a newer version: 16.1.3
Show newest version
package com.versionone.apiclient.services;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ValueStringizer {
    private final String valueWrapper;

    public ValueStringizer() {
        this("'");
    }

    public ValueStringizer(String valueWrapper) {
        this.valueWrapper = valueWrapper;
    }

    public String stringize(Object value) {
        String valueString = value != null ? format(value) : "";
        valueString = valueString.replace("'", "''").replace("\"", "\"\"");

        try {
            valueString = URLEncoder.encode(valueString, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            valueString = valueString.replace("+", "%2B");
            valueString = valueString.replace(" ", "+");
            valueString = valueString.replace("&", "%26");
            valueString = valueString.replace("#", "%23");
        }

        return String.format("%1$s%2$s%1$s", valueWrapper, valueString);
    }

    private static String format(Object value) {
        if(value instanceof Date) {
            Date date = (Date) value;
            Format dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
            return dateFormat.format(date);
        }

        return value.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy