Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

27 December, 2012

You Should Know, Java XML Parsing

As Java developer you should know that:

JAXP, Java API for XML Processing, lets you use any conforming parser implementation in a standard way. The code should be much more portable, and when you realise that a specific parser has grown too old, you can replace it with another without changing a line of your code (if you do it correctly).

    Basically there are three ways of handling XML in a standard way:
  1. SAX This is the simplest API. You read/modify the XML by defining a Handler class that receives the data inside elements/attributtes when the XML gets processed in a serial way. It is faster and simpler if you only plan to read some attributes/elements and/or write some values back (your case).
  2. DOM This method creates an object tree which lets you modify/access it randomly so it is better for complex XML manipulation and handling.
  3. StAX This is in the middle of the path between SAX and DOM. You just write code to pull the data from the parser you are interested in when it is processed.

Forget about propietary APIs such as Jdom or Apache ones (i.e. Apache Xerces XMLSerializer) because will tie you to a specific implementation that can evolve in time or lose backwards compatibility, which will make you change your code in the future when you want to upgrade to a new version of Jdom or whatever parser you use. If you stick to Java standard API (using factories and interfaces) your code will be much more modular and maintenable.

    Code samples
  1. XML parsing using SaxParser with complete code

23 April, 2012

Android issues with WebView.loadData and WebView.getContentHeight

If you tried the suggested code at Stack Overflow on Gingerbread you will see that it still doesn't work when using loadData or loadDataWithBaseURL. The proposed code only works when using the loadUrl - at least it didn't worked on my HTC Desire nor the 2.3.3 Emulator.

In the Andromedev blog you will see one possible solution using the android javascript bridge but I'm not still convinced that this is the best approach to take. Even if you do decide that it's the best approach you will face one bug already reported on Issue 12987 that affects any javascript bridged call on Gingerbread.

Right know I'm developing one pager fragment that uses one webView to display simple html texts and I had disabled the javascript support to try to keep the memory usage low. With the proposed solution I'll have to turn it on again and pray that my user don't use the gingerbread version ...

Well, the point is: IF you are using the WebView.loadUrl AND you are not on gingerbread you don't need to worry, you just need to write a listener - the onPageFinished and all height and width methods will work as expected.

However the code below still doesn't work! The java script bridge call the code in the JavaScriptInterface but the height is not there yet ...

/**
 * To use the webView embedded in another view (not full screen).
 */
public class EmbeddedWebView2 extends WebViewClient {

   private static final String P_MIMETYPE     = "text/html";
   private static final String P_ENCODING     = "utf-8";
   private static final String LOG_TAG        = "EmbeddedWebClientView2";
   private static final String JS_BRIDGE      = "JSB";
   private static final String JS_CONTENTSIZE = "javascript:window." + JS_BRIDGE + ".setContentSize(" + //
                                                    "document.getElementsByTagName('html')[0].scrollHeight," + //
                                                    "document.getElementsByTagName('html')[0].scrollWidth" + //
                                                    ");";

   @Override
   public void onPageFinished(WebView webView, String url) {

      super.onPageFinished(webView, url);

      WebSettings webSettings = webView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      webView.addJavascriptInterface(new JavaScriptInterface(), JS_BRIDGE);
      webView.loadUrl(JS_CONTENTSIZE);
   }

   public static void loadData(WebView webView, String data) {

      webView.setWebViewClient(new EmbeddedWebView2());

      WebSettings webSettings = webView.getSettings();
      webSettings.setSavePassword(false);
      webSettings.setSaveFormData(false);
      webSettings.setJavaScriptEnabled(true);
      webSettings.setUseWideViewPort(false);

      final String dataHtml = "" + data + "";
      webView.loadData(dataHtml, P_MIMETYPE, P_ENCODING);
   }

   public static void loadDataWithBaseURL(WebView webView, String baseUrl, String data) {

      webView.setWebViewClient(new EmbeddedWebView2());

      WebSettings webSettings = webView.getSettings();
      webSettings.setSavePassword(false);
      webSettings.setSaveFormData(false);
      webSettings.setJavaScriptEnabled(true);
      webSettings.setUseWideViewPort(false);

      final String dataHtml = "" + data + "";
      webView.loadDataWithBaseURL(baseUrl, dataHtml, P_MIMETYPE, P_ENCODING, null);
   }

   /**
    * getHeight, getContentHeight and getMeasuredHeight are ONLY updated when
    * using loadUrl that's why we need this guy. 
    * 
    * TODO: wait a fix for ISSUE 12987 on gingerbread.
    */
   public class JavaScriptInterface {

      private int height, width;

      public void getContentSize(String sHeight, String sWidth) {

         if (sHeight != null) {
            width = Integer.parseInt(sHeight);
            Log.d(LOG_TAG, "Result from javascript, height = " + height);
         }
         if (sWidth != null) {
            width = Integer.parseInt(sWidth);
            Log.d(LOG_TAG, "Result from javascript, width = " + width);
         }
      }
   }
}