Last active
May 14, 2019 10:18
-
-
Save WayneSan/3432958 to your computer and use it in GitHub Desktop.
Wrap W3C DOM Element as a fluent interface
This file contains hidden or 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 javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
public class ElementWrapper { | |
Document doc; | |
Element elemt; | |
ElementWrapper parent; | |
public ElementWrapper( String name ) { | |
this( name, null ); | |
} | |
public ElementWrapper( String name, ElementWrapper parent ) { | |
if ( parent == null ) { | |
try { | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
doc = builder.newDocument(); | |
elemt = doc.createElement( name ); | |
doc.appendChild( elemt ); | |
} catch ( ParserConfigurationException e ) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} else { | |
this.parent = parent; | |
this.doc = parent.doc; | |
elemt = doc.createElement( name ); | |
} | |
} | |
public ElementWrapper add( String name ) { | |
ElementWrapper new_elemt_wp = new ElementWrapper( name, this ); | |
elemt.appendChild( new_elemt_wp.getElement() ); | |
return new_elemt_wp; | |
} | |
public ElementWrapper set( String name, String value ) { | |
Element tmp_elemt = doc.createElement( name ); | |
tmp_elemt.setTextContent( value ); | |
elemt.appendChild( tmp_elemt ); | |
return this; | |
} | |
public ElementWrapper end() { | |
return parent; | |
} | |
public Document getDocument() { | |
return doc; | |
} | |
public Element getElement() { | |
return elemt; | |
} | |
} |
This file contains hidden or 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
public class testElementWrapper { | |
/** | |
* @param args | |
*/ | |
public static void main( String[] args ) { | |
ElementWrapper elemt1 = | |
new ElementWrapper( "TEMPLATE" ) | |
.set( "NAME", "test123" ) | |
.set( "CPU", "1" ) | |
.set( "MEMORY", "2048" ) | |
.add( "OS" ) | |
.set( "BOOT", "hd" ) | |
.set( "ARCH", "x86_64" ).end() | |
.add( "DISK" ).set( "IMAGE_ID", "1" ); | |
ElementWrapper elemt2 = | |
new ElementWrapper( "TEMPLATE" ) | |
.set( "NAME", "test456" ) | |
.set( "CPU", "2" ) | |
.set( "MEMORY", "4096" ) | |
.add( "OS" ) | |
.set( "BOOT", "hd" ) | |
.set( "ARCH", "x86_64" ).end() | |
.add( "DISK" ).set( "IMAGE_ID", "1" ); | |
//print xml | |
System.out.println( elemt1.toString() ); | |
System.out.println( elemt2.toString() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment