Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

java - How to parse text from multiple strings?

I am using a terminal emulator library to create a terminal and then I use it to send the data entered over serial to a serial device. When the data is sent back I want to parse it and show the most important information to the user in an editText. Currently I receive byte arrays/chunks and I convert them to a string. When I get a or a I crete a new string and the process repeats. This is fine for most commands, however some commands return results over multiple lines like "show vlan" here:

enter image description here

When I loop through this I get a string for each line. The first would contain VLAN Name Status and Ports, as an example. So now I have a problem, how can I VLAN 1 has x ports active. They are in different strings. Here is the code and screenshot for a current easier command where I am interested in one line:

enter image description here

Handler viewHandler = new Handler();
Runnable updateView = new Runnable() {
    @Override

    public void run() {

        mEmulatorView.invalidate();

        if (statusBool == true) {
            for (int i = 0; i < dataReceived.length(); i++) {
                parseCommand = parseCommand + dataReceived.charAt(i);

 if (dataReceived.charAt(i) == '
' || dataReceived.charAt(i) == '
'){

    if(parseCommand.contains("KlasOS"))
            {

        String[] tokens = parseCommand.split("\s{1,}");

        final String ReceivedText = mReceiveBox.getText().toString() + " "
        + new String("Software Version: " + tokens[1] + "
" );

        runOnUiThread(new Runnable() {
            public void run() {
                                mReceiveBox.setText(ReceivedText);
                                mReceiveBox.setSelection(ReceivedText.length());

                            }
                        });         

                            }

                parseCommand = "";

            }               
            }

            statusBool = false;
            viewHandler.postDelayed(updateView, 1000);
        }
    }
};

Now I would like to change this so i can deal with multiple lines. Would the ebst way be to store strings if they contain certain information?

I need this outputted on the right hand editText:

"The following ports are on vlan 1: Fa1/0, fa1/1, fa1/2, fa1/3, fa1/4, fa1/5, fa1/6, fa1/7, fa1/8, fa1/9, fa1/10, fa1/11, Gi0"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Basically, you need a way to reliably detect the end of a command result. Then it boils down to sending your command, reading data from the device until you encounter the end of result, and finally parsing that result.

I would scan for the prompt (switch#) as you do in your own answer. Maybe your are even able to force the device to use a more peculiar character sequence, which is unlikely to occur in the regular output of commands and makes it easier to detect the end of a result. For example, you could try to configure the prompt to include a control character like ^G or ^L. Or if your users don't mind, you could always send a second command that emits such a sequence, for example, "show vlan; echo ^G".

You should also be prepared for command errors, which result in a different output, for example, more or fewer lines as expected or a totally different output format. A result may even contain both, a regular output and a warning or an error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...