<commit_before>/***************************** BEGIN LICENSE BLOCK ***************************

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/MPL-1.1.html
 
 Software distributed under the License is distributed on an "AS IS" basis,
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 for the specific language governing rights and limitations under the License.
 
 The Original Code is the "SensorML DataProcessing Engine".
 
 The Initial Developer of the Original Code is the
 University of Alabama in Huntsville (UAH).
 Portions created by the Initial Developer are Copyright (C) 2006
 the Initial Developer. All Rights Reserved.
 
 Contributor(s): 
    <NAME> <<EMAIL>>
 
******************************* END LICENSE BLOCK ***************************/

package org.vast.data;

import java.util.Stack;
import org.vast.cdm.common.CDMException;
import org.vast.cdm.common.DataComponent;
import org.vast.cdm.common.DataEncoding;
import org.vast.cdm.common.DataHandler;
import org.vast.cdm.common.ErrorHandler;
import org.vast.cdm.common.RawDataHandler;


public abstract class DataIterator
{
	protected DataHandler dataHandler;
	protected RawDataHandler rawHandler;
	protected ErrorHandler errorHandler;
	protected AbstractDataComponent dataComponents;
	protected DataEncoding dataEncoding;
	protected Stack<Record> componentStack;
	protected Record currentComponent;
    protected boolean newBlock = true;
		
	protected class Record
    {
        public AbstractDataComponent parent;
        public int index;
        public int count;

        public Record(AbstractDataComponent parent)
        {
            this.parent = parent;
            this.count = parent.getComponentCount();
            this.index = 0;
        }
    }
    
    
	public DataIterator()
	{
		this.componentStack = new Stack<Record>();
	}
	
	
	protected abstract void processAtom(DataValue scalarInfo) throws CDMException;
	
	
	/**
	 * TODO nextInfo method description
	 * @return
	 */
	protected void processNextElement() throws CDMException
	{
        if (newBlock)
        {
            this.reset();
            newBlock = false;
        }
        
        // send begining of block events
    	if (currentComponent.index == 0 && dataHandler != null)
    	{
    		if (componentStack.isEmpty())
				dataHandler.startData(dataComponents);
			else
				dataHandler.startDataBlock(currentComponent.parent);
    	}
        
    	// update size of variable size arrays
        if (currentComponent.parent instanceof DataArray)
            ((DataArray)currentComponent.parent).updateSize();
        
    	// now get next child
    	AbstractDataComponent next = (AbstractDataComponent)currentComponent.parent.getComponent(currentComponent.index);
        currentComponent.index++;
        
        // if child is not a DataValue, go in !!
        if (!(next instanceof DataValue))
        {
            // update size of variable size arrays
            //if (next instanceof DataArray)
            //    ((DataArray)next).updateSize();
            
            componentStack.push(currentComponent);
        	currentComponent = new Record(next);
            processNextElement();
        }
        
        // otherwise parse element
        else
        {
            if (dataHandler != null)
                dataHandler.beginDataAtom(next);
            
            processAtom((DataValue)next);
            
            if (dataHandler != null)
                dataHandler.endDataAtom(next, next.getData());
        }
        
        // if at the end of previous record
        while(currentComponent.index >= currentComponent.count)
    	{
        	if (!componentStack.isEmpty())
        	{
                if (dataHandler != null)
                    dataHandler.endDataBlock(currentComponent.parent, currentComponent.parent.getData());
        		
                currentComponent = componentStack.pop();
        	}
        	else
        	{
                if (dataHandler != null)
                    dataHandler.endData(dataComponents, dataComponents.getData());
				
                newBlock = true;<commit_msg>Fixed bug in DataIterator where endData was called twice causing problems in SOS_Process among others<commit_after>/***************************** BEGIN LICENSE BLOCK ***************************

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/MPL-1.1.html
 
 Software distributed under the License is distributed on an "AS IS" basis,
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 for the specific language governing rights and limitations under the License.
 
 The Original Code is the "SensorML DataProcessing Engine".
 
 The Initial Developer of the Original Code is the
 University of Alabama in Huntsville (UAH).
 Portions created by the Initial Developer are Copyright (C) 2006
 the Initial Developer. All Rights Reserved.
 
 Contributor(s): 
    <NAME> <<EMAIL>>
 
******************************* END LICENSE BLOCK ***************************/

package org.vast.data;

import java.util.Stack;
import org.vast.cdm.common.CDMException;
import org.vast.cdm.common.DataComponent;
import org.vast.cdm.common.DataEncoding;
import org.vast.cdm.common.DataHandler;
import org.vast.cdm.common.ErrorHandler;
import org.vast.cdm.common.RawDataHandler;


public abstract class DataIterator
{
	protected DataHandler dataHandler;
	protected RawDataHandler rawHandler;
	protected ErrorHandler errorHandler;
	protected AbstractDataComponent dataComponents;
	protected DataEncoding dataEncoding;
	protected Stack<Record> componentStack;
	protected Record currentComponent;
    protected boolean newBlock = true;
		
	protected class Record
    {
        public AbstractDataComponent parent;
        public int index;
        public int count;

        public Record(AbstractDataComponent parent)
        {
            this.parent = parent;
            this.count = parent.getComponentCount();
            this.index = 0;
        }
    }
    
    
	public DataIterator()
	{
		this.componentStack = new Stack<Record>();
	}
	
	
	protected abstract void processAtom(DataValue scalarInfo) throws CDMException;
	
	
	/**
	 * TODO nextInfo method description
	 * @return
	 */
	protected void processNextElement() throws CDMException
	{
        if (newBlock)
        {
            this.reset();
            newBlock = false;
        }
        
        // send begining of block events
    	if (currentComponent.index == 0 && dataHandler != null)
    	{
    		if (componentStack.isEmpty())
				dataHandler.startData(dataComponents);
			else
				dataHandler.startDataBlock(currentComponent.parent);
    	}
        
    	// update size of variable size arrays
        if (currentComponent.parent instanceof DataArray)
            ((DataArray)currentComponent.parent).updateSize();
        
    	// now get next child
    	AbstractDataComponent next = (AbstractDataComponent)currentComponent.parent.getComponent(currentComponent.index);
        currentComponent.index++;
        
        // if child is not a DataValue, go in !!
        if (!(next instanceof DataValue))
        {
            // update size of variable size arrays
            //if (next instanceof DataArray)
            //    ((DataArray)next).updateSize();
            
            componentStack.push(currentComponent);
        	currentComponent = new Record(next);
            processNextElement();
        }
        
        // otherwise parse element
        else
        {
            if (dataHandler != null)
                dataHandler.beginDataAtom(next);
            
            processAtom((DataValue)next);
            
            if (dataHandler != null)
                dataHandler.endDataAtom(next, next.getData());
        }
        
        // if at the end of previous record
        while(currentComponent != null && currentComponent.index >= currentComponent.count)
    	{
        	if (!componentStack.isEmpty())
        	{
                if (dataHandler != null)
                    dataHandler.endDataBlock(currentComponent.parent, currentComponent.parent.getData());
        		
                currentComponent = componentStack.pop();
        	}
        	else
        	{
                if (dataHandler != null)
                    dataHandler.endData(dataComponents, dataComponents.getData());
				
                newBlock = true;
                currentComponent = null;
{'fraction_non_alphanumeric': 0.10117620189562636, 'fraction_numerical': 0.002283887175973507, 'mean_word_length': 2.4291307752545026, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5983520', 'n_tokens_mistral': 2239, 'n_tokens_neox': 2100, 'n_words': 651}