Android: SAX parser progress monitoring
Asked Answered
G

3

2

I have a SAX DefaultHandler which parses an InputStream. I don't know how many elements are in the XML so I can't count them on endElement or simmilar. I do know the byte length of the InputStream (read from the http header) but I can't find a method to get the current bytewise progress of the parsing process.

Is there a way to get the current progress (i.e. bits processed) of the parsing process?

This is how the DefaultHandler gets called:

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(inputStream, myDefaultHandler);
Giblets answered 16/2, 2011 at 11:29 Comment(0)
T
0

You can do this by writing a FilterInputStream to wrap the existing inputStream. In the read() method of your filter, increment a counter, and provide a getter so that something else can track the current count.

Since the parser may read ahead, this will be approximate, but it's probably the best you can do.

Thermocline answered 16/2, 2011 at 12:37 Comment(4)
Quite a good idea, but how do i access my FilterInputStream from within the parser? Or should i create a timer which calls the getter from somwhere outside and updates the status acordingly?Giblets
Do it from outside, to keep it independent of the parse itself.Thermocline
I tried it this way, but the parser does not increment the counter for quite a time and then suddenly consumes all the InputStream at once. Any other ideas how this could be achieved?Giblets
I guess the parser is reading ahead a lot after a long wait! Can't think of anything better right off, except for trying to debug through what's happening. Add some debug prints in your filter and the handler?Thermocline
U
3

For anyone who is still searching for the answer, try to pass the ProgressBar in the constructor of your derived FilterInputStream:

ProgressFilterInputStream.java

import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;

import android.widget.ProgressBar;
import android.util.Log;


public class ProgressFilterInputStream extends FilterInputStream {

int counter = 0;
ProgressBar progressBar;

public ProgressFilterInputStream(InputStream in, ProgressBar progressBar) {
    super(in);
    this.progressBar = progressBar;
    this.progressBar.setMax(69);  // SET MAX ACCORDIN TO THE DDMS LOGGING MESSAGE
}

@Override
public int read(byte[] buffer, int offset, int count) throws IOException {
    progressBar.setProgress(counter++);
    Log.i("PFIS" , "Counter: " + counter);
    return super.read(buffer, offset, count);
}
}

I used it in an AsyncTask in my

MyActivity.java

final ProgressBar bar_progress = (ProgressBar) findViewById(R.id.bar_progress);
ProgressFilterInputStream pfis = new ProgressFilterInputStream(getAssets().open("DATA.xml") , bar_progress );           
Reader reader = new InputStreamReader(pfis, "UTF-8");
InputSource is = new InputSource(reader);

// create the factory
SAXParserFactory factory = SAXParserFactory.newInstance();

// create a parser
SAXParser parser = factory.newSAXParser();

// create the reader (scanner)
XMLReader xmlreader = parser.getXMLReader();

// instantiate our handler
SaxHandler myHandler = new SaxHandler();

// assign our handler
xmlreader.setContentHandler(myHandler);

// perform the synchronous parse
xmlreader.parse(is);
Updo answered 23/3, 2013 at 12:5 Comment(0)
G
1

Extended from lx222 answer, I implemented for AsyncTask, ProgressDialog. Late but I hope it may useful with some one :D

public class OpenXMLFileOperator extends AsyncTask<Void, Integer, Void>{
    private ProgressDialog progressDialog;
    private String filePath;
    private Context context;
    public OpenFileOperator(Context context,String filePath){
        this.context = context;
        this.filePath = filePath;
    }

    @Override
    protected Void doInBackground(Void... arg0) {                               
        try {                               
            if (filePath != null) {

                File file = new File(filePath);
                if (file.exists()) {    
                    InputStream stream = new FileInputStream(file);
                    ProgressFilterInputStream pfis = new ProgressFilterInputStream(stream,file.length());
                    Reader reader = new InputStreamReader(pfis, "UTF-8");

                    YourXMLHandle yourHandle = new YourXMLHandle();                 
                    android.util.Xml.parse(reader,yourHandle);

                    stream.close();
                    reader.close();
                    pfis.close();          
                }                
            }
        }catch (Exception e) {
            if (BuildConfig.DEBUG) Log.e(TAG, "Exception", e);
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        try{
            progressDialog = new ProgressDialog(context);
            progressDialog.setMessage("Loading file");
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(false);
            progressDialog.setMax(100);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);           
            progressDialog.show();
        }catch(Exception e){
            if (BuildConfig.DEBUG) Log.e(TAG,"onPostExecute",e);
        }

        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        try{
            if (progressDialog!=null && progressDialog.isShowing()){
                progressDialog.dismiss();
            }
        }catch(Exception e){
            if (BuildConfig.DEBUG) Log.e(TAG,"onPostExecute",e);
        }
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(Void result) { 
        try{
            if (progressDialog!=null && progressDialog.isShowing()){
                progressDialog.dismiss();
            }
        }catch(Exception e){
            if (BuildConfig.DEBUG) Log.e(TAG,"onPostExecute",e);
        }

        //TODO: to do something after file loaded
        super.onPostExecute(result);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {        
        int progress = values[0];
        if (progressDialog!=null) {            
            progressDialog.setProgress(progress);
        }
        super.onProgressUpdate(values);
    }

    private class ProgressFilterInputStream extends FilterInputStream {
        private int counter = 0;
        private long fileLength;
        private int maximum = 100;
        public ProgressFilterInputStream(InputStream in, long fileLength) {
            super(in);
            this.fileLength = fileLength;
        }

        @Override
        public int read(byte[] buffer, int offset, int count) throws IOException {
            if (counter==0){
                maximum = (int)(fileLength/buffer.length);
            }
            publishProgress((counter++)*100/maximum);
            return super.read(buffer, offset, count);
        }
    }
}
Godard answered 2/12, 2014 at 15:1 Comment(0)
T
0

You can do this by writing a FilterInputStream to wrap the existing inputStream. In the read() method of your filter, increment a counter, and provide a getter so that something else can track the current count.

Since the parser may read ahead, this will be approximate, but it's probably the best you can do.

Thermocline answered 16/2, 2011 at 12:37 Comment(4)
Quite a good idea, but how do i access my FilterInputStream from within the parser? Or should i create a timer which calls the getter from somwhere outside and updates the status acordingly?Giblets
Do it from outside, to keep it independent of the parse itself.Thermocline
I tried it this way, but the parser does not increment the counter for quite a time and then suddenly consumes all the InputStream at once. Any other ideas how this could be achieved?Giblets
I guess the parser is reading ahead a lot after a long wait! Can't think of anything better right off, except for trying to debug through what's happening. Add some debug prints in your filter and the handler?Thermocline

© 2022 - 2024 — McMap. All rights reserved.