jtscriptinterpreter.java

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

JAVA
369
字号


package Jt.xml;

import Jt.*;


/**
  * Jt Script interpreter 
  */


public class JtScriptInterpreter extends JtInterpreter  {

  private static final long serialVersionUID = 1L;
  public static final String JtCLASS_NAME = JtScriptInterpreter.class.getName(); 
  public static final String JtSCRIPT = "JtSCRIPT"; 
  public static final String JtPARSE = "JtPARSE"; 
  private String filename;
  private String content;
  private boolean remoteInvocation = false;
  private transient JtXMLHelper xmlHelper = null;
  private transient Object scriptOutput = null; // script output
  private transient JtFactory factory = new JtFactory (); 

  

  public JtScriptInterpreter () {
  }


 /**
  * Specifies the script filename.
  * @param filename script filename
  */

  public void setFilename (String filename) {
     this.filename = filename; 
  }


 /**
   * Returns the script filename. 
   */

  public String getFilename () {
     return (filename);
  }

 /**
  * Specifies the script content.
  * @param content script content
  */

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


 /**
   * Returns the script content. 
   */
  public String getContent () {
     return (content);
  }




  public void setRemoteInvocation (boolean remoteInvocation) {
     this.remoteInvocation = remoteInvocation; 
  }


  public boolean getRemoteInvocation () {
     return (remoteInvocation);
  }

  // propagateException: 


  private Exception propagateException (Object obj)
  {
      Exception ex;

      if (obj == null)
          return null;

      ex = (Exception) factory.getValue (obj, "objException");


      if (ex != null)
          //factory.setValue (this, "objException", ex);
          this.setObjException(ex);

      return (ex);
  }
 

   // Interpret Jt Message: update scriptOutput for JtCREATE_OBJECT, JtSET_VALUE
   //                       and JtSEND_MESSAGE
 
   private Object interpretMsg (Object event) {

     String msgid;
     JtMessage msg = (JtMessage) event;
     Object result = null;
     String ctype;

     if (msg == null)
       return (null);

     msgid = (String) msg.getMsgId ();

     if (msgid == null)
       return (null);

     factory.setObjException(null);
     
     if (msgid.equals (JtObject.JtSET_VALUE)) {

       /*  
       if ("this".equals (msg.getMsgSubject ()))
         setValue (this,
                 msg.getMsgContent (),
                 msg.getMsgData());
       else
       */
       factory.setValue (msg.getMsgSubject (),
                 msg.getMsgContent (),
                 msg.getMsgData());

       propagateException (factory);
       return (null);
     }

     if (msgid.equals (JtObject.JtCREATE_OBJECT)) {
       //System.out.println ("JtScriptIterpreter(JtCREATE_OBJECT):" + msg.getMsgContent()
       //    + "," + msg.getMsgData());
 
       ctype = (String) msg.getMsgContent();

       if (remoteInvocation) {
         if (JtOSCommand.JtCLASS_NAME.equals (ctype) || JtFile.JtCLASS_NAME.equals (ctype) ||
                 JtDirectory.JtCLASS_NAME.equals (ctype)) {
           factory.handleError ("Security violation: unable to create a remote instance of " +
             ctype); 
           propagateException (factory);
           return (null);
         }

       }                                  
       result = factory.createObject (msg.getMsgContent (),
                 msg.getMsgData());

       if (remoteInvocation) {

           if (result == null)
               scriptOutput = null;               
           else
               scriptOutput = result.toString(); // Object ID is returned
           
           handleTrace ("JtScriptInterpreter.interpretMsg(JtCREATE_OBJECT):proxy:" + scriptOutput, 
                   JtObject.JtMIN_LOG_LEVEL);
           
           propagateException (factory);

           return (scriptOutput);
           //return (null);
       }  

       propagateException (factory);
       
       scriptOutput = result;
       return (result);
     }

     if (msgid.equals (JtObject.JtGET_VALUE)) {


       result = factory.getValue (msg.getMsgContent (),
                 msg.getMsgData());

       propagateException (factory);
       scriptOutput = result;   
       handleTrace ("JtScriptInterpreter.interpretMsg(JtGET_VALUE):" + scriptOutput, 
               JtObject.JtMIN_LOG_LEVEL);
       return (result);
     }

     if (msgid.equals (JtObject.JtREMOVE_OBJECT)) {
         
       factory.removeObject (msg.getMsgContent ());
       propagateException (factory);
       return (null);
     }


     if (msgid.equals (JtObject.JtSEND_MESSAGE)) {

       result = factory.sendMessage (msg.getMsgContent (),
                    msg.getMsgData ());

       propagateException (msg.getMsgContent ());

       scriptOutput = result;
       return (result);

     }

     if (msgid.equals (JtObject.JtREMOVE)) {
       return (null);
     }

     handleError ("JtScriptInterpreter.interpretMessage: invalid msg ID:"
     + msg.getMsgId ());
     return (null);
   }


