You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oro-user@jakarta.apache.org by ti...@spray.se on 2003/05/23 20:04:50 UTC

search JTextarea

Hi!

I have a little problem. I am writing a program wich fetch a file with
ftp and then read it in to a JTextarea. After that I want to be able to
search for words in the textarea, and if I find the word requested I want
the rows with searched word to be printed out in the textarea. If I compile
the code it compiles without any problem. But when I search for a word
I get a 

java.lang.ClassFormatError: com/oroinc/text/MatchActionProcessor (Local
variable name has bad constant pool index)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
        at Ftp_get5_new_metod_read.search_regex(Ftp_get5_new_metod_read.java:301)
        at Ftp_get5_new_metod_read.actionPerformed(Ftp_get5_new_metod_read.java:410)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
        at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
        at java.awt.Component.processMouseEvent(Component.java:5093)
        at java.awt.Component.processEvent(Component.java:4890)
        at java.awt.Container.processEvent(Container.java:1566)
        at java.awt.Component.dispatchEventImpl(Component.java:3598)
        at java.awt.Container.dispatchEventImpl(Container.java:1623)
        at java.awt.Component.dispatchEvent(Component.java:3439)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
        at java.awt.Container.dispatchEventImpl(Container.java:1609)
        at java.awt.Window.dispatchEventImpl(Window.java:1585)
        at java.awt.Component.dispatchEvent(Component.java:3439)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

Here is the code : 


import java.net.*;
import java.net.URL.*;
import java.net.URLConnection.*;
import java.io.*;
import java.lang.Object.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.enterprisedt.net.ftp.*;
import com.stevesoft.pat.apps.*;
import com.stevesoft.pat.*;
import org.apache.oro.io.RegexFilenameFilter;
import com.oroinc.text.regex.*;
import com.oroinc.text.*;

/**
 * @author  Martin H=E4ggstr=F6m 021117
 * revised 021121
   revised 021225
   revised 030502
   revised 030505
   revised 030513
   revised 030515
 * @version 1.0
 */

public class Ftp_get5_new_metod_read extends JFrame implements ActionListener
{

        /************** instancevariables **********************/

        // strings

        String[] dir_dir;
        String login_login;
        String host_host;
        String PWD;
        String password_password;
        char[] password_char;
        String regex1;
        String regex;
        String zero =3D new String("");
        String found_searched;
        String line_number_string;

        // iostreams

        InputStream file =3D null;

        // ftpclient

        FTPClient ftpclient;

        // int

        int columns_passwordfield =3D 10; // length of the textfield and
passwordfield
        int columns_adressfield =3D 30; // length of the adressfield
        int text_found_start;
        int text_found_end;
        int found_entrys =3D 0;
        int found_index;
        int zeroLength;
        int zeroLengthiszero =3D 0;
        int len;
        int line_number;

        // swingobjects

        //ImageIcon ii =3D new ImageIcon("kidmanpil.jpg");
        JFrame jf =3D new JFrame();
        JTextArea ta =3D new JTextArea(70, 90);
        //JLabel kula =3D new JLabel(ii);
        //JScrollPane kula_sp =3D new JScrollPane(kula);
        JScrollPane sp =3D new JScrollPane(ta);

        // panels

        JPanel jp =3D new JPanel();
        JPanel jp2 =3D new JPanel();
        JPanel kula_panel =3D new JPanel();

        // textobjects

        JPasswordField jpf =3D new JPasswordField("guest", columns_passwordfield);
        JTextField jt_login =3D new JTextField("anonymous", columns_passwordfield);
        JTextField jt_host =3D new JTextField("ftp.gnu.org",
columns_passwordfield);
        JTextField search_textfield =3D new JTextField("", columns_passwordfield);

        // labels

        JLabel login =3D new JLabel("login");
        JLabel password =3D new JLabel("password");
        JLabel host =3D new JLabel("host");
        JLabel search =3D new JLabel("search");

        // buttons

        JButton jb_clear =3D new JButton("clear");
        JButton login_ok =3D new JButton("ok");
        JButton search_ok =3D new JButton("ok");

        // regular expressions

        Perl5Matcher matcher;
        Perl5Compiler compiler;
        Perl5Pattern pattern =3D null;
        Perl5StreamInput input;
        MatchResult result;

        /**
         * Creates new Ftp_get5_new_metod_read
         */

        /************************* constructor *********************************/

