1 5 package xdoclet.modules.apache.struts; 6 7 import java.beans.Introspector ; 8 import java.text.MessageFormat ; 9 import java.util.Collection ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 14 import org.apache.commons.logging.Log; 15 import xjavadoc.XClass; 16 import xjavadoc.XMethod; 17 import xjavadoc.XTag; 18 import xdoclet.DocletContext; 19 import xdoclet.DocletTask; 20 import xdoclet.XDocletException; 21 import xdoclet.modules.apache.struts.ejb.StrutsFormSubTask; 22 import xdoclet.modules.ejb.EjbTagsHandler; 23 import xdoclet.modules.ejb.entity.PersistentTagsHandler; 24 import xdoclet.tagshandler.MethodTagsHandler; 25 import xdoclet.util.LogUtil; 26 27 33 public class StrutsFormTagsHandler extends EjbTagsHandler 34 { 35 42 public static String getStrutsFormClassFor(XClass clazz) throws XDocletException 43 { 44 String packageName = clazz.getContainingPackage().getName(); 45 46 packageName = choosePackage(packageName, null, DocletTask.getSubTaskName(StrutsFormSubTask.class)); 47 return packageName + '.' + getStrutsFormClassName(clazz); 48 } 49 50 57 public static String getStrutsFormClassName(XClass clazz) throws XDocletException 58 { 59 XTag currentTag = ((StrutsFormSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(StrutsFormSubTask.class))).getCurrentFormTag(); 60 61 String name = currentTag.getAttributeValue("name"); 63 64 if (name == null) { 65 return getShortEjbNameFor(clazz) + "Form"; 66 } 67 else { 68 return MessageFormat.format(getStrutsFormClassPattern(), new Object []{getShortEjbNameFor(clazz), name}); 69 } 70 } 71 72 79 public static boolean hasFormDefinition(XClass clazz) throws XDocletException 80 { 81 return clazz.getDoc().hasTag("struts:form", false); 82 } 83 84 89 protected static String getStrutsFormClassPattern() 90 { 91 return ((StrutsFormSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(StrutsFormSubTask.class))).getStrutsFormClassPattern(); 92 } 93 94 101 public String strutsFormClass() throws XDocletException 102 { 103 return getStrutsFormClassFor(getCurrentClass()); 104 } 105 106 111 public String strutsFormName() throws XDocletException 112 { 113 XTag currentTag = ((StrutsFormSubTask) getDocletContext().getSubTaskBy(DocletTask.getSubTaskName(StrutsFormSubTask.class))).getCurrentFormTag(); 114 String formName = currentTag.getAttributeValue("name"); 115 116 if (formName == null || formName.trim().length() == 0) { 117 return Introspector.decapitalize(getEjbNameFor(getCurrentClass()) + "Form"); 118 } 119 else { 120 return Introspector.decapitalize(getEjbNameFor(getCurrentClass()) + '.' + formName); 121 } 122 } 123 124 131 public void forAllFormFields(String template) throws XDocletException 132 { 133 137 Log log = LogUtil.getLog(StrutsFormTagsHandler.class, "forAllFormFields"); 138 XClass currentClass = getCurrentClass(); 139 Map foundFields = new HashMap (); 140 141 if (log.isDebugEnabled()) { 142 log.debug("BEGIN-----------------------------------------"); 143 } 144 145 do { 146 pushCurrentClass(currentClass); 147 148 if (log.isDebugEnabled()) { 149 log.debug("-----CLASS=" + getCurrentClass().getName() + "----------------"); 150 } 151 152 Collection methods = getCurrentClass().getMethods(); 153 154 for (Iterator j = methods.iterator(); j.hasNext(); ) { 155 setCurrentMethod((XMethod)j.next()); 156 if (MethodTagsHandler.isGetter(getCurrentMethod().getName()) && 158 !foundFields.containsKey(getCurrentMethod().getName()) && 159 useMethodInForm(getCurrentMethod())) { 160 if (useMethodInForm(getCurrentMethod())) { 161 if (log.isDebugEnabled()) { 162 log.debug("METHOD(I=" + getCurrentMethod().getName()); 163 } 164 foundFields.put(getCurrentMethod().getName(), getCurrentMethod().getName()); 166 167 generate(template); 168 } 169 } 170 } 171 172 if (getCurrentClass().getSuperclass().getQualifiedName().equals("java.lang.Object")) { 174 popCurrentClass(); 175 break; 176 } 177 178 popCurrentClass(); 179 currentClass = currentClass.getSuperclass(); 180 } while (true); 181 182 if (log.isDebugEnabled()) { 183 log.debug("END-------------------------------------------"); 184 } 185 } 186 187 194 public void ifUseMethodInForm(String template) throws XDocletException 195 { 196 if (useMethodInForm(getCurrentMethod())) 197 generate(template); 198 } 199 200 208 protected boolean useMethodInForm(XMethod method) throws XDocletException 209 { 210 XTag currentTag = ((StrutsFormSubTask) getDocletContext().getSubTaskBy(DocletTask.getSubTaskName(StrutsFormSubTask.class))).getCurrentFormTag(); 212 String value = currentTag.getAttributeValue("include-all"); 213 214 if (value != null && value.equals("true")) { 216 return true; 217 } 218 219 value = currentTag.getAttributeValue("include-pk"); 221 if (PersistentTagsHandler.isPkField(method) && (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 |