Skip to content

Commit

Permalink
Merge pull request dita-ot#4498 from dita-ot/feature/refactor-integra…
Browse files Browse the repository at this point in the history
…tion-ant-generation

Refactor integration
  • Loading branch information
jelovirt authored Jul 23, 2024
2 parents 0327a9c + 70112b8 commit 2505f09
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/dita/dost/ant/IntegratorTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void execute() throws BuildException {
try {
adaptee.execute();
} catch (final Exception e) {
throw new BuildException("Integration failed: " + e.getMessage(), e);
throw new BuildException("Integration failed: " + e.toString(), e);
}
}

Expand Down
50 changes: 16 additions & 34 deletions src/main/java/org/dita/dost/platform/Features.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,24 @@ record Features(
File pluginDir,
File ditaDir,
Map<String, ExtensionPoint> extensionPoints,
Map<String, List<String>> features,
Map<String, List<Value>> features,
List<PluginRequirement> requiredPlugins,
Map<String, String> metaTable,
List<String> templates
)
implements Plugin {
/**
* Return the feature name by id.
* @param id feature id
* @return feature name
*/
public List<String> getFeature(final String id) {
return features.get(id);
}

/**
* Return meat info specifying type.
* @param type type
* @return meat info
*/
public String getMeta(final String type) {
return metaTable.get(type);
}

public static Builder builder() {
return new Builder();
}

static class Builder {

private String id;
private String pluginId;
private SemVer pluginVersion;
private File pluginDir;
private File ditaDir;
private final Map<String, ExtensionPoint> extensionPoints;
private final Map<String, List<String>> featureTable;
private final Map<String, List<Value>> featureTable;
private final List<PluginRequirement> requireList;
private final Map<String, String> metaTable;
private final List<String> templateList;
Expand All @@ -79,7 +61,7 @@ public Builder() {

public Features build() {
return new Features(
id,
pluginId,
pluginVersion,
pluginDir,
ditaDir,
Expand Down Expand Up @@ -111,7 +93,7 @@ Builder setPluginId(final String id) {
if (!ID_PATTERN.matcher(id).matches()) {
throw new IllegalArgumentException("Plug-in ID '%s' doesn't follow syntax rules.".formatted(id));
}
this.id = id;
this.pluginId = id;
return this;
}

Expand All @@ -123,10 +105,10 @@ Builder addExtensionPoint(String extensionPointId, String name) {
/**
* Add feature to the feature table.
*
* @param id feature id
* @param featureId feature id
* @param elem configuration element
*/
Builder addFeature(final String id, final Element elem) {
Builder addFeature(final String featureId, final Element elem) {
boolean isFile;
String value = elem.getAttribute(FEATURE_FILE_ATTR);
if (!value.isEmpty()) {
Expand All @@ -136,29 +118,29 @@ Builder addFeature(final String id, final Element elem) {
isFile = FEATURE_TYPE_VALUE_FILE.equals(elem.getAttribute(FEATURE_TYPE_ATTR));
}
final StringTokenizer valueTokenizer = new StringTokenizer(value, Integrator.FEAT_VALUE_SEPARATOR);
final List<String> valueBuffer = new ArrayList<>();
if (featureTable.containsKey(id)) {
valueBuffer.addAll(featureTable.get(id));
final List<Value> valueBuffer = new ArrayList<>();
if (featureTable.containsKey(featureId)) {
valueBuffer.addAll(featureTable.get(featureId));
}
while (valueTokenizer.hasMoreElements()) {
final String valueElement = valueTokenizer.nextToken();
if (valueElement != null && valueElement.trim().length() != 0) {
if (isFile && !FileUtils.isAbsolutePath(valueElement)) {
if (id.equals("ant.import")) {
valueBuffer.add("${dita.plugin." + this.id + ".dir}" + File.separator + valueElement.trim());
if (featureId.equals("ant.import")) {
valueBuffer.add(new Value.PathValue(this.pluginId, pluginDir, valueElement.trim()));
} else {
valueBuffer.add(pluginDir + File.separator + valueElement.trim());
valueBuffer.add(new Value.PathValue(this.pluginId, pluginDir, valueElement.trim()));
}
} else {
if (id.equals("package.version")) {
if (featureId.equals("package.version")) {
setPluginVersion(valueElement.trim());
}
valueBuffer.add(valueElement.trim());
valueBuffer.add(new Value.StringValue(this.pluginId, valueElement.trim()));
}
}
}

featureTable.put(id, valueBuffer);
featureTable.put(featureId, valueBuffer);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dita/dost/platform/FileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void startElement(final String uri, final String localName, final String
action.addParam(PARAM_TEMPLATE, templateFile.getAbsolutePath());
final List<Value> value = Stream
.of(attributes.getValue(i).split(Integrator.FEAT_VALUE_SEPARATOR))
.map(val -> new Value(null, val))
.map(val -> new Value.StringValue(null, val))
.collect(Collectors.toList());
action.setInput(value);
final String result = action.getResult();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/dita/dost/platform/ImportAntAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ final class ImportAntAction extends ImportAction {
@Override
public void getResult(final ContentHandler buf) throws SAXException {
for (final Value value : valueSet) {
final String[] tokens = value.value().split("[/\\\\]", 2);
buf.startElement(NULL_NS_URI, "import", "import", XMLUtils.EMPTY_ATTRIBUTES);
buf.startElement(
NULL_NS_URI,
"fileset",
"fileset",
new AttributesBuilder().add("dir", tokens[0]).add("includes", tokens[1]).build()
new AttributesBuilder()
.add("dir", "${dita.plugin." + value.pluginId() + ".dir}")
.add("includes", value.value())
.build()
);
buf.endElement(NULL_NS_URI, "fileset", "fileset");
buf.endElement(NULL_NS_URI, "import", "import");
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/dita/dost/platform/ImportAntLibAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ final class ImportAntLibAction extends ImportAction {
public void getResult(final ContentHandler retBuf) throws SAXException {
final String templateFilePath = paramTable.get(FileGenerator.PARAM_TEMPLATE);
for (final Value value : valueSet) {
final String resolvedValue = FileUtils.getRelativeUnixPath(templateFilePath, value.value());
final String path;
if (value instanceof Value.PathValue pathValue) {
path = pathValue.getPath();
} else {
logger.error("Ant import must be a file feature: " + value.value());
continue;
}
final String resolvedValue = FileUtils.getRelativeUnixPath(templateFilePath, path);
if (FileUtils.isAbsolutePath(resolvedValue)) {
// if resolvedValue is absolute path
retBuf.startElement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ final class ImportCatalogActionRelative extends ImportAction {
public void getResult(final ContentHandler buf) throws SAXException {
final String templateFilePath = paramTable.get(FileGenerator.PARAM_TEMPLATE);
for (final Value value : valueSet) {
final String path;
if (value instanceof Value.PathValue pathValue) {
path = pathValue.getPath();
} else {
logger.error("Catalog import must be a file feature: " + value.value());
continue;
}
buf.startElement(
OASIS_CATALOG_NAMESPACE,
"nextCatalog",
"nextCatalog",
new AttributesBuilder().add("catalog", FileUtils.getRelativeUnixPath(templateFilePath, value.value())).build()
new AttributesBuilder().add("catalog", FileUtils.getRelativeUnixPath(templateFilePath, path)).build()
);
buf.endElement(OASIS_CATALOG_NAMESPACE, "nextCatalog", "nextCatalog");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public void getResult(final ContentHandler buf) throws SAXException {
final String name = PLUGIN_URI_SCHEME + ":" + e.getKey() + ":";
final StringBuilder location = new StringBuilder();

final List<String> baseDirValues = f.getFeature("dita.basedir-resource-directory");
if (Boolean.parseBoolean(baseDirValues == null || baseDirValues.isEmpty() ? null : baseDirValues.get(0))) {
final List<Value> baseDirValues = f.getFeature("dita.basedir-resource-directory");
if (
Boolean.parseBoolean(baseDirValues == null || baseDirValues.isEmpty() ? null : baseDirValues.get(0).value())
) {
location.append("./");
} else if (f.pluginDir().getAbsolutePath().startsWith(f.ditaDir().getAbsolutePath())) {
location.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public void getResult(final ContentHandler buf) throws SAXException {
final String name = "dita.plugin." + e.getKey() + ".dir";
final StringBuilder location = new StringBuilder();

final List<String> baseDirValues = f.getFeature("dita.basedir-resource-directory");
if (Boolean.parseBoolean(baseDirValues == null || baseDirValues.isEmpty() ? null : baseDirValues.get(0))) {
final List<Value> baseDirValues = f.getFeature("dita.basedir-resource-directory");
if (
Boolean.parseBoolean(baseDirValues == null || baseDirValues.isEmpty() ? null : baseDirValues.get(0).value())
) {
location.append("${dita.dir}");
} else if (f.pluginDir().getAbsolutePath().startsWith(f.ditaDir().getAbsolutePath())) {
location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ final class ImportStringsAction extends ImportAction {
public void getResult(final ContentHandler buf) throws SAXException {
final String templateFilePath = paramTable.get(FileGenerator.PARAM_TEMPLATE);
for (final Value value : valueSet) {
final String path;
if (value instanceof Value.PathValue pathValue) {
path = pathValue.getPath();
} else {
logger.error("String import must be a file feature: " + value.value());
continue;
}
buf.startElement(NULL_NS_URI, "stringfile", "stringfile", new AttributesBuilder().build());
final char[] location = FileUtils.getRelativeUnixPath(templateFilePath, value.value()).toCharArray();
final char[] location = FileUtils.getRelativeUnixPath(templateFilePath, path).toCharArray();
buf.characters(location, 0, location.length);
buf.endElement(NULL_NS_URI, "stringfile", "stringfile");
}
Expand Down
34 changes: 20 additions & 14 deletions src/main/java/org/dita/dost/platform/ImportXSLAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.dita.dost.platform;

import java.net.URI;
import org.dita.dost.platform.Value.PathValue;
import org.dita.dost.util.FileUtils;
import org.dita.dost.util.URLUtils;
import org.dita.dost.util.XMLUtils.AttributesBuilder;
Expand All @@ -27,26 +28,31 @@ final class ImportXSLAction extends ImportAction {
@Override
public void getResult(final ContentHandler buf) throws SAXException {
for (final Value value : valueSet) {
final URI href = getHref(value);
buf.startElement(
"http://www.w3.org/1999/XSL/Transform",
"import",
"xsl:import",
new AttributesBuilder().add("href", href.toString()).build()
);
buf.endElement("http://www.w3.org/1999/XSL/Transform", "import", "xsl:import");
if (value instanceof PathValue pathValue) {
final URI href = getHref(pathValue);
buf.startElement(
"http://www.w3.org/1999/XSL/Transform",
"import",
"xsl:import",
new AttributesBuilder().add("href", href.toString()).build()
);
buf.endElement("http://www.w3.org/1999/XSL/Transform", "import", "xsl:import");
} else {
logger.error("XSLT import must be a file feature");
}
}
}

private URI getHref(final Value value) {
Plugin features = featureTable.get(value.id());
private URI getHref(final PathValue value) {
final Plugin features = featureTable.get(value.pluginId());
final URI pluginDir = features.pluginDir().toURI();
final URI templateFile = URLUtils.toFile(value.value()).toURI().normalize();
final String path = value.getPath();
final URI templateFile = URLUtils.toFile(path).toURI().normalize();
final URI template = pluginDir.relativize(templateFile);
if (value.id() == null || template.isAbsolute()) {
if (value.pluginId() == null || template.isAbsolute()) {
final String templateFilePath = paramTable.get(FileGenerator.PARAM_TEMPLATE);
return URLUtils.toURI(FileUtils.getRelativeUnixPath(templateFilePath, value.value()));
return URLUtils.toURI(FileUtils.getRelativeUnixPath(templateFilePath, path));
}
return URI.create("plugin:" + value.id() + ":" + template);
return URI.create("plugin:" + value.pluginId() + ":" + template);
}
}
7 changes: 6 additions & 1 deletion src/main/java/org/dita/dost/platform/InsertAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ public void getResult(final ContentHandler retBuf) throws SAXException {
setContentHandler(retBuf);
try {
for (final Value fileName : fileNameSet) {
currentFile = fileName.value();
if (fileName instanceof Value.PathValue pathValue) {
currentFile = pathValue.getPath();
} else {
logger.error("Catalog import must be a file feature: " + fileName.value());
continue;
}
reader.parse(currentFile);
}
} catch (SAXException | RuntimeException e) {
Expand Down
34 changes: 20 additions & 14 deletions src/main/java/org/dita/dost/platform/Integrator.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.xml.XMLConstants;
Expand Down Expand Up @@ -334,8 +335,10 @@ private void integrate() throws Exception {
for (final Entry<String, Plugin> e : pluginTable.entrySet()) {
final Plugin f = e.getValue();
final String name = "plugin." + e.getKey() + ".dir";
final List<String> baseDirValues = f.getFeature("dita.basedir-resource-directory");
if (Boolean.parseBoolean(baseDirValues == null || baseDirValues.isEmpty() ? null : baseDirValues.get(0))) {
final List<Value> baseDirValues = f.getFeature("dita.basedir-resource-directory");
if (
Boolean.parseBoolean(baseDirValues == null || baseDirValues.isEmpty() ? null : baseDirValues.get(0).value())
) {
//configuration.put(name, ditaDir.getAbsolutePath());
configuration.put(name, ".");
} else {
Expand Down Expand Up @@ -647,7 +650,14 @@ private Collection<File> relativize(final Collection<Value> src) {
final File base = new File(ditaDir, "dummy");
return src
.stream()
.map(lib -> toFile(lib.value()))
.flatMap(lib -> {
if (lib instanceof Value.PathValue path) {
return Stream.of(toFile(path.getPath()));
} else {
logger.error("Library import must be a file feature: " + lib.value());
return Stream.empty();
}
})
.map(libFile -> {
if (!libFile.exists()) {
throw new IllegalArgumentException("Library file not found: " + libFile.getAbsolutePath());
Expand Down Expand Up @@ -855,14 +865,10 @@ private String readExtensions(final String featureName) {
private boolean loadPlugin(final String plugin) {
if (checkPlugin(plugin)) {
final Plugin pluginFeatures = pluginTable.get(plugin);
final Map<String, List<String>> featureSet = pluginFeatures.features();
for (final Map.Entry<String, List<String>> currentFeature : featureSet.entrySet()) {
final Map<String, List<Value>> featureSet = pluginFeatures.features();
for (final Map.Entry<String, List<Value>> currentFeature : featureSet.entrySet()) {
final String key = currentFeature.getKey();
final List<Value> values = currentFeature
.getValue()
.stream()
.map(val -> new Value(plugin, val))
.collect(Collectors.toList());
final List<Value> values = currentFeature.getValue();
if (!extensionPoints.contains(key)) {
throw new RuntimeException("Plug-in %s uses an undefined extension point %s".formatted(plugin, key));
}
Expand All @@ -880,7 +886,7 @@ private boolean loadPlugin(final String plugin) {
for (final String templateName : pluginFeatures.templates()) {
final String template = new File(pluginFeatures.pluginDir().toURI().resolve(templateName)).getAbsolutePath();
final String templatePath = FileUtils.getRelativeUnixPath(ditaDir + File.separator + "dummy", template);
templateSet.put(templatePath, new Value(pluginFeatures.pluginId(), templateName));
templateSet.put(templatePath, new Value.StringValue(pluginFeatures.pluginId(), templateName));
}
loadedPlugin.add(plugin);
return true;
Expand Down Expand Up @@ -1030,17 +1036,17 @@ public void setLogger(final DITAOTLogger logger) {
* @return combined extension value, {@code null} if no value available
*/
static String getValue(final Map<String, Plugin> featureTable, final String extension) {
final List<String> buf = new ArrayList<>();
final List<Value> buf = new ArrayList<>();
for (final Plugin f : featureTable.values()) {
final List<String> v = f.getFeature(extension);
final List<Value> v = f.getFeature(extension);
if (v != null) {
buf.addAll(v);
}
}
if (buf.isEmpty()) {
return null;
} else {
return StringUtils.join(buf, ",");
return buf.stream().map(Value::value).collect(Collectors.joining(","));
}
}

Expand Down
Loading

0 comments on commit 2505f09

Please sign in to comment.