  private Object parse () {
    

    JtMessage msg;
    JtList col;
    JtMessage msg1;
    JtIterator iterator;
    String str;
    JtFile file;
    //Object result = null;
    Exception ex;

    if (filename != null && content != null) {
      handleError ("JtScriptInterpreter.parse: set uri or string (only one)");
      return null;
    } 

    if (filename == null && content == null) {
      handleError ("JtScriptInterpreter.parse: both uri and string are null");
      return null;
    }    

    scriptOutput = null;

    if (filename != null) {

      file = new JtFile ();

      setValue (file, "name", filename);

      str = (String) sendMessage (file, new JtMessage (JtFile.JtCONVERT_TO_STRING));
    

    } else
      str = content;


    if (str == null)
      return (null);

    if (xmlHelper == null)
      xmlHelper = new JtXMLHelper ();  
      //xmlHelper = (JtXMLHelper) createObject (JtXMLHelper.JtCLASS_NAME, "xmlHelper");


    msg = new JtMessage (JtObject.JtXML_DECODE);

    msg.setMsgContent (str);

    col = (JtList) sendMessage (xmlHelper, msg);

    //destroyObject ("xmlHelper");

    if (col == null)
      return (null);

    msg = new JtMessage (JtIterator.JtNEXT);

    //iterator = (JtIterator) getValue (col, "iterator");
    iterator = (JtIterator) col.getIterator();

    if (iterator == null)
      return (null);

    for (;;) {
      msg1 = (JtMessage) iterator.processMessage (new JtMessage (JtIterator.JtNEXT));

      if (msg1 == null)
        break;    

      interpretMsg (msg1);  

      //ex = (Exception) 
      // getValue (this, "objException");
      ex = (Exception) this.getObjException();

      if (ex != null)
        break;   // stop processing the script
                 // if an exception is detected   
    }

    return (scriptOutput);
  }


  // Process object messages

  public Object processMessage (Object event) {

   String msgid = null;
   JtMessage e = (JtMessage) event;
   //Object content;

     if (e == null)
	return null;

     msgid = (String) e.getMsgId ();

     if (msgid == null)
	return null;

//   content = e.getMsgContent();

     if (msgid.equals (JtScriptInterpreter.JtPARSE) || msgid.equals (JtInterpreter.JtINTERPRET)) {
        return (parse ());
     }

     if (msgid.equals (JtObject.JtREMOVE)) {
         return (null);
     }
     
     return (super.processMessage(event));
     //handleError ("JtScriptInterpreter.processMessage: invalid message id:" + msgid);
     //return (null);

  }



 /**
   * Demonstrates all the messages processed by JtScriptInterpreter. 
   */

  public static void main(String[] args) {

    JtObject factory = new JtFactory ();


    if (args.length < 1) {
      System.err.println ("Usage: java JtScriptInterpreter uri");
      System.exit (1);
    }

    // Create message reader

    factory.createObject (JtScriptInterpreter.JtCLASS_NAME, "reader");

    factory.setValue ("reader", "filename", args[0]);


    factory.sendMessage ("reader", new JtMessage (JtInterpreter.JtINTERPRET));


  }

}


⌨️ 快捷键说明

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