-
Notifications
You must be signed in to change notification settings - Fork 124
/
AsciidoctorHelper.java
82 lines (74 loc) · 3.58 KB
/
AsciidoctorHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asciidoctor.maven;
import org.apache.maven.project.MavenProject;
import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;
import java.util.Map;
/**
* Utility class for re-usable logic.
*/
public class AsciidoctorHelper {
/**
* Adds attributes from a {@link Map} into a {@link AttributesBuilder} taking care of Maven's XML parsing special
* cases like toggles, nulls, etc.
*
* @param attributes map of Asciidoctor attributes
* @param attributesBuilder AsciidoctorJ AttributesBuilder
*/
public static void addAttributes(final Map<String, Object> attributes, AttributesBuilder attributesBuilder) {
// TODO Figure out how to reliably set other values (like boolean values, dates, times, etc)
for (Map.Entry<String, Object> attributeEntry : attributes.entrySet()) {
addAttribute(attributeEntry.getKey(), attributeEntry.getValue(), attributesBuilder);
}
}
/**
* Adds properties from the {@link MavenProject} into a {@link AttributesBuilder} taking care of Maven's XML parsing special
* cases like toggles, nulls, etc.
*
* @param project Maven project
* @param attributesBuilder AsciidoctorJ AttributesBuilder
*/
public static void addMavenProperties(MavenProject project, AttributesBuilder attributesBuilder) {
if (project.getProperties() != null) {
for (Map.Entry<Object, Object> entry : project.getProperties().entrySet()) {
attributesBuilder.attribute(((String) entry.getKey()).replaceAll("\\.", "-"), entry.getValue());
}
}
}
/**
* Adds an attribute into a {@link AttributesBuilder} taking care of Maven's XML parsing special cases like
* toggles toggles, nulls, etc.
*
* @param attribute Asciidoctor attribute name
* @param value Asciidoctor attribute value
* @param attributesBuilder AsciidoctorJ AttributesBuilder
*/
public static void addAttribute(String attribute, Object value, AttributesBuilder attributesBuilder) {
// NOTE Maven interprets an empty value as null, so we need to explicitly convert it to empty string (see #36)
// NOTE In Asciidoctor, an empty string represents a true value
if (value == null || "true".equals(value)) {
attributesBuilder.attribute(attribute, "");
}
// NOTE a value of false is effectively the same as a null value, so recommend the use of the string "false"
else if ("false".equals(value)) {
attributesBuilder.attribute(attribute, null);
}
// NOTE Maven can't assign a Boolean value from the XML-based configuration, but a client may
else if (value instanceof Boolean) {
attributesBuilder.attribute(attribute, Attributes.toAsciidoctorFlag((Boolean) value));
} else {
// Can't do anything about dates and times because all that logic is private in Attributes
attributesBuilder.attribute(attribute, value);
}
}
}