You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by ad...@apache.org on 2014/06/25 12:28:48 UTC

[1/2] git commit: WICKET-5629 Add an HeaderItem for meta data tags such as or canonical

Repository: wicket
Updated Branches:
  refs/heads/wicket-6.x 22bf3cabb -> 4bfa81eb7


WICKET-5629 Add an HeaderItem for meta data tags such as <meta> or
canonical <link>


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/87654b25
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/87654b25
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/87654b25

Branch: refs/heads/wicket-6.x
Commit: 87654b252b36defb4126b9e2c68b7ef590116246
Parents: 22bf3ca
Author: andrea del bene <ad...@apache.org>
Authored: Tue Jun 24 21:17:36 2014 +0200
Committer: Andrea Del Bene <an...@gmail.com>
Committed: Wed Jun 25 12:07:33 2014 +0200

----------------------------------------------------------------------
 .../wicket/markup/head/MetaDataHeaderItem.java  | 198 +++++++++++++++++++
 .../markup/head/MetaDataHeaderItemTest.java     |  45 +++++
 2 files changed, 243 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/87654b25/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java b/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
new file mode 100644
index 0000000..ea55abe
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
@@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.head;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.request.Response;
+import org.apache.wicket.util.value.ValueMap;
+
+/**
+ * {@link HeaderItem} for meta informations such as &lt;meta&gt; tags or 
+ * canonical &lt;link&gt;
+ * 
+ * @author andrea del bene
+ *
+ */
+public class MetaDataHeaderItem extends HeaderItem
+{
+
+	private final Map<String, Object> tagAttributes = new ValueMap();
+	private final String tagName;
+
+	public static final String META_TAG = "meta";
+	public static final String LINK_TAG = "link";
+	public static final String CANONICAL_LINK = "canonical";
+
+	/**
+	 * Build a new {@link MetaDataHeaderItem} having {@code tagName} as tag.
+	 * 
+	 * @param tagName
+	 * 		the name of the tag
+	 */
+	public MetaDataHeaderItem(String tagName)
+	{
+		this.tagName = tagName;
+	}
+
+	/**
+	 * Add a tag attribute to the item. If the attribute value is a {@link IModel}, 
+	 * the object wrapped inside the model is used as actual value.
+	 * 
+	 * @param attributeName
+	 * 		the attribute name
+	 * @param attributeValue
+	 * 		the attribute value
+	 * @return
+	 * 		The current item.
+	 */
+	public MetaDataHeaderItem addTagAttribute(String attributeName, Object attributeValue)
+	{
+		tagAttributes.put(attributeName, attributeValue);
+		return this;
+	}
+
+	/**
+	 * Generate the string representation for the current item.
+	 * 
+	 * @return
+	 * 		The string representation for the current item.
+	 */
+	public String generateString()
+	{
+		StringBuilder buffer = new StringBuilder();
+
+		buffer.append('<').append(tagName);
+
+		for (Map.Entry<String, Object> entry : tagAttributes.entrySet())
+		{
+			Object value = entry.getValue();
+
+			if (value instanceof IModel)
+			{
+				value = ((IModel<?>)value).getObject();
+			}
+
+			if (value != null)
+			{
+				buffer.append(' ')
+					.append(entry.getKey())
+					.append('=')
+					.append('"')
+					.append(value)
+					.append('"');
+			}
+		}
+
+		buffer.append(" />\n");
+
+		return buffer.toString();
+	}
+
+	@Override
+	public Iterable<?> getRenderTokens()
+	{
+		return Collections.singletonList(generateString());
+	}
+
+	@Override
+	public void render(Response response)
+	{
+		response.write(generateString());
+	}
+
+	/**
+	 * Factory method to create &lt;meta&gt; tag.
+	 * 
+	 * @param name
+	 * 		the 'name' attribute of the tag
+	 * @param content
+	 * 		the 'content' attribute of the tag
+	 * @return
+	 * 		A new {@link MetaDataHeaderItem}
+	 */
+	public static MetaDataHeaderItem forMetaTag(String name, String content)
+	{
+		return forMetaTag(Model.<String> of(name), Model.<String> of(content));
+	}
+
+	/**
+	 * Factory method to create &lt;meta&gt; tag.
+	 * 
+	 * @param name
+	 * 		the 'name' attribute of the tag as String model
+	 * @param content
+	 * 		the 'content' attribute of the tag as String model
+	 * @return
+	 * 		A new {@link MetaDataHeaderItem}
+	 */
+	public static MetaDataHeaderItem forMetaTag(IModel<String> name, IModel<String> content)
+	{
+		MetaDataHeaderItem headerItem = new MetaDataHeaderItem(META_TAG);
+
+		headerItem.addTagAttribute("name", name);
+		headerItem.addTagAttribute("content", content);
+
+		return headerItem;
+	}
+
+	/**
+	 * Factory method to create &lt;link&gt; tag.
+	 *  
+	 * @param rel
+	 * 		the 'rel' attribute of the tag
+	 * @param href
+	 * 		the 'href' attribute of the tag
+	 * @return
+	 * 		A new {@link MetaDataHeaderItem}
+	 */
+	public static MetaDataHeaderItem forLinkTag(String rel, String href)
+	{
+		return forLinkTag(Model.<String> of(rel), Model.<String> of(href));
+	}
+	
+	/**
+	 * Factory method to create &lt;link&gt; tag.
+	 *  
+	 * @param rel
+	 * 		the 'rel' attribute of the tag as String model
+	 * @param href
+	 * 		the 'href' attribute of the tag as String model
+	 * @return
+	 * 		A new {@link MetaDataHeaderItem}
+	 */
+	public static MetaDataHeaderItem forLinkTag(IModel<String> rel, IModel<String> href)
+	{
+		MetaDataHeaderItem headerItem = new MetaDataHeaderItem(LINK_TAG);
+
+		headerItem.addTagAttribute("rel", rel);
+		headerItem.addTagAttribute("href", href);
+
+		return headerItem;
+	}
+	
+	@Override
+	public boolean equals(Object obj)
+	{
+		if (obj instanceof MetaDataHeaderItem)
+			return ((MetaDataHeaderItem)obj).generateString().equals(generateString());
+		return false;
+	}
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/87654b25/wicket-core/src/test/java/org/apache/wicket/markup/head/MetaDataHeaderItemTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/head/MetaDataHeaderItemTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/head/MetaDataHeaderItemTest.java
new file mode 100644
index 0000000..472a722
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/markup/head/MetaDataHeaderItemTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.head;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.wicket.model.Model;
+import org.junit.Test;
+
+
+public class MetaDataHeaderItemTest
+{
+	@Test
+	public void testMetaTag() throws Exception
+	{
+		String expectedString = "<meta name=\"robots\" content=\"index,nofollow\" />\n";
+		MetaDataHeaderItem metaTag = MetaDataHeaderItem.forMetaTag("robots", "index,nofollow");
+		
+		assertEquals(expectedString, metaTag.generateString());
+	}
+	
+	@Test
+	public void testLinkTag() throws Exception
+	{
+		String expectedString = "<link rel=\"shortcut icon\" href=\"http://www.mysite.com/favicon.ico\" type=\"image/x-icon\" />\n";
+		MetaDataHeaderItem metaTag = MetaDataHeaderItem.forLinkTag("shortcut icon", "http://www.mysite.com/favicon.ico");
+		metaTag.addTagAttribute("type", Model.of("image/x-icon"));
+		
+		assertEquals(expectedString, metaTag.generateString());
+	}
+}


[2/2] git commit: WICKET-5629 Add an HeaderItem for meta data tags such as or canonical

Posted by ad...@apache.org.
WICKET-5629 Add an HeaderItem for meta data tags such as <meta> or canonical <link>

Add checks for the passed arguments.
Escape the name and the value before rendering.
Remove unused constant - CANONICAL_LINK


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/4bfa81eb
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/4bfa81eb
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/4bfa81eb

Branch: refs/heads/wicket-6.x
Commit: 4bfa81eb72a36ad8b177a6edce5432ed59b1797f
Parents: 87654b2
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Wed Jun 25 10:03:45 2014 +0300
Committer: Andrea Del Bene <an...@gmail.com>
Committed: Wed Jun 25 12:08:20 2014 +0200

----------------------------------------------------------------------
 .../wicket/markup/head/MetaDataHeaderItem.java  | 30 +++++++++++---------
 1 file changed, 16 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/4bfa81eb/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java b/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
index ea55abe..dee9e9d 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
@@ -22,6 +22,8 @@ import java.util.Map;
 import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.Model;
 import org.apache.wicket.request.Response;
+import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.string.Strings;
 import org.apache.wicket.util.value.ValueMap;
 
 /**
@@ -29,17 +31,15 @@ import org.apache.wicket.util.value.ValueMap;
  * canonical &lt;link&gt;
  * 
  * @author andrea del bene
- *
+ * @since 6.17.0
  */
 public class MetaDataHeaderItem extends HeaderItem
 {
-
-	private final Map<String, Object> tagAttributes = new ValueMap();
-	private final String tagName;
-
 	public static final String META_TAG = "meta";
 	public static final String LINK_TAG = "link";
-	public static final String CANONICAL_LINK = "canonical";
+
+	private final Map<String, Object> tagAttributes;
+	private final String tagName;
 
 	/**
 	 * Build a new {@link MetaDataHeaderItem} having {@code tagName} as tag.
@@ -49,7 +49,8 @@ public class MetaDataHeaderItem extends HeaderItem
 	 */
 	public MetaDataHeaderItem(String tagName)
 	{
-		this.tagName = tagName;
+		this.tagName = Args.notEmpty(tagName, "tagName");
+		this.tagAttributes = new ValueMap();
 	}
 
 	/**
@@ -65,6 +66,9 @@ public class MetaDataHeaderItem extends HeaderItem
 	 */
 	public MetaDataHeaderItem addTagAttribute(String attributeName, Object attributeValue)
 	{
+		Args.notEmpty(attributeName, "attributeName");
+		Args.notNull(attributeValue, "attributeValue");
+
 		tagAttributes.put(attributeName, attributeValue);
 		return this;
 	}
@@ -93,10 +97,10 @@ public class MetaDataHeaderItem extends HeaderItem
 			if (value != null)
 			{
 				buffer.append(' ')
-					.append(entry.getKey())
+					.append(Strings.escapeMarkup(entry.getKey()))
 					.append('=')
 					.append('"')
-					.append(value)
+					.append(Strings.escapeMarkup(value.toString()))
 					.append('"');
 			}
 		}
@@ -130,7 +134,7 @@ public class MetaDataHeaderItem extends HeaderItem
 	 */
 	public static MetaDataHeaderItem forMetaTag(String name, String content)
 	{
-		return forMetaTag(Model.<String> of(name), Model.<String> of(content));
+		return forMetaTag(Model.of(name), Model.of(content));
 	}
 
 	/**
@@ -165,7 +169,7 @@ public class MetaDataHeaderItem extends HeaderItem
 	 */
 	public static MetaDataHeaderItem forLinkTag(String rel, String href)
 	{
-		return forLinkTag(Model.<String> of(rel), Model.<String> of(href));
+		return forLinkTag(Model.of(rel), Model.of(href));
 	}
 	
 	/**
@@ -191,8 +195,6 @@ public class MetaDataHeaderItem extends HeaderItem
 	@Override
 	public boolean equals(Object obj)
 	{
-		if (obj instanceof MetaDataHeaderItem)
-			return ((MetaDataHeaderItem)obj).generateString().equals(generateString());
-		return false;
+		return obj instanceof MetaDataHeaderItem && ((MetaDataHeaderItem) obj).generateString().equals(generateString());
 	}
 }