This repository has been archived by the owner on Jun 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* parse script tags in html head for composition * introduced asset class for parsed asset links, added handling of js assets * filter options attribute when rendering asset links into response * Improved equals methods, added missing test, added code comment
- Loading branch information
Showing
14 changed files
with
238 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/com/rewedigital/composer/composing/Asset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.rewedigital.composer.composing; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Describes a <code>link</code> or <code>script</code> tag found in an html <code>head</code> section during parsing of | ||
* a template or content fragment. | ||
*/ | ||
class Asset { | ||
public static class Builder { | ||
|
||
private final String optionsAttributeName; | ||
private String type = "link"; | ||
private boolean selfClosing = false; | ||
private final Map<String, String> attributes = new HashMap<>(); | ||
|
||
public Builder(final String optionsAttributeName) { | ||
this.optionsAttributeName = optionsAttributeName; | ||
} | ||
|
||
public Asset.Builder type(final String type) { | ||
this.type = type; | ||
return this; | ||
} | ||
|
||
public Asset.Builder attribute(final String name, final String value) { | ||
this.attributes.put(name, value); | ||
return this; | ||
} | ||
|
||
public Asset.Builder selfClosing(final boolean selfClosing) { | ||
this.selfClosing = selfClosing; | ||
return this; | ||
} | ||
|
||
public boolean isInclude() { | ||
return optionsContain("include"); | ||
} | ||
|
||
private boolean optionsContain(final String option) { | ||
return attributes.getOrDefault(optionsAttributeName, "").contains(option); | ||
} | ||
|
||
public Asset build() { | ||
return new Asset(this); | ||
} | ||
} | ||
|
||
private final String optionsAttributeName; | ||
private final String type; | ||
private final Map<String, String> attributes; | ||
private final boolean selfClosing; | ||
|
||
private Asset(final Asset.Builder builder) { | ||
this.optionsAttributeName = builder.optionsAttributeName; | ||
this.type = builder.type; | ||
this.selfClosing = builder.selfClosing; | ||
this.attributes = new HashMap<>(builder.attributes); | ||
} | ||
|
||
public String render() { | ||
return attributes | ||
.entrySet() | ||
.stream() | ||
.filter(notOptionsAttribute()) | ||
.reduce(new StringBuilder().append(renderOpen()), | ||
(builder, e) -> builder.append(e.getKey()) | ||
.append("=\"") | ||
.append(e.getValue()) | ||
.append("\" "), | ||
(a, b) -> a.append(b)) | ||
.append(renderClosing()).toString(); | ||
} | ||
|
||
private Predicate<? super Entry<String, String>> notOptionsAttribute() { | ||
return e -> !e.getKey().equals(optionsAttributeName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(attributes, selfClosing, type); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
final Asset other = (Asset) obj; | ||
return selfClosing == other.selfClosing && | ||
Objects.equals(attributes, other.attributes) && | ||
Objects.equals(type, other.type); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Asset [type=" + type + ", attributes=" + attributes + ", selfClosing=" + selfClosing + "]"; | ||
} | ||
|
||
private String renderOpen() { | ||
return "<" + type + " "; | ||
} | ||
|
||
private String renderClosing() { | ||
if (selfClosing) { | ||
return "/>"; | ||
} | ||
return "></" + type + ">"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.