Skip to content

Commit

Permalink
lazy attribute _value_ creation
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jun 15, 2024
1 parent e2f911e commit ba81870
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
19 changes: 14 additions & 5 deletions src/main/java/ch/digitalfondue/jfiveparse/AttributeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class AttributeNode {

String name;
String originalName;
String value;

private String value;
private ResizableCharBuilder valueBuilder;

//
String prefix;
Expand All @@ -36,8 +38,9 @@ public AttributeNode(String name, String value) {
this.value = value;
}

AttributeNode(String name, String originalName, String value, int attributeQuoteType) {
this(name, value);
AttributeNode(String name, String originalName, ResizableCharBuilder value, int attributeQuoteType) {
this.name = name;
this.valueBuilder = value;
this.originalName = originalName;
this.attributeQuoteType = attributeQuoteType;
}
Expand All @@ -46,6 +49,7 @@ public AttributeNode(String name, String value) {
this.name = a.name;
this.originalName = a.originalName;
this.value = a.value;
this.valueBuilder = a.valueBuilder;
this.prefix = a.prefix;
this.namespace = a.namespace;
this.attributeQuoteType = a.attributeQuoteType;
Expand All @@ -58,10 +62,15 @@ public AttributeNode(String name, String value, String prefix, String namespace)
}

public String getValue() {
if (value == null && valueBuilder != null) {
value = valueBuilder.asString();
valueBuilder = null;
}
return value;
}

public void setValue(String value) {
this.valueBuilder = null;
this.value = value;
}

Expand All @@ -79,7 +88,7 @@ public String getName() {

@Override
public int hashCode() {
return Objects.hash(name, value, prefix, namespace);
return Objects.hash(name, getValue(), prefix, namespace);
}

@Override
Expand All @@ -91,7 +100,7 @@ public boolean equals(Object obj) {
if (obj instanceof AttributeNode) {
AttributeNode other = (AttributeNode) obj;
return name.equals(other.name) && //
value.equals(other.value) && //
getValue().equals(other.getValue()) && //
Objects.equals(prefix, other.prefix) && //
Objects.equals(namespace, other.namespace);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/digitalfondue/jfiveparse/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ public Iterator<AttributeNode> iterator() {
}

public String getNamedItem(String name) {
return attributes != null && attributes.containsKey(name) ? attributes.get(name).value : null;
return attributes != null && attributes.containsKey(name) ? attributes.get(name).getValue() : null;
}
}
2 changes: 1 addition & 1 deletion src/main/java/ch/digitalfondue/jfiveparse/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ static boolean isHtmlNS(Element element, byte nameID) {
return element.nodeNameID == nameID && element.namespaceID == Node.NAMESPACE_HTML_ID;
}

/** /!\ beware when using this function!, the from to must be carefully chosed! */
/** /!\ beware when using this function!, the "from"-"to" must be carefully chosen! */
static boolean isHtmlNSBetween(Element element, byte nameIDFrom, byte nameIDto) {
return element.nodeNameID >= nameIDFrom && element.nodeNameID <= nameIDto && element.namespaceID == Node.NAMESPACE_HTML_ID;
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/ch/digitalfondue/jfiveparse/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,12 @@ void addCurrentAttributeInAttributes() {
if (attributes.containsKey(curAttrName)) {
tokenHandler.emitParseError();
} else {
attributes.put(new AttributeNode(curAttrName, currentAttributeName.containsUpperCase ? currentAttributeName.asString() : curAttrName, currentAttributeValue.asString(), currentAttributeQuoteType));
attributes.put(new AttributeNode(
curAttrName,
currentAttributeName.containsUpperCase ? currentAttributeName.asString() : curAttrName,
currentAttributeValue,
currentAttributeQuoteType
));
}
}
} catch (NullPointerException npe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ private void setTagNameAndSaveOriginal(ResizableCharBuilder rawTagName) {

void setTagName(ResizableCharBuilder rawTagName) {
String tagName = rawTagName.toLowerCase();
//String maybeCached = Common.ELEMENTS_NAME_CACHE_V2.get(tagName);
this.tagName = tagName;
this.tagNameID = Common.tagNameToID(tagName);
}
Expand Down

0 comments on commit ba81870

Please sign in to comment.