<commit_before>/*************************************************************************************
 * Copyright (c) 2004 Actuate Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Actuate Corporation - Initial implementation.
 ************************************************************************************/

package org.eclipse.birt.report.taglib;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;

import org.eclipse.birt.report.IBirtConstants;
import org.eclipse.birt.report.resource.BirtResources;
import org.eclipse.birt.report.resource.ResourceConstants;
import org.eclipse.birt.report.taglib.util.BirtTagUtil;
import org.eclipse.birt.report.utility.ParameterAccessor;

/**
 * @author Administrator
 * 
 */
public class RequesterTag extends AbstractBaseTag
{

	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = -4360776653926113953L;

	/**
	 * Cache the parameter values defined in paramDef tag
	 */
	private Map parameters;

	/**
	 * Then entry to initialize tag
	 * 
	 * @throws Exception
	 */
	public void __init( )
	{
		super.__init( );

		parameters = new HashMap( );
	}

	/**
	 * When reach the start tag, fire this operation If set isCustom as true,
	 * use FORM to create user-defined parameter page
	 * 
	 * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
	 */
	public int doStartTag( ) throws JspException
	{
		try
		{
			if ( viewer.isCustom( ) )
			{
				JspWriter writer = pageContext.getOut( );

				// create DIV object to contain requester page
				writer.write( "<DIV " ); //$NON-NLS-1$
				if ( viewer.getId( ) != null )
					writer.write( " ID=\"" + viewer.getId( ) + "\" " ); //$NON-NLS-1$ //$NON-NLS-2$
				writer.write( __handleDivAppearance( ) + ">\n" ); //$NON-NLS-1$

				// create form
				writer
						.write( "<FORM NAME=\"" + viewer.getName( ) + "\" METHOD=\"post\" " ); //$NON-NLS-1$ //$NON-NLS-2$
				writer.write( " action=\"" + viewer.createURI( null ) + "\"" ); //$NON-NLS-1$ //$NON-NLS-2$
				if ( viewer.getTarget( ) != null )
					writer.write( " target=\"" + viewer.getTarget( ) + "\"" ); //$NON-NLS-1$//$NON-NLS-2$
				writer.write( ">\n" ); //$NON-NLS-1$
			}
		}
		catch ( Exception e )
		{
			__handleException( e );
		}

		return EVAL_PAGE;
	}

	/**
	 * validate the tag
	 * 
	 * @see org.eclipse.birt.report.taglib.AbstractBaseTag#__validate()
	 */
	public boolean __validate( ) throws Exception
	{
		// get Locale
		Locale locale = BirtTagUtil.getLocale( (HttpServletRequest) pageContext
				.getRequest( ), viewer.getLocale( ) );

		// Set locale information
		BirtResources.setLocale( locale );

		// Validate requester id
		if ( viewer.getId( ) == null || viewer.getId( ).length( ) <= 0 )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_NO_ATTR_ID ) );
		}

		if ( !__validateRequesterId( ) )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_INVALID_ATTR_ID ) );
		}

		// validate the viewer id if unique
		if ( pageContext.findAttribute( viewer.getId( ) ) != null )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_ATTR_ID_DUPLICATE ) );
		}

		// Validate requester name
		// if isCustom is true, the requester name should be required.
		if ( viewer.isCustom( )
				&& ( viewer.getName( ) == null || viewer.getName( ).length( ) <= 0 ) )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_NO_REQUESTER_NAME ) );
		}

		// Report design or document should be specified
		if ( viewer.getReportDesign( ) == null
				&& viewer.getReportDocument( ) == null )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_NO_REPORT_SOURCE ) );
		}

		// If preview reportlet, report document file should be specified.
		if ( viewer.getReportletId( ) != null
				&& viewer.getReportDocument( ) == null )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_NO_REPORT_DOCUMENT ) );
		}

		return true;
	}

	/**
	 * Validate the requester id. Requester id only can include number, letter<commit_msg>When there is a tag validation error in RequestTag, the tag body will no more be shown since it is not usable.<commit_after>/*************************************************************************************
 * Copyright (c) 2004 Actuate Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Actuate Corporation - Initial implementation.
 ************************************************************************************/

package org.eclipse.birt.report.taglib;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;

import org.eclipse.birt.report.IBirtConstants;
import org.eclipse.birt.report.resource.BirtResources;
import org.eclipse.birt.report.resource.ResourceConstants;
import org.eclipse.birt.report.taglib.util.BirtTagUtil;
import org.eclipse.birt.report.utility.ParameterAccessor;

/**
 * @author Administrator
 * 
 */
