<commit_before>/**
 * 
 */
package org.pentaho.ui.xul.impl;

import groovy.lang.GroovyClassLoader;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.ui.xul.XulComponent;
import org.pentaho.ui.xul.XulDataModel;
import org.pentaho.ui.xul.XulDomContainer;
import org.pentaho.ui.xul.XulEventSource;
import org.pentaho.ui.xul.XulException;
import org.pentaho.ui.xul.XulLoader;
import org.pentaho.ui.xul.XulOverlay;
import org.pentaho.ui.xul.XulPerspective;
import org.pentaho.ui.xul.binding.Binding;
import org.pentaho.ui.xul.binding.BindingContext;
import org.pentaho.ui.xul.binding.DefaultBinding;
import org.pentaho.ui.xul.containers.XulRoot;
import org.pentaho.ui.xul.dom.Document;

/**
 * @author nbaker
 *
 */
public abstract class AbstractXulDomContainer implements XulDomContainer {

  private static final Log logger = LogFactory.getLog(AbstractXulDomContainer.class);
  
  protected XulLoader xulLoader;
  protected HashMap<String, XulEventHandler> eventHandlers;
  protected HashMap<String, XulDataModel> models;
  protected Map<String, XulPerspective> perspectives = new HashMap<String, XulPerspective>();
  private XulPerspective currentOverlay;
  private List<Object> resourceBundleList;
  
  protected BindingContext bindings;
  
  private Object parentContext;
  
  private boolean initialized;

	public AbstractXulDomContainer() {
    eventHandlers = new HashMap<String, XulEventHandler>();
  }
  
  public AbstractXulDomContainer(XulLoader xulLoader) {
    this();
    this.xulLoader = xulLoader;
  }
  
  
  public XulLoader getXulLoader(){
  	return xulLoader;
  }
  
  private void addGroovyHandler(String id, String location) throws XulException{
    try{
    
      location = location.replace('.', '/').replace("/groovy", ".groovy");
      
      InputStream in = getClass().getClassLoader().getResourceAsStream(
          location
      );
      
      GroovyClassLoader gcl = new GroovyClassLoader();
      Class clazz = gcl.parseClass(in);
      Object script = clazz.newInstance();
      XulEventHandler groovyHandler = (XulEventHandler) script;

      groovyHandler.setXulDomContainer(this);
      eventHandlers.put(id, groovyHandler);
      
    } catch(Exception e){
      throw new XulException(e);
    }
  }
  
  @Deprecated
  public void addEventHandler(String id, String eventClassName) throws XulException{
   
    // Allow overrides of eventHandlers
    //if(eventHandlers.containsKey(id)){ //if already registered
    //  return;
    //}
    
    if(eventClassName.indexOf("groovy") > -1){
      addGroovyHandler(id, eventClassName);
      return;
    }
    
    try{
      Class cls = Class.forName(eventClassName);
      AbstractXulEventHandler eventHandler = (AbstractXulEventHandler) cls.newInstance();
      eventHandler.setXulDomContainer(this);
      eventHandlers.put(id, eventHandler);
      
    } catch(ClassNotFoundException e){
    	logger.error("Event Handler Class Not Found", e);
    	throw new XulException(e);
    } catch(Exception e){
    	logger.error("Error with Backing class creation", e);
    	throw new XulException(e);
    }
  }
  
  public XulEventHandler getEventHandler(String key) throws XulException{
    if(eventHandlers.containsKey(key)){
      return eventHandlers.get(key);
    } else {
      throw new XulException(String.format("Could not find Event Handler with the key : %s", key));
    }
  }
  
  public void initialize(){
    XulRoot rootEle = (XulRoot) this.getDocumentRoot().getRootElement();
    logger.debug("onload: "+ rootEle.getOnload());
    String onLoad = rootEle.getOnload();
    if(onLoad != null){
      if(onLoad.indexOf(',') > 0){ //comma separated list of onload calls
        String[] loadCalls = onLoad.split(",");
        for(String load : loadCalls){
        	load = load.trim();
          try{
        		invoke(load, new Object[]{});
        	} catch(XulException e){
        		logger.error("Error calling onLoad event: "+load,e);
        	}
        }
      } else { //single onLoad event
        try{
          invoke(rootEle.getOnload(), new Object[]{});
        } catch(XulException e){
          logger.error("Error calling onLoad event: "+onLoad,e);
        }
      }
    }
    initialized = true;
  }
  
  
  
  public boolean isInitialized() {
    return initialized;
  }