        public Ftp_get5_new_metod_read() {

                setTitle("Ftpprogram, Martin H=E4ggstr=F6m 2002");
                ta.setEditable(false);
                ta.setMargin(new Insets(0, 10, 0, 10)); // g=F6r att texten
kommer ut en bit fr=E5n v=E4nster och h=F6ger sida
                //getContentPane().add(kula_panel, BorderLayout.SOUTH);
//testing
                getContentPane().add(sp, BorderLayout.CENTER);
                getContentPane().add(jp, BorderLayout.SOUTH);
                getContentPane().add(jp2, BorderLayout.NORTH);
                //kula_panel.add(kula);
                jp.add(search);
                jp.add(search_textfield);
                jp.add(search_ok);
                jp.add(jb_clear);
                jp2.add(login);
                jp2.add(jt_login);
                jp2.add(password);
                jp2.add(jpf);
                jp2.add(host);
                jp2.add(jt_host);
                jp2.add(login_ok);
                jb_clear.addActionListener(this);
                login_ok.addActionListener(this);
                search_ok.addActionListener(this);
                setVisible(true);
                setDefaultCloseOperation(EXIT_ON_CLOSE); // g=F6r att jag
kan st=E4nga med krysset i h=F6gra h=F6rnet
                setSize(700, 600);

        }

        /***************************** methods *************************************/


        // login and fetch the specified file

