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

com.adaptrex.core.utilities.StringUtilities Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 Adaptrex, LLC
 *
 * 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.adaptrex.core.utilities;

import com.adaptrex.core.Adaptrex;
import com.adaptrex.core.AdaptrexProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

import java.util.List;

import javax.servlet.ServletContext;

public class StringUtilities {

	public static String json(Object object) throws Exception {
		return json(object, null);
	}
	
	/**
	 * Serialize an object to JSON.  If Adaptrex is set to debug mode,
	 * this outputs "pretty" JSON so it's readable during development.
	 * @throws Exception 
	 */
	public static String json(Object object, ServletContext context) throws Exception {
		Boolean debug = false;
		if (context != null) {
			debug = Boolean.valueOf(
					Adaptrex.get(context).getProperties().get(AdaptrexProperties.DEBUG));
		}
		ObjectMapper mapper = new ObjectMapper();
        if (debug) {
            ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
            return writer.writeValueAsString(object);
        } else {
            return mapper.writeValueAsString(object);
        }
    }

    
    /**
     * Convert JSON to an object of the specified type 
     * @throws Exception 
     */
    public static Object fromJson(String json, Class type) throws Exception {
    	return (new ObjectMapper()).readValue(json, type);
    }


    /**
     * Join a list of strings
     */
    public static String join(List strings, String delimiter) {
        String j = "";
        for (String item : strings) {
        	j += (j.isEmpty() ? "" : delimiter) + item;
        }
        return j;
    }

    /**
     * Capitalize a word
     */
    public static String capitalize(String string) {
    	return setCase(string, true);
    }

    /**
     * Uncapitalize a word
     */
    public static String uncapitalize(String string) {
        return setCase(string, false);
    }
    
    private static String setCase(String string, boolean capitalize) {
    	if (string == null) return null;
        String result = string.trim();
        if (result.length() == 0) return "";
        if (result.length() == 1) return capitalize ? result.toUpperCase() : result.toLowerCase();
        return (capitalize ? result.substring(0,1).toUpperCase() : result.substring(0,1).toLowerCase()) + result.substring(1);
    }
    
    public static String singularize(String str) {
    	return Inflector.getInstance().singularize(str);
    }

    public static String pluralize(String str) {
    	return Inflector.getInstance().pluralize(str);
    }
    
    
	/*
	 * Generate javascript that writes our error to the javascript console using
	 * code that is also readable when viewing the page source.
	 */
	public static String getErrorJavaScript(String... lines) {
		StringBuilder output = new StringBuilder();
		output.append("");
		return output.toString();
	}
	
	private static String leftPadLog(String... rows) {
		StringBuilder output = new StringBuilder();
		for (String row : rows) {
			output.append("console.log(\"  " + String.format("%1$-100s", row) + "\");\n");
		}
		return output.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy