de.enough.polish.browser
Class Browser

java.lang.Object
  extended by javax.microedition.lcdui.Item
      extended by javax.microedition.lcdui.CustomItem
          extended by de.enough.polish.ui.FakeCustomItem
              extended by de.enough.polish.ui.FakeContainerCustomItem
                  extended by de.enough.polish.browser.Browser
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
HtmlBrowser

public abstract class Browser
extends FakeContainerCustomItem
implements java.lang.Runnable

TODO: Write good docs. polish.Browser.UserAgent polish.Browser.MaxRedirects polish.Browser.Gzip polish.Browser.POISupport polish.Browser.PaintDownloadIndicator

Author:
Michael Koch
See Also:
HttpProtocolHandler, ResourceProtocolHandler, RedirectHttpConnection

Field Summary
protected  BrowserListener browserListener
           
protected  Container currentContainer
          Currently used container for storing parsing results.
protected  java.lang.String currentDocumentBase
           
protected  java.util.Stack history
           
protected  Gauge loadingIndicator
           
protected  HashMap protocolHandlersByProtocol
           
protected  ArrayList tagHandlers
           
protected  HashMap tagHandlersByTag
           
 
Fields inherited from class de.enough.polish.ui.FakeContainerCustomItem
allowCycling, autoFocusEnabled, autoFocusIndex, availableHeight, containerView, enableScrolling, focusedIndex, focusedItem, focusedStyleFirst, focusedStyleLast, isExpandItems, itemsList, itemStyle, lastPointerPressY, plainStyle, SCROLL_DEFAULT, SCROLL_SMOOTH, scrollSmooth, targetYOffset, yOffset
 
Fields inherited from class de.enough.polish.ui.FakeCustomItem
_bbField, _bbFieldAdded, appearanceMode, background, backgroundHeight, backgroundWidth, backgroundYOffset, border, borderWidth, BUTTON, colSpan, commands, contentHeight, contentWidth, contentX, contentY, cssSelector, defaultCommand, focusedStyle, HORIZONTAL, HYPERLINK, includeLabel, INTERACTIVE, internalHeight, internalWidth, internalX, internalY, isFocused, isInitialized, isInvisible, isLayoutCenter, isLayoutExpand, isLayoutRight, isPressed, isShown, isStyleInitialised, itemCommandListener, itemHeight, itemWidth, label, labelStyle, layout, LAYOUT_2, LAYOUT_BOTTOM, LAYOUT_CENTER, LAYOUT_DEFAULT, LAYOUT_EXPAND, LAYOUT_LEFT, LAYOUT_NEWLINE_AFTER, LAYOUT_NEWLINE_BEFORE, LAYOUT_RIGHT, LAYOUT_SHRINK, LAYOUT_TOP, LAYOUT_VCENTER, LAYOUT_VEXPAND, LAYOUT_VSHRINK, marginBottom, marginLeft, marginRight, marginTop, maximumHeight, maximumWidth, minimumHeight, minimumWidth, NO_POSITION_SET, paddingBottom, paddingHorizontal, paddingLeft, paddingRight, paddingTop, paddingVertical, parent, PLAIN, preferredHeight, preferredWidth, preserveViewType, relativeX, relativeY, rowSpan, screen, style, TRANSPARENT, VERTICAL, view
 
Fields inherited from class javax.microedition.lcdui.CustomItem
KEY_PRESS, KEY_RELEASE, KEY_REPEAT, NONE, POINTER_DRAG, POINTER_PRESS, POINTER_RELEASE, TRAVERSE_HORIZONTAL, TRAVERSE_VERTICAL
 
Constructor Summary
Browser()
          Creates a new Browser without any protocol handlers, tag handlers or style.
Browser(ProtocolHandler[] protocolHandlers)
          Creates a new Browser with the specified handlers and style.
Browser(ProtocolHandler[] protocolHandlers, Style style)
          Creates a new Browser with the specified handlers and style.
Browser(java.lang.String[] tagNames, TagHandler[] tagHandlers, ProtocolHandler[] protocolHandlers)
          Creates a new Browser with the specified handlers and style.
Browser(java.lang.String[] tagNames, TagHandler[] tagHandlers, ProtocolHandler[] protocolHandlers, Style style)
          Creates a new Browser with the specified handlers and style.
Browser(Style style)
          Creates a new Browser without any protocol handler or tag handlers.
 