  public Map<String, XulEventHandler> getEventHandlers(){
  	//if(this.eventHandlers.size() > 0){
  		return this.eventHandlers;
  	//} else {
  	  //called when someone merges a fragment container into another container
  	  
  	  /*================= Pretty sure this is never called ==================*/
  	  <commit_msg>[PAD-62] Checking onload strings vs empty before attempting to load.<commit_after>/**
 * 
 */
package org.pentaho.ui.xul.impl;

import groovy.lang.GroovyClassLoader;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.ui.xul.XulComponent;
import org.pentaho.ui.xul.XulDataModel;
import org.pentaho.ui.xul.XulDomContainer;
import org.pentaho.ui.xul.XulEventSource;
import org.pentaho.ui.xul.XulException;
import org.pentaho.ui.xul.XulLoader;
import org.pentaho.ui.xul.XulOverlay;
import org.pentaho.ui.xul.XulPerspective;
import org.pentaho.ui.xul.binding.Binding;
import org.pentaho.ui.xul.binding.BindingContext;
import org.pentaho.ui.xul.binding.DefaultBinding;
import org.pentaho.ui.xul.containers.XulRoot;
import org.pentaho.ui.xul.dom.Document;
import org.apache.commons.lang.StringUtils;


/**
 * @author nbaker
 *
 */
public abstract class AbstractXulDomContainer implements XulDomContainer {

  private static final Log logger = LogFactory.getLog(AbstractXulDomContainer.class);
  
  protected XulLoader xulLoader;
  protected HashMap<String, XulEventHandler> eventHandlers;
  protected HashMap<String, XulDataModel> models;
  protected Map<String, XulPerspective> perspectives = new HashMap<String, XulPerspective>();
  private XulPerspective currentOverlay;
  private List<Object> resourceBundleList;
  
  protected BindingContext bindings;
  
  private Object parentContext;
  
  private boolean initialized;

	public AbstractXulDomContainer() {
    eventHandlers = new HashMap<String, XulEventHandler>();
  }
  
  public AbstractXulDomContainer(XulLoader xulLoader) {
    this();
    this.xulLoader = xulLoader;
  }
  
  
  public XulLoader getXulLoader(){
  	return xulLoader;
  }
  
  private void addGroovyHandler(String id, String location) throws XulException{
    try{
    
      location = location.replace('.', '/').replace("/groovy", ".groovy");
      
      InputStream in = getClass().getClassLoader().getResourceAsStream(
          location
      );
      
      GroovyClassLoader gcl = new GroovyClassLoader();
      Class clazz = gcl.parseClass(in);
      Object script = clazz.newInstance();
      XulEventHandler groovyHandler = (XulEventHandler) script;

      groovyHandler.setXulDomContainer(this);
      eventHandlers.put(id, groovyHandler);
      
    } catch(Exception e){
      throw new XulException(e);
    }
  }
  
  @Deprecated
  public void addEventHandler(String id, String eventClassName) throws XulException{
   
    // Allow overrides of eventHandlers
    //if(eventHandlers.containsKey(id)){ //if already registered
    //  return;
    //}
    
    if(eventClassName.indexOf("groovy") > -1){
      addGroovyHandler(id, eventClassName);
      return;
    }
    
    try{
      Class cls = Class.forName(eventClassName);
      AbstractXulEventHandler eventHandler = (AbstractXulEventHandler) cls.newInstance();
      eventHandler.setXulDomContainer(this);
      eventHandlers.put(id, eventHandler);
      
    } catch(ClassNotFoundException e){
    	logger.error("Event Handler Class Not Found", e);
    	throw new XulException(e);
    } catch(Exception e){
    	logger.error("Error with Backing class creation", e);
    	throw new XulException(e);
    }
  }
  
  public XulEventHandler getEventHandler(String key) throws XulException{
    if(eventHandlers.containsKey(key)){
      return eventHandlers.get(key);
    } else {
      throw new XulException(String.format("Could not find Event Handler with the key : %s", key));
    }
  }
  
  public void initialize(){
    XulRoot rootEle = (XulRoot) this.getDocumentRoot().getRootElement();
    logger.debug("onload: "+ rootEle.getOnload());
    String onLoad = rootEle.getOnload();
    if(onLoad != null){
      if(onLoad.indexOf(',') > 0){ //comma separated list of onload calls
        String[] loadCalls = onLoad.split(",");
        for(String load : loadCalls){
        	load = load.trim();
        	if(StringUtils.isNotEmpty(load)){
        	  try{
          		invoke(load, new Object[]{});
          	} catch(XulException e){
          		logger.error("Error calling onLoad event: "+load,e);
          	}
        	}
        }
      } else { //single onLoad event
        if(StringUtils.isNotEmpty(rootEle.getOnload())){
          try{
            invoke(rootEle.getOnload(), new Object[]{});
          } catch(XulException e){
            logger.error("Error calling onLoad event: "+onLoad,e);
          }
        }
      }
    }
    initialized = true;
  }
  
  
  
  public boolean isInitialized() {
    return initialized;
  }

  public Map<String, XulEventHandler> getEventHandlers(){
  	//if(this.eventHandlers.size() > 0){
  		return this.eventHandlers;
  	//} else {
  	  //called when someone merges a fragment container into another container
  	  
  	  /*================= Pretty sure this is never called ==================*/
  	  
{'fraction_non_alphanumeric': 0.1176110884631064, 'fraction_numerical': 0.0008153281695882593, 'mean_word_length': 3.2720940356987374, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '377513', 'n_tokens_mistral': 3059, 'n_tokens_neox': 3027, 'n_words': 641}