List of usage examples for org.apache.commons.lang StringUtils capitalize
public static String capitalize(String str)
Capitalizes a String changing the first letter to title case as per Character#toTitleCase(char) .
From source file:net.ymate.platform.core.beans.support.PropertyStateSupport.java
public T duplicate(Object source, boolean ignoreNull) { ClassUtils.BeanWrapper<Object> _wrapperSource = ClassUtils.wrapper(source); for (String _fieldName : _wrapperSource.getFieldNames()) { Field _field = _wrapperSource.getField(_fieldName); try {/*from w ww . ja v a 2 s . c o m*/ Object _obj = _field.get(source); if (ignoreNull && _obj == null) { continue; } Method _method = __bound.getClass().getMethod("set" + StringUtils.capitalize(_fieldName), _field.getType()); _method.invoke(__bound, _field.get(source)); } catch (Exception e) { // Nothing... } } return __bound; }
From source file:net.ymate.platform.persistence.base.EntityMeta.java
/** * ????JavaBean?<br>/*from www . jav a 2 s. c om*/ * ??"user_name"?"UserName"<br> * * @param propertyName ?? * @return ?JavaBean? */ public static String propertyNameToFieldName(String propertyName) { if (StringUtils.contains(propertyName, '_')) { String[] _words = StringUtils.split(propertyName, '_'); if (_words != null) { if (_words.length > 1) { StringBuilder _returnBuilder = new StringBuilder(); for (String _word : _words) { _returnBuilder.append(StringUtils.capitalize(_word.toLowerCase())); } return _returnBuilder.toString(); } return StringUtils.capitalize(_words[0].toLowerCase()); } } return propertyName; }
From source file:net.ymate.platform.persistence.jdbc.scaffold.EntityGenerator.java
/** * ??/*from w w w . j av a2s . com*/ */ public void createEntityClassFiles() { Map<String, Object> _propMap = buildPropMap(); // boolean _isUseBaseEntity = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_base_entity")) .toBooleanValue(); boolean _isUseClassSuffix = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_class_suffix")) .toBooleanValue(); boolean _isUseChainMode = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_chain_mode")) .toBooleanValue(); boolean _isUseStateSupport = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_state_support")) .toBooleanValue(); _propMap.put("isUseBaseEntity", _isUseBaseEntity); _propMap.put("isUseClassSuffix", _isUseClassSuffix); _propMap.put("isUseChainMode", _isUseChainMode); _propMap.put("isUseStateSupport", _isUseStateSupport); if (_isUseBaseEntity) { buildTargetFile("/model/BaseEntity.java", "/BaseEntity.ftl", _propMap); } // List<String> _tableList = Arrays.asList(StringUtils .split(StringUtils.defaultIfBlank(__owner.getConfig().getParam("jdbc.table_list"), ""), "|")); if (_tableList.isEmpty()) { _tableList = getTableNames(); } // String _dbName = __owner.getConfig().getParam("jdbc.db_name"); String _dbUser = __owner.getConfig().getParam("jdbc.db_username"); String[] _prefixs = StringUtils .split(StringUtils.defaultIfBlank(__owner.getConfig().getParam("jdbc.table_prefix"), ""), '|'); boolean _isRemovePrefix = new BlurObject(__owner.getConfig().getParam("jdbc.remove_table_prefix")) .toBooleanValue(); List<String> _tableExcludeList = Arrays.asList(StringUtils.split(StringUtils .defaultIfBlank(__owner.getConfig().getParam("jdbc.table_exclude_list"), "").toLowerCase(), "|")); for (String _tableName : _tableList) { // ??? if (!_tableExcludeList.isEmpty()) { if (_tableExcludeList.contains(_tableName.toLowerCase())) { continue; } else { boolean _flag = false; for (String _excludedName : _tableExcludeList) { if (StringUtils.contains(_excludedName, "*") && StringUtils.startsWithIgnoreCase(_tableName, StringUtils.substringBefore(_excludedName, "*"))) { _flag = true; break; } } if (_flag) { continue; } } } TableMeta _tableMeta = getTableMeta(_dbName, _dbUser, _tableName); if (_tableMeta != null) { String _modelName = null; for (String _prefix : _prefixs) { if (_tableName.startsWith(_prefix)) { if (_isRemovePrefix) { _tableName = _tableName.substring(_prefix.length()); } _modelName = StringUtils.capitalize(EntityMeta.propertyNameToFieldName(_tableName)); break; } } if (StringUtils.isBlank(_modelName)) { _modelName = StringUtils.capitalize(EntityMeta.propertyNameToFieldName(_tableName)); } // _propMap.put("tableName", _tableName); _propMap.put("modelName", _modelName); List<Attr> _fieldList = new ArrayList<Attr>(); // List<Attr> _fieldListForNotNullable = new ArrayList<Attr>(); // ? List<Attr> _allFieldList = new ArrayList<Attr>(); // ???? if (_tableMeta.getPkSet().size() > 1) { _propMap.put("primaryKeyType", _modelName + "PK"); _propMap.put("primaryKeyName", StringUtils.uncapitalize((String) _propMap.get("primaryKeyType"))); List<Attr> _primaryKeyList = new ArrayList<Attr>(); _propMap.put("primaryKeyList", _primaryKeyList); Attr _pkAttr = new Attr((String) _propMap.get("primaryKeyType"), (String) _propMap.get("primaryKeyName"), null, false, false, 0, 0, 0, null, null); _fieldList.add(_pkAttr); _fieldListForNotNullable.add(_pkAttr); // for (String pkey : _tableMeta.getPkSet()) { ColumnInfo _ci = _tableMeta.getFieldMap().get(pkey); _primaryKeyList.add(_ci.toAttr()); _allFieldList.add(new Attr("String", _ci.getColumnName().toUpperCase(), _ci.getColumnName(), _ci.isAutoIncrement(), _ci.isSigned(), _ci.getPrecision(), _ci.getScale(), _ci.getNullable(), _ci.getDefaultValue(), _ci.getRemarks())); } for (String key : _tableMeta.getFieldMap().keySet()) { if (_tableMeta.getPkSet().contains(key)) { continue; } ColumnInfo _ci = _tableMeta.getFieldMap().get(key); Attr _attr = _ci.toAttr(); _fieldList.add(_attr); _fieldListForNotNullable.add(_attr); _allFieldList.add(new Attr("String", _ci.getColumnName().toUpperCase(), _ci.getColumnName(), _ci.isAutoIncrement(), _ci.isSigned(), _ci.getPrecision(), _ci.getScale(), _ci.getNullable(), _ci.getDefaultValue(), _ci.getRemarks())); } } else { _propMap.put("primaryKeyType", _tableMeta.getFieldMap().get(_tableMeta.getPkSet().get(0)).getColumnType()); _propMap.put("primaryKeyName", StringUtils .uncapitalize(EntityMeta.propertyNameToFieldName(_tableMeta.getPkSet().get(0)))); for (String key : _tableMeta.getFieldMap().keySet()) { ColumnInfo _ci = _tableMeta.getFieldMap().get(key); Attr _attr = _ci.toAttr(); _fieldList.add(_attr); if (_attr.getNullable() == 0) { _fieldListForNotNullable.add(_attr); } _allFieldList.add(new Attr("String", _ci.getColumnName().toUpperCase(), _ci.getColumnName(), _ci.isAutoIncrement(), _ci.isSigned(), _ci.getPrecision(), _ci.getScale(), _ci.getNullable(), _ci.getDefaultValue(), _ci.getRemarks())); } } _propMap.put("fieldList", _fieldList); // ?????? _propMap.put("notNullableFieldList", _fieldList.size() == _fieldListForNotNullable.size() ? Collections.emptyList() : _fieldListForNotNullable); _propMap.put("allFieldList", _allFieldList); // buildTargetFile("/model/" + _modelName + (_isUseClassSuffix ? "Model.java" : ".java"), "/Entity.ftl", _propMap); // if (_tableMeta.getPkSet().size() > 1) { _propMap.put("modelName", _modelName); if (_tableMeta.getPkSet().size() > 1) { List<Attr> _primaryKeyList = new ArrayList<Attr>(); _propMap.put("primaryKeyList", _primaryKeyList); // for (String pkey : _tableMeta.getPkSet()) { ColumnInfo _ci = _tableMeta.getFieldMap().get(pkey); _primaryKeyList.add(_ci.toAttr()); } } buildTargetFile("/model/" + _modelName + "PK.java", "/EntityPK.ftl", _propMap); } } } }
From source file:net.ymate.platform.persistence.jdbc.support.JdbcEntityMeta.java
/** * ????JavaBean?<br/>/*from www. j av a 2 s .co m*/ * ??"user_name"?"UserName"<br/> * @param fieldName ?? * @return ?JavaBean? */ public static String buildFieldNameToClassAttribute(String fieldName) { String[] _words = StringUtils.split(fieldName, '_'); if (_words != null) { if (_words.length > 1) { StringBuilder _returnBuilder = new StringBuilder(); for (String _word : _words) { _returnBuilder.append(StringUtils.capitalize(_word.toLowerCase())); } return _returnBuilder.toString(); } else { return StringUtils.capitalize(_words[0].toLowerCase()); } } return null; }
From source file:net.ymate.platform.persistence.support.EntityMeta.java
/** * ????JavaBean?<br/>/*www .j av a2 s . com*/ * ??"user_name"?"UserName"<br/> * @param fieldName ?? * @return ?JavaBean? */ public static String buildFieldNameToClassAttribute(String fieldName) { if (StringUtils.contains(fieldName, '_')) { String[] _words = StringUtils.split(fieldName, '_'); if (_words != null) { if (_words.length > 1) { StringBuilder _returnBuilder = new StringBuilder(); for (String _word : _words) { _returnBuilder.append(StringUtils.capitalize(_word.toLowerCase())); } return _returnBuilder.toString(); } else { return StringUtils.capitalize(_words[0].toLowerCase()); } } } return fieldName; }
From source file:net.ymate.platform.serv.nio.datagram.NioUdpServer.java
public synchronized void start() throws IOException { if (!__isStarted) { __isStarted = true;/* w ww . ja v a 2 s . co m*/ __eventGroup = new NioEventGroup<NioUdpListener>(__serverCfg, __listener, __codec) { @Override protected SelectableChannel __doChannelCreate(INioServerCfg cfg) throws IOException { DatagramChannel _channel = DatagramChannel.open(); _channel.configureBlocking(false); _channel.socket().bind(new InetSocketAddress(cfg.getServerHost(), cfg.getPort())); return _channel; } @Override protected String __doBuildProcessorName() { return StringUtils.capitalize(name()).concat("UdpServer-NioEventProcessor-"); } @Override protected void __doInitProcessors() throws IOException { __processors = new NioEventProcessor[__selectorCount]; for (int _idx = 0; _idx < __selectorCount; _idx++) { __processors[_idx] = new NioEventProcessor<NioUdpListener>(this, __doBuildProcessorName() + _idx) { @Override protected void __doExceptionEvent(SelectionKey key, final Throwable e) { final INioSession _session = (INioSession) key.attachment(); if (_session != null) { __eventGroup.executorService().submit(new Runnable() { public void run() { try { __eventGroup.listener().onExceptionCaught(e, _session); } catch (Throwable ex) { _LOG.error(e.getMessage(), RuntimeUtils.unwrapThrow(ex)); } } }); } else { _LOG.error(e.getMessage(), RuntimeUtils.unwrapThrow(e)); } } }; __processors[_idx].start(); } } @Override protected void __doRegisterEvent() throws IOException { for (NioEventProcessor _processor : __processors) { _processor.registerEvent(__channel, SelectionKey.OP_READ, new NioSession<NioUdpListener>(this, __channel) { @Override protected int __doChannelRead(ByteBuffer buffer) throws IOException { SocketAddress _address = ((DatagramChannel) __channel).receive(buffer); if (_address != null) { attr(SocketAddress.class.getName(), _address); return __buffer.remaining(); } return 0; } @Override protected int __doChannelWrite(ByteBuffer buffer) throws IOException { SocketAddress _address = attr(SocketAddress.class.getName()); if (_address != null) { return ((DatagramChannel) __channel).send(buffer, _address); } buffer.reset(); return 0; } }); } } }; __eventGroup.start(); // _LOG.info("UdpServer [" + __eventGroup.name() + "] started at " + __serverCfg.getServerHost() + ":" + __serverCfg.getPort()); } }
From source file:net.ymate.platform.serv.nio.support.NioEventGroup.java
protected String __doBuildProcessorName() { return StringUtils.capitalize(name()).concat(isServer() ? "Server" : "Client") .concat("-NioEventProcessor-"); }
From source file:nl.inl.util.StringUtil.java
/** * Convert the first character in a string to uppercase. * @param str the string to be capitalized * @return the capitalized string/*from w w w. j a v a2s . co m*/ * @deprecated use StringUtils.capitalize() from commons-lang */ @Deprecated public static String capitalizeFirst(String str) { return StringUtils.capitalize(str); // if (str.length() == 0) // return str; // return str.substring(0, 1).toUpperCase() + str.substring(1); }
From source file:nl.knaw.dans.common.lang.search.bean.AbstractSearchBeanFactory.java
@SuppressWarnings("unchecked") public Object createSearchBean(String type, Document document) throws SearchBeanFactoryException, SearchBeanException { Class<?> sbClass = null; Object searchBean = null;//from ww w .j av a2s . com sbClass = getTypeMap().get(type); if (sbClass == null) throw new UnknownSearchBeanTypeException(type); Map<String, java.lang.reflect.Field> fieldsMap = null; try { fieldsMap = getFieldsMap(sbClass); searchBean = sbClass.newInstance(); } catch (Exception e) { throw new SearchBeanException(e); } // check if the primary key is in the document Index index = SearchBeanUtil.getDefaultIndex(sbClass); if (index != null) { if (document.getFieldByName(index.getPrimaryKey()) == null) throw new PrimaryKeyMissingException("Primary key not found in document. " + document.toString()); } // convert fields for (Map.Entry<String, java.lang.reflect.Field> searchField : fieldsMap.entrySet()) { Field docField = document.getFieldByName(searchField.getKey()); java.lang.reflect.Field classField = searchField.getValue(); if (docField != null) { String propName = classField.getName(); String setMethodName = "set" + StringUtils.capitalize(propName); try { Method setMethod = sbClass.getMethod(setMethodName, new Class[] { classField.getType() }); Class fieldType = classField.getType(); Object value = docField.getValue(); SearchField sbField = classField.getAnnotation(SearchField.class); Class<? extends SearchFieldConverter<?>> converterClass = sbField.converter(); // convert to basic types boolean basicConversionFailed = false; if (!ClassUtil.instanceOf(value, fieldType)) { // Don't change the type of value if we have a non-default converter // instead, let the converter do it's work //if (ClassUtil.classImplements(fieldType, Collection.class) ) if (ClassUtil.classImplements(fieldType, Collection.class) && converterClass.equals(DefaultSearchFieldConverter.class)) { ArrayList listValue = new ArrayList(1); listValue.add(value); value = listValue; } else if (fieldType.equals(String.class)) { value = value.toString(); } else if (fieldType.equals(DateTime.class) && value.getClass().equals(Date.class)) { value = new DateTime((Date) value); } else if (fieldType.equals(DateTime.class) && value.getClass().equals(String.class)) { value = new DateTime(value.toString()); } else if (ClassUtil.instanceOf(fieldType, Enum.class)) { value = Enum.valueOf(fieldType, value.toString()); } else if (fieldType.equals(int.class) && value.getClass().equals(Integer.class)) { value = ((Integer) value).intValue(); } else // if basic conversion fails, the converter might still // save the day basicConversionFailed = true; } if (!converterClass.equals(DefaultSearchFieldConverter.class)) { SearchFieldConverter<?> converter = converterClass.newInstance(); value = converter.fromFieldValue(value); } else { if (basicConversionFailed) // converter did not save the day throw new DocumentReturnedInvalidTypeException( "expected " + fieldType.toString() + " but got " + value.getClass().toString()); } setMethod.invoke(searchBean, value); } catch (Exception e) { throw new SearchBeanException(e); } } else { SearchField sbField = classField.getAnnotation(SearchField.class); if (sbField.required()) throw new MissingRequiredFieldException(sbField.name()); } } return searchBean; }
From source file:nl.knaw.dans.common.lang.search.bean.GenericSearchBeanConverter.java
@SuppressWarnings("unchecked") public IndexDocument toIndexDocument(Object searchBean) throws SearchBeanConverterException, SearchBeanException { Class sbClass = searchBean.getClass(); if (!sbClass.isAnnotationPresent(SearchBean.class)) throw new ObjectIsNotASearchBeanException(sbClass.toString()); SimpleIndexDocument indexDocument = new SimpleIndexDocument(SearchBeanUtil.getDefaultIndex(sbClass)); for (java.lang.reflect.Field classField : ClassUtil.getAllFields(sbClass).values()) { if (classField.isAnnotationPresent(SearchField.class)) { SearchField sbField = classField.getAnnotation(SearchField.class); String fieldName = sbField.name(); boolean isRequired = sbField.required(); Class<? extends SearchFieldConverter<?>> converter = sbField.converter(); String propName = classField.getName(); String getMethodName = "get" + StringUtils.capitalize(propName); addFieldToDocument(searchBean, indexDocument, getMethodName, fieldName, isRequired, converter); if (classField.isAnnotationPresent(CopyField.class)) { for (Annotation annot : classField.getAnnotations()) { if (annot instanceof CopyField) { CopyField sbCopyField = (CopyField) annot; fieldName = sbCopyField.name(); isRequired = sbCopyField.required(); getMethodName = "get" + StringUtils.capitalize(propName) + StringUtils.capitalize(sbCopyField.getterPostfix()); converter = sbCopyField.converter(); addFieldToDocument(searchBean, indexDocument, getMethodName, fieldName, isRequired, converter); }/*from w ww .j a va2 s. c o m*/ } } } } if (indexDocument.getIndex() != null) { if (indexDocument.getFields().getByFieldName(indexDocument.getIndex().getPrimaryKey()) == null) throw new PrimaryKeyMissingException("Primary key not set to search bean object."); } return indexDocument; }