Method Summary
 void add(Item item)
          Adds an item to this container.
 void add(javax.microedition.lcdui.Item item)
           
 void addAttributeCommand(java.lang.String attributeName, java.lang.String attributeValue, javax.microedition.lcdui.Command command)
           
 void addAttributeCommand(java.lang.String tagName, java.lang.String attributeName, java.lang.String attributeValue, javax.microedition.lcdui.Command command)
           
 void addProtocolHandler(ProtocolHandler handler)
           
 void addProtocolHandler(java.lang.String protocolName, ProtocolHandler handler)
           
 void addTagCommand(java.lang.String tagName, javax.microedition.lcdui.Command command)
           
 void addTagHandler(java.lang.String tagName, java.lang.String attributeName, java.lang.String attributeValue, TagHandler handler)
           
 void addTagHandler(java.lang.String tagName, TagHandler handler)
           
 void animate(long currentTime, ClippingRegion repaintRegion)
          Animates this item.
 void cancel()
           
 boolean canGoBack()
          Checks if the browser can go back
 void clearHistory()
          Clears the history
 Container closeContainer()
          Closes the current container If the current container only contains a single item, that item will be extracted and directly appended using the current container's style.
 void followLink()
           
 BrowserListener getBrowserListener()
           
protected static ProtocolHandler[] getDefaultProtocolHandlers()
          Instantiates and returns the default tag handlers for "http", "https" and "resource" URLs.
protected  ProtocolHandler getProtocolHandler(java.lang.String protocolName)
           
protected  ProtocolHandler getProtocolHandlerForURL(java.lang.String url)
           
 TagHandler getTagHandler(java.lang.String tagName)
           
 TagHandler getTagHandler(java.lang.String tagName, java.lang.String attributeName, java.lang.String attributeValue)
           
 void go(int historySteps)
          Schedules the given history document for loading.
 void go(java.lang.String url)
          Schedules the given URL for loading.
 void go(java.lang.String url, java.lang.String postData)
          Schedules the given URL for loading with HTTP POST data.
 boolean goBack()
          Goes back one history step.
protected  void goImpl(java.lang.String url, java.lang.String postData)
           
 boolean handleCommand(javax.microedition.lcdui.Command command)
          Tries to handle the specified command.
protected abstract  void handleText(java.lang.String text)
          Handles normal text.
protected  void initContent(int firstLineWidth, int lineWidth)
          Initialises this item.
 boolean isCanceled()
           
 boolean isRunning()
           
 boolean isWorking()
           
 javax.microedition.lcdui.Image loadImage(java.lang.String url)
           
 void loadPage(java.io.InputStream in)
          Loads a new HTML page with the specified input stream
 void loadPage(java.io.InputStream in, java.lang.String encoding)
          Loads a new HTML page with the specified input stream
 void loadPage(java.io.Reader reader)
          Loads a page from a given Reader.
 void loadPage(java.lang.String document)
           
 void loadPartialPage(java.io.Reader reader)
          Loads a page from a given Reader.
 java.lang.String makeAbsoluteURL(java.lang.String url)
          Takes a possibly relative URL, and generate an absolute URL, merging with the current documentbase if needed.
protected  void notifyDownloadEnd()
           
protected  void notifyDownloadStart(java.lang.String url)
           
protected  void notifyPageEnd()
           
protected  void notifyPageError(java.lang.String url, java.lang.Exception e)
           
protected  void notifyPageStart(java.lang.String url)
           
 void openContainer(Container container)
          Opens a new Container into which forthcoming elements should be added.
 void openContainer(FakeContainerCustomItem container)
          Opens a new Container into which forthcoming elements should be added.
 void openContainer(Style containerStyle)
          Opens a new Container into which forthcoming elements should be added.
protected  void paintContent(int x, int y, int leftBorder, int rightBorder, javax.microedition.lcdui.Graphics g)
          Paints the content of this item.
protected  java.lang.String protocolAndHostOf(java.lang.String url)
          "http://foo.bar.com/baz/blah.html" => "http://foo.bar.com"
protected  java.lang.String protocolAndPathOf(java.lang.String url)
          "http://foo.bar.com/baz/boo/blah.html" => "http://foo.bar.com/baz/boo"
" "http://foo.bar.com" => "http://foo.bar.com"
"resource://baz/blah.html" => "resource://baz"
"resource://blah.html" => "resource://"
 void requestStop()
           
 void run()
           
protected  void schedule(java.lang.String url, java.lang.String postData)
           
 void setBackCommand(javax.microedition.lcdui.Command cmdBack)
          Sets the back command for this browser.
 void setBrowserListener(BrowserListener browserListener)
           
 
