juneau-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamesbog...@apache.org
Subject [juneau] branch master updated: Support serialized POJOs inside HTML5 beans.
Date Sun, 18 Mar 2018 17:02:25 GMT
This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 2dd2c86  Support serialized POJOs inside HTML5 beans.
2dd2c86 is described below

commit 2dd2c864d731d7fccdd99124bd226d4ed904616e
Author: JamesBognar <jamesbognar@apache.org>
AuthorDate: Sun Mar 18 13:01:55 2018 -0400

    Support serialized POJOs inside HTML5 beans.
---
 .../java/org/apache/juneau/html/BasicHtmlTest.java |  9 ++--
 .../test/java/org/apache/juneau/html/HtmlTest.java |  9 ++--
 .../org/apache/juneau/dto/html5/HtmlElement.java   |  3 +-
 .../apache/juneau/html/HtmlBeanPropertyMeta.java   | 50 ++++++++++++++--------
 .../java/org/apache/juneau/html/HtmlClassMeta.java | 41 ++++++++++++------
 .../org/apache/juneau/html/HtmlParserSession.java  |  2 +-
 .../apache/juneau/html/HtmlSerializerSession.java  | 19 +++++++-
 .../org/apache/juneau/html/annotation/Html.java    | 15 +------
 .../apache/juneau/html/annotation/HtmlFormat.java  | 43 +++++++++++++++++++
 .../apache/juneau/xml/XmlSerializerSession.java    |  4 +-
 juneau-doc/src/main/javadoc/overview.html          |  3 ++
 11 files changed, 141 insertions(+), 57 deletions(-)

diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/html/BasicHtmlTest.java
b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/html/BasicHtmlTest.java
index d9b84e1..775da1a 100644
--- a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/html/BasicHtmlTest.java
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/html/BasicHtmlTest.java
@@ -13,6 +13,7 @@
 package org.apache.juneau.html;
 
 import static org.apache.juneau.TestUtils.*;
+import static org.apache.juneau.html.annotation.HtmlFormat.*;
 
 import static org.junit.Assert.*;
 
@@ -2970,7 +2971,7 @@ public class BasicHtmlTest {
 		}
 	}
 
-	@Html(asXml=true)
+	@Html(format=XML)
 	public static class BeanWithWhitespaceTextFields {
 		@Xml(format=XmlFormat.TEXT)
 		public String a;
@@ -2981,7 +2982,7 @@ public class BasicHtmlTest {
 		}
 	}
 
-	@Html(asXml=true)
+	@Html(format=XML)
 	public static class BeanWithWhitespaceTextPwsFields {
 		@Xml(format=XmlFormat.TEXT_PWS)
 		public String a;
@@ -2992,7 +2993,7 @@ public class BasicHtmlTest {
 		}
 	}
 
-	@Html(asXml=true)
+	@Html(format=XML)
 	public static class BeanWithWhitespaceMixedFields {
 		@Xml(format=XmlFormat.MIXED)
 		public String[] a;
@@ -3003,7 +3004,7 @@ public class BasicHtmlTest {
 		}
 	}
 
