How to minimize the following code using java's features ... looking for some workaround with the switch-case statement
I've seen several question regarding switch-case design pattern / best practices but because I am new to java, I am having difficulties in implementing them.
Here is the code:
protected void readTree(Node node, Branch current)
{
Element element = null;
Document document = null;
if(current instanceof Element)
{
element = (Element) current;
}
else
{
document = (Document) current;
}
String nodeVal = node.getNodeValue();
String nodeName = node.getNodeName();
switch(node.getNodeType())
{
case ELEMENT_NODE:
readElement(node, current);
break;
case PROCESSING_INSTRUCTION_NODE:
if(current instanceof Element)
{
element.addProcessingInstruction(nodeName, nodeVal);
break;
}
document.addProcessingInstruction(nodeName, nodeVal);
break;
case COMMENT_NODE:
if(current instanceof Element)
{
element.addComment(nodeVal);
break;
}
document.addComment(nodeVal);
break;
case DOCUMENT_TYPE_NODE:
DocumentType domDocType = (DocumentType) node;
document.addDocType(domDocType.getName(), domDocType.getPublicId(), domDocType.getSystemId());
break;
case TEXT_NODE:
element.addText(nodeVal);
break;
case CDATA_SECTION_NODE:
element.addCDATA(nodeVal);
break;
case ENTITY_REFERENCE_NODE:
if(node.getFirstChild() != null)
{
element.addEntity(nodeName, node.getFirstChild().getNodeValue());
break;
}
element.addEntity(nodeName, "");
break;
case ENTITY_NODE:
element.addEntity(nodeName, nodeVal);
break;
default:
System.out.println("WARNING: Unknown node type: " + node.getNodeType());
}
}