Methods inherited from class de.enough.polish.ui.FakeContainerCustomItem
add, add, add, add, add, changeChildStyles, changeChildStyles, clear, createCssSelector, defocus, focus, focus, focus, focusClosestItem, focusClosestItem, focusClosestItemAbove, get, getFocusedIndex, getFocusedItem, getFocusedStyle, getItemAt, getItems, getNumberOfInteractiveItems, getPosition, getRelativeScrollHeight, getRelativeScrollYOffset, getScrollHeight, getScrollYOffset, handleKeyPressed, handleKeyReleased, handleKeyRepeated, handleNavigate, handlePointerPressed, handlePointerReleased, handlePointerScrollReleased, hideNotify, indexOf, paintBackgroundAndBorder, parseIndexUrl, releaseResources, remove, remove, requestDefocus, requestFullInit, scroll, scroll, set, set, setItemsList, setScrollHeight, setScrollYOffset, setScrollYOffset, setStyle, setStyle, showCommands, showNotify, size, toString
 
Methods inherited from class de.enough.polish.ui.FakeCustomItem
addCommand, addCommand, addCommands, addRelativeToBackgroundRegion, addRelativeToContentRegion, animate, containsCommand, getAbsoluteX, getAbsoluteY, getAppearanceMode, getAttribute, getAttributes, getBackgroundHeight, getBackgroundWidth, getBackgroundX, getBackgroundY, getContentHeight, getContentWidth, getContentX, getContentY, getDefaultCommand, getFocusedStyle, getItemCommandListener, getItemHeight, getItemStateListener, getItemWidth, getLabel, getLabelItem, getLayout, getMinContentHeight, getMinContentWidth, getMinimumHeight, getMinimumWidth, getParent, getPrefContentHeight, getPrefContentWidth, getPreferredHeight, getPreferredWidth, getScreen, getStyle, init, initStyle, isInContentArea, isInItemArea, isVisible, notifyItemPressedEnd, notifyItemPressedStart, notifyStateChanged, paint, paint, paintBackground, paintBorder, removeCommand, repaintFully, requestInit, setAppearanceMode, setAttribute, setDefaultCommand, setItemCommandListener, setItemCommandListener, setItemStateListener, setLabel, setLayout, setPreferredSize, setVisible, show
 
Methods inherited from class javax.microedition.lcdui.CustomItem
getGameAction, getInteractionModes, invalidate, keyPressed, keyReleased, keyRepeated, pointerDragged, pointerPressed, pointerReleased, repaint, repaint, sizeChanged, traverse, traverseOut
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

currentDocumentBase

protected java.lang.String currentDocumentBase

protocolHandlersByProtocol

protected HashMap protocolHandlersByProtocol

tagHandlersByTag

protected HashMap tagHandlersByTag

tagHandlers

protected ArrayList tagHandlers

history

protected java.util.Stack history

loadingIndicator

protected Gauge loadingIndicator

browserListener

protected BrowserListener browserListener

currentContainer

protected Container currentContainer
Currently used container for storing parsing results.

Constructor Detail

Browser

public Browser()
Creates a new Browser without any protocol handlers, tag handlers or style.


Browser

public Browser(Style style)
Creates a new Browser without any protocol handler or tag handlers.

Parameters:
style - the style of this browser

Browser

public Browser(ProtocolHandler[] protocolHandlers)
Creates a new Browser with the specified handlers and style.

Parameters:
protocolHandlers - the tag handlers

Browser

public Browser(ProtocolHandler[] protocolHandlers,
               Style style)
Creates a new Browser with the specified handlers and style.

Parameters:
protocolHandlers - the tag handlers
style - the style to use for the browser item

Browser

public Browser(java.lang.String[] tagNames,
               TagHandler[] tagHandlers,
               ProtocolHandler[] protocolHandlers)
Creates a new Browser with the specified handlers and style.

Parameters:
tagNames - the names of the tags that the taghandler should handle (this allows to use a single taghandler for several tags)
tagHandlers - the tag handlers
protocolHandlers - the protocol handlers

Browser

public Browser(java.lang.String[] tagNames,
               TagHandler[] tagHandlers,
               ProtocolHandler[] protocolHandlers,
               Style style)
Creates a new Browser with the specified handlers and style.

Parameters:
tagNames - the names of the tags that the taghandler should handle (this allows to use a single taghandler for several tags)
tagHandlers - the tag handlers
protocolHandlers - the protocol handlers
style - the style of this browser
Method Detail

getDefaultProtocolHandlers

protected static ProtocolHandler[] getDefaultProtocolHandlers()
Instantiates and returns the default tag handlers for "http", "https" and "resource" URLs.

