1 5 package xdoclet.modules.apache.struts; 6 7 import java.beans.Introspector ; 8 import java.util.Collection ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 13 import org.apache.commons.logging.Log; 14 import xjavadoc.XClass; 15 import xjavadoc.XMethod; 16 import xjavadoc.XTag; 17 18 import xdoclet.DocletContext; 19 import xdoclet.DocletTask; 20 import xdoclet.XDocletException; 21 import xdoclet.XDocletTagSupport; 22 import xdoclet.tagshandler.MethodTagsHandler; 23 import xdoclet.tagshandler.PackageTagsHandler; 24 import xdoclet.util.LogUtil; 25 26 32 public class ActionFormTagsHandler extends XDocletTagSupport 33 { 34 41 public static String getActionFormClassFor(XClass clazz) throws XDocletException 42 { 43 String packageName = clazz.getContainingPackage().getName(); 44 45 packageName = PackageTagsHandler.getPackageNameFor(packageName, true); 46 return packageName + '.' + getActionFormClassName(clazz); 47 } 48 49 56 public static String getActionFormClassName(XClass clazz) throws XDocletException 57 { 58 XTag currentTag = ((ActionFormSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(ActionFormSubTask.class))).getCurrentFormTag(); 59 60 String name = currentTag.getAttributeValue("name"); 62 63 if (name == null) { 64 return clazz.getTransformedName() + "Form"; 65 } 66 else { 67 if (Character.isLowerCase(name.charAt(0))) { 68 char chars[] = name.toCharArray(); 69 70 chars[0] = Character.toUpperCase(chars[0]); 71 return new String (chars); 72 } 73 else { 74 return name; 75 } 76 } 77 } 78 79 86 public static boolean hasFormDefinition(XClass clazz) throws XDocletException 87 { 88 return clazz.getDoc().hasTag("struts.form", false); 89 } 90 91 96 protected static String getActionFormClassPattern() 97 { 98 return ((ActionFormSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(ActionFormSubTask.class))).getActionFormClassPattern(); 99 } 100 101 108 public String actionFormClass() throws XDocletException 109 { 110 return getActionFormClassFor(getCurrentClass()); 111 } 112 113 118 public String actionFormName() throws XDocletException 119 { 120 XTag currentTag = ((ActionFormSubTask) getDocletContext().getSubTaskBy(DocletTask.getSubTaskName(ActionFormSubTask.class))).getCurrentFormTag(); 121 String formName = currentTag.getAttributeValue("name"); 122 123 if (formName == null || formName.trim().length() == 0) { 124 return Introspector.decapitalize(getCurrentClass().getTransformedName() + "Form"); 125 } 126 else { 127 return Introspector.decapitalize(formName); 128 } 129 } 130 131 138 public void forAllFormFields(String template) throws XDocletException 139 { 140 143 Log log = LogUtil.getLog(ActionFormTagsHandler.class, "forAllFormFields"); 144 XClass currentClass = getCurrentClass(); 145 Map foundFields = new HashMap (); 146 147 if (log.isDebugEnabled()) { 148 log.debug("BEGIN-----------------------------------------"); 149 } 150 151 do { 152 pushCurrentClass(currentClass); 153 154 if (log.isDebugEnabled()) { 155 log.debug("-----CLASS=" + getCurrentClass().getName() + "----------------"); 156 } 157 158 Collection methods = getCurrentClass().getMethods(); 159 160 for (Iterator j = methods.iterator(); j.hasNext(); ) { 161 setCurrentMethod((XMethod) j.next()); 162 if (MethodTagsHandler.isGetter(getCurrentMethod().getName()) && 164 !foundFields.containsKey(getCurrentMethod().getName()) && 165 useMethodInForm(getCurrentMethod())) { 166 if (useMethodInForm(getCurrentMethod())) { 167 if (log.isDebugEnabled()) { 168 log.debug("METHOD(I=" + getCurrentMethod().getName()); 169 } 170 foundFields.put(getCurrentMethod().getName(), getCurrentMethod().getName()); 172 173 generate(template); 174 } 175 } 176 } 177 178 if (getCurrentClass().getSuperclass().getQualifiedName().equals("java.lang.Object")) { 180 popCurrentClass(); 181 break; 182 } 183 184 popCurrentClass(); 185 currentClass = currentClass.getSuperclass(); 186 } while (true); 187 188 if (log.isDebugEnabled()) { 189 log.debug("END-------------------------------------------"); 190 } 191 } 192 193 200 public void ifUseMethodInForm(String template) throws XDocletException 201 { 202 if (useMethodInForm(getCurrentMethod())) 203 generate(template); 204 } 205 206 214 protected boolean useMethodInForm(XMethod method) throws XDocletException 215 { 216 XTag currentTag = ((ActionFormSubTask) getDocletContext().getSubTaskBy(DocletTask.getSubTaskName(ActionFormSubTask.class))).getCurrentFormTag(); 218 String value = currentTag.getAttributeValue("include-all"); 219 220 if (value != null && value.equals("true")) { 222 return true; 223 } 224 225 Collection mTags = method.getDoc().getTags("struts:form-field"); 227 228 String fname = currentTag.getAttributeValue("name"); 231 232 if (fname == null && !mTags.isEmpty()) { 233 return true; 234 } 235 else { 236 for (Iterator i = mTags.iterator(); i.hasNext(); ) { 237 XTag mTag = (XTag) i.next(); 238 String pname = mTag.getAttributeValue("form-name"); 239 240 if (pname != null && fname != null && fname.equals(pname)) { 241 return true; 242 } 243 } 244 } 245 246 return false; 248 } 249 } 250 | Popular Tags |