Skip to content

Commit

Permalink
Merge pull request dita-ot#4407 from dita-ot/feature/refactor-java
Browse files Browse the repository at this point in the history
Refactor Java
  • Loading branch information
jelovirt authored Feb 15, 2024
2 parents 4c39e10 + 99a8a38 commit dc0518b
Show file tree
Hide file tree
Showing 43 changed files with 179 additions and 331 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/dita/dost/ant/DITAOTCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.tools.ant.BuildException;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/dita/dost/ant/ExtensibleAntInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,7 @@ private static Predicate<FileInfo> combine(final Collection<FileInfoFilterElem>
if (filters.isEmpty()) {
return f -> true;
}
final List<Predicate<FileInfo>> res = filters
.stream()
.map(FileInfoFilterElem::toFilter)
.collect(Collectors.toList());
final List<Predicate<FileInfo>> res = filters.stream().map(FileInfoFilterElem::toFilter).toList();
return f -> {
for (final Predicate<FileInfo> filter : res) {
if (filter.test(f)) {
Expand Down
35 changes: 14 additions & 21 deletions src/main/java/org/dita/dost/ant/types/JobMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
*/
package org.dita.dost.ant.types;

import static org.dita.dost.util.Constants.ANT_TEMP_DIR;
import static org.dita.dost.util.URLUtils.toFile;
import static org.dita.dost.util.URLUtils.toURI;

import java.io.File;
import java.net.URI;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.io.FilenameUtils;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.util.*;
import org.apache.tools.ant.util.FileNameMapper;
import org.dita.dost.ant.ExtensibleAntInvoker;
import org.dita.dost.util.Job;

Expand Down Expand Up @@ -70,23 +67,19 @@ public String[] mapFileName(String sourceFileName) {
if (fi == null) {
fi = job.getFileInfo(job.getInputDir().resolve(uri));
}
final String res;
switch (type) {
case TEMP:
res = fi.file.getPath();
break;
case RESULT:
if (fi.result == null) {
res = sourceFileName;
} else {
final URI base = job.getInputDir();
final URI rel = base.relativize(fi.result);
res = toFile(rel).getPath();
final String res =
switch (type) {
case TEMP -> fi.file.getPath();
case RESULT -> {
if (fi.result == null) {
yield sourceFileName;
} else {
final URI base = job.getInputDir();
final URI rel = base.relativize(fi.result);
yield toFile(rel).getPath();
}
}
break;
default:
throw new IllegalArgumentException();
}
};
return new String[] { extension != null ? (FilenameUtils.removeExtension(res) + extension) : res };
}

Expand All @@ -97,7 +90,7 @@ public String[] getValues() {
return Arrays
.stream(Type.values())
.map(t -> t.toString().toLowerCase())
.collect(Collectors.toList())
.toList()
.toArray(new String[Type.values().length]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dita/dost/chunk/ChunkModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ private List<ChunkOperation> rewriteCombineChunks(
.stream()
.filter(chunk -> chunk.operation() == COMBINE)
.map(chunk -> rewriteCombineChunk(mapFile, mapDoc, normalTopicRefs, rewriteMap, chunk).build())
.collect(Collectors.toUnmodifiableList());
.toList();
}

private ChunkBuilder rewriteCombineChunk(
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/dita/dost/chunk/ChunkOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

package org.dita.dost.chunk;

import static java.util.Collections.unmodifiableList;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -109,8 +107,8 @@ public ChunkBuilder addChild(final ChunkBuilder child) {
public ChunkOperation build() {
final URI src = this.src;
final URI dst = this.dst;
final List<ChunkOperation> cos = children.stream().map(ChunkBuilder::build).collect(Collectors.toList());
return new ChunkOperation(operation, src, dst, id, topicref, unmodifiableList(cos));
final List<ChunkOperation> cos = children.stream().map(ChunkBuilder::build).toList();
return new ChunkOperation(operation, src, dst, id, topicref, cos);
}
}
}
77 changes: 5 additions & 72 deletions src/main/java/org/dita/dost/index/TopicrefElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,15 @@
* Copyright 2005 IBM Corporation
*
* See the accompanying LICENSE file for applicable license.
*/
package org.dita.dost.index;

/**
* This class represent the topicref element in ditamap.
*
* @version 1.0 2005-06-03
*
* @author Wu, Zhi Qiang
* @param href The href attribute of the topicref element.
* @param format The format attribute of the topicref element.
* @param navtitle the navtitle attribute of topicref element.
* @since 1.0
*/
public final class TopicrefElement {

/** The href attribute of the topicref element. */
private String href = null;

/** The format attribute of the topicref element. */
private String format = null;
/**the navtitle attribute of topicref element.*/
private String navtitle = null;

/**
* Default constructor.
*/
public TopicrefElement() {}

/**
* Get the format attribute.
*
* @return Returns the format.
*/
public String getFormat() {
return format;
}

/**
* Set the format attribute with the given value.
*
* @param formatValue The format to set.
*/
public void setFormat(final String formatValue) {
format = formatValue;
}

/**
* Get href attribute.
*
* @return Returns the href.
*/
public String getHref() {
return href;
}

/**
* Set href attribute with the given value.
*
* @param hrefValue The href to set.
*/
public void setHref(final String hrefValue) {
href = hrefValue;
}

/**
* Set navtitle attribute with the given value.
* @param aNavtitle The navtitle to set.
*/
public void setNavTitle(final String aNavtitle) {
navtitle = aNavtitle;
}

/**
* Get navtitle attribute.
*
* @return Returns the navtitle.
*/
public String getNavTitle() {
return navtitle;
}
}
public record TopicrefElement(String href, String format, String navtitle) {}
17 changes: 8 additions & 9 deletions src/main/java/org/dita/dost/invoker/ArgumentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,21 @@ static Arguments.Argument getArgument(Element param) {
final String name = param.getAttribute("name");
final String type = param.getAttribute("type");
final String desc = param.getAttribute("desc");
switch (type) {
case "file":
return new Arguments.FileArgument(name, desc);
case "enum":
return switch (type) {
case "file" -> new Arguments.FileArgument(name, desc);
case "enum" -> {
final Set<String> vals = getChildElements(param).stream().map(XMLUtils::getText).collect(Collectors.toSet());
if (vals.size() == 2) {
for (Map.Entry<String, String> pair : TRUTHY_VALUES.entrySet()) {
if (vals.contains(pair.getKey()) && vals.contains(pair.getValue())) {
return new Arguments.BooleanArgument(name, desc, pair.getKey(), pair.getValue());
yield new Arguments.BooleanArgument(name, desc, pair.getKey(), pair.getValue());
}
}
}
return new Arguments.EnumArgument(name, desc, vals);
default:
return new Arguments.StringArgument(name, desc);
}
yield new Arguments.EnumArgument(name, desc, vals);
}
default -> new Arguments.StringArgument(name, desc);
};
}

private static Map<String, Arguments.Argument> PLUGIN_ARGUMENTS;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/dita/dost/invoker/DefaultLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ class DefaultLogger extends AbstractLogger implements BuildLogger {
/** PrintStream to write error messages to */
private PrintStream err;

/** Lowest level of message to write out */
//** Lowest level of message to write out */
// private int msgOutputLevel = Project.MSG_ERR;

/** Time of the start of the build */
private long startTime = System.currentTimeMillis();

// CheckStyle:ConstantNameCheck OFF - bc
/** Line separator */
//** Line separator */
// protected static final String lSep = StringUtils.LINE_SEP;
// CheckStyle:ConstantNameCheck ON

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/dita/dost/invoker/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public class Main extends org.apache.tools.ant.Main implements AntMain {
);
private static final String CONFIGURATION_FILE = ".ditaotrc";

@Deprecated
/** @deprecated since 4.2 */
@Deprecated
private static final String CONFIGURATION_FILE_OLD = "local.properties";

/**
Expand Down Expand Up @@ -609,7 +609,7 @@ List<Map<String, Object>> collectProperties(
});
final List<org.dita.dost.project.Project.Deliverable.Profile.DitaVal> ditavals = Stream
.concat(publications.profiles().ditavals().stream(), context.profiles().ditavals().stream())
.collect(Collectors.toList());
.toList();
if (!ditavals.isEmpty()) {
final String filters = ditavals
.stream()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dita/dost/log/AbstractLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class AbstractLogger extends MarkerIgnoringBase implements DITAO

protected int msgOutputLevel = Project.MSG_DEBUG;
protected boolean useColor;
protected boolean legacyFormat = Configuration.configuration
protected final boolean legacyFormat = Configuration.configuration
.getOrDefault("cli.log-format", "legacy")
.equals("legacy");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ URI getCommonBase(final URI left, final URI right) {
if (OS_NAME.toLowerCase().contains(OS_NAME_WINDOWS) && commons.size() <= 1) {
return null;
} else {
final String path = commons.stream().collect(Collectors.joining("/")) + "/";
final String path = String.join("/", commons) + "/";
return URLUtils.setPath(left, path);
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/dita/dost/module/ConrefPushModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Collection;
import java.util.Hashtable;
import java.util.Map;
import java.util.stream.Collectors;
import org.dita.dost.exception.DITAOTException;
import org.dita.dost.pipeline.AbstractPipelineInput;
import org.dita.dost.pipeline.AbstractPipelineOutput;
Expand All @@ -29,11 +28,7 @@ final class ConrefPushModule extends AbstractPipelineModuleImpl {

@Override
public AbstractPipelineOutput execute(final AbstractPipelineInput input) {
final Collection<FileInfo> fis = job
.getFileInfo(fileInfoFilter)
.stream()
.filter(f -> f.isConrefPush)
.collect(Collectors.toList());
final Collection<FileInfo> fis = job.getFileInfo(fileInfoFilter).stream().filter(f -> f.isConrefPush).toList();
if (!fis.isEmpty()) {
final ConrefPushReader reader = new ConrefPushReader();
reader.setLogger(logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,7 @@ private void outputResult() throws DITAOTException {
job.setInputMap(rootTemp);

//If root input file is marked resource only due to conref or other feature, remove that designation
if (resourceOnlySet.contains(rootFile)) {
resourceOnlySet.remove(rootFile);
}
resourceOnlySet.remove(rootFile);

job.setProperty(INPUT_DITAMAP_LIST_FILE_LIST, USER_INPUT_FILE_LIST_FILE);
final File inputfile = new File(job.tempDir, USER_INPUT_FILE_LIST_FILE);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/dita/dost/module/XmlFilterModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.dita.dost.exception.DITAOTException;
Expand Down Expand Up @@ -76,12 +75,11 @@ private List<XMLFilter> getProcessingPipe(final FileInfo fi) {
.stream()
.filter(p -> p.predicate.test(fi))
.map(FilterPair::newInstance)
.map(f -> {
.peek(f -> {
logger.debug("Configure filter " + f.getClass().getCanonicalName());
f.setCurrentFile(fileToParse);
f.setJob(job);
f.setLogger(logger);
return f;
})
.collect(Collectors.toList());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dita/dost/module/XsltModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public AbstractPipelineOutput execute(AbstractPipelineInput input) throws DITAOT
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
.toList();
for (Entry<File, File> entry : tmps) {
try {
logger.info("Move " + entry.getKey().toURI() + " to " + entry.getValue().toURI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ SubjectScheme getSubjectScheme(final XdmNode root) {
logger.debug("Loading subject schemes");
var enumerationDefs = root.select(Steps.descendant(SUBJECTSCHEME_ENUMERATIONDEF::matches)).toList();
if (!enumerationDefs.isEmpty()) {
enumerationDefs
.stream()
.forEach(enumerationDef -> {
final XdmNode schemeRoot = enumerationDef.select(Steps.ancestor(SUBMAP::matches)).findFirst().orElse(root);
var subjectDefinitions = subjectSchemeReader.getSubjectDefinition(schemeRoot);
subjectSchemeReader.processEnumerationDef(subjectDefinitions, enumerationDef);
});
enumerationDefs.forEach(enumerationDef -> {
final XdmNode schemeRoot = enumerationDef.select(Steps.ancestor(SUBMAP::matches)).findFirst().orElse(root);
var subjectDefinitions = subjectSchemeReader.getSubjectDefinition(schemeRoot);
subjectSchemeReader.processEnumerationDef(subjectDefinitions, enumerationDef);
});
return subjectSchemeReader.getSubjectSchemeMap();
} else {
return new SubjectScheme(Map.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void initFilters() {
* Init xml reader used for pipeline parsing.
*
* @param validate whether validate input file
* @throws SAXException parsing exception
* @throws DITAOTException parsing exception
*/
void initXMLReader(final boolean validate) throws DITAOTException {
try {
Expand Down Expand Up @@ -696,8 +696,7 @@ void outputResult() throws DITAOTException {

job.setProperty("tempdirToinputmapdir.relative.value", StringUtils.escapeRegExp(getPrefix(relativeRootFile)));

final Set<URI> res = new HashSet<>();
res.addAll(listFilter.getResourceOnlySet());
final Set<URI> res = new HashSet<>(listFilter.getResourceOnlySet());
res.removeAll(listFilter.getNormalProcessingRoleSet());
resourceOnlySet.addAll(res);

Expand Down
Loading

0 comments on commit dc0518b

Please sign in to comment.