All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.megatronking.svg.generator.xml.CommonAbstractAttributeParser Maven / Gradle / Ivy

There is a newer version: 1.3.6
Show newest version
package com.github.megatronking.svg.generator.xml;


import com.github.megatronking.svg.generator.utils.Color;
import com.github.megatronking.svg.generator.utils.Dimen;
import com.github.megatronking.svg.generator.utils.SCU;

import org.dom4j.Attribute;
import org.dom4j.Element;

/**
 * We define some common methods here.
 *
 * @author Megatron King
 * @since 2016/9/1 9:40
 */

public abstract class CommonAbstractAttributeParser implements IAttributeParser {

    protected String parseString(Element element, String name) {
        Attribute attribute = element.attribute(name);
        return attribute == null ? null : attribute.getValue();
    }

    protected String parseString(Element element, String name, String defaultValue) {
        Attribute attribute = element.attribute(name);
        String value = attribute == null ? null : attribute.getValue();
        return value == null || value.trim().length() == 0 ? defaultValue : value;
    }

    protected float parseFloat(Element element, String name) {
        return parseFloat(element, name, 0.0f);
    }

    protected float parseFloat(Element element, String name, float defaultValue) {
        return SCU.parseFloat(parseString(element, name), defaultValue);
    }

    protected boolean parseBoolean(Element element, String name) {
        return parseBoolean(element, name, false);
    }

    protected boolean parseBoolean(Element element, String name, boolean defaultValue) {
        return SCU.parseBoolean(parseString(element, name), defaultValue);
    }

    protected int parseColor(Element element, String name) {
        return Color.convert(parseString(element, name));
    }

    protected int parseColor(Element element, String name, int defaultColor) {
        return Color.convert(parseString(element, name), defaultColor);
    }

    protected float parseDimen(Element element, String name) {
        return Dimen.convert(parseString(element, name));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy