xref: /aosp_15_r20/external/jsoup/src/main/java/org/jsoup/parser/ParseSettings.java (revision 6da8f8c4bc310ad659121b84dd089062417a2ce2)
1 package org.jsoup.parser;
2 
3 import org.jsoup.nodes.Attributes;
4 import org.jspecify.annotations.Nullable;
5 
6 import static org.jsoup.internal.Normalizer.lowerCase;
7 
8 /**
9  * Controls parser case settings, to optionally preserve tag and/or attribute name case.
10  */
11 public class ParseSettings {
12     /**
13      * HTML default settings: both tag and attribute names are lower-cased during parsing.
14      */
15     public static final ParseSettings htmlDefault;
16     /**
17      * Preserve both tag and attribute case.
18      */
19     public static final ParseSettings preserveCase;
20 
21     static {
22         htmlDefault = new ParseSettings(false, false);
23         preserveCase = new ParseSettings(true, true);
24     }
25 
26     private final boolean preserveTagCase;
27     private final boolean preserveAttributeCase;
28 
29     /**
30      * Returns true if preserving tag name case.
31      */
preserveTagCase()32     public boolean preserveTagCase() {
33         return preserveTagCase;
34     }
35 
36     /**
37      * Returns true if preserving attribute case.
38      */
preserveAttributeCase()39     public boolean preserveAttributeCase() {
40         return preserveAttributeCase;
41     }
42 
43     /**
44      * Define parse settings.
45      * @param tag preserve tag case?
46      * @param attribute preserve attribute name case?
47      */
ParseSettings(boolean tag, boolean attribute)48     public ParseSettings(boolean tag, boolean attribute) {
49         preserveTagCase = tag;
50         preserveAttributeCase = attribute;
51     }
52 
ParseSettings(ParseSettings copy)53     ParseSettings(ParseSettings copy) {
54         this(copy.preserveTagCase, copy.preserveAttributeCase);
55     }
56 
57     /**
58      * Normalizes a tag name according to the case preservation setting.
59      */
normalizeTag(String name)60     public String normalizeTag(String name) {
61         name = name.trim();
62         if (!preserveTagCase)
63             name = lowerCase(name);
64         return name;
65     }
66 
67     /**
68      * Normalizes an attribute according to the case preservation setting.
69      */
normalizeAttribute(String name)70     public String normalizeAttribute(String name) {
71         name = name.trim();
72         if (!preserveAttributeCase)
73             name = lowerCase(name);
74         return name;
75     }
76 
normalizeAttributes(@ullable Attributes attributes)77     @Nullable Attributes normalizeAttributes(@Nullable Attributes attributes) {
78         if (attributes != null && !preserveAttributeCase) {
79             attributes.normalize();
80         }
81         return attributes;
82     }
83 
84     /** Returns the normal name that a Tag will have (trimmed and lower-cased) */
normalName(String name)85     static String normalName(String name) {
86         return lowerCase(name.trim());
87     }
88 }
89