001package org.jsoup.parser;
002
003import org.jsoup.internal.StringUtil;
004import org.jsoup.nodes.Attributes;
005
006import static org.jsoup.internal.Normalizer.asciiLowerCase;
007
008/**
009 * Controls parser case settings, to optionally preserve tag and/or attribute name case.
010 * Case conversion uses ASCII rules.
011 * Programmatic name normalization also trims surrounding ASCII whitespace.
012 */
013public class ParseSettings {
014    /**
015     * HTML defaults: lower-case tag and attribute names.
016     */
017    public static final ParseSettings htmlDefault;
018    /**
019     * Preserve both tag and attribute case.
020     */
021    public static final ParseSettings preserveCase;
022
023    static {
024        htmlDefault = new ParseSettings(false, false);
025        preserveCase = new ParseSettings(true, true);
026    }
027
028    private final boolean preserveTagCase;
029    private final boolean preserveAttributeCase;
030
031    /**
032     * Returns true if preserving tag name case.
033     */
034    public boolean preserveTagCase() {
035        return preserveTagCase;
036    }
037
038    /**
039     * Returns true if preserving attribute case.
040     */
041    public boolean preserveAttributeCase() {
042        return preserveAttributeCase;
043    }
044
045    /**
046     * Define parse settings.
047     * @param tag preserve tag case?
048     * @param attribute preserve attribute name case?
049     */
050    public ParseSettings(boolean tag, boolean attribute) {
051        preserveTagCase = tag;
052        preserveAttributeCase = attribute;
053    }
054
055    ParseSettings(ParseSettings copy) {
056        this(copy.preserveTagCase, copy.preserveAttributeCase);
057    }
058
059    /**
060     * Normalizes a tag name according to these settings.
061     */
062    public String normalizeTag(String name) {
063        name = StringUtil.trimAsciiWhitespace(name);
064        if (!preserveTagCase)
065            name = asciiLowerCase(name);
066        return name;
067    }
068
069    /**
070     * Normalizes an attribute name according to these settings.
071     */
072    public String normalizeAttribute(String name) {
073        name = StringUtil.trimAsciiWhitespace(name);
074        if (!preserveAttributeCase)
075            name = asciiLowerCase(name);
076        return name;
077    }
078
079    void normalizeAttributes(Attributes attributes) {
080        if (!preserveAttributeCase) {
081            attributes.normalize();
082        }
083    }
084}