/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  Santhosh Kumar T <santhosh.tekuri@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package jlibs.xml.sax.async;

import jlibs.xml.sax.SAXDelegate;
import org.xml.sax.SAXException;

import java.util.ArrayDeque;

/**
 * @author Santhosh Kumar T
 */
class Elements{
    private final Namespaces namespaces;
    private final Attributes attributes;
    private final SAXDelegate handler;

    public Elements(SAXDelegate handler, Namespaces namespaces, Attributes attributes){
        this.handler = handler;
        this.namespaces = namespaces;
        this.attributes = attributes;
    }

    private final ArrayDeque<String> uris = new ArrayDeque<String>();
    private final ArrayDeque<String> localNames = new ArrayDeque<String>();
    private final ArrayDeque<String> names = new ArrayDeque<String>();

    public void reset(){
        uris.clear();
        localNames.clear();
        names.clear();
    }

    public String currentElementName(){
        return names.peekFirst();
    }
    
    public void push1(QName qname){
        uris.addFirst(qname.prefix); // replaced with actual uri in push2()
        localNames.addFirst(qname.localName);
        names.addFirst(qname.name);

        namespaces.push();
        attributes.reset();
    }

    public String push2() throws SAXException{
        String name = names.peekFirst();
        String error = attributes.fixAttributes(name);
        if(error!=null)
            return error;

        String prefix = uris.pollFirst();
        String uri = namespaces.getNamespaceURI(prefix);
        if(uri==null)
            return "Unbound prefix: "+prefix;
        uris.addFirst(uri);
        
        handler.startElement(uri, localNames.peekFirst(), name, attributes.get());

        return null;
    }

    public void pop() throws SAXException{
        handler.endElement(uris.pollFirst(), localNames.pollFirst(), names.pollFirst());
        namespaces.pop();
    }
}

@@ -746,21 +746,17 @@
 il.Array
-Deque
+s
 ;%0A%0A/**%0A 
@@ -1143,140 +1143,41 @@
 ate 
-final ArrayDeque%3CString%3E uris = new ArrayDeque%3CString%3E();%0A    private final ArrayDeque%3CString%3E localNames = new ArrayDeque%3CString%3E()
+String stack%5B%5D = new String%5B15*3%5D
 ;%0A  
@@ -1190,164 +1190,64 @@
 ate 
-f
 in
-al ArrayDeque%3CString%3E names = new ArrayDeque%3CString%3E();%0A%0A    public void reset()%7B%0A        uris.clear();%0A        localNames.clear();%0A        names.clear()
+t free = 0;%0A%0A    public void reset()%7B%0A        free = 0
 ;%0A  
@@ -1306,33 +1306,29 @@
  return 
-names.peekFirst()
+stack%5Bfree-1%5D
 ;%0A    %7D%0A
@@ -1376,30 +1376,131 @@
         
-uris.addFirst(
+free += 3;%0A        if(free%3Estack.length)%0A            stack = Arrays.copyOf(stack, free*2);%0A        stack%5Bfree-3%5D = 
 qname.pr
@@ -1503,17 +1503,16 @@
 e.prefix
-)
 ; // rep
@@ -1556,28 +1556,24 @@
     
-localNames.addFirst(
+stack%5Bfree-2%5D = 
 qnam
@@ -1583,17 +1583,16 @@
 ocalName
-)
 ;%0A      
@@ -1597,23 +1597,24 @@
     
-names.addFirst(
+stack%5Bfree-1%5D = 
 qnam
@@ -1619,17 +1619,16 @@
 ame.name
-)
 ;%0A%0A     
@@ -1757,25 +1757,21 @@
 e = 
-names.peekFirst()
+stack%5Bfree-1%5D
 ;%0A  
@@ -1898,32 +1898,29 @@
 refix = 
-uris.pollFirst()
+stack%5Bfree-3%5D
 ;%0A      
@@ -2050,36 +2050,29 @@
     
-uris.addFirst(
+stack%5Bfree-3%5D = 
 uri
-)
 ;%0A
-        
 %0A   
@@ -2106,30 +2106,21 @@
 ri, 
-localNames.peekFirst()
+stack%5Bfree-2%5D
 , na
@@ -2245,68 +2245,71 @@
 ent(
-uris.pollFirst(), localNames.pollFirst(), names.pollFirst())
+stack%5Bfree-3%5D, stack%5Bfree-2%5D, stack%5Bfree-1%5D);%0A        free -= 3
 ;%0A  

use single string array to maintain stack of uris, localnames and names