Returns:
new default tag handlers
See Also:
HttpProtocolHandler, ResourceProtocolHandler

addTagCommand

public void addTagCommand(java.lang.String tagName,
                          javax.microedition.lcdui.Command command)

addAttributeCommand

public void addAttributeCommand(java.lang.String attributeName,
                                java.lang.String attributeValue,
                                javax.microedition.lcdui.Command command)

addAttributeCommand

public void addAttributeCommand(java.lang.String tagName,
                                java.lang.String attributeName,
                                java.lang.String attributeValue,
                                javax.microedition.lcdui.Command command)

addProtocolHandler

public void addProtocolHandler(ProtocolHandler handler)

addProtocolHandler

public void addProtocolHandler(java.lang.String protocolName,
                               ProtocolHandler handler)

getProtocolHandler

protected ProtocolHandler getProtocolHandler(java.lang.String protocolName)

getProtocolHandlerForURL

protected ProtocolHandler getProtocolHandlerForURL(java.lang.String url)
                                            throws java.io.IOException
Throws:
java.io.IOException

addTagHandler

public void addTagHandler(java.lang.String tagName,
                          TagHandler handler)

addTagHandler

public void addTagHandler(java.lang.String tagName,
                          java.lang.String attributeName,
                          java.lang.String attributeValue,
                          TagHandler handler)

getTagHandler

public TagHandler getTagHandler(java.lang.String tagName)

getTagHandler

public TagHandler getTagHandler(java.lang.String tagName,
                                java.lang.String attributeName,
                                java.lang.String attributeValue)

openContainer

public void openContainer(Style containerStyle)
Opens a new Container into which forthcoming elements should be added.

Parameters:
containerStyle - the style of the container

openContainer

public void openContainer(Container container)
Opens a new Container into which forthcoming elements should be added.

Parameters:
container - the new the container

openContainer

public void openContainer(FakeContainerCustomItem container)
Opens a new Container into which forthcoming elements should be added.

Parameters:
container - the new the container

closeContainer

public Container closeContainer()
Closes the current container If the current container only contains a single item, that item will be extracted and directly appended using the current container's style.

Returns:
the previous container, if any is known

handleText

protected abstract void handleText(java.lang.String text)
Handles normal text.

Parameters:
text - the text

add

public void add(Item item)
Description copied from class: FakeContainerCustomItem
Adds an item to this container.

Overrides:
add in class FakeContainerCustomItem
Parameters:
item - the item which should be added.

add

public void add(javax.microedition.lcdui.Item item)
Overrides:
add in class FakeContainerCustomItem

loadPage

public void loadPage(java.io.Reader reader)
              throws java.io.IOException
Loads a page from a given Reader.

Parameters:
reader - the reader to load the page from
Throws:
java.io.IOException - of an error occurs

loadPartialPage

public void loadPartialPage(java.io.Reader reader)
                     throws java.io.IOException
Loads a page from a given Reader.

Parameters:
reader - the reader to load the page from
Throws:
java.io.IOException - of an error occurs

protocolAndHostOf

protected java.lang.String protocolAndHostOf(java.lang.String url)
"http://foo.bar.com/baz/blah.html" => "http://foo.bar.com"

"resource://baz/blah.html" => "resource://"

Parameters:
url - the URL to the get protocol and host part from
Returns:
the protocol and host part from the given URL

makeAbsoluteURL

public java.lang.String makeAbsoluteURL(java.lang.String url)
Takes a possibly relative URL, and generate an absolute URL, merging with the current documentbase if needed.
  1. If URL starts with http:// or resource:// leave it alone
  2. If URL starts with '/', prepend document base protocol and host name.
  3. Otherwise, it's a relative URL, so prepend current document base and directory path.

Parameters:
url - the (possibly relative) URL
Returns:
absolute URL

loadPage

public void loadPage(java.lang.String document)

loadPage

public void loadPage(java.io.InputStream in)
              throws java.io.IOException
Loads a new HTML page with the specified input stream

Parameters:
in - the input stream
Throws:
java.io.IOException - when the page could not be read or when the input stream is null

loadPage

public void loadPage(java.io.InputStream in,
                     java.lang.String encoding)
              throws java.io.IOException
Loads a new HTML page with the specified input stream

Parameters:
in - the input stream
encoding - the encoding, is ignored when null
Throws:
java.io.IOException - when the page could not be read or when the input stream is null or when the specified encoding is not supported

loadImage

public javax.microedition.lcdui.Image loadImage(java.lang.String url)

