You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2017/02/03 02:04:21 UTC

incubator-juneau git commit: JUNEAU-33 - HTML5 DOM objects.

Repository: incubator-juneau
Updated Branches:
  refs/heads/master dba656f35 -> 22b24ff57


JUNEAU-33 - HTML5 DOM objects.

Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/22b24ff5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/22b24ff5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/22b24ff5

Branch: refs/heads/master
Commit: 22b24ff57f395fc939e70a0a335471811c24edb2
Parents: dba656f
Author: JamesBognar <ja...@apache.org>
Authored: Thu Feb 2 21:04:16 2017 -0500
Committer: JamesBognar <ja...@apache.org>
Committed: Thu Feb 2 21:04:16 2017 -0500

----------------------------------------------------------------------
 .../juneau/dto/html5/HtmlTemplatesTest.java     | 173 +++++++++++++++++++
 .../main/java/org/apache/juneau/Swappable.java  |  44 -----
 .../apache/juneau/dto/html5/HtmlElement.java    |  13 ++
 .../org/apache/juneau/transform/PojoSwap.java   |   4 +-
 .../org/apache/juneau/xml/XmlSerializer.java    |   5 +
 5 files changed, 193 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/22b24ff5/juneau-core-test/src/test/java/org/apache/juneau/dto/html5/HtmlTemplatesTest.java
