List of usage examples for org.apache.commons.lang CharUtils toChar
public static char toChar(String str)
Converts the String to a char using the first character, throwing an exception on empty Strings.
From source file:org.apache.ctakes.jdl.data.loader.CsvLoader.java
/** * @param loader/*from ww w .jav a 2 s . c o m*/ * the loader * @param file * the file * @throws FileNotFoundException * exception */ @SuppressWarnings({ "rawtypes", "unchecked" }) public CsvLoader(final CsvLoadType loader, final File file) throws FileNotFoundException { InputStream inputStrem = new FileInputStream(file); Reader reader = new InputStreamReader(inputStrem); char delimiter = CharUtils.toChar(loader.getDelimiter()); char encapsulator = (loader.getEncapsulator() == null || loader.getEncapsulator().length() == 0) ? CSVStrategy.ENCAPSULATOR_DISABLED : CharUtils.toChar(loader.getEncapsulator()); log.info(String.format("delimiter %d encapsulator %d", (int) delimiter, (int) encapsulator)); CSVStrategy strategy = new CSVStrategy(delimiter, encapsulator, CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, true, true, false, true); parser = new CSVParser(reader, strategy); this.loader = loader; formatMap = new HashMap<String, Format>(); try { for (Column col : loader.getColumn()) { if (col.getFormat() != null && col.getFormat().length() > 0) { Class cf = Class.forName(col.getFormat()); Constructor ccf = cf.getConstructor(String.class); this.formatMap.put(col.getName(), (Format) ccf.newInstance(col.getPattern())); } } } catch (Exception e) { throw new RuntimeException("oops", e); } }
From source file:reconf.client.constructors.SimpleConstructor.java
public Object construct(MethodData data) throws Throwable { if (data.hasAdapter()) { return data.getAdapter().adapt(data.getValue()); }//from w ww. j a v a 2s .c o m Class<?> returnClass = (Class<?>) data.getReturnType(); String trimmed = StringUtils.defaultString(StringUtils.trim(data.getValue())); if (!trimmed.startsWith("'") || !trimmed.endsWith("'")) { throw new RuntimeException(msg.format("error.invalid.string", data.getValue(), data.getMethod())); } String wholeValue = StringUtils.substring(trimmed, 1, trimmed.length() - 1); if (String.class.equals(returnClass)) { return wholeValue; } if (null == data.getValue()) { return null; } if (Object.class.equals(returnClass)) { returnClass = String.class; } if (char.class.equals(data.getReturnType()) || Character.class.equals(data.getReturnType())) { if (StringUtils.length(wholeValue) == 1) { return CharUtils.toChar(wholeValue); } return CharUtils.toChar(StringUtils.replace(wholeValue, " ", "")); } if (primitiveBoxing.containsKey(returnClass)) { if (StringUtils.isBlank(wholeValue)) { return null; } Method parser = primitiveBoxing.get(returnClass).getMethod( "parse" + StringUtils.capitalize(returnClass.getSimpleName()), new Class<?>[] { String.class }); return parser.invoke(primitiveBoxing.get(returnClass), new Object[] { StringUtils.trim(wholeValue) }); } Method valueOf = null; try { valueOf = returnClass.getMethod("valueOf", new Class<?>[] { String.class }); } catch (NoSuchMethodException ignored) { } if (valueOf == null) { try { valueOf = returnClass.getMethod("valueOf", new Class<?>[] { Object.class }); } catch (NoSuchMethodException ignored) { } } if (null != valueOf) { if (StringUtils.isEmpty(wholeValue)) { return null; } return valueOf.invoke(data.getReturnType(), new Object[] { StringUtils.trim(wholeValue) }); } Constructor<?> constructor = null; try { constructor = returnClass.getConstructor(String.class); constructor.setAccessible(true); } catch (NoSuchMethodException ignored) { throw new IllegalStateException( msg.format("error.string.constructor", returnClass.getSimpleName(), data.getMethod())); } try { return constructor.newInstance(wholeValue); } catch (Exception e) { if (e.getCause() != null && e.getCause() instanceof NumberFormatException) { return constructor.newInstance(StringUtils.trim(wholeValue)); } throw e; } }