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

📄 browser.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    //#debug debug
    //# System.out.println("makeAbsoluteURL: currentDocumentBase = " + this.currentDocumentBase);
    
    // If no ":", assume it's a relative link, (no protocol),
    // and append current page
    if (url.indexOf("://") != -1)
    {
      return url;
    }
    else if (url.startsWith("/"))
    {
      if ("resource://".regionMatches(true, 0, this.currentDocumentBase, 0, 11))
      {
        // we need to strip a leading slash if it's a local resource, i.e.,
        // "resource://" + "/foo.png" => "resource://foo.png"
        return protocolAndHostOf(this.currentDocumentBase) + url.substring(1);
      }
      else
      {
        // for HTTP, we don't need to strip the leading slash, i.e.,
        // "http://foo.bar.com" + "/foo.png" => "http://foo.bar.com/foo.png"
        return protocolAndHostOf(this.currentDocumentBase) + url;
      }
    }
    else
    {
      // It's a relative url, so merge it with the current document
      // path.
      String prefix = protocolAndPathOf(this.currentDocumentBase);
      
      if (prefix.endsWith("/"))
      {
        return prefix + url;
      }
      else
      {
        return prefix + "/" + url;
      }
    }
  }

  public void loadPage(String document)
  {
    try
    {
      loadPage(new StringReader(document));
    }
    catch (IOException e)
    {
      // StringReader never throws an IOException.
    }
  }

  public void loadPage(InputStream in)
  throws IOException
  {
	  if (in == null)
	  {
		  throw new IOException("no input stream");
	  }

    loadPage(new InputStreamReader(in));
  }

  private Image loadImageInternal(String url)
  {
    Image image = (Image) this.imageCache.get(url);

    if (image == null)
    {
      try
      {
        ProtocolHandler handler = getProtocolHandlerForURL(url);
        
        StreamConnection connection = handler.getConnection(url);
        InputStream is = connection.openInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int bytesRead;

        do
        {
          bytesRead = is.read(buf);
          
          if (bytesRead > 0)
          {
            bos.write(buf, 0, bytesRead);
          }
        }
        while (bytesRead >= 0);
        
        buf = bos.toByteArray();
        
        //#debug
        //# System.out.println("Image requested: " + url);

        image = Image.createImage(buf, 0, buf.length);
        this.imageCache.put(url, image);
        return image;
      }
      catch (Exception e)
      {
        // TODO: Implement proper error handling.
        
        //#debug debug
        //# e.printStackTrace();
        
        return null;
      }
    }
    
    return image;
  }

  public Image loadImage(String url)
  {
    Image image = loadImageInternal(url);
    
    if (image == null)
    {
      image = loadImageInternal(BROKEN_IMAGE);
    }
    
    if (image == null)
    {
      image = Image.createImage(10, 10);
      Graphics g = image.getGraphics();
      g.setColor(0xFFFFFF);
      g.fillRect(0, 0, 10, 10);
      g.setColor(0xFF0000);
      g.drawLine(0, 0, 10, 10);
      g.drawLine(0, 10, 10, 0);
    }
    
    return image;
  }

  /**
   * "http://foo.bar.com/baz/boo/blah.html" => "http://foo.bar.com/baz/boo"
   * <br>
   " "http://foo.bar.com" => "http://foo.bar.com"
   * <br>
   * "resource://baz/blah.html" => "resource://baz"
   * <br>
   * "resource://blah.html" => "resource://"
   */
  protected String protocolAndPathOf (String url)
  {
    // 1. Look for query args, or end of string.
    // 2. from there, scan backward for first '/', 
    // 3. cut the string there.
    // figure out what error checking to do here
  
    int end = url.indexOf('?');
    
    if (end == -1)
    {
      end = url.length()-1;
    }
        
    int hostStart = url.indexOf("//");
    // figure out what error checking to do here
    hostStart += 2;
  
    int lastSlash = url.lastIndexOf('/', end);
  
    // RESOURCE urls have no host portion, so return everything between
    // the "resource://" and last slash, if it exists.
    if ("resource://".regionMatches(true, 0, url, 0, 11))
    {
      if ((lastSlash == -1) || (lastSlash <= hostStart))
      {
        return "resource://";
      }

      return url.substring(0, lastSlash);
    }
    else
    {
      if ((lastSlash == -1) || (lastSlash <= hostStart))
      {
        return url;
      }
      
      return url.substring(0, lastSlash);
    }
  }
  
  public boolean handleCommand(Command command)
  {
    TagHandler[] handlers = (TagHandler[])
      this.tagHandlers.values(new TagHandler[this.tagHandlers.size()]);
    
    for (int i = 0; i < handlers.length; i++)
    {
      if (handlers[i].handleCommand(command))
      {
        return true;
      }
    }
    
    return false;
  }
  
  protected void goImpl(String url)
  {
	 String previousDocumentBase = this.currentDocumentBase; 
    try
    {
      // Throws an exception if no handler found.
      ProtocolHandler handler = getProtocolHandlerForURL(url);
    
      this.currentDocumentBase = url;
    
      StreamConnection connection = handler.getConnection(url);
      
      if (connection != null)
      {
        loadPage(connection.openInputStream());
        connection.close();
      }
    }
    catch (IOException e)
    {
      //#debug error
      //# System.out.println("Unable to load page " + url + e );
      this.currentDocumentBase = previousDocumentBase;
    }
  }
  
  //////////////// download indicator handling /////////////
  
  //#if polish.Browser.PaintDownloadIndicator
