Skip to content

Commit

Permalink
#7 can now add line comments to tags that will get serialised to SDL
Browse files Browse the repository at this point in the history
  • Loading branch information
SingingBush committed Jul 21, 2018
1 parent bd858c7 commit c556ee5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/java/com/singingbush/sdl/Tag.java
Expand Up @@ -341,6 +341,7 @@ public class Tag implements Serializable {
private final String namespace;
private final String name;

private String comment;
private List<SdlValue> values = new ArrayList();
private List<SdlValue> valuesView = Collections.unmodifiableList(values);
private Map<String,String> attributeToNamespace = new HashMap<>();
Expand Down Expand Up @@ -852,7 +853,15 @@ public String getNamespace() {
return namespace;
}

/**
public String getComment() {
return comment;
}

public void setComment(final String comment) {
this.comment = comment;
}

/**
* Add all the tags specified in the file at the given URL to this Tag.
*
* @param url A UTF8 encoded .sdl file
Expand Down Expand Up @@ -981,6 +990,13 @@ private String toString(@Nullable String linePrefix) {

final StringBuilder builder = new StringBuilder(linePrefix);

if(comment != null && !comment.isEmpty()) {
final String[] lines = comment.split("\n");
for (final String line : lines) {
builder.append("// ").append(line).append(newLine).append(linePrefix);
}
}

boolean skipValueSpace=false;
if("content".equals(name) && "".equals(namespace)) {
skipValueSpace=true;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/singingbush/sdl/TagBuilder.java
Expand Up @@ -17,6 +17,7 @@ public class TagBuilder {

private final String name;
private String namespace;
private String comment;
private List<SdlValue> values = new ArrayList<>();
private List<Tag> children = new ArrayList<>();
private Map<String, SdlValue> attributes = new HashMap<>();
Expand All @@ -41,6 +42,17 @@ public TagBuilder withNamespace(@NotNull final String name) {
return this;
}

/**
*
* @param comment a single line of text that will precede the tag when serialised
* @return this TagBuilder
* @since 2.0.2
*/
public TagBuilder withComment(@NotNull final String comment) {
this.comment = comment;
return this;
}

/**
* In SDL you can optionally have one or more values
* @param value an SDL object, see {@link SdlValue}
Expand Down Expand Up @@ -147,6 +159,7 @@ public TagBuilder withAttributes(@NotNull final Map<String, SdlValue> attributes
@NotNull
public Tag build() {
final Tag t = namespace != null? new Tag(namespace, name) : new Tag(name);
t.setComment(comment);
values.forEach(t::addValue);
children.forEach(t::addChild);
t.setAttributes(attributes); // attributes.forEach(t::setAttribute);
Expand Down

0 comments on commit c556ee5

Please sign in to comment.