updatestrutsconfigfile.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 175 行

JAVA
175
字号
package Jt.wizard.struts;

import java.util.Collection;
import java.util.LinkedList;

import org.apache.struts.action.ActionForm;
import org.jdom.Element;

import Jt.JtContext;
import Jt.JtFactory;
import Jt.JtMessage;
import Jt.JtObject;
import Jt.xml.JDOMAdapter;

/**
 * Saves a file (JtWizard internal class).
 */

public class UpdateStrutsConfigFile extends JtObject {
    private static final long serialVersionUID = 1L;
    String path;
    String content;


    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
    
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    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 void updateConfigFile () {
        JDOMAdapter adapter = new JDOMAdapter ();
        JDOMAdapter adapter1 = new JDOMAdapter ();
        JtMessage msg = new JtMessage (JDOMAdapter.READ_FILE);
        JtMessage msg1 = new JtMessage (JDOMAdapter.STRING_TO_XML);       
        JtFactory factory = new JtFactory ();
        Element elem;
        
        adapter.setPath(path);
        
        if (content == null)
            return;
        
        factory.sendMessage (adapter, msg);
        
        msg1.setMsgContent(content);       
        factory.sendMessage (adapter1, msg1);
               
        msg1 = new JtMessage (JDOMAdapter.FIND_ELEMENT);
        msg1.setMsgContent("action");
        
        elem = (Element) factory.sendMessage (adapter1, msg1);
        
        if (elem == null) {
            handleError ("Invalid Action format. Element not found (action).");
            return;
        }        
        
        msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);
        msg.setMsgContent((Element) elem.detach());
        msg.setMsgData("struts-config/action-mappings");
        
        factory.sendMessage (adapter, msg);
                
        msg = new JtMessage (JDOMAdapter.SAVE_FILE);
        factory.sendMessage (adapter, msg);
        
        if (adapter.getObjException() != null)
            this.setObjException(adapter.getObjException());
       
        handleTrace ("updated:" + adapter.getContent());
    }

    private Object updateFile (SaveFileForm form) {

        //JtFactory factory = new JtFactory ();
        //String fileContent;
        //JtFile file = new JtFile ();
        //JtMessage msg = new JtMessage (JtFile.JtSAVE_STRING);

        if (form == null) {
            return (null);            
        }

        path = (String) form.getPath();

        if (path == null || path.equals ("")) {
            handleUIError ("Please enter a path.");
        }    

        content = (String) form.getContent ();      
        if (content == null || content.equals("")) {
            handleUIError ("Please enter the file content.");
        }    

        
        if (getObjErrors() != null) {
            return (null);
        }

        //msg.setMsgContent(fileContent);
        //file.setName (path);
        //factory.sendMessage(file, msg);
        
        // Propagate the exception
        //if (file.getObjException() != null)
        //    this.setObjException(file.getObjException());
        
        updateConfigFile ();
        
        return (null);
    }

    /**
     * Process object messages.
     * <ul>
     * <li> JtACTIVATE
     * </ul>
     * @param message message
     */

    public Object processMessage (Object message) {
        //Object data;
        
        JtContext context;
        ActionForm form = null;

        JtMessage e = (JtMessage) message;

        if (e == null ||  (e.getMsgId() == null))
            return (null);
        
        context = (JtContext) e.getMsgContext();
        if (context != null)
            form = (ActionForm) context.getActionForm();  


        if (e.getMsgId().equals(JtObject.JtACTIVATE)) {

            // pass the form information   
            //data = e.getMsgData ();
            return ( updateFile ((SaveFileForm) form));
        }

        return (super.processMessage(message));

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?