protocolAndPathOf

protected java.lang.String protocolAndPathOf(java.lang.String url)
"http://foo.bar.com/baz/boo/blah.html" => "http://foo.bar.com/baz/boo"
" "http://foo.bar.com" => "http://foo.bar.com"
"resource://baz/blah.html" => "resource://baz"
"resource://blah.html" => "resource://"

Parameters:
url - the URL to the get protocol and path part from
Returns:
the protocol and path part from the given URL

handleCommand

public boolean handleCommand(javax.microedition.lcdui.Command command)
Description copied from class: FakeCustomItem
Tries to handle the specified command. The item checks if the command belongs to this item and if it has an associated ItemCommandListener. Only then it handles the command.

Overrides:
handleCommand in class FakeContainerCustomItem
Parameters:
command - the command
Returns:
true when the command has been handled by this item

goImpl

protected void goImpl(java.lang.String url,
                      java.lang.String postData)

initContent

protected void initContent(int firstLineWidth,
                           int lineWidth)
Description copied from class: FakeCustomItem
Initialises this item. The implementation needs to calculate and set the contentWidth and contentHeight fields. The implementation should take the fields preferredWidth and preferredHeight into account.

Overrides:
initContent in class FakeContainerCustomItem
Parameters:
firstLineWidth - the maximum width of the first line
lineWidth - the maximum width of any following lines
See Also:
FakeCustomItem.contentWidth, FakeCustomItem.contentHeight, FakeCustomItem.preferredWidth, FakeCustomItem.preferredHeight

paintContent

protected void paintContent(int x,
                            int y,
                            int leftBorder,
                            int rightBorder,
                            javax.microedition.lcdui.Graphics g)
Description copied from class: FakeCustomItem
Paints the content of this item. The background has already been painted and the border will be added after this method returns.

Overrides:
paintContent in class FakeContainerCustomItem
Parameters:
x - the left start position
y - the upper start position
leftBorder - the left border, nothing must be painted left of this position
rightBorder - the right border, nothing must be painted right of this position
g - the Graphics on which this item should be painted.

animate

public void animate(long currentTime,
                    ClippingRegion repaintRegion)
Description copied from class: FakeCustomItem
Animates this item. Subclasses can override this method to create animations. The default implementation animates the background and the item view if present.

Overrides:
animate in class FakeContainerCustomItem
Parameters:
currentTime - the current time in milliseconds
repaintRegion - the repaint area that needs to be updated when this item is animated
See Also:
#addRelativeRegion(ClippingRegion, int, int, int, int)

run

public void run()
Specified by:
run in interface java.lang.Runnable

schedule

protected void schedule(java.lang.String url,
                        java.lang.String postData)

cancel

public void cancel()

requestStop

public void requestStop()

isRunning

public boolean isRunning()

isCanceled

public boolean isCanceled()

isWorking

public boolean isWorking()

go

public void go(java.lang.String url)
Schedules the given URL for loading.

Parameters:
url - the URL that should be loaded

go

public void go(java.lang.String url,
               java.lang.String postData)
Schedules the given URL for loading with HTTP POST data.

Parameters:
url - the URL that should be loaded
postData - the data to be sent via HTTP POST

go

public void go(int historySteps)
Schedules the given history document for loading.

Parameters:
historySteps - the steps that should go back, e.g. 1 for the last page that has been shown

followLink

public void followLink()

setBackCommand

public void setBackCommand(javax.microedition.lcdui.Command cmdBack)
Sets the back command for this browser. The back command will be appended to the parent screen when the browser can go back and it will be removed when the browser cannot got back anymore.

Parameters:
cmdBack - the back command - set to null to remove it completely

goBack

public boolean goBack()
Goes back one history step.

Returns:
true when the browser has a previous document in its history
See Also:
go(int)

canGoBack

public boolean canGoBack()
Checks if the browser can go back

Returns:
true when there is a known previous document
See Also:
goBack()

clearHistory

public void clearHistory()
Clears the history

See Also:
goBack(), go(int)

notifyPageError

protected void notifyPageError(java.lang.String url,
                               java.lang.Exception e)

notifyPageStart

protected void notifyPageStart(java.lang.String url)

notifyPageEnd

protected void notifyPageEnd()

notifyDownloadStart

protected void notifyDownloadStart(java.lang.String url)

notifyDownloadEnd

protected void notifyDownloadEnd()

getBrowserListener

public BrowserListener getBrowserListener()

setBrowserListener

public void setBrowserListener(BrowserListener browserListener)