📄 browser.java
字号:
while (true) { if(type==oldType) type= parser.next(); // only progress parser if a tag didnt already call parser.next() oldType = type; // some tags call parser.next(), old type helps keep track if parser.next() was called. if(type==KXmlParser.START_TAG) /* **** Handle Opening TAGs ***** */ { String name = parser.getName().toLowerCase();; Class tagClass = (Class)knownTags.get(name); if(tagClass!=null) { try { Tag t = (Tag)tagClass.newInstance(); if(tagStack.size()==0) t.inheritStyle(rootTag); // inherit basic style information. t.handleTagStart(this,page,parser); pushTag(t); }catch(InstantiationException e) { Log.logError("Failed to instantiate a Tag class for tag name "+name+".",e); } catch (Exception e) { Log.logError("Exception while handling tag start "+name,e); } } else Log.logWarn("Unknown Opening TAG "+name); } else if(type==KXmlParser.END_TAG) /* **** Handle Closing TAGs ***** */ { String name = parser.getName().toLowerCase();; Tag t = (Tag)topTag(); if(t!=null && name.equals(t.getName())) { t.handleTagEnd(this,page,parser); popTag(); } else Log.logWarn("Unknown Closing TAG "+name+" expected "+(t==null?"none":t.getName())); } else if(type==KXmlParser.TEXT) /* **** Handle Text inside a TAG ***** */ { Tag top = (Tag)topTag(); String txt = parser.getText(); if(top!=null && txt.length()>0) { top.handleText(top,txt); } } else if(type==KXmlParser.END_DOCUMENT) { break; // parsing completed. } else /* **** Default action, just log the unknown type and continue **** */ { Log.logWarn("Unknown tag "+parser.getName() +" type " + type); } type = parser.getEventType(); // get type again since some tags call parser.next() } gauge.progress(); Log.logDebug("=>End Of Document<="); }finally{ try{ if(reader!=null) reader.close(); }catch(Throwable e) {} } if(imageLoadingPolicy==LOAD_IMAGES_ASYNC) page.startAsyncImageLoad(httpClient); return page; } /** * When a Tag is pushed it is considered to be inside the last one pushed. * The Tag implementation is responsible for doing so. * * @param node */ private void pushTag(Tag node) { tagStack.addElement(node); } public Tag topTag() { if(tagStack.size()>0) return (Tag)tagStack.lastElement(); // else return null; } private Tag popTag() { int size = tagStack.size(); if(size>0) { Tag tc = (Tag)tagStack.lastElement(); tagStack.removeElementAt(size-1); if(size<=3) gauge.progress(); // easy (but not so aquarate) method to show progress relative the parsing of the page... return tc; } return null; } /** * The Browser instance is the default CommandListener for any rendered page. Use setListener method to use your * own custom listener. * @see #setListener(CommandListener) * @see gr.fire.core.CommandListener#commandAction(javax.microedition.lcdui.Command, gr.fire.core.Component) */ public void commandAction(Command command, Component c) { if(command instanceof gr.fire.browser.util.Command) { // only handle known command types gr.fire.browser.util.Command cmd = (gr.fire.browser.util.Command)command; String url = cmd.getUrl(); loadPageAsync(url,HttpConnection.GET,null,null); } } /** * The Browser instance if the default PageListener for loadPageAsync requests. Use the setPageListener method to * use your own custom pageListener * @see PageListener * @see #setPageListener(PageListener) */ public void pageLoadCompleted(String url,String method,Hashtable requestParams, Page page) { if(page!=null) { Container cnt = page.getPageContainer(); String title= page.getPageTitle(); Log.logInfo("Loaded Page ["+url+"]["+title+"]"); if(cnt!=null) { Panel panel = new Panel(cnt,Panel.VERTICAL_SCROLLBAR|Panel.HORIZONTAL_SCROLLBAR,true); panel.setLabel(title); Component current = FireScreen.getScreen().getCurrent(); Command left=null,right=null; if(current!=null) { left = current.getLeftSoftKeyCommand(); right = current.getRightSoftKeyCommand(); } panel.setLeftSoftKeyCommand(left); panel.setRightSoftKeyCommand(right); FireScreen screen = FireScreen.getScreen(); Component last = screen.getCurrent(); if(last!=null) // show a transition animation { panel.setAnimation(new TransitionAnimation(last,panel,TransitionAnimation.TRANSITION_SCROLL|TransitionAnimation.TRANSITION_RIGHT)); } FireScreen.getScreen().setCurrent(panel); panel.setCommandListener(listener); panel.setDragScroll(true); return; } } // Error case. Alert user String t=url; if(url.length()>15) t = url.substring(0,15)+"..."; FireScreen.getScreen().showAlert(Lang.get("Failed to load page")+": "+t,Alert.TYPE_ERROR,Alert.USER_SELECTED_OK,null,null); } /** * This is the asynchronous version of the loadPage{@link #loadPage(String, String, Hashtable, byte[])} method. * It will start a new thread to handle the request and it will send the result Page to the registered PageListener * instead of returning it to the caller. * * @see #loadPage(InputStream, String) * * @param url * @param method * @param requestParameters * @param data */ public void loadPageAsync(final String url,final String method,final Hashtable requestParameters,final byte []data) { Thread th = new Thread() { public void run() { try { Page pageMeta =loadPage(url,method,requestParameters,data); if(pageListener!=null) pageListener.pageLoadCompleted(url,method,requestParameters,pageMeta); else pageLoadCompleted(url,method,requestParameters,pageMeta); // defauly handler if(pageMeta!=null) { if(pageMeta.getRefresh()!=null) { int seconds = pageMeta.getRefreshSeconds(); if(seconds>0) try{Thread.sleep(seconds*1000);}catch(InterruptedException e){} Component current = FireScreen.getScreen().getCurrent(); if((current instanceof Panel && ((Panel)current).getComponent(0)==pageMeta.getPageContainer()) || current==pageMeta.getPageContainer()) // only execute refresh, if the user is still on the same page { loadPage(pageMeta.getRefresh(),HttpConnection.GET,null,null); } else { Log.logWarn("Ignoring refresh to "+pageMeta.getRefresh() ); } } } } catch (Throwable er) { if(pageListener!=null) pageListener.pageLoadFailed(url,method,requestParameters,er); else pageLoadFailed(url,method,requestParameters,er); // defauly handler } } }; th.start(); } public void commandAction(Command cmd, Displayable d) { } /** * The Browser renders each page based on a set width and unbounded height. The viewportWidth is * the width of a rendered page. If the page contains elements that do not fit in the viewpoerWidth * It will increase the width of the resulting page ignoring the viewportWidth. * * @return */ public int getViewportWidth() { if(viewportWidth<=0) return FireScreen.getScreen().getWidth(); return viewportWidth; } /** * Sets the width of the screen that the browser will use to render properly each Page. * @param viewportWidth */ public void setViewportWidth(int viewportWidth) { this.viewportWidth = viewportWidth; } /** * Returns the CommandListener that is set to handle the Browser requests * @return */ public CommandListener getListener() { return listener; } /** * Overides the default listener for link and form events for all rendered pages of this Browser intance. * The default listener is the Browser instance itself. * * @param listener The CommandListener for link and form events. If null then the Browser instance if used (default) */ public void setListener(CommandListener listener) { if(listener==null) listener=this; this.listener = listener; } /** * Returns the HttpClient instance that this Browser instance will use to make Http Requests to http servers. * @return */ public HttpClient getHttpClient() { return httpClient; } /** * Sets the HttpClient of this Browser instance. * @see #getHttpClient() * @param httpClient */ public void setHttpClient(HttpClient httpClient) { this.httpClient = httpClient; } /** * There are different policies on loading the images of a Page.<br/> * - Browser.NO_IMAGES <br/> * - Browser.LOAD_IMAGES<br/> * - Browser.LOAD_IMAGES_ASYNC (default) <br/> * * The load LOAD_IMAGES_ASYNC will skip images with preset width and height (as img tag properties) and will * try to load them after the Page is done loading. This greatly speeds up Page loading. * * @return */ public byte getImageLoadingPolicy() { return imageLoadingPolicy; } /** * @see #getImageLoadingPolicy() * @param imageLoadingPolicy */ public void setImageLoadingPolicy(byte imageLoadingPolicy) { this.imageLoadingPolicy = imageLoadingPolicy; } /** * Returns the PageListener for loadPageAsync requests. The default pagelistener is the Browser. * @see #pageLoadCompleted(String, String, Hashtable, Page) * @see #pageLoadFailed(String, String, Hashtable, Throwable) * @return */ public PageListener getPageListener() { return pageListener; } /** * @see #getPageListener() * @param pageListener */ public void setPageListener(PageListener pageListener) { if(pageListener==null) pageListener=this; this.pageListener = pageListener; } public void pageLoadFailed(String url, String method, Hashtable requestParams, Throwable error) { if(error instanceof OutOfMemoryError) { System.gc(); try { Log.logError("Out of Memory! Request to URL ["+url+"] failed",error); FireScreen.getScreen().showAlert(Lang.get("Could not load page. Out of memory!"),Alert.TYPE_ERROR,Alert.USER_SELECTED_OK,null,null); }catch (OutOfMemoryError e) { System.gc(); } } else { Log.logError("Request to URL ["+url+"] failed",error); FireScreen.getScreen().showAlert(Lang.get("Error loading page. ")+" "+error.getMessage(),Alert.TYPE_ERROR,Alert.USER_SELECTED_OK,null,null); } } public boolean isShowLoadingGauge() { return showLoadingGauge; } public void setShowLoadingGauge(boolean showLoadingGauge) { this.showLoadingGauge = showLoadingGauge; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -