Created
January 27, 2017 18:31
-
-
Save anshdivu/50f6201cdba207f2fbf3c457f456524b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
import org.apache.commons.lang3.StringUtils; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.Text; | |
public class XmlTag { | |
private String tagName; | |
private Collection<XmlTag> children; | |
private String value; | |
private boolean ignoreBlank = false; | |
private boolean isTextNode = false; | |
private XmlTag() { | |
} | |
public static XmlTag empty() { | |
return new XmlTag(); | |
} | |
public static XmlTag text(String tagName, String value) { | |
return new XmlTag(tagName, value, true, true); | |
} | |
public static XmlTag textDontIgnoreBlank(String tagName, String value) { | |
return new XmlTag(tagName, value, true, false); | |
} | |
public static XmlTag nested(String tagName, Collection<XmlTag> children) { | |
return new XmlTag(tagName, children); | |
} | |
public static XmlTag nested(String tagName, XmlTag... children) { | |
return new XmlTag(tagName, Arrays.asList(children)); | |
} | |
private XmlTag(String tagName, String value, boolean isTextNode, boolean ignoreBlank) { | |
this.tagName = tagName; | |
this.value = value; | |
this.isTextNode = isTextNode; | |
this.ignoreBlank = ignoreBlank; | |
} | |
private XmlTag(String tagName, Collection<XmlTag> children) { | |
this.tagName = tagName; | |
this.children = children; | |
} | |
public Node appendToDoc(Document doc) { | |
return appendToRoot(doc, doc); | |
} | |
public Node appendToRoot(Document creator, Node root) { | |
if (tagName == null) { | |
return null; | |
} | |
Node childRoot = creator.createElement(tagName); | |
if (isTextNode) { | |
return createTextNode(creator, root, childRoot); | |
} | |
return createNodes(creator, root, childRoot); | |
} | |
private Node createNodes(Document creator, Node root, Node childRoot) { | |
Collection<Node> childNodes = createChildNodes(creator, childRoot); | |
for (Node node : childNodes) { | |
childRoot.appendChild(node); | |
} | |
if(childNodes.size() != 0) { | |
root.appendChild(childRoot); | |
return childRoot; | |
} | |
return null; | |
} | |
private Collection<Node> createChildNodes(Document creator, Node root) { | |
if (children == null) { | |
return Collections.emptyList(); | |
} | |
List<Node> list = new ArrayList<Node>(); | |
for (XmlTag child : children) { | |
Node childNode = child.appendToRoot(creator, root); | |
if (childNode != null) { | |
list.add(childNode); | |
} | |
} | |
return list; | |
} | |
private Node createTextNode(Document creator, Node root, Node childRoot) { | |
if (appendTextNode(creator, childRoot)) { | |
root.appendChild(childRoot); | |
return childRoot; | |
} | |
return null; | |
} | |
private boolean appendTextNode(Document creator, Node root) { | |
if (!isTextAvailable()) { | |
return false; | |
} | |
Text text = creator.createTextNode(value); | |
root.appendChild(text); | |
return true; | |
} | |
private boolean isTextAvailable() { | |
if (ignoreBlank) { | |
return StringUtils.isNotBlank(value); | |
} | |
return value != null; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((children == null) ? 0 : children.hashCode()); | |
result = prime * result + (ignoreBlank ? 1231 : 1237); | |
result = prime * result + (isTextNode ? 1231 : 1237); | |
result = prime * result + ((tagName == null) ? 0 : tagName.hashCode()); | |
result = prime * result + ((value == null) ? 0 : value.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) { | |
return true; | |
} | |
if (obj == null) { | |
return false; | |
} | |
if (getClass() != obj.getClass()) { | |
return false; | |
} | |
XmlTag other = (XmlTag) obj; | |
if (children == null) { | |
if (other.children != null) { | |
return false; | |
} | |
} else if (!children.equals(other.children)) { | |
return false; | |
} | |
if (ignoreBlank != other.ignoreBlank) { | |
return false; | |
} | |
if (isTextNode != other.isTextNode) { | |
return false; | |
} | |
if (tagName == null) { | |
if (other.tagName != null) { | |
return false; | |
} | |
} else if (!tagName.equals(other.tagName)) { | |
return false; | |
} | |
if (value == null) { | |
if (other.value != null) { | |
return false; | |
} | |
} else if (!value.equals(other.value)) { | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public String toString() { | |
StringBuilder builder = new StringBuilder(); | |
builder.append("XmlTag [tagName=").append(tagName); | |
builder.append(", value=").append(value); | |
builder.append(", ignoreBlank=").append(ignoreBlank); | |
builder.append(", isTextNode=").append(isTextNode); | |
builder.append(", children=").append(children); | |
builder.append("]"); | |
return builder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment