ivaneeo's blog

自由的力量,自由的生活。

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

Scala代码
  1. package org.apache.pivot.scala.log  
  2.   
  3. import scala.reflect.BeanProperty  
  4. import io.Source  
  5. import org.apache.pivot.wtk.content.ListViewItemRenderer  
  6. import java.lang.String  
  7. import org.apache.pivot.wtkx.{WTKX, WTKXSerializer}  
  8.   
  9. /*为了避免和scala.Application的名称冲突,这里修改了别名*/  
  10. import org.apache.pivot.wtk.{ Application => PivotApplication, _}  
  11. import org.apache.pivot.collections.{ArrayList, Map}  
  12.   
  13. /**  
  14.  * Created by IntelliJ IDEA.  
  15.  * User: Administrator  
  16.  * Date: 2010-8-26  
  17.  * Time: 10:36:45  
  18.  * To change this template use File | Settings | File Templates.  
  19.  */  
  20.   
  21. /*日志记录Bean对象,由于Scala标准生成的字段不使用getter/setter的格式,  
  22.     但是提供了注解 @scala.reflect.BeanProperty ,使得编译器可以生成标准的JavaBean对象的getter/setter接口   
  23.     val 只生成了getter  
  24.     var 同时生成了getter和setter */  
  25. class LogRecord ( @BeanProperty val threadName : String,  
  26.                   @BeanProperty val date : String,  
  27.                   @BeanProperty val time : String,  
  28.                   @BeanProperty val module : String,  
  29.                   @BeanProperty val level : String,  
  30.                   @BeanProperty val content : String){  
  31.   
  32.     /*  
  33.         重载了 Any的 toString接口  
  34.         override关键字是必须的,在java中使用的是注解 @override,但是java中@override并不是必须的。  
  35.         省略了 函数还回值,由编译器进行类型推演得到  
  36.     */  
  37.   override def toString()  = {  
  38.     threadName +" "+date +" "+ time +" "+module +" "+level+" "+content  
  39.   }  
  40.   
  41. }  
  42.   
  43. /*  
  44.     LogRecord 类的半生对象  
  45.     定义了 scala中的魔术接口 apply ,使得可以使用  LogRecord("sting 文本") 可以创建一个class LogRecord对象。  
  46.     apply方法的调用由编译器自动调用,我们只负责定义即可  
  47.     我们要解析的日志格式  
  48.       
  49. threadName date    time     module       Level   content  
  50. DVOSMAIN 08/26/10 10:17:17 LOGINFO     : INFO  - Debug level: 2  
  51. DVOSMAIN 08/26/10 10:17:17 LOGINFO     : INFO  - *=ERROR WARNING EXCEPT  
  52. DVOSMAIN 08/26/10 10:17:17 LOGINFO     : INFO  - LM=*  
  53.   
  54. */  
  55. object LogRecord {  
  56.   def apply( line : String  ) : LogRecord = {  
  57.   
  58.         /*生成一个 Regex对象,用于模式匹配*/  
  59.     val logRegex = """([A-Za-z0-9]+) +([0-9/]*) +([0-9:]*) +([A-Z]*) +: *([A-Z_]+).*""".r  
  60.   
  61.     line match {  
  62.         /*如果模式匹配成功,threadName,date,time,module,level 分配按次序绑定到模式匹配表达式中()的内容 */  
  63.       case logRegex(threadName,date,time,module,level) =>  
  64.           val logRecord: LogRecord = new LogRecord( threadName, date, time, module, level,line)  
  65.             logRecord  
  66.             /*模式匹配通配符,在没有匹配到的情况下,做通用处理。如果不添加,在匹配不到时会抛出 MatchError异常*/  
  67.       case _ =>  
  68.           val logRecord: LogRecord = new LogRecord("N/A","N/A","N/A","N/A","N/A","N/A")  
  69.             logRecord  
  70.     }  
  71.   
  72.   }  
  73. }  
  74.   
  75. /*  
  76.     Apache Pivot ListView ItemRenderer   
  77.     重新定义了如何显示 LogRecord对象的 列表项目的渲染。  
  78.     如果使用默认的,那么ListView显示对象时,直接调用对象的toString获得其字符串表示  
  79. */  
  80. class LogListViewItemRenderer extends ListViewItemRenderer {  
  81.   
  82.   imageView.setVisible(false)  
  83.   
  84.   override def render(item: AnyRef, index: Int, listView: ListView, selected: Boolean, checked: Boolean, highlighted: Boolean, disabled: Boolean) = {  
  85.       if ( item != null && item.isInstanceOf[LogRecord])  
  86.         {  
  87.           val log = item.asInstanceOf[LogRecord]  
  88.           label.setText(log.content)  
  89.   
  90.        
  91.         }  
  92.   }  
  93.   
  94.   
  95.     
  96. }  
  97.   
  98. /**  
  99.     定义主窗口界面代码,必须继承自 org.apache.pivot.Application  
  100.     使用了 @WTKX注解,该注解属于 wtkx的命名对象的绑定语法,在从wtkx文件加载GUI时,  
  101.     调用bind接口可以自动和wtkx文件中声明的wtkx:id对象进行绑定,无需在掉吗中调用 get("name")  
  102. */  
  103. class MainWindow extends PivotApplication {  
  104.   var window : Window   = null  
  105.   @WTKX var textInputFilePath : TextInput  = null  
  106.   @WTKX var browsePushButton : PushButton  = null  
  107.   @WTKX var loadPushButton : PushButton   = null  
  108.   @WTKX var textInputThreadName :TextInput   = null  
  109.   @WTKX var textInputModule : TextInput    = null  
  110.   @WTKX var textInputLevel : TextInput    = null  
  111.   @WTKX var textInputContent : TextInput   = null  
  112.   @WTKX var logListView : ListView = null  
  113.   
  114.   
  115.   def resume = {}  
  116.   
  117.   def suspend = {}  
  118.   
  119.   def shutdown(optional: Boolean) = {  
  120.     if ( window != null)  
  121.       {  
  122.         window.close  
  123.         true  
  124.       }  
  125.     false  
  126.   }  
  127.   
  128.   def startup(display: Display, properties: Map[String, String]) = {  
  129.     val wtkxSerializer = new WTKXSerializer()  
  130.     var matchString : String = null  
  131.   
  132.         /*从xml(wtkx)文件加载GUI*/  
  133.     window = wtkxSerializer.readObject(this,"MainWindow.xml").asInstanceOf[Window]  
  134.   
  135.     wtkxSerializer.bind(this)  
  136.         if ( properties containsKey "logfile")  
  137.         {  
  138.                 textInputFilePath setText ( properties get "logfile")  
  139.         }  
  140.           
  141.         /*给 Button添加事件处理函数*/  
  142.     browsePushButton.getButtonPressListeners.add( function2Listener (browseButtonPressed ) )  
  143.     loadPushButton.getButtonPressListeners.add( function2Listener(loadButtonPressed ))  
  144.       
  145.       
  146.     window.open(display)  
  147.   
  148.   }  
  149.   
  150.     /*浏览按钮事件处理,打开一个文件浏览窗口,让用户选择文件,并在用户关闭对话框时,捕获用户选择的文件名*/  
  151.   def browseButtonPressed( button : Button ) : Unit = {  
  152.      val dialog : FileBrowserSheet = new   FileBrowserSheet(FileBrowserSheet.Mode.OPEN)  
  153.   
  154.     dialog.open( window, new SheetCloseListener() {  
  155.       def sheetClosed(sheet: Sheet) = {  
  156.         if ( sheet.getResult)  
  157.           {  
  158.              val fileBrowseSheet = sheet.asInstanceOf[FileBrowserSheet]  
  159.              textInputFilePath.setText( fileBrowseSheet.getSelectedFile.getPath.toString)  
  160.           }  
  161.       }  
  162.     })  
  163.   }  
  164.     
  165.   /*从log文件加载内容,每一行是一个日志记录  
  166.     for中使用了 if过滤器,只有条件符合了,才会进入for循环体。  
  167.       
  168.     scala没有提供continu,而是在 for中提供了条件过滤来替代  
  169.   */  
  170.   def loadButtonPressed( button : Button ) : Unit = {  
  171.      val logFile = Source.fromFile(textInputFilePath.getText)  
  172.      val list = new ArrayList[LogRecord]  
  173.     for ( line <- logFile.getLines ; logRecord = LogRecord(line.trim);  
  174.           if ( textInputThreadName.getText == "" || textInputThreadName.getText.contains(logRecord.threadName) );  
  175.           if ( textInputModule.getText == "" || textInputModule.getText.contains(logRecord.module));  
  176.           if ( textInputLevel.getText == "" || textInputLevel.getText.contains(logRecord.level))){  
  177.         
  178.       list add logRecord  
  179.     }  
  180.   
  181.     logListView.setListData( list)  
  182.       
  183.   }  
  184.     /*按钮事件辅助接口,用于把一个 事件处理函数转换为ButtonPressListener对象,也可以采取更高级的内容,使用implicit,这里没有使用*/  
  185.    def function2Listener( fun : Button => Unit ) :ButtonPressListener =  {  
  186.     val listener = new ButtonPressListener()  
  187.     {  
  188.       def buttonPressed(button: Button) = {  
  189.         fun(button)  
  190.       }  
  191.     }  
  192.   
  193.     listener  
  194.   }  
  195. }  
  196.   
  197. /*  
  198.     主函数  
  199.     符合Pivot主函数入口规则  
  200. */  
  201. object LogAnalyse {  
  202.   def main ( args : Array[String]) : Unit = {  
  203.   
  204.       
  205.     DesktopApplicationContext.main( classOf[MainWindow], args)  
  206.      
  207.   
  208.   }  
  209. }  

 run:

使用 java时:

java  -classpath scala-library.jar;pivot-core-1.5.jar;pivot-tools-1.5.jar;pivot-wtk-1.5.jar;pivot-wtk-terra-1.5.jar;. org.apache.pivot.scala.log.LogAnalyse

 

使用 scala时

java  -classpath pivot-core-1.5.jar;pivot-tools-1.5.jar;pivot-wtk-1.5.jar;pivot-wtk-terra-1.5.jar;. org.apache.pivot.scala.log.LogAnalyse

posted on 2010-12-23 20:37 ivaneeo 阅读(620) 评论(0)  编辑  收藏 所属分类: java魔力scala-fun!

只有注册用户登录后才能发表评论。


网站导航: