Skip to content

Commit

Permalink
Merge pull request #511 from yangzhm/master
Browse files Browse the repository at this point in the history
add project of builder pattern
  • Loading branch information
onlyliuxin authored Jul 25, 2017
2 parents 9e514d7 + 3c2f342 commit 01331f6
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 0 deletions.
42 changes: 42 additions & 0 deletions students/495232796/OOD/builderpattern/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.coderising</groupId>
<artifactId>ood-assignment</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>ood-assignment</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

</dependencies>
<repositories>
<repository>
<id>aliyunmaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.coderising.dp.builder;

public class TagBuilder {
private TagNode rootNode = null;
private TagNode curNode = null;
private TagNode parentNode = null;

public TagBuilder(String rootTagName){
rootNode = new TagNode(rootTagName);
curNode = rootNode;
}

public TagBuilder addChild(String childTagName){
TagNode tn = new TagNode(childTagName);
curNode.add(tn);
parentNode = curNode;
curNode = tn;

return this;
}
public TagBuilder addSibling(String siblingTagName){
TagNode tn = new TagNode(siblingTagName);
parentNode.add(tn);
curNode = tn;

return this;
}
public TagBuilder setAttribute(String name, String value){
curNode.setAttribute(name, value);

return this;
}
public TagBuilder setText(String value){
curNode.setValue(value);

return this;
}
public String toXML(){
return rootNode.toXML();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.coderising.dp.builder;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TagBuilderTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testToXML() {

TagBuilder builder = new TagBuilder("order");

String xml = builder.addChild("line-items")
.addChild("line-item").setAttribute("pid", "P3677").setAttribute("qty", "3")
.addSibling("line-item").setAttribute("pid", "P9877").setAttribute("qty", "10")
.toXML();

String expected = "<order>"
+ "<line-items>"
+ "<line-item pid=\"P3677\" qty=\"3\"/>"
+ "<line-item pid=\"P9877\" qty=\"10\"/>"
+ "</line-items>"
+ "</order>";

System.out.println(xml);
System.out.println(expected.equals(xml));
assertEquals(expected, xml);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.coderising.dp.builder;

import java.util.ArrayList;
import java.util.List;

public class TagNode {
private String tagName;
private String tagValue;
private List<TagNode> children = new ArrayList<>();
private List<Attribute> attributes = new ArrayList<>();

public TagNode(String name){
this.tagName = name;
}
public void add(TagNode child){
this.children.add(child);
}
public void setAttribute(String name, String value) {
Attribute attr = findAttribute(name);
if(attr != null){
attr.value = value;
} else {
attributes.add(new Attribute(name,value));
}
}
private Attribute findAttribute(String name){
for(Attribute attr : attributes){
if(attr.name.equals(name)){
return attr;
}
}
return null;
}
public void setValue(String value) {
this.tagValue = value;

}
public String getTagName() {
return tagName;
}
public List<TagNode> getChildren() {
return children;
}

private static class Attribute{
public Attribute(String name, String value) {
this.name = name;
this.value = value;
}
String name;
String value;

}
public String toXML(){
return toXML(this);
}
private String toXML(TagNode node){
StringBuilder buffer = new StringBuilder();
buffer.append("<").append(node.tagName);
if(node.attributes.size()> 0){
for(int i=0;i<node.attributes.size();i++){
Attribute attr = node.attributes.get(i);
buffer.append(" ").append(toXML(attr));
}
}
if(node.children.size()== 0){
buffer.append("/>");
return buffer.toString();
}
buffer.append(">");
for(TagNode childNode : node.children){
buffer.append(toXML(childNode));
}
buffer.append("</").append(node.tagName).append(">");


return buffer.toString();
}
private String toXML(Attribute attr){
return attr.name+"=\""+attr.value + "\"";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.coderising.dp.main;

import com.coderising.dp.builder.TagBuilderTest;

public class TestAll {

public void testTag() {
TagBuilderTest tt = new TagBuilderTest();
tt.testToXML();
}

public static void main(String[] args) {
TestAll ta = new TestAll();
ta.testTag();
}

}

0 comments on commit 01331f6

Please sign in to comment.