        public int login() {


                login_login =3D jt_login.getText();
                host_host =3D jt_host.getText();
                password_char =3D jpf.getPassword();
                password_password =3D new String(jpf.getPassword());

                try {

                        // I use trim() to remove leading and trailing
whitespace, important!!
                        ftpclient =3D new FTPClient(host_host.trim());
                        ftpclient.login(login_login.trim(), password_password.trim());
// login

                        PWD =3D ftpclient.pwd(); // path to working directory

                        // get the file

                        String ftp_in =3D new String("c:\\temp\\ftp_in.txt");
                        ftpclient.get(ftp_in, "pub/README");
                        //ftpclient.get(ftp_in, "/usr/local/lp/log/lptool.log");

                        // iostream

                        FileReader fr =3D new FileReader(ftp_in);
                        BufferedReader br =3D new BufferedReader(fr, 4096);

                        // read the file in the textarea and then close
the stream

                        ta.read(br, null);
                        br.close();

                        ftpclient.quit();

                } catch (IOException e) {

                        e.printStackTrace();
                        //ta.setText("hmmm, I have some communicationproblems");

                        JOptionPane.showMessageDialog(null, "hmmm,
I don't kow the host " + host_host, "information",
                                JOptionPane.INFORMATION_MESSAGE);

                }
                catch (FTPException msg) {

                        msg.printStackTrace();
                        //ta.setText("hmmm, I have some FTPcommunicationproblems");

                        JOptionPane.showMessageDialog(null, "hmmm,
login incorrect", "information", JOptionPane.INFORMATION_MESSAGE);


                }

                // return 0 if everything is ok

                return 0;

        }


        // to clear the textarea

        public int clear_text() {

                ta.setText("");

                return 0;
        }

        // go to line

        public int gotoLine() {

                // count rows and get and string get the string representation
of the number of lines
                line_number =3D ta.getRows();
                String line_number_string1 =3D line_number_string.valueOf(line_number);
                //ta.setText(line_number_string1);

                return 0;

        }

        // search-method

        public int search_regex() {


                regex1 =3D search_textfield.getText(); // get the inputstring

                // I use trim() to remove leading and trailing whitespace,
important!!
                regex =3D regex1.trim();

                // start to search only if the searchstring is not null
                if (regex.compareTo(zero) > 0) {

                        compiler =3D new Perl5Compiler();
                        matcher =3D new Perl5Matcher();

                        try {
                                pattern =3D (Perl5Pattern) compiler.compile(regex,
Perl5Compiler.CASE_INSENSITIVE_MASK);

                        } catch (MalformedPatternException e) {
                                System.err.println("Bad pattern."
+ regex);
                                System.err.println(e.getMessage());
                                //ta.setText("Bad pattern " +
regex);

                                JOptionPane.showMessageDialog(null, "Bad
pattern " + regex, "information",
                                        JOptionPane.INFORMATION_MESSAGE);

                        }

                        // Open input file.

                        try {
                                file =3D new FileInputStream("c:\\temp\\ftp_in.txt");

                        } catch (IOException e) {
                                System.err.println("Error opening
" + file);
                                System.err.println(e.getMessage());
                                //ta.setText("Error opening "
+ file);
                                JOptionPane.showMessageDialog(null, "Error
opening " + file, "warning", JOptionPane.ERROR_MESSAGE);
                                //System.exit(1);
                        }

                        // Create a Perl5StreamInput instance to search
the input stream.
                        input =3D new Perl5StreamInput(file);

                        // We need to put the search loop in a try block
because when searching
                        // a Perl5StreamInput instance, an IOException
may occur, and it must be
                        // caught.
                        try {
                                // Loop until there are no more matches
left.
                                while (matcher.contains(input, pattern))
{
                                        // Since we're still in the loop,
fetch match that was found.
                                        result =3D matcher.getMatch();

                                        /*getSelectionStart
                                        getSelectionEnd
                                        setCaretPosition followed by a
call to moveCaretPosition*/

                                        //setLocation(containerPanel.getLocationOnScreen());

                                        found_entrys++; // counts the entrys

                                        String res =3D result.toString();

         //**************************** // I want to print the line containing
the result in the textarea ****************************************************
                                        MatchActionProcessor processor
=3D new MatchActionProcessor();

                                        try {
                                                //if (args.length >
2) {
                                                        // Print filename
before line if more than one file is specified.
                                                        // Rely on _file
to point to current file being processed.
                                                        processor.addAction(regex,
new MatchAction() {
                                                                      
             public void processMatch(MatchActionInfo info) {
                                                                      
                     info.output.println(regex + ":" +
                                                                      
                             info.line);
                                                                      
             }
                                                                      
     }
                                                                      
    );
                                                } //else {
                                                        // We rely on the
default action of printing matched
                                                        // lines to the
given OutputStream
                                                  //      processor.addAction(args[0]);
                                                //}
                                        /*}*/ catch (MalformedPatternException
e) {
                                                System.err.println("Bad
pattern.");
                                                e.printStackTrace();
                                                System.exit(1);
                                        }
                                        
         //**************************** code 030523 *****************************************************/

                                        // I do this to find the index
of the searchstring

                                        found_index =3D res.indexOf(regex);

                                        // if an entry is found append
it to the textarea
                                        if (res.compareTo(zero) > 0)
{

                                                ta.append(found_entrys
+ " " + res + " " + found_index + "\n");

                                        } else if (res.compareTo(zero)
=3D=3D 0)/*(regex.matches(res) =3D=3D false)*/ {

                                                JOptionPane.showMessageDialog(null,
"the pattern didn't match", "information",
                                                        JOptionPane.INFORMATION_MESSAGE);

                                        }

                                        /*if (found_entrys.compareTo(zeroLength)
!=3D 0) {

                                                ta.append(found_entrys
+ " " + res + "\n");

                                } else if (found_entrys.compareTo(zeroLength)
=3D=3D 0) {

                                                JOptionPane.showMessageDialog(null,
"the pattern didn't match", "information",
                                                        JOptionPane.INFORMATION_MESSAGE);

                                }*/

                                }

                                file.close(); // close the iostream

                        }
                        catch (IOException e) {
                                System.err.println("Error occurred
while reading " + file);
                                System.err.println(e.getMessage());
                                //ta.setText("Error occurred while
reading " + file);

                                JOptionPane.showMessageDialog(null, "Error
occurred while reading " + file, "information",
                                        JOptionPane.INFORMATION_MESSAGE);

                        }
                }
                else if (regex.compareTo(zero) =3D=3D 0) {

                        //ta.setText("the searchstring is null!!");

                        //custom title, error icon
                        JOptionPane.showMessageDialog(null, "the searchstring
is null!!", "warning", JOptionPane.ERROR_MESSAGE);


                }

                found_entrys =3D 0; // important to set to 0

                // return 0 if everything is ok
                return 0;

        }


        // action-eventmethod

        public void actionPerformed(ActionEvent e) {


                if (e.getSource() =3D=3D jb_clear) {

                        //ta.setOpaque(false);
                        //jl.setVisible(true);
                        clear_text(); // clears the textarea
                        //gotoLine();

                }

                // login
                if (e.getSource() =3D=3D login_ok) {
                        //ta.setOpaque(true);
                        login();

                }

                // method for search
                if (e.getSource() =3D=3D search_ok) {
                        search_regex();

                }

                /*if (e.getSource() =3D=3D goto_line) {

                        gotoLine();
        }*/
        }


        /**
         * @param args the command line arguments
         */

        /***************************** main *************************************/

        public static void main(String args[]) {
                JFrame.setDefaultLookAndFeelDecorated(true); // g=F6r att
framen default f=E5r metalutseende
                new Ftp_get5_new_metod_read();
        }
}

// End of Ftp_get5_new_metod_read.java

_____________________________________________________________
Här börjar internet!
Skaffa gratis e-mail och gratis internet på http://www.spray.se

Hitta rätt på internet med Lycos -  http://lycos.spray.se