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

📄 mtwb.java

📁 embedded linux web browser
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * mtwb.java
 *
 * Created on 2006, February 22, 0:53
 * Juraj Borza
 *
 *Changelog:
 *
 *0.2 - can replace some HTML tags with corresponding stuff (e.g. <br> with \n etc and strip other tags.
 *
 *0.1 - first working version, could only get http connection and grab files over html
 */

package mtwb;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;


/**
 *
 * @author Administrator
 */
public class mtwb extends MIDlet implements CommandListener, ItemCommandListener
{
    
    /** Creates a new instance of mtwb */
    public mtwb()
    {
        initialize();
    }
    
    private Form MainMenu;//GEN-BEGIN:MVDFields
    private StringItem stringItem1;
    private Spacer spacer2;
    private TextField TFaddress;
    private Spacer spacer1;
    private TextField TFsearch;
    private Command exitCommand1;
    private Command cmdGo;
    private Form Browser;
    private Command cmdSearch;
    private Command exitCommand2;
    private Command backCommand1;
    private StringItem TFcontent;
    private Spacer spacer3;
    private StringItem status;
    private Spacer spacer4;
    private Command helpCommand1;
    private Alert helpAlert;//GEN-END:MVDFields
    private String URL = null; //URL string
    private String SearchString; //string we are searching for
    private String Content; //the html code
    private String TempString; //temoporary string
    
    private HttpConnection hc; //our httpconnection
    private DataInputStream dis; //and data input stream
    
//GEN-LINE:MVDMethods

    private void Browse()
    {
        //first display that we are connecting
        status.setText("Connecting...");
                try
                {
                hc = (HttpConnection) Connector.open(URL);
                dis = new DataInputStream(hc.openInputStream());
                status.setText("Connected, reading page...");
                int ch; //character
                while ((ch = dis.read()) != -1)  //read stream to end
                {
                     TempString += String.valueOf((char) ch);
                }
                status.setText("Parsing...");
                //now we should parse the HTML
                Content = Parse(TempString);
                TempString = ""; //clear the TempString
                status.setText("Ready");
                }
                
                catch(Throwable t)
                {
                   status.setText("Error occured while opening or parsing page. Check your connection please.");
                }

                finally {
                    //dummy?
                }
    }
    
    public String Parse(String s)
    {
        //new version now
        /*so this will decode HTML code we loaded from server, 
         *now it will only strip everything between < and > (e.g <br>, <hr>, <html>, <img src=...> etc)
         */
        
        //variables needed come here
        boolean hasLT = false; //indicates if we have found < tag
        boolean first = false; //if we are inside of tag and on '<' character  
        //int cursor = 0; //sets position in 'decoded' array
        String decoded = ""; //for now empty string
        String tag = ""; //the tag value
        //for later implementation
        //int LTposition = 0; //this shows us position on < tag 
        //now code
        
        char[] chary = new char[s.length()];
        //char[] decoded = new char[s.length()]; //new array for decoded html
        s.getChars(0,s.length(),chary,0); //copy source string into char array
        for(int i = 0; i < s.length();i++)
        { //copy entire stuff
            /*method:
             *we don't want to include text between < and >
             *so when we find "<" then we don't copy over any character inside "decoded" char[] until we find >
             *
             *newer stuff: we should replace &nbsp; with normal space
             *how to do that?
             *
             *also memorize what is the tag we are replacing
             *
             *if it's one of known "good" tags, we don't wipe it out completely, but only replace with something.
             *for example <br> with line break \n, <hr> with "-------------\n" etc...
             *
             *it's maybe good idea to catch the thing in < > into String, then convert it into lowercase and compare to some known 
             *tags
             *
             *so use the "first" variable to know when we started to be in tag
             *then copy each character fo tag into some string (String tag) 
             *and then lowercase it and then convert it into desired stuff and copy into result (String decoded)
             */
            
            
            if(chary[i] == '<'){
                hasLT = true; //mark we found LT
                first = true; //we are on '<'
            }
            
            if(!hasLT){
            //decoded[cursor] = chary[i];
            decoded += String.valueOf(chary[i]);
            //cursor++;
            }
            else{
            //we are inside of tag    
            //so get the char and convert it into lowercase
             if(!first){
                tag += String.valueOf(chary[i]).toLowerCase();
             }
            }
            
            if(chary[i] == '>'){
                hasLT = false;   
                first = false; //just in case, maybe not needed
                //and now decode the tag
                if(tag == "hr")
                    decoded += "\n------------\n";
                else if(tag == "br")
                    decoded += "\n";
                else if(tag == "br /")
                    decoded += "\n";
                else if(tag.indexOf("href") != -1) //link is here
                    decoded += "#link#:";
            }
        }
        //return parsed  string in the end 
        return decoded;        
    }
   
    /** Called by the system to indicate that a command has been invoked on a particular item.//GEN-BEGIN:MVDICABegin
     * @param command the Command that ws invoked
     * @param item the Item on which the command was invoked
     */
    public void commandAction(Command command, Item item)
    {//GEN-END:MVDICABegin
    // Insert global pre-action code here
        if (item == TFaddress)//GEN-BEGIN:MVDICABody
        {
            if (command == cmdGo)
            {//GEN-END:MVDICABody
                // Insert pre-action code here
                URL = TFaddress.getString();
                Browse();
                getDisplay().setCurrent(get_Browser());//GEN-LINE:MVDICAAction12
                TFcontent.setText(Content);
                TFcontent.setLabel(URL);
                
            }//GEN-BEGIN:MVDICACase12
        }
        else if (item == TFsearch)
        {
            if (command == cmdSearch)
            {//GEN-END:MVDICACase12
                // Insert pre-action code here
                //go to search page -> construct URL
                //replace spaces with +
                SearchString = TFsearch.getString().replace(' ','+');
                URL = "http://www.google.com/search?q="+TFsearch.getString();
                Browse();
                getDisplay().setCurrent(get_Browser());//GEN-LINE:MVDICAAction15
                TFcontent.setText(Content);
                TFcontent.setLabel("Search:"+TFsearch.getString());
            }//GEN-BEGIN:MVDICACase15
        }//GEN-END:MVDICACase15
    // Insert global post-action code here
}//GEN-LINE:MVDICAEnd

    /** Called by the system to indicate that a command has been invoked on a particular displayable.//GEN-BEGIN:MVDCABegin
     * @param command the Command that ws invoked
     * @param displayable the Displayable on which the command was invoked
     */
    public void commandAction(Command command, Displayable displayable)
    {//GEN-END:MVDCABegin
    // Insert global pre-action code here
        if (displayable == MainMenu)//GEN-BEGIN:MVDCABody
        {
            if (command == exitCommand1)
            {//GEN-END:MVDCABody
                // Insert pre-action code here
                exitMIDlet();//GEN-LINE:MVDCAAction10
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase10
            else if (command == helpCommand1)
            {//GEN-END:MVDCACase10
                // Insert pre-action code here
                getDisplay().setCurrent(get_helpAlert(), get_MainMenu());//GEN-LINE:MVDCAAction25
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase25
        }
        else if (displayable == Browser)
        {
            if (command == backCommand1)
            {//GEN-END:MVDCACase25
                // Insert pre-action code here
                getDisplay().setCurrent(get_MainMenu());//GEN-LINE:MVDCAAction19
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase19
        }//GEN-END:MVDCACase19
    // Insert global post-action code here
}//GEN-LINE:MVDCAEnd

    /** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin
     */
    private void initialize()
    {//GEN-END:MVDInitBegin
        // Insert pre-init code here
        getDisplay().setCurrent(get_MainMenu());//GEN-LINE:MVDInitInit
        // Insert post-init code here
    }//GEN-LINE:MVDInitEnd
    
    /**
     * This method should return an instance of the display.
     */
    public Display getDisplay()//GEN-FIRST:MVDGetDisplay
    {
        return Display.getDisplay(this);
    }//GEN-LAST:MVDGetDisplay
    
    /**
     * This method should exit the midlet.
     */
    public void exitMIDlet()//GEN-FIRST:MVDExitMidlet
    {
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }//GEN-LAST:MVDExitMidlet

    /** This method returns instance for MainMenu component and should be called instead of accessing MainMenu field directly.//GEN-BEGIN:MVDGetBegin2
     * @return Instance for MainMenu component
     */
    public Form get_MainMenu()
    {
        if (MainMenu == null)
        {//GEN-END:MVDGetBegin2
            // Insert pre-init code here

⌨️ 快捷键说明

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