dynaformbean.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 632 行 · 第 1/2 页
JAVA
632 行
package Jt.wizard.struts;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import Jt.JtContext;
import Jt.JtFactory;
import Jt.JtMessage;
import Jt.JtObject;
import Jt.wizard.WizardConfig;
import Jt.xml.JDOMAdapter;
import org.apache.struts.action.ActionForm;
import org.jdom.Element;
/**
* Handles Dyna Form Bean Descriptors
*/
public class DynaFormBean extends JtObject {
private static final long serialVersionUID = 1L;
public static final String READ = "READ";
public static final String DELETE = "DELETE";
public static final String UPDATE = "UPDATE";
public static final String CREATE = "CREATE";
private JtFactory factory = new JtFactory ();
private WizardConfig config;
private String type;
private String name;
private String delete;
private String configFile;
public String getConfigFile() {
return configFile;
}
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
public String getDelete() {
return "delete";
}
public void setDelete(String delete) {
this.delete = this.delete; // null
}
public String getName() {
return (name);
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
private void handleUIError (String error) {
Collection col;
if (error == null)
return;
col = (Collection) this.getObjErrors();
if (col == null) {
col = new LinkedList ();
setObjErrors (col);
}
col.add(error);
}
private Object buildFormBean (DynaDefinitionForm form) {
JDOMAdapter adapter;
Element element, actionElem, propertyElement;
JtMessage msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);
JtFactory factory = new JtFactory ();
DynaMapping mappings[];
int i;
if (form == null) {
return (null);
}
/*
type = (String) form.getType ();
if (type == null || type.equals ("")) {
handleUIError ("Please enter a type.");
}
*/
name = (String) form.getName ();
if (name == null || name.equals ("")) {
handleUIError ("Please enter the name of the form bean.");
}
if (getObjErrors() != null) {
return (null);
}
adapter = (JDOMAdapter) factory.createObject (JDOMAdapter.JtCLASS_NAME);
element = new Element ("form-bean");
actionElem = element;
element.setAttribute("name", name);
element.setAttribute("type", "org.apache.struts.action.DynaActionForm");
msg.setMsgContent (element);
mappings = form.getMappings();
if (mappings != null) {
for (i = 0; i < mappings.length; i++) {
propertyElement = new Element ("form-property");
if (mappings[i] == null)
continue;
propertyElement.setAttribute("name", mappings[i].getName());
propertyElement.setAttribute("type", mappings[i].getType());
propertyElement.setAttribute("initial", mappings[i].getInitial());
element.addContent(propertyElement);
}
}
factory.sendMessage(adapter, msg);
return (actionElem);
}
private Object findFormBean (String name) {
JDOMAdapter adapter = new JDOMAdapter ();
JtMessage msg = new JtMessage (JDOMAdapter.GET_CHILDREN);
JtFactory factory = new JtFactory ();
Element elem;
List children;
int i;
String tmp;
if (name == null)
return null;
config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
if (config == null) {
handleError ("Unable to create configuration class.");
return (null);
}
factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));
adapter.setPath(config.getConfigPath());
msg.setMsgContent("struts-config/form-beans");
//adapter.setPath("c:/tmp/test.xml");
factory.sendMessage (adapter, new JtMessage (JDOMAdapter.READ_FILE));
children = (List) factory.sendMessage (adapter, msg);
if (children == null)
return (null);
for (i = 0; i < children.size(); i++) {
elem = (Element) children.get(i);
tmp = elem.getAttributeValue("name");
if (name.equals(tmp)) {
//elem.getParentElement();
//elem.detach ();
//factory.sendMessage (adapter, msg1);
return (elem);
}
}
return (null);
}
private Object deleteFormBean (DynaDefinitionForm form) {
JDOMAdapter adapter = new JDOMAdapter ();
JtMessage msg = new JtMessage (JDOMAdapter.GET_CHILDREN);
JtMessage msg1 = new JtMessage (JDOMAdapter.SAVE_FILE);
JtFactory factory = new JtFactory ();
Element elem;
List children;
int i;
String tmp;
String name;
if (form == null)
return null;
name = form.getName();
if (name == null)
return null;
config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
if (config == null) {
handleError ("Unable to create configuration class");
return (null);
}
factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));
adapter.setPath(config.getConfigPath());
msg.setMsgContent("struts-config/form-beans");
//adapter.setPath("c:/tmp/test.xml");
factory.sendMessage (adapter, new JtMessage (JDOMAdapter.READ_FILE));
if (adapter.getObjException() != null) {
this.setObjException(adapter.getObjException());
return (null);
}
children = (List) factory.sendMessage (adapter, msg);
if (children == null)
return (null);
for (i = 0; i < children.size(); i++) {
elem = (Element) children.get(i);
tmp = elem.getAttributeValue("name");
if (name.equals(tmp)) {
elem.getParentElement();
elem.detach ();
factory.sendMessage (adapter, msg1);
return (null);
}
}
return (null);
}
private Object createFormBean (DynaDefinitionForm form) {
JDOMAdapter adapter = new JDOMAdapter ();
JtMessage msg1 = new JtMessage (JDOMAdapter.SAVE_FILE);
JtMessage msg2 = new JtMessage (JDOMAdapter.ADD_ELEMENT);
JtFactory factory = new JtFactory ();
Element newElem;
String name;
if (form == null)
return null;
name = form.getName();
if (name == null)
return null;
if (findFormBean (name) != null) {
handleUIError ("Form bean already exists");
return null;
}
newElem = (Element) buildFormBean (form);
if (newElem == null) {
handleError ("Unable to create the Form Bean.");
return (null);
}
config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
if (config == null) {
handleError ("Unable to create configuration class");
return (null);
}
factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));
adapter.setPath(config.getConfigPath());
//adapter.setPath("c:/tmp/test.xml");
factory.sendMessage (adapter, new JtMessage (JDOMAdapter.READ_FILE));
if (adapter.getObjException() != null) {
this.setObjException(adapter.getObjException());
return (null);
}
msg2.setMsgData("struts-config/form-beans");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?