public class RequesterTag extends AbstractBaseTag
{

	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = -4360776653926113953L;

	/**
	 * Cache the parameter values defined in paramDef tag
	 */
	private Map parameters;

	private Boolean validated;

	/**
	 * Then entry to initialize tag
	 * 
	 * @throws Exception
	 */
	public void __init( )
	{
		super.__init( );

		parameters = new HashMap( );
		validated = null;
	}

	/**
	 * When reach the start tag, fire this operation If set isCustom as true,
	 * use FORM to create user-defined parameter page
	 * 
	 * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
	 */
	public int doStartTag( ) throws JspException
	{
		try
		{
			// TODO: move this logic in AbstractBaseTag if possible
			if ( __validate( ) )
			{
				if ( viewer.isCustom( ) )
				{
					JspWriter writer = pageContext.getOut( );

					// create DIV object to contain requester page
					writer.write( "<DIV " ); //$NON-NLS-1$
					if ( viewer.getId( ) != null )
						writer.write( " ID=\"" + viewer.getId( ) + "\" " ); //$NON-NLS-1$ //$NON-NLS-2$
					writer.write( __handleDivAppearance( ) + ">\n" ); //$NON-NLS-1$

					// create form
					writer
							.write( "<FORM NAME=\"" + viewer.getName( ) + "\" METHOD=\"post\" " ); //$NON-NLS-1$ //$NON-NLS-2$
					writer
							.write( " action=\"" + viewer.createURI( null ) + "\"" ); //$NON-NLS-1$ //$NON-NLS-2$
					if ( viewer.getTarget( ) != null )
						writer
								.write( " target=\"" + viewer.getTarget( ) + "\"" ); //$NON-NLS-1$//$NON-NLS-2$
					writer.write( ">\n" ); //$NON-NLS-1$
				}
			}
			else
			{
				return SKIP_BODY;
			}
		}
		catch ( Exception e )
		{
			__handleException( e );
			return SKIP_BODY;
		}

		return EVAL_PAGE;
	}

	/**
	 * Process only if the attributes are validated. Overrides default
	 * doEndTag().
	 */
	public int doEndTag( ) throws JspException
	{
		try
		{
			if ( __validate( ) )
			{
				__beforeEndTag( );
				__process( );
			}
			else
			{
				return SKIP_BODY;
			}
		}
		catch ( Exception e )
		{
			__handleException( e );
			return SKIP_BODY;
		}
		return EVAL_PAGE;
	}

	/**
	 * Validate the tag. *
	 * 
	 * @see org.eclipse.birt.report.taglib.AbstractBaseTag#__validate()
	 */
	public boolean __validate( ) throws Exception
	{
		if ( validated != null )
		{
			return validated.booleanValue( );
		}
		validated = Boolean.FALSE;

		// get Locale
		Locale locale = BirtTagUtil.getLocale( (HttpServletRequest) pageContext
				.getRequest( ), viewer.getLocale( ) );

		// Set locale information
		BirtResources.setLocale( locale );

		// Validate requester id
		if ( viewer.getId( ) == null || viewer.getId( ).length( ) <= 0 )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_NO_ATTR_ID ) );
		}

		if ( !__validateRequesterId( ) )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_INVALID_ATTR_ID ) );
		}

		// validate the viewer id if unique
		if ( pageContext.findAttribute( viewer.getId( ) ) != null )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_ATTR_ID_DUPLICATE ) );
		}

		// Validate requester name
		// if isCustom is true, the requester name should be required.
		if ( viewer.isCustom( )
				&& ( viewer.getName( ) == null || viewer.getName( ).length( ) <= 0 ) )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_NO_REQUESTER_NAME ) );
		}

		// Report design or document should be specified
		if ( viewer.getReportDesign( ) == null
				&& viewer.getReportDocument( ) == null )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_NO_REPORT_SOURCE ) );
		}

		// If preview reportlet, report document file should be specified.
		if ( viewer.getReportletId( ) != null
				&& viewer.getReportDocument( ) == null )
		{
			throw new JspTagException( BirtResources
					.getMessage( ResourceConstants.TAGLIB_NO_REPORT_DOCUMENT ) );
		}

		validated = Boolean.TRUE;
		return true;
	}

	/**
	 * Validate the requester id. Requester id only can include number, letter
{'fraction_non_alphanumeric': 0.15678302261007537, 'fraction_numerical': 0.007933359777865925, 'mean_word_length': 3.257070493879274, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5112686', 'n_tokens_mistral': 3658, 'n_tokens_neox': 3136, 'n_words': 767}