-	@Html(asXml=true)
+	@Html(format=XML)
 	public static class BeanWithWhitespaceMixedPwsFields {
 		@Xml(format=XmlFormat.MIXED_PWS)
 		public String[] a;
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/html/HtmlTest.java
b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/html/HtmlTest.java
index 122ef44..7018205 100755
--- a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/html/HtmlTest.java
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/html/HtmlTest.java
@@ -13,6 +13,7 @@
 package org.apache.juneau.html;
 
 import static org.junit.Assert.*;
+import static org.apache.juneau.html.annotation.HtmlFormat.*;
 
 import java.util.*;
 
@@ -222,7 +223,7 @@ public class HtmlTest {
 		assertEquals("<table><tr><th>key</th><th>value</th></tr><tr><td>f1</td><td><f1></td></tr></table>",
r);
 	}
 
-	@Html(asPlainText=true)
+	@Html(format=PLAIN_TEXT)
 	public static class B1 {
 		public String f1 = "<f1>";
 		@Override /* Object */
@@ -232,7 +233,7 @@ public class HtmlTest {
 	}
 
 	public static class B2 {
-		@Html(asPlainText=true)
+		@Html(format=PLAIN_TEXT)
 		public String f1 = "<f1>";
 	}
 
@@ -254,13 +255,13 @@ public class HtmlTest {
 		assertEquals("<table><tr><th>key</th><th>value</th></tr><tr><td>f1</td><td>&lt;f1&gt;</td></tr></table>",
r);
 	}
 
-	@Html(asXml=true)
+	@Html(format=XML)
 	public static class C1 {
 		public String f1 = "<f1>";
 	}
 
 	public static class C2 {
-		@Html(asXml=true)
+		@Html(format=XML)
 		public String f1 = "<f1>";
 	}
 
diff --git a/juneau-core/juneau-dto/src/main/java/org/apache/juneau/dto/html5/HtmlElement.java
b/juneau-core/juneau-dto/src/main/java/org/apache/juneau/dto/html5/HtmlElement.java
index 730b678..b07d778 100644
--- a/juneau-core/juneau-dto/src/main/java/org/apache/juneau/dto/html5/HtmlElement.java
+++ b/juneau-core/juneau-dto/src/main/java/org/apache/juneau/dto/html5/HtmlElement.java
@@ -13,6 +13,7 @@
 package org.apache.juneau.dto.html5;
 
 import static org.apache.juneau.xml.annotation.XmlFormat.*;
+import static org.apache.juneau.html.annotation.HtmlFormat.*;
 
 import java.net.*;
 import java.net.URI;
@@ -36,7 +37,7 @@ import org.apache.juneau.xml.annotation.*;
  * 	<li class='link'><a class='doclink' href='../../../../../overview-summary.html#juneau-dto.HTML5'>Overview
&gt; juneau-dto &gt; HTML5</a>
  * </ul>
  */
-@org.apache.juneau.html.annotation.Html(asXml=true)
+@org.apache.juneau.html.annotation.Html(format=XML)
 public abstract class HtmlElement {
 
 	private LinkedHashMap<String,Object> attrs;
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
index 991927e..6416811 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
@@ -22,7 +22,8 @@ import org.apache.juneau.html.annotation.*;
 @SuppressWarnings("rawtypes")
 public final class HtmlBeanPropertyMeta extends BeanPropertyMetaExtended {
 
-	private final boolean asXml, noTables, noTableHeaders, asPlainText;
+	private final boolean noTables, noTableHeaders;
+	private final HtmlFormat format;
 	private final HtmlRender render;
 	private final String link, anchorText;
 
@@ -42,31 +43,28 @@ public final class HtmlBeanPropertyMeta extends BeanPropertyMetaExtended
{
 		if (bpm.getSetter() != null)
 			b.findHtmlInfo(bpm.getSetter().getAnnotation(Html.class));
 
-		this.asXml = b.asXml;
+		this.format = b.format;
 		this.noTables = b.noTables;
 		this.noTableHeaders = b.noTableHeaders;
-		this.asPlainText = b.asPlainText;
 		this.render = bpm.getBeanMeta().getClassMeta().getBeanContext().newInstance(HtmlRender.class,
b.render);
 		this.link = b.link;
 		this.anchorText = b.anchorText;
 	}
 
 	static final class Builder {
-		boolean asXml, noTables, noTableHeaders, asPlainText;
+		boolean noTables, noTableHeaders;
+		HtmlFormat format = HtmlFormat.DEFAULT;
 		Class<? extends HtmlRender> render = HtmlRender.class;
 		String link, anchorText;
 
 		void findHtmlInfo(Html html) {
 			if (html == null)
 				return;
-			if (html.asXml())
-				asXml = html.asXml();
+			format = html.format();
 			if (html.noTables())
 				noTables = html.noTables();
 			if (html.noTableHeaders())
 				noTableHeaders = html.noTableHeaders();
-			if (html.asPlainText())
-				asPlainText = html.asPlainText();
 			if (html.render() != HtmlRender.class)
 				render = html.render();
 			if (! html.link().isEmpty())
@@ -77,23 +75,39 @@ public final class HtmlBeanPropertyMeta extends BeanPropertyMetaExtended
{
 	}
 
 	/**
-	 * Returns whether this bean property should be serialized as XML instead of HTML.
+	 * Returns the format of this bean property
 	 * 
-	 * @return <jk>true</jk> if the the {@link Html @Html} annotation is specified,
and {@link Html#asXml() @Html.asXml()} is <jk>true</jk>.
+	 * @return The value of the {@link Html#format()} annotation.
 	 */
-	protected boolean isAsXml() {
-		return asXml;
+	protected HtmlFormat getFormat() {
+		return format;
 	}
 
 	/**
-	 * Returns whether this bean property should be serialized as plain text instead of HTML.
+	 * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
 	 * 
-	 * @return
-	 * 	<jk>true</jk> if the the {@link Html @Html} annotation is specified, and
{@link Html#asPlainText() @Html.asPlainText()} is
-	 * 	<jk>true</jk>.
+	 * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
+	 */
+	protected boolean isXml() {
+		return format == HtmlFormat.XML;
+	}
+	
+	/**
+	 * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
+	 * 
+	 * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
+	 */
+	protected boolean isPlainText() {
+		return format == HtmlFormat.PLAIN_TEXT;
+	}
+
+	/**
+	 * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
+	 * 
+	 * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
 	 */
-	protected boolean isAsPlainText() {
-		return asPlainText;
+	protected boolean isHtml() {
+		return format == HtmlFormat.HTML;
 	}
 
 	/**
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlClassMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlClassMeta.java
index a396958..439c2ea 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlClassMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlClassMeta.java
@@ -23,7 +23,8 @@ import org.apache.juneau.internal.*;
 public class HtmlClassMeta extends ClassMetaExtended {
 
 	private final Html html;
-	private final boolean asXml, noTables, noTableHeaders, asPlainText;
+	private final boolean noTables, noTableHeaders;
+	private final HtmlFormat format;
 	private final HtmlRender<?> render;
 
 	/**
@@ -35,16 +36,14 @@ public class HtmlClassMeta extends ClassMetaExtended {
 		super(cm);
 		this.html = ReflectionUtils.getAnnotation(Html.class, getInnerClass());
 		if (html != null) {
-			asXml = html.asXml();
+			format = html.format();
 			noTables = html.noTables();
 			noTableHeaders = html.noTableHeaders();
-			asPlainText = html.asPlainText();
 			render = cm.getBeanContext().newInstance(HtmlRender.class, html.render());
 		} else {
-			asXml = false;
+			format = HtmlFormat.HTML;
 			noTables = false;
 			noTableHeaders = false;
-			asPlainText = false;
 			render = null;
 		}
 	}
@@ -59,21 +58,39 @@ public class HtmlClassMeta extends ClassMetaExtended {
 	}
 
 	/**
-	 * Returns the {@link Html#asXml() @Html.asXml()} annotation defined on the class.
+	 * Returns the {@link Html#format() @Html.format()} annotation defined on the class.
 	 * 
 	 * @return The value of the annotation.
 	 */
-	protected boolean isAsXml() {
-		return asXml;
+	protected HtmlFormat getFormat() {
+		return format;
 	}
 
 	/**
-	 * Returns the {@link Html#asPlainText() @Html.asPlainText()} annotation defined on the
class.
+	 * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
 	 * 
-	 * @return The value of the annotation.
+	 * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
+	 */
+	protected boolean isXml() {
+		return format == HtmlFormat.XML;
+	}
+	
+	/**
+	 * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
+	 * 
+	 * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
+	 */
+	protected boolean isPlainText() {
+		return format == HtmlFormat.PLAIN_TEXT;
+	}
+
+	/**
+	 * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
+	 * 
+	 * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
 	 */
-	protected boolean isAsPlainText() {
-		return asPlainText;
+	protected boolean isHtml() {
+		return format == HtmlFormat.HTML;
 	}
 
 	/**
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlParserSession.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlParserSession.java
index 23dc130..2ece9ea 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlParserSession.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlParserSession.java
@@ -112,7 +112,7 @@ public final class HtmlParserSession extends XmlParserSession {
 
 		// Handle @Html(asXml=true) beans.
 		HtmlClassMeta hcm = sType.getExtendedMeta(HtmlClassMeta.class);
-		if (hcm.isAsXml())
+		if (hcm.getFormat() == HtmlFormat.XML)
 			return super.parseAnything(eType, null, r, outer, false, pMeta);
 
 		Object o = null;
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlSerializerSession.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlSerializerSession.java
index a64efc8..f94fb30 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlSerializerSession.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlSerializerSession.java
@@ -210,6 +210,21 @@ public class HtmlSerializerSession extends XmlSerializerSession {
 		return w;
 	}
 	
+	@Override /* XmlSerializerSession */
+	protected ContentResult serializeAnything(
+		XmlWriter out,
+		Object o,
+		ClassMeta<?> eType,
+		String elementName,
+		Namespace elementNamespace,
+		boolean addNamespaceUris,
+		XmlFormat format,
+		boolean isMixed,
+		boolean preserveWhitespace,
+		BeanPropertyMeta pMeta) throws Exception {
+		
+		return super.serializeAnything(out, o, eType, elementName, elementNamespace, addNamespaceUris,
format, isMixed, preserveWhitespace, pMeta);
+	}
 	/**
 	 * Serialize the specified object to the specified writer.
 	 * 
@@ -301,14 +316,14 @@ public class HtmlSerializerSession extends XmlSerializerSession {
 				}
 			}
 
-			if (html.isAsXml() || (pMeta != null && pMeta.getExtendedMeta(HtmlBeanPropertyMeta.class).isAsXml()))
{
+			if (html.isXml() || (pMeta != null && pMeta.getExtendedMeta(HtmlBeanPropertyMeta.class).isXml()))
{
 				pop();
 				indent++;
 				super.serializeAnything(out, o, null, null, null, false, XmlFormat.MIXED, false, false,
null);
 				indent -= xIndent+1;
 				return cr;
 
-			} else if (html.isAsPlainText() || (pMeta != null && pMeta.getExtendedMeta(HtmlBeanPropertyMeta.class).isAsPlainText()))
{
+			} else if (html.isPlainText() || (pMeta != null && pMeta.getExtendedMeta(HtmlBeanPropertyMeta.class).isPlainText()))
{
 				out.write(o == null ? "null" : o.toString());
 				cr = CR_MIXED;
 
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/annotation/Html.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/annotation/Html.java
index d675982..3298e88 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/annotation/Html.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/annotation/Html.java
@@ -48,21 +48,10 @@ public @interface Html {
 	String anchorText() default "";
 
 	/**
-	 * Treat as plain text.
+	 * Specifies what format to use for the HTML element.
 	 * 
-	 * <p>
-	 * Object is serialized to a String using the <code>toString()</code> method
and written directly to output.
-	 * Useful when you want to serialize custom HTML.
-	 */
-	boolean asPlainText() default false;
-
-	/**
-	 * Treat as XML.
-	 * 
-	 * <p>
-	 * Useful when creating beans that model HTML elements.
 	 */
-	boolean asXml() default false;
+	HtmlFormat format() default HtmlFormat.DEFAULT;
 
 	/**
 	 * Adds a hyperlink to a bean property when rendered as HTML.
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/annotation/HtmlFormat.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/annotation/HtmlFormat.java
new file mode 100644
index 0000000..9d82f95
--- /dev/null
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/annotation/HtmlFormat.java
@@ -0,0 +1,43 @@
+// ***************************************************************************************************************************
+// * 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.html.annotation;
+
+/**
+ * Possible values for the {@link Html#format()} annotation.
+ */
+public enum HtmlFormat {
+	
+	/**
+	 * Object is serialized to a String using the <code>toString()</code> method
and written directly to output.
+	 * <br>Useful when you want to serialize custom HTML.
+	 */
+	PLAIN_TEXT,
+	
+	/**
+	 * Object is serialized to HTML.
+	 * <br>Useful if you want to switch back to serializing POJOs as HTML within elements
+	 * marked for XML serialization.
+	 */
+	HTML,
+	
+	/**
+	 * Object is serialized to XML.
+	 * <br>Useful when creating beans that model HTML elements.
+	 */
+	XML,
+	
+	/**
+	 * Object is serialized using whatever the current context is.
+	 */
+	DEFAULT
+}
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlSerializerSession.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlSerializerSession.java
index 6a17dd3..a931151 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlSerializerSession.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlSerializerSession.java
@@ -271,7 +271,7 @@ public class XmlSerializerSession extends WriterSerializerSession {
 	 * @return The same writer passed in so that calls to the writer can be chained.
 	 * @throws Exception If a problem occurred trying to convert the output.
 	 */
-	protected XmlWriter serializeAnything(
+	protected ContentResult serializeAnything(
 			XmlWriter out,
 			Object o,
 			ClassMeta<?> eType,
@@ -504,7 +504,7 @@ public class XmlSerializerSession extends WriterSerializerSession {
 				out.nl(i);
 		}
 
-		return out;
+		return rc;
 	}
 
 	private ContentResult serializeMap(XmlWriter out, Map m, ClassMeta<?> sType,
diff --git a/juneau-doc/src/main/javadoc/overview.html b/juneau-doc/src/main/javadoc/overview.html
index dc983ab..e657521 100644
--- a/juneau-doc/src/main/javadoc/overview.html
+++ b/juneau-doc/src/main/javadoc/overview.html
@@ -21250,6 +21250,9 @@
 			<li>
 				A runtime exception is now thrown if you define a <code><ja>@BeanProperty</ja>(name)</code>
but forget to
 				add it to your <code><ja>@Bean</ja>(properties)</code> annotation.
+			<li>
+				<code><ja>@Html</ja>(asXml)</code> and <code><ja>@Html</ja>(asPlainText)</code>
replaced with
+				{@link org.apache.juneau.html.annotation.Html#format() @Html(format)}.
 		</ul>
 		
 		<h5 class='topic w800'>juneau-dto</h5>

-- 
To stop receiving notification emails like this one, please contact
jamesbognar@apache.org.

Mime
View raw message