Skip to content

Instantly share code, notes, and snippets.

@wizardjedi
Last active April 1, 2018 08:19
Show Gist options
  • Save wizardjedi/d5c94088410975c380ff9cc1005910cc to your computer and use it in GitHub Desktop.
Save wizardjedi/d5c94088410975c380ff9cc1005910cc to your computer and use it in GitHub Desktop.
xml writer api
class Main {
public static class XMLStringBuilder {
public static final String OPEN_BRACE = "<";
public static final String CLOSE_BRACE = ">";
public static final String CLOSE_SUFFIX = " />";
public static final String OPEN_CLOSE_TAG = "</";
public static final String ATTR_PREFIX = "=\"";
public static final String ATTR_SUFFIX = "\"";
protected StringBuilder stringBuilder = new StringBuilder();
protected boolean openingTag = false;
public XMLStringBuilder open(String tag, String... attributes) {
if (openingTag) {
throw new IllegalArgumentException("Tag have been already opened");
}
openingTag = true;
stringBuilder
.append(OPEN_BRACE)
.append(tag);
attributes(attributes);
return this;
}
public XMLStringBuilder tag(String tag, String body, String...attributes) {
open(tag, attributes);
body(body);
close(tag);
return this;
}
public XMLStringBuilder body(String body) {
if (openingTag) {
stringBuilder.append(CLOSE_BRACE);
openingTag = false;
}
stringBuilder.append(body);
return this;
}
public XMLStringBuilder close(String tag) {
if (openingTag) {
stringBuilder.append(CLOSE_SUFFIX);
openingTag = false;
} else {
stringBuilder.append(OPEN_CLOSE_TAG).append(tag).append(CLOSE_BRACE);
}
return this;
}
public XMLStringBuilder attributes(String... attributes) {
if (!openingTag) {
throw new IllegalArgumentException("Tag not open");
}
if (attributes!=null && attributes.length>0) {
for (int i = 0; i < attributes.length; i++) {
if (i % 2 == 0) {
stringBuilder
.append(" ")
.append(attributes[i]);
} else {
stringBuilder
.append(ATTR_PREFIX)
.append(attributes[i])
.append(ATTR_SUFFIX);
}
}
}
return this;
}
@Override
public String toString() {
return stringBuilder.toString();
}
}
public static void main(String... args) {
final XMLStringBuilder sb = new XMLStringBuilder();
sb.open("a","href", "http://yandex.ru");
sb.body("Click me");
sb.close("a");
System.out.println(sb.toString());
final XMLStringBuilder sb2 = new XMLStringBuilder();
sb2.open("a","href", "http://yandex.ru");
sb2.close("a");
System.out.println(sb2.toString());
final XMLStringBuilder sb3 = new XMLStringBuilder();
sb3.tag("a","Click me","href", "http://yandex.ru");
System.out.println(sb3.toString());
final XMLStringBuilder sb4 = new XMLStringBuilder();
sb4.close("div");
sb4.close("a");
System.out.println(sb4.toString());
}
}
final XMLStringBuilder wr = new XMLStringBuilder();

wr.opening("tag", "attr1", "attrvalue1");

wr.body("Tag body");

wr.close("tag");

wr.toString();
<tag attr1="attrvalue1">Tag body</tag>
wr.opening("tag", "attr1", "attrvalue1");
wr.close("tag");

wr.toString();
<tag attr1="attrvalue1" />
wr.tag("tag","Body","attr1","attrvalue1");

wr.toString();
<tag attr1="attrvalue1">Body</tag>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment