Skip to content

Commit c556ee5

Browse files
committed
#7 can now add line comments to tags that will get serialised to SDL
1 parent bd858c7 commit c556ee5

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/java/com/singingbush/sdl/Tag.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ public class Tag implements Serializable {
341341
private final String namespace;
342342
private final String name;
343343

344+
private String comment;
344345
private List<SdlValue> values = new ArrayList();
345346
private List<SdlValue> valuesView = Collections.unmodifiableList(values);
346347
private Map<String,String> attributeToNamespace = new HashMap<>();
@@ -852,7 +853,15 @@ public String getNamespace() {
852853
return namespace;
853854
}
854855

855-
/**
856+
public String getComment() {
857+
return comment;
858+
}
859+
860+
public void setComment(final String comment) {
861+
this.comment = comment;
862+
}
863+
864+
/**
856865
* Add all the tags specified in the file at the given URL to this Tag.
857866
*
858867
* @param url A UTF8 encoded .sdl file
@@ -981,6 +990,13 @@ private String toString(@Nullable String linePrefix) {
981990

982991
final StringBuilder builder = new StringBuilder(linePrefix);
983992

993+
if(comment != null && !comment.isEmpty()) {
994+
final String[] lines = comment.split("\n");
995+
for (final String line : lines) {
996+
builder.append("// ").append(line).append(newLine).append(linePrefix);
997+
}
998+
}
999+
9841000
boolean skipValueSpace=false;
9851001
if("content".equals(name) && "".equals(namespace)) {
9861002
skipValueSpace=true;

src/main/java/com/singingbush/sdl/TagBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class TagBuilder {
1717

1818
private final String name;
1919
private String namespace;
20+
private String comment;
2021
private List<SdlValue> values = new ArrayList<>();
2122
private List<Tag> children = new ArrayList<>();
2223
private Map<String, SdlValue> attributes = new HashMap<>();
@@ -41,6 +42,17 @@ public TagBuilder withNamespace(@NotNull final String name) {
4142
return this;
4243
}
4344

45+
/**
46+
*
47+
* @param comment a single line of text that will precede the tag when serialised
48+
* @return this TagBuilder
49+
* @since 2.0.2
50+
*/
51+
public TagBuilder withComment(@NotNull final String comment) {
52+
this.comment = comment;
53+
return this;
54+
}
55+
4456
/**
4557
* In SDL you can optionally have one or more values
4658
* @param value an SDL object, see {@link SdlValue}
@@ -147,6 +159,7 @@ public TagBuilder withAttributes(@NotNull final Map<String, SdlValue> attributes
147159
@NotNull
148160
public Tag build() {
149161
final Tag t = namespace != null? new Tag(namespace, name) : new Tag(name);
162+
t.setComment(comment);
150163
values.forEach(t::addValue);
151164
children.forEach(t::addChild);
152165
t.setAttributes(attributes); // attributes.forEach(t::setAttribute);

0 commit comments

Comments
 (0)