[ 
https://issues.apache.org/jira/browse/XERCESJ-1429?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13051579#comment-13051579
 ] 

Thiwanka Somasiri commented on XERCESJ-1429:
--------------------------------------------

Hi Michael,

     I have modified the class to add and remove listeners & made the changes 
you have asked. I used a similar way to add and remove listeners as it is in 
DocumentImpl class implementation. The modified class is as follows.

Note: When a user tries to use the AsyncLSParser, he can check whether the 
parser is busy using the getBusy() method. But, if the user tries to access the 
parse() method without checking the busy flag, it should not be allowed. So I 
suggest to throw an exception when trying to set the busy flag(when invoking 
setBusyFlag() in parse() method) in to "true" where it is already set to 
"true". Hope your opinion on this.

public class AsynchronousDOMParserImpl extends DOMParserImpl implements 
EventTarget{

    //Create the HashSet to hold the event listeners that the users are 
subscribed to
    private Hashtable eventListeners;
    private DOMParserImpl parser;
    private volatile boolean fBusy = false;//TODO make this volatile to avoid 
other threads using the async parser when busy
    //private LSInput ls;
    
    //Call the super class constructors - there are four constructors in the 
DOMParserImpl class
    
    public AsynchronousDOMParserImpl(String configuration, String schemaType){
        super(configuration, schemaType);   //Check whether these super() 
should be removed or not
        parser = new DOMParserImpl(configuration, schemaType);
    }
    
    public AsynchronousDOMParserImpl(XMLParserConfiguration config){
        super(config);
        parser = new DOMParserImpl(config);
    }
    
    public AsynchronousDOMParserImpl(SymbolTable symbolTable){
        super(symbolTable);
        parser = new DOMParserImpl(symbolTable);
    }
    
    public AsynchronousDOMParserImpl(SymbolTable symbolTable, XMLGrammarPool 
grammarPool){
        super(symbolTable, grammarPool);
        parser = new DOMParserImpl(symbolTable, grammarPool);
    }
    
    public void addEventListener(String type, EventListener listener, boolean 
useCapture){
        if (type == null || type.length() == 0 || listener == null)
            return;
        
        //Remove the previous entry for the same EventListener
        //removeEventListener(type, listener, useCapture);
        
        Vector listeners = getEventListeners(type);
        
        if(listeners  == null) {
            listeners  = new Vector();
            setEventListeners(type, listeners);
        }
        
        listeners .addElement(new ListenerHolder(listener, useCapture));
    }
    
    private Vector getEventListeners(String type){
        if (eventListeners == null) {
            return null;
        }
        return (Vector) eventListeners.get(type);
    }
    
    private void setEventListeners(String type, Vector listeners){
        if (eventListeners == null) {
            eventListeners = new Hashtable();
        }             
        eventListeners.put(type, listeners);                
    }
    
    public void removeEventListener(String type, EventListener listener, 
boolean useCapture){
        if (type == null || type.length() == 0 || listener == null)
            return;
        
        //If the vector is null for a particular type it will return
        Vector listeners = getEventListeners(type);
        if (listeners == null)
            return;
        
        //If the vector is not null, compare the listener and useCapture and 
remove the listener
        for(int i = 0; i < listeners.size(); i++){
            ListenerHolder lh = (ListenerHolder) listeners.get(i);
            if(lh.useCapture == useCapture && lh.el == listener){
                listeners.removeElementAt(i);
                //TODO assign null value in to hashtable when the vector is 
empty
                break;
            }
        }
        
    }
    
    public boolean dispatchEvent(Event evt){
        return false;//TODO change the return value
    }
    
    public Document parse(LSInput is){        
        final LSInput ls = is;
        setBusyFlag();        
        
        //____________________________________
        Runnable target = new Runnable() {
            
            @Override
            public void run() {
                try{
                    parser.parse(ls);                    
                }catch(LSException e){ 
                    e.printStackTrace();
                }finally{
                    resetBusyFlag();
                }                
            }
        };                
        //____________________________________
        
        //AsynchronousDOMParserImpl.AsyncInnerParser target = new 
AsynchronousDOMParserImpl.AsyncInnerParser();
        Thread t = new Thread(target);
        t.start();                        
        return null;
        
    }
    
    public void setBusyFlag(){
        fBusy = true;
    }    
    
    public void resetBusyFlag(){
        fBusy = false;
    }
    
    public boolean getBusy () {
        return fBusy;
    }       
    
    /*class AsyncInnerParser implements Runnable{
        public void run(){
            try{
                parser.parse(ls);
            }catch(LSException e){ 
                e.printStackTrace();
            }finally{
                resetBusyFlag();
            }
        }
    }*/
    
    //This structure holds the EventListner associated with it
    class ListenerHolder{
                
        EventListener el;
        boolean useCapture;
        
        ListenerHolder(EventListener el, boolean useCapture) {
            this.el = el;
            this.useCapture = useCapture;            
        }
    }       
}

> [GSoC]: Asynchronous LSParser and parseWithContext
> --------------------------------------------------
>
>                 Key: XERCESJ-1429
>                 URL: https://issues.apache.org/jira/browse/XERCESJ-1429
>             Project: Xerces2-J
>          Issue Type: New Feature
>          Components: DOM (Level 3 Load & Save)
>    Affects Versions: 2.9.1
>            Reporter: Michael Glavassevich
>              Labels: gsoc2011
>
> The goal of this project is to complete the implementation of the DOM Level 3 
> LSParser. Though Xerces has a functional LSParser, there are a couple parts 
> of the spec which still need to be implemented. This includes an asynchronous 
> [1] version which returns from the parse method immediately and builds the 
> DOM tree on another thread as well as parseWithContext [2] which allows a 
> document fragment to be parsed and attached to an existing DOM.
> Possible Mentors: Michael Glavassevich
> [1] 
> http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/load-save.html#LS-LSParser
> [2] 
> http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/load-save.html#LS-LSParser-parseWithContext

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscr...@xerces.apache.org
For additional commands, e-mail: j-dev-h...@xerces.apache.org

Reply via email to