Skip to content

Commit

Permalink
#18 Export document via SnakeYAML nodes (draft)
Browse files Browse the repository at this point in the history
- Fix issues with new lines
- Remove unused code
  • Loading branch information
ljacqu committed Jul 4, 2023
1 parent ea4dd7f commit 4fd80fa
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 373 deletions.
114 changes: 49 additions & 65 deletions src/main/java/ch/jalu/configme/resource/PropertyPathTraverser.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,63 @@
package ch.jalu.configme.resource;

import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.utils.CollectionUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
* Helper class for the export of properties: it keeps track of the previously traversed property
* and returns which path parts are new and defines the level of indentation.
* and returns which path parts are new.
* <p>
* For example if the property for path {@code config.datasource.mysql.type} was exported and we now
* For example, if the property for path {@code config.datasource.mysql.type} was exported and we now
* encounter the property for path {@code config.datasource.driver.version}, the newly encountered
* sections are {@code driver} and {@code version}.
*/
public class PropertyPathTraverser { // todo: delete?
public class PropertyPathTraverser {

private final ConfigurationData configurationData;
/** Contains all path elements besides the last, e.g. {datasource, mysql} for "datasource.mysql.table". */
private List<String> parentPathElements = new ArrayList<>(0);
private String parentPathElements;
private boolean isFirstProperty = true;

public PropertyPathTraverser(@NotNull ConfigurationData configurationData) {
this.configurationData = configurationData;
}

/**
* Returns all path elements for the given property that have not been traversed yet.
*
* @param pathElements all elements that make up the path of the property
* @return the new path elements
*/
public @NotNull List<PathElement> getPathElements(@NotNull List<String> pathElements) {
List<String> commonPathParts = CollectionUtils.filterCommonStart(
parentPathElements, pathElements.subList(0, pathElements.size() - 1));
List<String> newPathParts = CollectionUtils.getRange(pathElements, commonPathParts.size());

parentPathElements = pathElements.subList(0, pathElements.size() - 1);
public @NotNull List<PathElement> getPathElements(@NotNull String path) {
String[] pathParts = path.split("\\.");
int totalParts = pathParts.length;
int indentationOfFirstNewPart = returnIndentationOfFirstNewPathPart(path);
parentPathElements = path;

StringBuilder fullPathBuilder = new StringBuilder();
List<PathElement> pathElements = new ArrayList<>(totalParts);
int indentation = 0;
for (int i = 0; i < totalParts; ++i) {
fullPathBuilder.append(pathParts[i]);
PathElement element = new PathElement(indentation, pathParts[i], fullPathBuilder.toString(), isFirstProperty);
element.setEndOfPath(i == totalParts - 1);
element.setFirstOfGroup(indentationOfFirstNewPart == indentation);
pathElements.add(element);

int indentationLevel = commonPathParts.size();
String prefix = commonPathParts.isEmpty() ? "" : String.join(".", commonPathParts) + ".";
return convertToPathElements(indentationLevel, prefix, newPathParts);
}

private @NotNull List<PathElement> convertToPathElements(int indentation, @NotNull String prefix,
@NotNull List<String> elements) {
List<PathElement> pathElements = new ArrayList<>(elements.size());
for (String element : elements) {
List<String> comments = isFirstProperty
? getCommentsIncludingRoot(prefix + element)
: configurationData.getCommentsForSection(prefix + element);
pathElements.add(new PathElement(indentation, element, comments, isFirstProperty));
isFirstProperty = false;
prefix += element + ".";
++indentation;
fullPathBuilder.append(".");
isFirstProperty = false;
}
pathElements.get(0).setFirstOfGroup(true);
parentPathElements = path;
return pathElements;
}

private @NotNull List<String> getCommentsIncludingRoot(@NotNull String path) {
List<String> rootComments = configurationData.getCommentsForSection("");
if ("".equals(path)) {
return rootComments;
private int returnIndentationOfFirstNewPathPart(String path) {
if (parentPathElements == null) {
return 0;
}
List<String> sectionComments = configurationData.getCommentsForSection(path);
// One or the other list might be empty, but we only do this once so we can ignore performance considerations
if (sectionComments.isEmpty()) {
return rootComments;

int minLength = Math.min(parentPathElements.length(), path.length());
int i = 0;
int indentation = 0;
while (i < minLength && path.charAt(i) == parentPathElements.charAt(i)) {
if (path.charAt(i) == '.') {
++indentation;
}
++i;
}
List<String> allComments = new ArrayList<>(rootComments);
allComments.addAll(sectionComments);
return allComments;
return indentation;
}

/**
Expand All @@ -83,15 +68,16 @@ public static class PathElement {

private final int indentationLevel;
private final String name;
private List<String> comments;
private final String fullPath;
private final boolean isFirstElement;
private boolean isFirstOfGroup;
private boolean isEndOfPath;

public PathElement(int indentationLevel, @NotNull String name, @NotNull List<String> comments,
public PathElement(int indentationLevel, @NotNull String name, @NotNull String fullPath,
boolean isFirstElement) {
this.indentationLevel = indentationLevel;
this.name = name;
this.comments = comments;
this.fullPath = fullPath;
this.isFirstElement = isFirstElement;
}

Expand All @@ -103,17 +89,8 @@ public int getIndentationLevel() {
return name;
}

public @NotNull List<String> getComments() {
return comments;
}

public void addComments(@NotNull List<String> commentsToAdd) {
if (!commentsToAdd.isEmpty()) {
if (comments.getClass() != ArrayList.class) {
comments = new ArrayList<>(comments);
}
comments.addAll(commentsToAdd);
}
public @NotNull String getFullPath() {
return fullPath;
}

public boolean isFirstElement() {
Expand All @@ -127,6 +104,13 @@ public boolean isFirstOfGroup() {
void setFirstOfGroup(boolean firstOfGroup) {
isFirstOfGroup = firstOfGroup;
}
}

public boolean isEndOfPath() {
return isEndOfPath;
}

void setEndOfPath(boolean isEndOfPath) {
this.isEndOfPath = isEndOfPath;
}
}
}
Loading

0 comments on commit 4fd80fa

Please sign in to comment.