//#   
  //# /* (non-Javadoc)
   //# * @see de.enough.polish.ui.Container#initContent(int, int)
   //# */
  //# protected void initContent(int firstLineWidth, int lineWidth) {
  	//# super.initContent(firstLineWidth, lineWidth);
  	//# // when there is a loading indicator, we need to specify the minmum size:
  	//# int width = this.loadingIndicator.getItemWidth( lineWidth, lineWidth );
  	//# if (width > this.contentWidth) {
  		//# this.contentWidth = width;
  	//# }
  	//# int height = this.loadingIndicator.itemHeight;
  	//# if (height > this.contentHeight) {
  		//# this.contentHeight = height;
  	//# }
  //# }
//#   
  //# /* (non-Javadoc)
   //# * @see de.enough.polish.ui.Container#paintContent(int, int, int, int, javax.microedition.lcdui.Graphics)
   //# */
  //# protected void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g)
  //# {
    //# super.paintContent(x, y, leftBorder, rightBorder, g);
//# 
    //# if (this.isWorking)
    //# {
    	//# int originalY = y;
    	//# if (this.parent instanceof Container) {
    		//# y += ((Container)this.parent).getScrollYOffset();
    	//# }
    	//# y += this.yOffset;
    	//# int lineWidth = rightBorder - leftBorder;
      	//# int liHeight = this.loadingIndicator.getItemHeight( lineWidth, lineWidth );
      	//# int liLayout = this.loadingIndicator.getLayout();
      	//# if ( (liLayout & LAYOUT_VCENTER) == LAYOUT_VCENTER ) {
      		//# y += this.contentHeight>>1 + liHeight>>1;
      	//# } else if ( (liLayout & LAYOUT_BOTTOM ) == LAYOUT_BOTTOM ) {
      		//# y += this.contentHeight - liHeight;
      	//# }
      	//# this.loadingIndicator.relativeX = x;
      	//# this.loadingIndicator.relativeY = y - originalY;
      	//# //System.out.println(">>>>>> download indicator at " + x + ", " + y + ", originalY=" + originalY +", yOffset=" + this.yOffset);
      	//# this.loadingIndicator.paint(x, y, leftBorder, rightBorder, g);
    //# }
  //# }
//#   
  //# /* (non-Javadoc)
   //# * @see de.enough.polish.ui.Container#animate()
   //# */
  //# public boolean animate()
  //# {
    //# boolean result = false;
    //# if (this.isWorking) {
    	//# result = this.loadingIndicator.animate();
    //# } else if (!this.isStoppedWorking) {
    	//# this.isStoppedWorking = true;
    	//# result = true;
    //# }
//# 
    //# return super.animate() | result;
  //# }
  //#endif
 
  
  ////////////////  downloading thread /////////////////
  public void run()
  {
	  // ensure that the user is able to specify the first location before this thread is going to sleep/wait.
	  try {
		Thread.sleep( 100 );
	} catch (InterruptedException e) {
		// ignore
	}
    this.isRunning = true;

    while (this.isRunning)
    {
      if (this.isRunning && this.nextUrl != null)
      {
        this.isWorking = true;
        //#if polish.Browser.PaintDownloadIndicator
        	//# this.isStoppedWorking = false;
        //#endif
        String url = this.nextUrl;
        this.nextUrl = null;
          
        if (this.isCancelRequested != true)
        {
          goImpl(url);
        }
        
        this.isWorking = false;
        repaint();
      }
        
      if (this.isCancelRequested == true)
      {
        this.isWorking = false;
        repaint();
        this.isCancelRequested = false;
        this.nextUrl = null;
        loadPage("Request canceled");
      }
        
      try
      {
        this.isWorking = false;
        synchronized( this.loadingThread ) {
        	this.loadingThread.wait();
        }
      }
      catch (InterruptedException ie)
      {
//        interrupt();
      }
    } // end while(isRunning)
  } // end run()
  
  protected void schedule(String url)
  {
    this.nextUrl = url;
    synchronized( this.loadingThread ) {
    	this.loadingThread.notify();
    }
  }
      
  public void cancel()
  {
    this.isCancelRequested = true;
  }

  public synchronized void requestStop()
  {
    this.isRunning = false;
    synchronized( this.loadingThread ) {
    	this.loadingThread.notify();
    }
  }

  public boolean isRunning()
  {
    return this.isRunning;
  }
  
  public boolean isCanceled()
  {
    return this.isCancelRequested;
  }
  
  public boolean isWorking()
  {
    return this.isWorking;
  }
  
  
  //////////////////////////// History //////////////////////////////
  
  public void go(String url)
  {
      if (this.currentDocumentBase != null)
      {
      	this.history.push(this.currentDocumentBase);
      }
      schedule(url);
  }
  

  
  public void go(int historySteps)
  {
    String document = null;
    
    while (historySteps > 0 && this.history.size() > 0)
    {
      document = (String) this.history.pop();
      historySteps--;
    }
    
    if (document != null)
    {
      goImpl(document);
    }
  }
  
  public void followLink()
  {
    Item item = getFocusedItem();
    String href = (String) item.getAttribute("href");
    
    if (href != null)
    {
      go(makeAbsoluteURL(href));
    }
  }
  
  public void goBack()
  {
    go(1);
  }
  
  public boolean canGoBack()
  {
    return this.history.size() > 0;
  }
  
  public void clearHistory()
  {
    this.history.removeAllElements();
  }
}

⌨️ 快捷键说明

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