<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2008-2009 The eXist Project
 *  http://exist-db.org
 *  
 *  This program 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
 *  of the License, or (at your option) any later version.
 *  
 *  This program 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.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xslt.expression;

import org.exist.interpreter.ContextAtExist;
import org.exist.memtree.MemTreeBuilder;
import org.exist.xquery.AnalyzeContextInfo;
import org.exist.xquery.AnyNodeTest;
import org.exist.xquery.Constants;
import org.exist.xquery.Expression;
import org.exist.xquery.LocationStep;
import org.exist.xquery.PathExpr;
import org.exist.xquery.XPathException;
import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceIterator;
import org.exist.xquery.value.ValueSequence;
import org.exist.xslt.XSLContext;
import org.exist.xslt.XSLStylesheet;
import org.exist.xslt.pattern.Pattern;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

/**
 * <!-- Category: instruction -->
 * <xsl:apply-templates
 *   select? = expression
 *   mode? = token>
 *   <!-- Content: (xsl:sort | xsl:with-param)* -->
 * </xsl:apply-templates>
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 *
 */
public class ApplyTemplates extends SimpleConstructor {

	private String attr_select = null; 
	
	private PathExpr select = null;
	private String mode = null;

	private static LocationStep anyChild;

	public ApplyTemplates(XSLContext context) {
		super(context);

		anyChild = new LocationStep(getContext(), Constants.CHILD_AXIS, new AnyNodeTest());
	}
	
	public void setToDefaults() {
		select = null;
		mode = null;
	}

	public void prepareAttribute(ContextAtExist context, Attr attr) throws XPathException {
		String attr_name = attr.getNodeName();
		if (attr_name.equals(SELECT)) {
			attr_select = attr.getValue();
		} else if (attr_name.equals(MODE)) {
			mode = attr.getValue();
		}
	}
	
    public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
    	super.analyze(contextInfo);

    	if (attr_select != null) {
    		select = new PathExpr(getContext());
    		Pattern.parse(contextInfo.getContext(), attr_select, select);
    		
    		_check_(select);
    	}
    }
	
	public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    	Sequence result = new ValueSequence();
    	
    	Sequence selected;
    	if (select == null) {
    		selected = anyChild.eval(contextSequence, contextItem);
    	} else {
    		selected = select.eval(contextSequence, contextItem);
    	}
    	
    	for (Expression expr : steps) {
    		if (expr instanceof Sort) {
				Sort sort = (Sort) expr;
				selected = sort.eval(selected, null);
			} else {
				throw new XPathException("not suported for now "+expr);//TODO: error?
			}
    	}
    		
    	XSLStylesheet xslt = getXSLContext().getXSLStylesheet();

		int pos = 0;
//    	for (Item item : selected) {
        for (SequenceIterator iterInner = selected.iterate(); iterInner.hasNext();) {
            Item item = iterInner.nextItem();   
    		
        	context.setContextPosition(pos);
    		Sequence res = xslt.templates(selected, item);
    		if (res == null) {
            	if (item instanceof Text) {
                    MemTreeBuilder builder = context.getDocumentBuilder();
            		builder.characters(item.getStringValue());
            		result.add(item);
            	} else if (item instanceof Node) {<commit_msg>[ignore] fix xslt after 'store sequence & position in it to context'<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2008-2009 The eXist Project
 *  http://exist-db.org
 *  
 *  This program 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
 *  of the License, or (at your option) any later version.
 *  
 *  This program 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.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xslt.expression;

import org.exist.interpreter.ContextAtExist;
import org.exist.memtree.MemTreeBuilder;
import org.exist.xquery.AnalyzeContextInfo;
import org.exist.xquery.AnyNodeTest;
import org.exist.xquery.Constants;
import org.exist.xquery.Expression;
import org.exist.xquery.LocationStep;
import org.exist.xquery.PathExpr;
import org.exist.xquery.XPathException;
import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceIterator;
import org.exist.xquery.value.ValueSequence;
import org.exist.xslt.XSLContext;
import org.exist.xslt.XSLStylesheet;
import org.exist.xslt.pattern.Pattern;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

/**
 * <!-- Category: instruction -->
 * <xsl:apply-templates
 *   select? = expression
 *   mode? = token>
 *   <!-- Content: (xsl:sort | xsl:with-param)* -->
 * </xsl:apply-templates>
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 *
 */
public class ApplyTemplates extends SimpleConstructor {

	private String attr_select = null; 
	
	private PathExpr select = null;
	private String mode = null;

	private static LocationStep anyChild;

	public ApplyTemplates(XSLContext context) {
		super(context);

		anyChild = new LocationStep(getContext(), Constants.CHILD_AXIS, new AnyNodeTest());
	}
	
	public void setToDefaults() {
		select = null;
		mode = null;
	}

	public void prepareAttribute(ContextAtExist context, Attr attr) throws XPathException {
		String attr_name = attr.getNodeName();
		if (attr_name.equals(SELECT)) {
			attr_select = attr.getValue();
		} else if (attr_name.equals(MODE)) {
			mode = attr.getValue();
		}
	}
	
    public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
    	super.analyze(contextInfo);

    	if (attr_select != null) {
    		select = new PathExpr(getContext());
    		Pattern.parse(contextInfo.getContext(), attr_select, select);
    		
    		_check_(select);
    	}
    }
	
	public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    	Sequence result = new ValueSequence();
    	
    	Sequence selected;
    	if (select == null) {
    		selected = anyChild.eval(contextSequence, contextItem);
    	} else {
    		selected = select.eval(contextSequence, contextItem);
    	}
    	
    	for (Expression expr : steps) {
    		if (expr instanceof Sort) {
				Sort sort = (Sort) expr;
				selected = sort.eval(selected, null);
			} else {
				throw new XPathException("not suported for now "+expr);//TODO: error?
			}
    	}
    		
    	XSLStylesheet xslt = getXSLContext().getXSLStylesheet();

		int pos = 0;
//    	for (Item item : selected) {
        for (SequenceIterator iterInner = selected.iterate(); iterInner.hasNext();) {
            Item item = iterInner.nextItem();   
    		
        	context.setContextSequencePosition(pos, selected);
    		Sequence res = xslt.templates(selected, item);
    		if (res == null) {
            	if (item instanceof Text) {
                    MemTreeBuilder builder = context.getDocumentBuilder();
            		builder.characters(item.getStringValue());
            		result.add(item);
            	} else if (item instanceof Node) {
{'fraction_non_alphanumeric': 0.10604428485936565, 'fraction_numerical': 0.005026929982046679, 'mean_word_length': 3.3543512245961438, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '6345829', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2511, 'n_words': 707}