Java tutorial
/** * Copyright (C) 2015-2016 OurBeehive(http://ourbeehive.github.io/) * * 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. * Project Name: MyBatisPioneer * File Name: JavaFormatter.java * Package Name: org.ourbeehive.mbp.util * * Date: Jan 20, 2016 * Author: Sericloud * */ package org.ourbeehive.mbp.util; import org.apache.commons.lang3.StringUtils; import org.ourbeehive.mbp.exception.AppException; import org.ourbeehive.mbp.lang.JavaSrcElm; import org.ourbeehive.mbp.lang.MapperElm; import org.ourbeehive.mbp.model.ant.AllMapperProfile; import org.ourbeehive.mbp.model.ant.ComnArtifact; import org.ourbeehive.mbp.model.ant.InsertConfig; import org.ourbeehive.mbp.model.ant.MapperArtifact; import org.ourbeehive.mbp.model.ant.MapperProfile; import org.ourbeehive.mbp.model.ant.ResultMapConfig; import org.ourbeehive.mbp.model.ant.UpdateConfig; import org.ourbeehive.mbp.model.db.OrmColumn; import org.ourbeehive.mbp.register.CtxCacheFacade; public class JavaFormatter { public static String getDbStyle(String javaPhrase) { // The return value. StringBuffer javaWord = new StringBuffer(); StringBuffer dbPhrase = new StringBuffer(); String dbWord = null; // Get "javaNameToDbName" cache from ctxCache. // Hashtable<String, String> javaNameToDbNameCache = CtxCacheFacade.getJavaNameToDbNameCache(); int len = javaPhrase.length(); char c = 0; for (int i = 0; i < len; i++) { c = javaPhrase.charAt(i); // Java Bean????"_" if (String.valueOf(c).equals(MapperElm.UNDER_LINE) == true) { continue; } if (Character.isLowerCase(c) == true || Character.isDigit(c) == true) { javaWord.append(c); if (i < len - 1) { continue; } } if (javaWord.length() != 0) { // Find corresponding dbWord with previous javaWord. // dbWord = javaNameToDbNameCache.get(javaWord.toString().toLowerCase()); dbWord = javaWord.toString().toUpperCase(); // if (dbWord != null) { if (dbPhrase.length() != 0) { dbPhrase.append(MapperElm.UNDER_LINE); } dbPhrase.append(dbWord); } // else { // // No need to look up others. // logger.warn("NO DB WORD: Fail to find corresponding DB word for " + "Java word '" + javaWord + "' in Java phrase '" + javaPhrase // + "'"); // return null; // } // Begin a new javaWord. javaWord.delete(0, javaWord.length()); if (Character.isUpperCase(c) == true) { javaWord.append(c); } } // javaWord.append(c); // Processing the last javaWord. // if (javaWord.length() != 0) { // // // Find corresponding dbWord with the last javaWord. // dbWord = fullNameToAbbrCache.get(javaWord.toString().toLowerCase()); // if (dbWord != null) { // if (dbPhrase.length() != 0) { // dbPhrase.append(MapperElm.UNDER_LINE); // } // dbPhrase.append(dbWord); // } else { // // No need to look up others. // logger.warn("NO DB WORD: Fail to find corresponding DB word for " + "Java word '" + javaWord + "' in Java phrase '" + javaPhrase // + "'"); // return null; // } // } return dbPhrase.toString(); } public static String getCollectionParamType(String phrase) { if (phrase.contains(JavaSrcElm.LESS_THAN) && phrase.contains(JavaSrcElm.GREATER_THAN)) { int start = phrase.indexOf(JavaSrcElm.LESS_THAN); int end = phrase.indexOf(JavaSrcElm.GREATER_THAN); phrase = phrase.substring(start + 1, end); } return phrase; } public static String getAttributeName(String phrase) { //judge if is a list if (phrase.contains(JavaSrcElm.LESS_THAN) && phrase.contains(JavaSrcElm.GREATER_THAN)) { int start = phrase.indexOf(JavaSrcElm.LESS_THAN); int end = phrase.indexOf(JavaSrcElm.GREATER_THAN); String collectionName = phrase.substring(0, start); String paramName = phrase.substring(start + 1, end); phrase = paramName + collectionName; } String first = phrase.substring(0, 1); return first.toLowerCase() + phrase.substring(1); } public static String getJavaStyle(String dbPhrase, boolean isLargeCamel) { // If 'dbPhrase' contain schema name, then remove it. int idx = dbPhrase.indexOf(MapperElm.DOT); if (idx != -1) { dbPhrase = dbPhrase.substring(idx + 1, dbPhrase.length()); } // If table name begin with defined prefix, then remove it. if (dbPhrase.startsWith(MapperElm.SQL_TABLE_NAME_PREFIX) == true) { dbPhrase = dbPhrase.substring(MapperElm.SQL_TABLE_NAME_PREFIX.length(), dbPhrase.length()); } // Prepare the return value. StringBuffer dbWord = new StringBuffer(); StringBuffer javaPhrase = new StringBuffer(); String javaWord = null; // Get 'abbrToFullNameCache' from SrcGenContext. // Hashtable<String, String> abbrToFullNameCache = CtxCacheFacade.getAbbrToFullNameCache(); int len = dbPhrase.length(); char c = 0; // Separate 'dbPhrase' into 'dbWord', and transform to corresponding 'javaWord'. for (int i = 0; i < len; i++) { c = dbPhrase.charAt(i); // Append any characters not equals underline at the end of current dbWord. if (String.valueOf(c).equals(MapperElm.UNDER_LINE) == false) { dbWord.append(c); if (i < len - 1) { continue; } } if (dbWord.length() != 0) { // Find corresponding 'javaWord' with previous 'dbWord'. // if (changeToFullName == true) { // javaWord = abbrToFullNameCache.get(dbWord.toString()); // } else { javaWord = dbWord.toString().toLowerCase(); // } // If 'javaWord' was found, then append it into 'javaPhrase'. // if (javaWord != null) { if (isLargeCamel == true || javaPhrase.length() != 0) { javaWord = StringHelper.toUpperCase(javaWord, 0); } javaPhrase.append(javaWord); // } // else { // // If no 'javaWord' was found, then no need to look up others. // logger.warn("NO JAVA PHRASE: Fail to find corresponding Java phrase " + "for the given DB phrase: " + dbPhrase // + ", because no corresponding Java word found for DB word: " + dbWord); // return null; // } // Refresh current 'dbWord' and begin with new finding. dbWord.delete(0, dbWord.length()); } } // Processing the situation where no 'DB_WORD_SEPARATOR' was occurred. // if (dbWord.length() != 0) { // // // Find corresponding 'javaWord' with previous 'dbWord'. // // if (changeToFullName == true) { // // javaWord = abbrToFullNameCache.get(dbWord.toString()); // // } else { // javaWord = dbWord.toString().toLowerCase(); // // } // // // If 'javaWord' was found, then append it into 'javaPhrase'. // if (javaWord != null) { // if (javaPhrase.length() != 0) { // javaWord = StringHelper.toUpperCase(javaWord, 0); // } // javaPhrase.append(javaWord); // } // // // else { // // // If no 'javaWord' was found, then no need to look up others. // // logger.warn("NO JAVA PHRASE: Fail to find corresponding Java phrase " + "for the given DB phrase: " + dbPhrase // // + ", because no corresponding Java word found for DB word: " + dbWord); // // return null; // // } // // } return javaPhrase.toString(); } public static String getJavaNullExpr(OrmColumn ormColumn) throws AppException { String jdbcTypeName = ormColumn.getJdbcTypeName(); String javaTypeName = CtxCacheFacade.lookupJavaDataType(ormColumn.getJdbcTypeName()); if (StringUtils.isNotBlank(javaTypeName) == true) { return JavaSrcElm.EXPR_OBJ_NOT_NULL; } else { throw new AppException("NOT FOUND: Could not find Java type name with JDBC type name: " + jdbcTypeName); } } public static String getClassSimpleName(String classFullName) { // Judge whether the given name contain generic type declaration. String partName = classFullName; int lesserThan = classFullName.indexOf(JavaSrcElm.LESS_THAN); if (lesserThan != -1) { partName = classFullName.substring(0, lesserThan); } String classSimpleName = classFullName; int lastDot = partName.lastIndexOf(JavaSrcElm.DOT); if (lastDot != -1) { // Handle the situation that DOT is the last character of a string. // idx = idx == (classFullName.length() - 1) ? idx : idx + 1; classSimpleName = classFullName.substring(lastDot + 1); } return classSimpleName; } public static String getClassPkgName(String classFullName) { String classPkgName = classFullName; int idx = classFullName.lastIndexOf(JavaSrcElm.DOT); if (idx != -1) { classPkgName = classFullName.substring(0, idx); } return classPkgName; } public static String getVoPkgName(ResultMapConfig resultMapConfig) { String className = resultMapConfig.getClassName(); // TODO 2016.1.22 Pioneer vo dto /** * dto vo??? */ // Replace ".dto." with ".vo." className = StringHelper.replace(className, JavaSrcElm.DTOX_BASE_PKG_NAME_PAT, JavaSrcElm.VO_PKG_NAME_PAT); String voPkgName = className; int idx = className.lastIndexOf(JavaSrcElm.DOT); if (idx != -1) { voPkgName = className.substring(0, idx); } return voPkgName; } public static String getVoFullName(ResultMapConfig resultMapConfig, boolean isLargeCamel) { // Added by jiongbo at 2015.03 String voClassName = resultMapConfig.getVoClassName(); if (voClassName != null) { return voClassName; } // Added by jiongbo at 2015.03 String className = resultMapConfig.getClassName(); String tableName = resultMapConfig.getTableName(); // Get full name of corresponding VO class. String voFullName = null; String voSimpleName = JavaFormatter.getJavaStyle(tableName, isLargeCamel) + JavaSrcElm.VO_NAME_SUFFIX; int idx = className.indexOf(JavaSrcElm.DTOX_BASE_PKG_NAME_PAT); if (idx != -1) { int moduleIdx = className.indexOf(JavaSrcElm.DOT, idx + JavaSrcElm.DTOX_BASE_PKG_NAME_PAT.length()); String moduleName = className.substring(idx + JavaSrcElm.DTOX_BASE_PKG_NAME_PAT.length(), moduleIdx); voFullName = className.substring(0, idx) + JavaSrcElm.VO_PKG_NAME_PAT + moduleName + JavaSrcElm.DOT + voSimpleName; } else { voFullName = className; } return voFullName; } public static String getVoSimpleName(ResultMapConfig resultMapConfig, boolean isLargeCamel) { // Added by jiongbo at 2015.03 String voClassName = resultMapConfig.getVoClassName(); if (voClassName != null) { return voClassName.substring(voClassName.lastIndexOf(JavaSrcElm.DOT) + 1, voClassName.length()); } // Added by jiongbo at 2015.03 String tableName = resultMapConfig.getTableName(); // Get name of corresponding VO class. String voName = JavaFormatter.getJavaStyle(tableName, isLargeCamel) + JavaSrcElm.VO_NAME_SUFFIX; return voName; } public static String getVoFileName(MapperProfile mapperProfile, ResultMapConfig resultMapConfig) { AllMapperProfile allMapperProfile = mapperProfile.getAllMapperProfile(); ComnArtifact comnArtifact = allMapperProfile.getComnArtifact(); // Get the target directory. String ideWorkSpace = comnArtifact.getIdeWorkspace(); String srcPathName = comnArtifact.getSrcPathName(); String transObjPrjName = comnArtifact.getTransObjPrj(); String dtoClassName = resultMapConfig.getClassName(); // Replace ".dto." with ".vo." String voClassName = StringHelper.replace(dtoClassName, JavaSrcElm.DTOX_BASE_PKG_NAME_PAT, JavaSrcElm.VO_PKG_NAME_PAT); // Trim "Dto" and append "Vo" voClassName = StringHelper.trimAndAppend(voClassName, JavaSrcElm.DTOX_NAME_SUFFIX, JavaSrcElm.VO_NAME_SUFFIX); // Change package name to file name. String voFileName = JavaFormatter.classNameToFileName(voClassName); // Assemble the complete file name for Sql Map XML file. String fileFullName = ideWorkSpace + transObjPrjName + srcPathName + voFileName + JavaSrcElm.JAVA_FILE_EXT; return fileFullName; } public static String getVoPkgName(InsertConfig insertConfig) { String className = insertConfig.getClassName(); String voPkgName = className; int idx = className.lastIndexOf(JavaSrcElm.DOT); if (idx != -1) { voPkgName = className.substring(0, idx); } return voPkgName; } public static String getVoFullName(InsertConfig insertConfig, boolean isLargeCamel) { String className = insertConfig.getClassName(); return className; } public static String getVoSimpleName(InsertConfig insertConfig, boolean isLargeCamel) { String tableName = insertConfig.getTableName(); // Get name of corresponding VO class. String voName = JavaFormatter.getJavaStyle(tableName, isLargeCamel) + JavaSrcElm.VO_NAME_SUFFIX; return voName; } public static String getVoFileName(MapperProfile mapperProfile, InsertConfig insertConfig) { AllMapperProfile allMapperProfile = mapperProfile.getAllMapperProfile(); ComnArtifact comnArtifact = allMapperProfile.getComnArtifact(); // Get the target directory. String ideWorkSpace = comnArtifact.getIdeWorkspace(); String srcPathName = comnArtifact.getSrcPathName(); String transObjPrjName = comnArtifact.getTransObjPrj(); String voClassName = insertConfig.getClassName(); // Change package name to file name. String voFileName = JavaFormatter.classNameToFileName(voClassName); // Assemble the complete file name for Sql Map XML file. String fileFullName = ideWorkSpace + transObjPrjName + srcPathName + voFileName + JavaSrcElm.JAVA_FILE_EXT; return fileFullName; } public static String getVoPkgName(UpdateConfig updateConfig) { String className = updateConfig.getClassName(); String voPkgName = className; int idx = className.lastIndexOf(JavaSrcElm.DOT); if (idx != -1) { voPkgName = className.substring(0, idx); } return voPkgName; } public static String getVoFullName(UpdateConfig updateConfig, boolean isLargeCamel) { String className = updateConfig.getClassName(); return className; } public static String getVoSimpleName(UpdateConfig updateConfig, boolean isLargeCamel) { String tableName = updateConfig.getTableName(); // Get name of corresponding VO class. String voName = JavaFormatter.getJavaStyle(tableName, isLargeCamel) + JavaSrcElm.VO_NAME_SUFFIX; return voName; } public static String getVoFileName(MapperProfile mapperProfile, UpdateConfig updateConfig) { AllMapperProfile allMapperProfile = mapperProfile.getAllMapperProfile(); ComnArtifact comnArtifact = allMapperProfile.getComnArtifact(); // Get the target directory. String ideWorkSpace = comnArtifact.getIdeWorkspace(); String srcPathName = comnArtifact.getSrcPathName(); String transObjPrjName = comnArtifact.getTransObjPrj(); String voClassName = updateConfig.getClassName(); // Change package name to file name. String voFileName = JavaFormatter.classNameToFileName(voClassName); // Assemble the complete file name for Sql Map XML file. String fileFullName = ideWorkSpace + transObjPrjName + srcPathName + voFileName + JavaSrcElm.JAVA_FILE_EXT; return fileFullName; } // public static String getClassConstFullName(AllMapperProfile allMapperProfile) { // // ComnArtifact comnArtifact = allMapperProfile.getComnArtifact(); // String constPkg = comnArtifact.getConstPkg(); // String constClassName = comnArtifact.getClassConst(); // // return constPkg + JavaSrcElm.DOT + constClassName; // // } public static String getClassConstFileName(MapperProfile mapperProfile) { ComnArtifact comnArtifact = mapperProfile.getAllMapperProfile().getComnArtifact(); // Get the target directory and file name. String ideWorkSpace = comnArtifact.getIdeWorkspace(); String srcPathName = comnArtifact.getSrcPathName(); String comnSrcPrjName = comnArtifact.getComnSrcPrj(); String classConstName = comnArtifact.getClassConst(); String classConstFileName = JavaFormatter.classNameToFileName(classConstName); // Assemble the file name for AttrConst. String fileFullName = ideWorkSpace + comnSrcPrjName + srcPathName + classConstFileName + JavaSrcElm.JAVA_FILE_EXT; return fileFullName; } // public static String getAttrConstFullName(AllMapperProfile allMapperProfile) { // // ComnArtifact comnArtifact = allMapperProfile.getComnArtifact(); // String constPkg = comnArtifact.getConstPkg(); // String constClassName = comnArtifact.getAttrConst(); // // return constPkg + JavaSrcElm.DOT + constClassName; // // } public static String getAttrConstFileName(MapperProfile mapperProfile) { ComnArtifact comnArtifact = mapperProfile.getAllMapperProfile().getComnArtifact(); // Get the target directory and file name. String ideWorkSpace = comnArtifact.getIdeWorkspace(); String srcPathName = comnArtifact.getSrcPathName(); String comnSrcPrjName = comnArtifact.getComnSrcPrj(); String attrConstName = comnArtifact.getAttrConst(); String attrConstFileName = JavaFormatter.classNameToFileName(attrConstName); // Assemble the file name for AttrConst. String fileFullName = ideWorkSpace + comnSrcPrjName + srcPathName + attrConstFileName + JavaSrcElm.JAVA_FILE_EXT; return fileFullName; } public static String getMapperFileName(MapperProfile mapperProfile, MapperArtifact mapperArtifact) { AllMapperProfile allMapperProfile = mapperProfile.getAllMapperProfile(); ComnArtifact comnArtifact = allMapperProfile.getComnArtifact(); // Get the target directory. String ideWorkSpace = comnArtifact.getIdeWorkspace(); String srcPathName = comnArtifact.getSrcPathName(); String ormPrjName = comnArtifact.getOrmPrj(); String mapperNs = mapperArtifact.getMapperNs(); // Change the ".dao." into ".map." String mapperLogicalName = StringHelper.replace(mapperNs, JavaSrcElm.DAO_PKG_NAME_PAT, JavaSrcElm.MAPPER_PKG_NAME_PAT); String mapperFileName = JavaFormatter.classNameToFileName(mapperLogicalName); // Assemble the file name for Sql Map XML file. String fileFullName = ideWorkSpace + ormPrjName + srcPathName + mapperFileName + MapperElm.MAPPER_FILENAME_SUFFIX; return fileFullName; } public static String tableNameToDtoIntfName(String tableName) { String className = JavaFormatter.getJavaStyle(tableName, true); String dtoIntfName = JavaSrcElm.INTF_PREFIX + className + JavaSrcElm.DAO_INTF_SUFFIX; return dtoIntfName; } public static String classNameToFileName(String className) { String fileName = StringHelper.replace(className, JavaSrcElm.DOT, JavaSrcElm.SLASH); return fileName; } public static String pkgNameToPathName(String pkgName) { String pathName = StringHelper.replace(pkgName, JavaSrcElm.DOT, JavaSrcElm.SLASH); if (pathName.endsWith(JavaSrcElm.SLASH) == false) { pathName = pathName + JavaSrcElm.SLASH; } return pathName; } public static String javaFileNameToClassName(String javaFileName) { int indexOfJava = javaFileName.indexOf(JavaSrcElm.JAVA_FILE_EXT); if (-1 != indexOfJava) { javaFileName = javaFileName.substring(0, indexOfJava); } return javaFileName; } public static String dtoClassNameToOprIntfName(String dtoClassName) { // Get DTO package name and DTO class name. String dtoPkgName = getClassPkgName(dtoClassName); String dtoSimpleName = getClassSimpleName(dtoClassName); // Change DTO package name into OPR package name. String oprPkgName = StringHelper.replace(dtoPkgName, JavaSrcElm.MODEL_DTOX_PKG_NAME_PAT, JavaSrcElm.OPR_BASE_PKG_NAME_PAT); // Change DTO class name into OPR interface name. String oprIntfName = dtoSimpleName; int idx = dtoSimpleName.lastIndexOf(JavaSrcElm.DTOX_NAME_SUFFIX); if (idx != -1) { oprIntfName = dtoSimpleName.substring(0, idx); } oprIntfName = JavaSrcElm.INTF_PREFIX + oprIntfName + JavaSrcElm.OPR_INTF_SUFFIX; oprIntfName = oprPkgName + JavaSrcElm.DOT + oprIntfName; return oprIntfName; } public static String dtoClassNameToOprImplName(String dtoClassName) { // Get DTO package name and DTO class name. String dtoPkgName = getClassPkgName(dtoClassName); String dtoSimpleName = getClassSimpleName(dtoClassName); // Change DTO package name into OPR package name. String oprPkgName = StringHelper.replace(dtoPkgName, JavaSrcElm.MODEL_DTOX_PKG_NAME_PAT, JavaSrcElm.OPR_BASE_PKG_NAME_PAT); // Change DTO class name into OPR interface name. String oprImplName = dtoSimpleName; int idx = dtoSimpleName.lastIndexOf(JavaSrcElm.DTOX_NAME_SUFFIX); if (idx != -1) { oprImplName = dtoSimpleName.substring(0, idx); } oprImplName = StringHelper.toUpperCase(oprImplName, 0); oprImplName = oprImplName + JavaSrcElm.OPR_IMPL_SUFFIX; oprImplName = oprPkgName + JavaSrcElm.DOT + oprImplName; return oprImplName; } public static String dtoClassNameToDaoIntfName(String dtoClassName) { // Get DTO package name and DTO class name. String dtoPkgName = getClassPkgName(dtoClassName); String dtoSimpleName = getClassSimpleName(dtoClassName); // Change DTO package name into DAO package name. String daoPkgName = StringHelper.replace(dtoPkgName, JavaSrcElm.MODEL_DTOX_PKG_NAME_PAT, JavaSrcElm.DAO_BASE_PKG_NAME_PAT); // Change DTO class name into DAO interface name. String daoIntfName = dtoSimpleName; int idx = dtoSimpleName.lastIndexOf(JavaSrcElm.DTOX_NAME_SUFFIX); if (idx != -1) { daoIntfName = dtoSimpleName.substring(0, idx); } daoIntfName = JavaSrcElm.INTF_PREFIX + daoIntfName + JavaSrcElm.DAO_INTF_SUFFIX; daoIntfName = daoPkgName + JavaSrcElm.DOT + daoIntfName; return daoIntfName; } public static String voClassNameToDaoIntfName(String voClassName) { // Get vo package name and vo class name. String voPkgName = getClassPkgName(voClassName); String voSimpleName = getClassSimpleName(voClassName); // Change vo package name into DAO package name. String daoPkgName = StringHelper.replace(voPkgName, JavaSrcElm.MODEL_VO_PKG_NAME_PAT, JavaSrcElm.DAO_BASE_PKG_NAME_PAT); // Change vo class name into DAO interface name. String daoIntfName = voSimpleName; int idx = voSimpleName.lastIndexOf(JavaSrcElm.VO_NAME_SUFFIX); if (idx != -1) { daoIntfName = voSimpleName.substring(0, idx); } daoIntfName = JavaSrcElm.INTF_PREFIX + daoIntfName + JavaSrcElm.DAO_INTF_SUFFIX; daoIntfName = daoPkgName + JavaSrcElm.DOT + daoIntfName; return daoIntfName; } public static String dtoxClassNameToVoClassName(String dtoxClassName) { // Get dtox package name and dtox class name. String dtoxPkgName = getClassPkgName(dtoxClassName); String dtoxSimpleName = getClassSimpleName(dtoxClassName); // Change dtox package name into vo package name. String voPkgName = StringHelper.replace(dtoxPkgName, JavaSrcElm.MODEL_DTOX_PKG_NAME_PAT, JavaSrcElm.MODEL_VO_PKG_NAME_PAT); // Change vo class name into DAO interface name. String voName = dtoxSimpleName; int idx = dtoxSimpleName.lastIndexOf(JavaSrcElm.DTOX_NAME_SUFFIX); if (idx != -1) { voName = voName.substring(0, idx); } voName = voName + JavaSrcElm.VO_NAME_SUFFIX; return voPkgName + JavaSrcElm.DOT + voName; } }