formgenerator.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 1,156 行 · 第 1/3 页
JAVA
1,156 行
oBuffer.append("import javax.servlet.http.HttpServletRequest;\n");
oBuffer.append("import org.apache.struts.action.*;\n\n");
oBuffer.append("public class " + name + " extends ActionForm {\n\n");
//generateInputFields (baseClassName);
ngenerateInputFields (inputStream);
oBuffer.append("}");
return (oBuffer.toString());
}
private String generateActionForm (ActionForm4 form) {
//InputStream inputStream = null;
//FormFile theFile;
String formName;
FormBean formBean = new FormBean ();
FormBeanForm tmp;
JtMessage msg = new JtMessage (FormBean.READ);
String className;
String name;
if (form == null)
return null;
//className = (String) form.getClassname();
formName = (String) form.getFormName();
msg.setMsgContent(formName);
if (formName == null || formName.equals ("")) {
handleUIError ("Please enter a form name.");
}
tmp = (FormBeanForm) factory.sendMessage(formBean, msg);
if (tmp == null) {
handleUIError ("ActionForm not found:" + formName);
}
if (getObjErrors() != null) {
return (null);
}
className = tmp.getType();
pack = retrievePackageName (className);
if (pack != null && !pack.equals(""))
oBuffer.append("package " + pack +";\n");
name = retrieveName (className);
if (name == null) {
handleError ("Invalid class name");
return null;
}
oBuffer.append("import javax.servlet.http.HttpServletRequest;\n");
if ("ActionForm".equals(form.getSubclassOf())) {
oBuffer.append("import org.apache.struts.action.*;\n\n");
oBuffer.append("public class " + name + " extends ActionForm {\n\n");
} else {
oBuffer.append("import org.apache.struts.validator.ValidatorForm;\n\n");
oBuffer.append("public class " + name + " extends ValidatorForm {\n\n");
}
generateBeanAttributes (tmp);
oBuffer.append("}");
return (oBuffer.toString());
}
private FormBeanForm generateDynaActionBean (ActionForm2 form) {
//Element element;
//JtMessage msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);
JtMessage msg1 = new JtMessage (JtHashTable.JtGET);
InputStream inputStream = null;
FormFile theFile;
FormBeanForm formBean = new FormBeanForm ();
JtHashTable attributes;
JtIterator iterator;
String attrName;
BeanProperty prop;
String type;
DynaMapping mappings[] = new DynaMapping[FormBeanForm.MAX_ENTRIES];
DynaMapping mapping;
int index = 0, i;
if (form == null)
return (null);
formName = form.getFormName();
if (formName == null || formName.equals ("")) {
handleUIError ("Please enter a form name.");
}
theFile = form.getTheFile();
if (theFile == null || !checkExtension (theFile.getFileName())) {
handleUIError ("File must be a java class file (.java)");
}
if (getObjErrors() != null) {
return (null);
}
try {
inputStream = theFile.getInputStream();
} catch (Exception e) {
handleException (e);
}
if (inputStream == null) {
handleUIError ("Invalid java class file"); // check
return null;
}
//adapter = (JDOMAdapter) factory.createObject (JDOMAdapter.JtCLASS_NAME);
//element = new Element ("form-bean");
//element.setAttribute("name", formName);
formBean.setName(formName);
formBean.setType("org.apache.struts.action.DynaActionForm");
attributes = retrieveAttributes (inputStream);
if (attributes == null || (attributes.getSize() < FormBeanForm.MAX_ENTRIES))
mappings = formBean.getMappings();
else {
mappings = new DynaMapping[attributes.getSize()];
for (i = 0; i < attributes.getSize(); i++) {
mappings[i] = new DynaMapping ();
}
formBean.setMappings(mappings);
}
if (attributes != null) {
iterator = (JtIterator) attributes.processMessage (new JtMessage (JtHashTable.JtGET_KEYS));
for (;;) {
attrName = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));
//mapping = new DynaMapping ();
mapping = mappings[index];
mapping.setName(attrName);
if (attrName == null)
break;
msg1.setMsgData (attrName);
prop = (BeanProperty) attributes.processMessage(msg1);
type = prop.getType();
if (!(prop.isPrimitive() ||
"java.lang.String".equals(type) || "String".equals(type) ||
"java.util.Date".equals(type) || "Date".equals(type)))
continue;
if ("boolean".equals(type))
mapping.setType ("java.lang.Boolean");
else
mapping.setType ("java.lang.String");
//mappings[index] = mapping;
index++;
}
}
if (index == 0 || attributes == null) {
mapping = mappings[0];
mapping.setName("");
//mappings[0] = mapping;
}
//formBean.setMappings(mappings);
return (formBean);
}
private String generateDynaForm (ActionForm2 form) {
//JtFactory factory = new JtFactory ();
Element element;
JtMessage msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);
InputStream inputStream = null;
FormFile theFile;
//DocType docType;
//Namespace nameSpace = Namespace.getNamespace("html", "http://www.freedom.org");
if (form == null)
return (null);
//formName = (String) form.get ("formName");
formName = form.getFormName();
if (formName == null || formName.equals ("")) {
handleUIError ("Please enter a form name.");
}
/*
baseClassName = (String) form.get ("baseClassName");
if (baseClassName == null || baseClassName.equals ("")) {
handleUIError ("Please enter a base class name.");
}
*/
theFile = form.getTheFile();
//handleTrace ("generateForm:" + theFile.getFileName());
if (theFile == null || !checkExtension (theFile.getFileName())) {
handleUIError ("File must be a java class file (.java)");
}
if (getObjErrors() != null) {
return (null);
}
try {
inputStream = theFile.getInputStream();
} catch (Exception e) {
handleException (e);
}
if (inputStream == null) {
return null;
}
/*
if (baseClassName == null) {
handleError ("The className attribute needs to be set.");
return null;
}
if (formName == null) {
handleError ("The formName attribute needs to be set.");
return null;
}
*/
adapter = (JDOMAdapter) factory.createObject (JDOMAdapter.JtCLASS_NAME);
element = new Element ("form-bean");
element.setAttribute("name", formName);
element.setAttribute("type", "org.apache.struts.action.DynaActionForm");
msg.setMsgContent (element);
factory.sendMessage(adapter, msg);
//generateDynaFields (baseClassName);
ngenerateDynaFields (inputStream);
//oBuffer.append("}");
return (adapter.getContent());
}
// Process object messages
public Object processMessage (Object message) {
String msgid = null;
JtMessage msg = (JtMessage) message;
Object data;
JtContext context;
ActionForm form = null;
if (msg == null)
return null;
msgid = (String) msg.getMsgId ();
if (msgid == null)
return null;
factory.setStopClass(JtObject.class);
context = (JtContext) msg.getMsgContext();
if (context != null)
form = (ActionForm) context.getActionForm();
// Generate descriptors
if (msgid.equals (JtObject.JtACTIVATE)) {
//data = msg.getMsgData ();
return ( generateForm ((ActionForm1) form));
}
if (msgid.equals (FormGenerator.GENERATE_FORM)) {
//data = msg.getMsgData ();
//return ( generateDynaForm ((ActionForm2) form));
return ( generateDynaActionBean ((ActionForm2) form));
}
if (msgid.equals (FormGenerator.GENERATE_ACTION_FORM)) {
//data = msg.getMsgData ();
//return ( generateDynaForm ((ActionForm2) form));
return ( generateActionForm ((ActionForm4) form));
}
// Let the superclass handle all other messages
return (super.processMessage (message));
}
public static void main(String[] args) {
JtFactory factory = new JtFactory (); // Jt Factory
String reply;
FormGenerator jspGenerator;
JtMessage msg;
//factory.setLogging(true);
if (args.length < 1) {
System.err.println ("Usage: java Jt.struts.FormGenerator classname");
return;
}
jspGenerator = (FormGenerator) factory.createObject (FormGenerator.JtCLASS_NAME);
jspGenerator.setClassName(args[0]);
jspGenerator.setDynaActionForm(true);
jspGenerator.setFormName ("JtForm");
msg = new JtMessage (JtObject.JtACTIVATE);
reply = (String) factory.sendMessage (jspGenerator, msg);
jspGenerator.processMessage(new JtMessage (JtObject.JtPRINT));
System.out.println (reply);
factory.removeObject (jspGenerator);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?