1 /******************************************************************************* 2 * Copyright (c) 2015 LegSem. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the GNU Lesser Public License v2.1 5 * which accompanies this distribution, and is available at 6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 7 * 8 * Contributors: 9 * LegSem - initial API and implementation 10 ******************************************************************************/ 11 package com.legstar.codegen; 12 13 /** 14 * Provides the generator with convenience methods. The class can be 15 * passed as an instance to the velocity engine and used by templates. 16 */ 17 public class CodeGenHelper { 18 19 /** 20 * Checks a string for emptiness. This is needed because velocity 21 * cannot check for nulls. 22 * @param str the string to check 23 * @return true if the string has a content (not empty) 24 */ 25 public boolean isEmpty(final String str) { 26 return (str == null || str.length() == 0); 27 } 28 29 /** 30 * Determines the package name of a fully qualified class name. 31 * @param qualClassName class name including package 32 * @param defaultPackageName a default to return if class is not qualified 33 * @return the package name or default if class not qualified 34 */ 35 public String getPackageName( 36 final String qualClassName, final String defaultPackageName) { 37 int idx = qualClassName.lastIndexOf('.'); 38 if (idx < 1) { 39 return defaultPackageName; 40 } 41 return qualClassName.substring(0, idx); 42 } 43 44 /** 45 * Get the simple class name from a fully qualified class name. 46 * @param qualClassName class name including package 47 * @return the last part of the fully qualified name 48 */ 49 public String getClassName(final String qualClassName) { 50 int idx = qualClassName.lastIndexOf('.'); 51 if (idx < 0) { 52 return qualClassName; 53 } 54 return qualClassName.substring(idx + 1, qualClassName.length()); 55 } 56 57 /** 58 * Get a fully qualified class name. 59 * @param packageName the package or null if none 60 * @param className the class name 61 * @return the class name prefixed with the package name unless there 62 * is no package in which case, the class name is returned. 63 */ 64 public String getQualClassName(final String packageName, final String className) { 65 if (packageName == null || packageName.length() == 0) { 66 return className; 67 } 68 return packageName + '.' + className; 69 } 70 }