----------------------------------------------------------------------
diff --git a/juneau-core-test/src/test/java/org/apache/juneau/dto/html5/HtmlTemplatesTest.java b/juneau-core-test/src/test/java/org/apache/juneau/dto/html5/HtmlTemplatesTest.java
new file mode 100644
index 0000000..93073bc
--- /dev/null
+++ b/juneau-core-test/src/test/java/org/apache/juneau/dto/html5/HtmlTemplatesTest.java
@@ -0,0 +1,173 @@
+// ***************************************************************************************************************************
+// * 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.juneau.dto.html5;
+
+import static org.junit.Assert.*;
+import static org.apache.juneau.dto.html5.HtmlBuilder.*;
+
+import java.util.*;
+
+import org.apache.juneau.BeanSession;
+import org.apache.juneau.annotation.Bean;
+import org.apache.juneau.html.*;
+import org.apache.juneau.parser.*;
+import org.apache.juneau.serializer.*;
+import org.apache.juneau.xml.*;
+import org.junit.*;
+import org.junit.runner.*;
+import org.junit.runners.*;
+
+
+@RunWith(Parameterized.class)
+@SuppressWarnings({"javadoc"})
+public class HtmlTemplatesTest {
+
+	private static final WriterSerializer
+		sXmlSq = XmlSerializer.DEFAULT_SQ,
+		sXmlNsSq = XmlSerializer.DEFAULT_NS_SQ,
+		sXmlSqReadable = XmlSerializer.DEFAULT_SQ_READABLE,
+		sHtmlSq = HtmlSerializer.DEFAULT_SQ,
+		sHtmlSqReadable = HtmlSerializer.DEFAULT_SQ_READABLE;
+
+	private static final ReaderParser
+		pXml = XmlParser.DEFAULT,
+		pHtml = HtmlParser.DEFAULT;
+
+	@Parameterized.Parameters
+	public static Collection<Object[]> getParameters() {
+		return Arrays.asList(new Object[][] {
+			{
+				"FormTemplate-1",
+				new FormTemplate("myaction", "foo", "bar"),
+				"<form action='myaction'><input type='text' name='v1' value='foo'/><input type='text' name='v2' value='bar'/></form>",
+				"<form action='myaction'><input type='text' name='v1' value='foo'/><input type='text' name='v2' value='bar'/></form>\n",
+				"<form action='myaction'><input type='text' name='v1' value='foo'/><input type='text' name='v2' value='bar'/></form>",
+				"<form action='myaction'><input type='text' name='v1' value='foo'/><input type='text' name='v2' value='bar'/></form>\n",
+			},
+		});
+	}
+
+	
+	@Bean(beanDictionary=HtmlBeanDictionary.class)
+	public static class FormTemplate {
+		
+		private String action;
+		private String value1, value2;
+		
+		public FormTemplate(Form f) {
+			this.action = f.getAttr("action");
+			this.value1 = ((Input)f.getChildren().get(0)).getAttr("value");
+			this.value2 = ((Input)f.getChildren().get(1)).getAttr("value");
+		}
+		
+		public FormTemplate(String action, String value1, String value2) {
+			this.action = action;
+			this.value1 = value1;
+			this.value2 = value2;
+		}
+		
+		public Form swap(BeanSession session) {
+			return form(action,
+				input("text").name("v1").value(value1),
+				input("text").name("v2").value(value2)
+			);
+		}
+	}
+	
+
+	private String label, xml, xmlr, html, htmlr;
+	private Object in;
+
+	public HtmlTemplatesTest(String label, Object in, String xml, String xmlr, String html, String htmlr) throws Exception {
+		this.label = label;
+		this.in = in;
+		this.xml = xml;
+		this.xmlr = xmlr;
+		this.html = html;
+		this.htmlr = htmlr;
+	}
+
+	private void testSerialize(WriterSerializer s, String expected) throws Exception {
+		try {
+			String r = s.serialize(in);
+			assertEquals(label + " serialize-normal failed", expected, r);
+		} catch (AssertionError e) {
+			throw e;
+		} catch (Exception e) {
+			throw new AssertionError(label + " test failed", e);
+		}
+	}
+
+	private void testParse(WriterSerializer s, ReaderParser p, String expected) throws Exception {
+		try {
+			String r = s.serialize(in);
+			Object o = p.parse(r, in == null ? Object.class : in.getClass());
+			r = s.serialize(o);
+			assertEquals(label + " parse-normal failed", expected, r);
+		} catch (AssertionError e) {
+			throw e;
+		} catch (Exception e) {
+			throw new AssertionError(label + " test failed", e);
+		}
+	}
+
+	@Test
+	public void serializeXmlDefaultSq() throws Exception {
+		testSerialize(sXmlSq, xml);
+	}
+
+	@Test
+	public void parseXmlDefaultSq() throws Exception {
+		testParse(sXmlSq, pXml, xml);
+	}
+
+	@Test
+	public void serializeXmlDefaultNsSq() throws Exception {
+		testSerialize(sXmlNsSq, xml);
+	}
+
+	@Test
+	public void parseXmlDefaultNsSq() throws Exception {
+		testParse(sXmlNsSq, pXml, xml);
+	}
+
+	@Test
+	public void serializeHtmlDefaultSq() throws Exception {
+		testSerialize(sHtmlSq, html);
+	}
+
+	@Test
+	public void parseHtmlDefaultSq() throws Exception {
+		testParse(sHtmlSq, pHtml, html);
+	}
+
+	@Test
+	public void serializeXmlDefaultSqReadable() throws Exception {
+		testSerialize(sXmlSqReadable, xmlr);
+	}
+
+	@Test
+	public void parseXmlDefaultSqReadable() throws Exception {
+		testParse(sXmlSqReadable, pXml, xmlr);
+	}
+
+	@Test
+	public void serializeHtmlDefaultSqReadable() throws Exception {
+		testSerialize(sHtmlSqReadable, htmlr);
+	}
+
+	@Test
+	public void parseHtmlDefaultSqReadable() throws Exception {
+		testParse(sHtmlSqReadable, pHtml, htmlr);
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/22b24ff5/juneau-core/src/main/java/org/apache/juneau/Swappable.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/Swappable.java b/juneau-core/src/main/java/org/apache/juneau/Swappable.java
deleted file mode 100644
index 46a15a1..0000000
--- a/juneau-core/src/main/java/org/apache/juneau/Swappable.java
+++ /dev/null
@@ -1,44 +0,0 @@
-// ***************************************************************************************************************************
-// * 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.juneau;
-
-/**
- * Identifies a class that gets swapped out for another class during serialization.
- * <p>
- * *** This feature has not yet been implemented ***
- * <p>
- * Allows fine-tuned controlling of serialization of classes by allowing you to create a surrogate
- * form of the class that then gets serialized instead of the original class.
- * <p>
- * During serialization, the {@link #swap(BeanSession)} method is used to convert this object into a serialized
- * form.
- * <p>
- * Serialized form can be any object that can be serialized by this framework.
- * <p>
- * Parsing back into the original object can be accomplished by specifying a public constructor that takes in
- * a single parameter of type T.
- *
- * @param <T> The class of the serialized form of this class.
- */
-public interface Swappable<T> {
-
-	/**
-	 * Method to implement that converts this object to a surrogate serialized form.
-	 *
-	 * @param session The current bean session.
-	 * 	For example, you can use this to access the media type (e.g. <js>"application/json"</js>) and return
-	 * 	different values for different media types.
-	 * @return The surrogate object.
-	 */
-	public T swap(BeanSession session);
-}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/22b24ff5/juneau-core/src/main/java/org/apache/juneau/dto/html5/HtmlElement.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/dto/html5/HtmlElement.java b/juneau-core/src/main/java/org/apache/juneau/dto/html5/HtmlElement.java
index cdbdfed..69a2023 100644
--- a/juneau-core/src/main/java/org/apache/juneau/dto/html5/HtmlElement.java
+++ b/juneau-core/src/main/java/org/apache/juneau/dto/html5/HtmlElement.java
@@ -17,6 +17,7 @@ import static org.apache.juneau.xml.annotation.XmlFormat.*;
 import java.util.*;
 
 import org.apache.juneau.html.*;
+import org.apache.juneau.internal.*;
 import org.apache.juneau.xml.annotation.*;
 
 /**
@@ -62,6 +63,18 @@ public abstract class HtmlElement {
 		return this;
 	}
 
+	/**
+	 * Returns the attribute with the specified name.
+	 *
+	 * @param key The attribute name.
+	 * @return The attribute value, or <jk>null</jk> if the named attribute does not exist.
+	 */
+	public String getAttr(String key) {
+		if (attrs == null)
+			return null;
+		return StringUtils.toString(attrs.get(key));
+	}
+
 	/**
 	 * <a class='doclink' href='https://www.w3.org/TR/html5/editing.html#the-accesskey-attribute'>accesskey</a> attribute.
 	 * @param accesskey - The new value for this attribute.

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/22b24ff5/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java b/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java
index f9027ec..ec97dd8 100644
--- a/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java
+++ b/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java
@@ -107,8 +107,8 @@ public abstract class PojoSwap<T,S> {
 	 */
 	@SuppressWarnings("unchecked")
 	protected PojoSwap() {
-		normalClass = (Class<T>)ClassUtils.resolveParameterType(PojoSwap.class, 0, this);
-		swapClass = ClassUtils.resolveParameterType(PojoSwap.class, 1, this);
+		normalClass = (Class<T>)ClassUtils.resolveParameterType(PojoSwap.class, 0, this.getClass());
+		swapClass = ClassUtils.resolveParameterType(PojoSwap.class, 1, this.getClass());
 	}
 
 	/**

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/22b24ff5/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java b/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java
index fc8385f..006b32d 100644
--- a/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java
+++ b/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java
@@ -351,7 +351,12 @@ public class XmlSerializer extends WriterSerializer {
 		}
 
 		String resolvedDictionaryName = isExpectedType ? null : aType.getResolvedDictionaryName();
+
+		// Note that the dictionary name may be specified on the actual type or the serialized type.
+		// HTML templates will have them defined on the serialized type.
 		String dictionaryName = aType.getDictionaryName();
+		if (dictionaryName == null)
+			dictionaryName = sType.getDictionaryName();
 
 		// char '\0' is interpreted as null.
 		if (o != null && sType.isChar() && ((Character)o).charValue() == 0)