<commit_before>package nl.hsleiden.tags;

import javax.servlet.http.HttpServletRequest;

import org.hippoecm.hst.configuration.hosting.Mount;
import org.hippoecm.hst.configuration.site.HstSite;
import org.hippoecm.hst.configuration.sitemenu.HstSiteMenuConfiguration;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.request.HstRequestContext;
import org.hippoecm.hst.core.sitemenu.EditableMenu;
import org.hippoecm.hst.core.sitemenu.EditableMenuItem;
import org.hippoecm.hst.core.sitemenu.HstSiteMenuImpl;

public class MenuFunctions {

    private MenuFunctions() {
    }

    public static EditableMenuItem getTopMenuItem(EditableMenu menu, Integer level) {
        EditableMenuItem result = null;
        Integer depthLevel = level;
        
        for (EditableMenuItem menuItem : menu.getMenuItems()) {
            if(menuItem!=null && menuItem.isExpanded()){
                while(depthLevel!=0){
                    depthLevel--;
                    menuItem = recurseMenuItems(menuItem);
                }
                result = menuItem;
            }
        }        
        return result;
    }
    
    private static EditableMenuItem recurseMenuItems(EditableMenuItem menuItem) {
        EditableMenuItem result = null;
        if(menuItem!=null){            
            for (EditableMenuItem child : menuItem.getChildMenuItems()) {
                if(child.isExpanded()){
                    result = child;
                }
            }
        }
        if(result==null){
            result = menuItem;
        }
        return result;
    }
    
    public static EditableMenuItem getMenuItem(HttpServletRequest req, EditableMenuItem originalMenuItem){
        EditableMenuItem result = originalMenuItem;
        
        String sitemenuConfigParameter = ParametersFunctions.getSitemenuConfigParameter(originalMenuItem, "useMainMenuConfig");
        
        if(sitemenuConfigParameter!=null && "true".equalsIgnoreCase(sitemenuConfigParameter)){
            HstRequest request = (HstRequest) req;
            
            EditableMenu mainMenu = getSiteMainMenu(request);
            
            String originalMenuItemName = originalMenuItem.getName();
            for (EditableMenuItem menuItem : mainMenu.getMenuItems()) {
                if(originalMenuItemName.equals(menuItem.getName())){
                    result = menuItem;
                    break;
                }
            }
        }

        return result;
    }
    
    private static EditableMenu getSiteMainMenu(HstRequest request) {
        HstRequestContext requestContext = request.getRequestContext();
        Mount mount = requestContext.getMount("hsl");
        HstSite hstSite = mount.getHstSite();
        HstSiteMenuConfiguration siteMenuConfiguration = hstSite
                .getSiteMenusConfiguration().getSiteMenuConfiguration("main");
        
        return new HstSiteMenuImpl(null, siteMenuConfiguration, requestContext).getEditableMenu();
    }
}<commit_msg>null check was added to MenuFunctions.java<commit_after>package nl.hsleiden.tags;

import javax.servlet.http.HttpServletRequest;

import org.hippoecm.hst.configuration.hosting.Mount;
import org.hippoecm.hst.configuration.site.HstSite;
import org.hippoecm.hst.configuration.sitemenu.HstSiteMenuConfiguration;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.request.HstRequestContext;
import org.hippoecm.hst.core.sitemenu.EditableMenu;
import org.hippoecm.hst.core.sitemenu.EditableMenuItem;
import org.hippoecm.hst.core.sitemenu.HstSiteMenuImpl;

public class MenuFunctions {

	private MenuFunctions() {
	}

	public static EditableMenuItem getTopMenuItem(EditableMenu menu,
			Integer level) {
		EditableMenuItem result = null;
		Integer depthLevel = level;
		if (menu != null) {
			for (EditableMenuItem menuItem : menu.getMenuItems()) {
				if (menuItem != null && menuItem.isExpanded()) {
					while (depthLevel != 0) {
						depthLevel--;
						menuItem = recurseMenuItems(menuItem);
					}
					result = menuItem;
				}
			}
		}
		return result;

	}

	private static EditableMenuItem recurseMenuItems(EditableMenuItem menuItem) {
		EditableMenuItem result = null;
		if (menuItem != null) {
			for (EditableMenuItem child : menuItem.getChildMenuItems()) {
				if (child.isExpanded()) {
					result = child;
				}
			}
		}
		if (result == null) {
			result = menuItem;
		}
		return result;
	}

	public static EditableMenuItem getMenuItem(HttpServletRequest req,
			EditableMenuItem originalMenuItem) {
		EditableMenuItem result = originalMenuItem;

		String sitemenuConfigParameter = ParametersFunctions
				.getSitemenuConfigParameter(originalMenuItem,
						"useMainMenuConfig");

		if (sitemenuConfigParameter != null
				&& "true".equalsIgnoreCase(sitemenuConfigParameter)) {
			HstRequest request = (HstRequest) req;

			EditableMenu mainMenu = getSiteMainMenu(request);

			String originalMenuItemName = originalMenuItem.getName();
			for (EditableMenuItem menuItem : mainMenu.getMenuItems()) {
				if (originalMenuItemName.equals(menuItem.getName())) {
					result = menuItem;
					break;
				}
			}
		}

		return result;
	}

	private static EditableMenu getSiteMainMenu(HstRequest request) {
		HstRequestContext requestContext = request.getRequestContext();
		Mount mount = requestContext.getMount("hsl");
		HstSite hstSite = mount.getHstSite();
		HstSiteMenuConfiguration siteMenuConfiguration = hstSite
				.getSiteMenusConfiguration().getSiteMenuConfiguration("main");

		return new HstSiteMenuImpl(null, siteMenuConfiguration, requestContext)
				.getEditableMenu();
	}
}
{'fraction_non_alphanumeric': 0.08940632776395308, 'fraction_numerical': 0.00035549235691432633, 'mean_word_length': 3.1072992700729927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5996421', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1591, 'n_words': 286}