⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ruletool.java

📁 用来为垂直搜索引擎抓取数据的采集系统
💻 JAVA
字号:
/*
 * *****************************************************
 * Copyright (c) 2005 IIM Lab. All  Rights Reserved.
 * Created by xuehao at 2005-10-12
 * Contact: zxuehao@mail.ustc.edu.cn
 * *****************************************************
 */

package org.indigo.util;

import java.awt.BorderLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.Vector;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
/**
 * 模板批处理修改工具。
 * @author wbz
 *
 */
public class RuleTool
{
    private JFrame itsFrame=null;
    private JFileChooser itsChooser=null;
    private JCheckBox itsNestCheck=null;
    private JTextField itsDirPath=null;
    private final String DEL_STRING="Delete";
    private final String APP_STRING="Append";
    private final String MOD_STRING="Modify";
    private Vector itsFiles=null;
    private JTextField itsKeyField=null;
    private JTextField itsValField=null;
    private JTextArea itsArea=null;
    
    public RuleTool()
    {
        itsChooser = new JFileChooser();
        itsChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
        
//        JFrame frame = new JFrame( "规则修改工具" );
        JFrame frame = new JFrame( "Agent批量修改工具" );
        itsFrame = frame;
        JPanel panel = new JPanel();
        panel.setLayout( new BorderLayout() );
        
        JPanel centerPanel = new JPanel( );
        centerPanel.setLayout( new BorderLayout() );
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        
        final JRadioButton delBtn = new JRadioButton( DEL_STRING );
        final JRadioButton appBtn = new JRadioButton( APP_STRING );
        final JRadioButton modBtn = new JRadioButton( MOD_STRING );
        itsNestCheck = new JCheckBox( "Nested Task?" );
        appBtn.setSelected( true );
        

        ButtonGroup bg = new ButtonGroup();
        bg.add( delBtn );
        bg.add( appBtn );
        bg.add( modBtn );
        p1.add( delBtn );
        p1.add( appBtn );
        p1.add( modBtn );
        p1.add( itsNestCheck );

        itsKeyField = new JTextField( "LastItem", 20 );
        p2.add( new JLabel("Key:") );
        p2.add( itsKeyField );
        
        itsValField = new JTextField( "", 20 );
        p2.add( new JLabel("Value:") );
        p2.add( itsValField );
        
        centerPanel.add( p1, BorderLayout.NORTH );
        centerPanel.add( p2, BorderLayout.CENTER );
        
        JPanel southPanel = new JPanel();
        southPanel.setLayout( new BorderLayout() );
        JPanel ps1 = new JPanel();
        JPanel ps2 = new JPanel();
        JPanel ps21 = new JPanel();
        JPanel ps22 = new JPanel();
        ps2.setLayout( new BorderLayout() );
        
        itsDirPath = new JTextField( "", 40 );
        JButton dirBtn = new JButton( "open..." );
        dirBtn.addActionListener( new ActionListener()
                {
	            public void actionPerformed(ActionEvent ae)
	            {
	                int ret = itsChooser.showOpenDialog( itsFrame );
	                if( ret==JFileChooser.APPROVE_OPTION )
	                {
	                    String str = itsChooser.getSelectedFile().toString();
	                    itsDirPath.setText( str );
//	                    System.out.println( itsChooser.getSelectedFile() );
	                }
	            }
                });
        
        ps1.add( new JLabel("Directory:") );
        ps1.add( itsDirPath );
        ps1.add( dirBtn );
        
        JButton actBtn = new JButton( "Start" );
        JButton extBtn = new JButton( "Exit" );
        itsArea = new JTextArea( "", 20, 20 );
        actBtn.addActionListener( new ActionListener()
                {
            	public void actionPerformed( ActionEvent e )
            	{
            	    int iFlag=0;
            	    if( delBtn.isSelected() )
            	        iFlag = 0;
            	    else
            	    if( appBtn.isSelected() )
            	        iFlag = 1;
            	    else
            	    if( modBtn.isSelected() )
            	        iFlag = 2;

            	    operateDir( itsDirPath.getText(), iFlag, itsNestCheck.isSelected() );
            	}
                });
        extBtn.addActionListener( new ActionListener()
                {
            	public void actionPerformed( ActionEvent e )
            	{
            	    System.exit( 0 );
            	}
                });
        ps21.add( actBtn );
        ps21.add( extBtn );
        ps2.add( ps21, BorderLayout.NORTH );
        ps2.add( new JScrollPane(itsArea), BorderLayout.CENTER );
        
        southPanel.add( ps1, BorderLayout.NORTH );
        southPanel.add( ps2, BorderLayout.CENTER );
        
        panel.add( centerPanel, BorderLayout.CENTER );
        panel.add( southPanel, BorderLayout.SOUTH );
        
        frame.getContentPane().add( panel );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        
        frame.pack();
        frame.show();
        
    }
    private void getFiles( String fileName )
    {
        File source = new File( fileName );
        String []files = source.list();
        for( int i=0; i<files.length; i++ )
        {
            File file = new File( source.getPath(), files[i] );
            if( file.isDirectory() )
            {
                getFiles( file.toString() );
            }else
            {
                String str = file.getAbsolutePath();
                if( itsFiles==null )
                {
                    itsFiles = new Vector();
                }
                if( str.endsWith(".task") )
                    itsFiles.add( str );
//                System.out.println( str );
            }
        }
    }
    private void operateDir(String path, int flag, boolean b)
    {
        if( path==null || path.equals("") )
            return;
        getFiles( path );
        if( itsFiles==null )
        {
            System.out.println( "No task file in the directory, please try another directory." );
        }else
        {
            iFileCount = 0;
            itsArea.setText( "" );
            for( int i=0; i<itsFiles.size(); i++ )
            {
                String str = itsFiles.get( i ).toString();
//                System.out.println( str );
                doOperate( str, flag, b );
            }
        }
    }
    private static int iFileCount=0;
    private void doOperate( String file, int flag, boolean bNested )
    {
        
        String key,value;
        TaskProperties props = new TaskProperties();
        props.open( file );
        boolean bTheNestFlag=false;
        String str = props.getProperty( "Nested" );
        if( str==null )
            bTheNestFlag = false;
        else
        if( str.equalsIgnoreCase("true") )
            bTheNestFlag = true;
        else
        if( str.equals("") || str.equalsIgnoreCase("false") )
            bTheNestFlag = false;
        if( bNested!=bTheNestFlag )
            return;
        key = itsKeyField.getText();
        value = itsValField.getText();
        key = key.trim();
        value = value.trim();
        switch( flag )
        {
        //delete
        case 0:
//            System.out.println( "Delete key=" + key );
            props.deleteProperties( key );
            break;
        //append
        case 1:
//            System.out.println( "append " + key + "=" + value );
            props.setProperty( key, value );
            break;
        //modify
        case 2:
//            System.out.println( "modify " + key + "=" + value );
            props.changeProperty( key, value );
            break;
        }
       itsArea.append( ++iFileCount + ") " +file+"\n" );
    }
    public static void main(String[] args)
    {
        new RuleTool();
    }
  
}

⌨️ 快捷键说明

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