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

com.x5.util.DataCapsuleReader Maven / Gradle / Ivy

package com.x5.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;

public class DataCapsuleReader
{
	// For efficiency, keep around instances of DataCapsuleReader objects
	// in a static hashtable so we don't have to wait for slow reflection
	// after the first time...
	private static Hashtable readerCache
		= new Hashtable();
	
	private String[] labels;
	private String[] bareLabels;
	private String[] methodNames;
	private Method[] methods;
	
	@SuppressWarnings("rawtypes")
	private Class capsuleClass;
	
	public static DataCapsuleReader getReader(DataCapsule[] dataCapsules)
	{
		DataCapsuleReader reader = getReaderFromCache(dataCapsules);
		if (reader == null) {
			reader = new DataCapsuleReader(dataCapsules);
			readerCache.put(reader.getDataClassName(), reader);
		}
		return reader;
	}
	
	public static DataCapsuleReader getReader(DataCapsule dataCapsule)
	{
		if (dataCapsule == null) return null;
		
		DataCapsuleReader reader = getReaderFromCache(dataCapsule);
		if (reader == null) {
			reader = new DataCapsuleReader(new DataCapsule[]{dataCapsule});
			readerCache.put(reader.getDataClassName(), reader);
		}
		return reader;
	}
	
	private static DataCapsuleReader getReaderFromCache(DataCapsule[] dataCapsules)
	{
		for (int i=0; i -1) {
			// use specified name
			exportName = directive.substring(spacePos+1).trim();
			methodName = directive.substring(0,spacePos);
		} else {
			// transmogrify method name
			methodName = directive;
			exportName = transmogrify(directive);
		}

		String prefixedName = exportName;
		if (exportPrefix != null) {
			prefixedName = exportPrefix + "_" + exportName;
		}
		
		labels[i] = prefixedName;
		bareLabels[i] = exportName;
		methodNames[i] = methodName;
	}

	// transmogrify converts getDatePretty to date_pretty
	private static String transmogrify(String s)
	{
		String spaced = splitCamelCase(s);
		return spaced.toLowerCase().replaceFirst("^get_", "");
	}
	
	// splitCamelCase converts SimpleXMLStuff to Simple_XML_Stuff
	private static String splitCamelCase(String s)
	{
	   return s.replaceAll(
	      String.format("%s|%s|%s",
	         "(?<=[A-Z])(?=[A-Z][a-z])",
	         "(?<=[^A-Z])(?=[A-Z])",
	         "(?<=[A-Za-z])(?=[^A-Za-z])"
	      ),
	      "_"
	   );
	}
	
	public String[] getColumnLabels(String altPrefix)
	{
		if (altPrefix == null) return getColumnLabels();
		
		String[] altLabels = new String[bareLabels.length];
		for (int i=0; i=0 && matchCount > 0; i--) {
	        if (isMatch[i]) {
	            matchCount--;
	            simpleMethods[matchCount] = allMethods[i];
	        }
	    }
	    
	    return simpleMethods;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy