<commit_before>/*****************************************************************************
 * Copyright (C) The Apache Software Foundation. All rights reserved.        *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included with this distribution in  *
 * the LICENSE file.                                                         *
 *****************************************************************************/

package org.apache.batik.css.svg;

import org.apache.batik.css.CSSDOMExceptionFactory;
import org.apache.batik.css.CSSOMValue;
import org.apache.batik.css.value.AbstractLengthFactory;
import org.apache.batik.css.value.AbstractValueFactory;
import org.apache.batik.css.value.ImmutableFloat;
import org.apache.batik.css.value.ImmutableString;
import org.apache.batik.css.value.ImmutableValue;
import org.apache.batik.css.value.ImmutableValueList;
import org.apache.batik.css.value.ValueFactory;
import org.w3c.css.sac.LexicalUnit;
import org.w3c.css.sac.Parser;
import org.w3c.dom.DOMException;
import org.w3c.dom.css.CSSPrimitiveValue;

/**
 * This class provides a factory for the 'stroke-dasharray' property values.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class StrokeDasharrayFactory extends AbstractValueFactory {
    /**
     * The numbers factory.
     */
    protected final ValueFactory NUMBER_FACTORY =
	new NumberFactory(getParser());

    /**
     * Creates a new StrokeDasharrayFactory object.
     * @param p The CSS parser used to parse the CSS texts.
     */
    public StrokeDasharrayFactory(Parser p) {
	super(p);
    }

    /**
     * Returns the name of the property handled.
     */
    public String getPropertyName() {
	return SVGValueConstants.CSS_STROKE_DASHARRAY_PROPERTY;
    }
    
    /**
     * Creates a value from a lexical unit.
     */
    public ImmutableValue createValue(LexicalUnit lu) throws DOMException {
	short type = lu.getLexicalUnitType();
	switch (type) {
	case LexicalUnit.SAC_INHERIT:
	    return INHERIT;
	case LexicalUnit.SAC_IDENT:
	    return createStringValue(CSSPrimitiveValue.CSS_IDENT, CSS_NONE_VALUE);
	default:
	    return createValueList(lu);
	}
    }

    /**
     * Creates and returns a new string value.
     * @param type  A string code as defined in CSSPrimitiveValue. The string
     *   code can only be a string unit type.
     * @param value  The new string value. 
     */
    public ImmutableValue createStringValue(short type, String value)
	throws DOMException {
	if (type != CSSPrimitiveValue.CSS_IDENT ||
            !value.equalsIgnoreCase(CSS_NONE_VALUE)) {
	throw CSSDOMExceptionFactory.createDOMException
	    (DOMException.INVALID_ACCESS_ERR,
	     "invalid.identifier",
	     new Object[] { value });
	}
	return NONE_VALUE;
    }

    /**
     * Creates the quotes list.
     */
    protected ImmutableValue createValueList(LexicalUnit lu)
        throws DOMException {
	ImmutableValueList list = new ImmutableValueList(' ');
	do {
	    list.append(new CSSOMValue(NUMBER_FACTORY,
                                       NUMBER_FACTORY.createValue(lu)));
	    lu = lu.getNextLexicalUnit();
	} while (lu != null);
	return list;
    }

    /**
     * To manage number values.
     */
    protected static class NumberFactory extends AbstractValueFactory {
	/**
	 * Creates a new NumberFactory object.<commit_msg>Modified to accept comma-separated lists. PR: Obtained from: Submitted by: Reviewed by:<commit_after>/*****************************************************************************
 * Copyright (C) The Apache Software Foundation. All rights reserved.        *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included with this distribution in  *
 * the LICENSE file.                                                         *
 *****************************************************************************/

package org.apache.batik.css.svg;

import org.apache.batik.css.CSSDOMExceptionFactory;
import org.apache.batik.css.CSSOMValue;
import org.apache.batik.css.value.AbstractLengthFactory;
import org.apache.batik.css.value.AbstractValueFactory;
import org.apache.batik.css.value.ImmutableFloat;
import org.apache.batik.css.value.ImmutableString;
import org.apache.batik.css.value.ImmutableValue;
import org.apache.batik.css.value.ImmutableValueList;
import org.apache.batik.css.value.ValueFactory;
import org.w3c.css.sac.LexicalUnit;
import org.w3c.css.sac.Parser;
import org.w3c.dom.DOMException;
import org.w3c.dom.css.CSSPrimitiveValue;

/**
 * This class provides a factory for the 'stroke-dasharray' property values.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class StrokeDasharrayFactory extends AbstractValueFactory {
    /**
     * The numbers factory.
     */
    protected final ValueFactory NUMBER_FACTORY =
	new NumberFactory(getParser());

    /**
     * Creates a new StrokeDasharrayFactory object.
     * @param p The CSS parser used to parse the CSS texts.
     */
    public StrokeDasharrayFactory(Parser p) {
	super(p);
    }

    /**
     * Returns the name of the property handled.
     */
    public String getPropertyName() {
	return SVGValueConstants.CSS_STROKE_DASHARRAY_PROPERTY;
    }
    
    /**
     * Creates a value from a lexical unit.
     */
    public ImmutableValue createValue(LexicalUnit lu) throws DOMException {
	short type = lu.getLexicalUnitType();
	switch (type) {
	case LexicalUnit.SAC_INHERIT:
	    return INHERIT;
	case LexicalUnit.SAC_IDENT:
	    return createStringValue(CSSPrimitiveValue.CSS_IDENT, CSS_NONE_VALUE);
	default:
	    return createValueList(lu);
	}
    }

    /**
     * Creates and returns a new string value.
     * @param type  A string code as defined in CSSPrimitiveValue. The string
     *   code can only be a string unit type.
     * @param value  The new string value. 
     */
    public ImmutableValue createStringValue(short type, String value)
	throws DOMException {
	if (type != CSSPrimitiveValue.CSS_IDENT ||
            !value.equalsIgnoreCase(CSS_NONE_VALUE)) {
	throw CSSDOMExceptionFactory.createDOMException
	    (DOMException.INVALID_ACCESS_ERR,
	     "invalid.identifier",
	     new Object[] { value });
	}
	return NONE_VALUE;
    }

    /**
     * Creates the quotes list.
     */
    protected ImmutableValue createValueList(LexicalUnit lu)
        throws DOMException {
	ImmutableValueList list = new ImmutableValueList(' ');
	for (;;) {
	    list.append(new CSSOMValue(NUMBER_FACTORY,
                                       NUMBER_FACTORY.createValue(lu)));
	    lu = lu.getNextLexicalUnit();
            if (lu == null) {
                break;
            }
            if (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
                throw CSSDOMExceptionFactory.createDOMException
                    (DOMException.INVALID_ACCESS_ERR,
                     "bad.unit.type",
                     new Object[] { lu.getLexicalUnitType() });
            }
	    lu = lu.getNextLexicalUnit();
            if (lu == null) {
                break;
            }
	}
	return list;
    }

    /**
     * To manage number values.
     */
    protected static class NumberFactory extends AbstractValueFactory {
	/**
	 * Creates a new NumberFactory object.
{'fraction_non_alphanumeric': 0.14873837981407703, 'fraction_numerical': 0.0015936254980079682, 'mean_word_length': 2.7356150793650795, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5829870', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2066, 'n_words': 527}