Permalink
Browse files

added link and anchor

  • Loading branch information...
1 parent be93c70 commit ece2daf600a4df9e2f002ee95e6b683fd2609d6f @digitalsanctum committed Aug 3, 2010
Showing with 188 additions and 117 deletions.
  1. +92 −59 .idea/workspace.xml
  2. +11 −0 src/Anchor.java
  3. +12 −18 src/Body.java
  4. +1 −1 src/Component.java
  5. +21 −3 src/Head.java
  6. +11 −0 src/Link.java
  7. +2 −18 src/LinkType.java
  8. +7 −4 src/Main.java
  9. +18 −8 src/Page.java
  10. +5 −0 src/Script.java
  11. +8 −6 src/Tag.java
View
View
@@ -0,0 +1,11 @@
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author shane
+ * @since Aug 2, 2010 11:52:10 PM
+ */
+public class Anchor extends Component {
+ public Tag getTag() {
+ return Tag.A;
+ }
+}
View
@@ -9,39 +9,33 @@ public Tag getTag() {
return Tag.BODY;
}
- public Body addP(String id) {
- addChild(new Paragraph().addAttribute("id", id));
+ public Body addP(String body) {
+ addChild(new Paragraph().setBody(body));
return this;
}
- public Body addDiv(String id) {
- addChild(new Div().addAttribute("id", id));
+ public Body addP(String body, String... attributes) {
+ addChild(new Paragraph().setBody(body).withAttributes(attributes));
return this;
}
- public Body addP(String id, String body) {
- addChild(
- new Paragraph()
- .addAttribute("id", id)
- .setBody(body));
+ public Body addDiv(String body) {
+ addChild(new Div().setBody(body));
return this;
}
- public Body addDiv(String id, String body) {
- addChild(
- new Div()
- .addAttribute("id", id)
- .setBody(body));
+ public Body addDiv(String body, String... attributes) {
+ addChild(new Div().setBody(body).withAttributes(attributes));
return this;
}
- public Body addP(String... attributes) {
- addChild(new Paragraph().withAttributes(attributes));
+ public Body addDiv(String... attributes) {
+ addChild(new Div().withAttributes(attributes));
return this;
}
- public Body addDiv(String... attributes) {
- addChild(new Div().withAttributes(attributes));
+ public Body addAnchor(String title, String... attributes) {
+ addChild(new Anchor().setBody(title).withAttributes(attributes));
return this;
}
}
View
@@ -136,7 +136,7 @@ public Component addAttribute(String key, String value) {
public Component withAttributes(String... attributes) {
for (String attribute : attributes) {
- String[] keyVal = attribute.split(":");
+ String[] keyVal = attribute.split("=");
addAttribute(keyVal[0], keyVal[1]);
}
return this;
View
@@ -11,6 +11,7 @@
public class Head extends Component {
private static Map<String, Component> metaTags = new HashMap<String, Component>();
+ private static Map<String, Link> links = new HashMap<String, Link>();
private static Map<String, Script> scripts = new HashMap<String, Script>();
public Tag getTag() {
@@ -22,6 +23,13 @@ public Head addMeta(Component meta) {
return this;
}
+ public Head addLink(String... attributes) {
+ Link link = new Link();
+ link.withAttributes(attributes);
+ links.put(link.getId(), link);
+ return this;
+ }
+
public Head addMeta(String... attributes) {
Meta m = new Meta();
m.withAttributes(attributes);
@@ -36,9 +44,10 @@ public Head addScript(ScriptType type, String... attributes) {
return this;
}
- public Head addStylesheet(String path) {
-// todo
-
+ public Head addStylesheet(String href) {
+ Link link = new Link();
+ link.withAttributes("href=" + href, "rel=stylesheet", "type=text/css", "media=screen, projection");
+ links.put(link.getId(), link);
return this;
}
@@ -61,12 +70,21 @@ public boolean hasScripts() {
return scripts != null && !scripts.isEmpty();
}
+ public boolean hasLinks() {
+ return links != null && !scripts.isEmpty();
+ }
+
public Head build() {
if (hasMetaTags()) {
for(Component m : metaTags.values()) {
addChild(m);
}
}
+ if (hasLinks()) {
+ for(Link l: links.values()) {
+ addChild(l);
+ }
+ }
if (hasScripts()) {
for(Script s: scripts.values()) {
addChild(s);
View
@@ -0,0 +1,11 @@
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author shane
+ * @since Aug 2, 2010 11:35:28 PM
+ */
+public class Link extends Component {
+ public Tag getTag() {
+ return Tag.LINK;
+ }
+}
View
@@ -5,22 +5,6 @@
* Time: 9:58:22 PM
*/
public enum LinkType {
- STYLESHEET("stylesheet", "text/css"),
- SHORTCUT_ICON("Shortcut Icon", "image/x-icon");
-
- private String rel;
- private String type;
-
- LinkType(String rel, String type) {
- this.rel = rel;
- this.type = type;
- }
-
- public String getRel() {
- return rel;
- }
-
- public String getType() {
- return type;
- }
+ A,
+ LINK
}
View
@@ -8,10 +8,13 @@
public static void main(String[] args) {
System.out.println(
new Page("some title")
- .addMeta("name:author", "content:Shane Witbeck")
- .addJavaScript("src:../scripts/myscript.js")
- .addDiv("some-id", "content")
- .addP("pid", "some paragraph")
+ .addMeta("name=author", "content=Shane Witbeck")
+ .addStylesheet("http://yahoo.com/blah.css")
+ .addJavaScript("src=../scripts/myscript.js")
+ .addDiv(null, "id=wam")
+ .addDiv("content", "id=bam")
+ .addP("some paragraph", "class=someclass")
+ .addAnchor("Google", "href=http://google.com")
.getMarkup()
);
}
View
@@ -26,27 +26,32 @@ public Page(DocType docType) {
addChild(body);
}
+ public Page addAnchor(String title, String... attributes) {
+ body.addAnchor(title, attributes);
+ return this;
+ }
+
public Tag getTag() {
return Tag.HTML;
}
- public Page addP(String id) {
- body.addP(id);
+ public Page addP(String bodyContent) {
+ body.addP(bodyContent);
return this;
}
- public Page addDiv(String id) {
- body.addDiv(id);
+ public Page addP(String bodyContent, String... attributes) {
+ body.addP(bodyContent, attributes);
return this;
}
- public Page addP(String id, String bodyContent) {
- body.addP(id, bodyContent);
+ public Page addDiv(String bodyContent) {
+ body.addDiv(bodyContent);
return this;
}
- public Page addDiv(String id, String bodyContent) {
- body.addDiv(id, bodyContent);
+ public Page addDiv(String bodyContent, String... attributes) {
+ body.addDiv(bodyContent, attributes);
return this;
}
@@ -70,6 +75,11 @@ public Page addJavaScript(String... attributes) {
return this;
}
+ public Page addStylesheet(String href) {
+ head.addStylesheet(href);
+ return this;
+ }
+
public Page addTitle(String title) {
head.addTitle(title);
return this;
View
@@ -10,6 +10,11 @@
public Script(ScriptType scriptType) {
this.scriptType = scriptType;
+ if (scriptType != null) addAttribute("type", scriptType.getType());
+ }
+
+ public ScriptType getScriptType() {
+ return scriptType;
}
public Tag getTag() {
View
@@ -7,14 +7,16 @@
* @since Jul 30, 2010 10:51:32 AM
*/
public enum Tag {
- HTML(true),
- TITLE(true),
- HEAD(true),
+ A(true),
BODY(true),
- SCRIPT(true),
- META(false),
DIV(true),
- P(true);
+ HEAD(true),
+ HTML(true),
+ LINK(true),
+ META(false),
+ P(true),
+ SCRIPT(true),
+ TITLE(true);
private boolean symmetrical;

0 comments on commit ece2daf

Please sign in to comment.