001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.examples.core.html;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022
023import org.apache.juneau.commons.logging.*;
024import org.apache.juneau.examples.core.pojo.*;
025import org.apache.juneau.marshall.html.*;
026
027/**
028 * Sample class which shows the complex usage of HtmlSerializer and HtmlParser.
029 *
030 */
031public class HtmlComplexExample {
032
033   /**
034    * Serializing PojoComplex bean into Html type
035    * and Deserialize back to PojoComplex instance type.
036    *
037    * @param args Unused.
038    * @throws Exception Unused.
039    */
040   public static void main(String[] args) throws Exception {
041      // Juneau provides static constants with the most commonly used configurations
042      // Get a reference to a serializer - converting POJO to flat format
043      /**
044       * Produces
045       * <table><tr><td>innerPojo</td><td><table><tr><td>name</td><td>name0</td></tr>
046       * <tr><td>id</td><td>1.0</td></tr></table></td></tr><tr><td>values</td><td><table>
047       * <tr><td>setOne</td><td><table _type="array"><tr><th>name</th><th>id</th></tr>
048       * <tr><td>name1</td><td>1.1</td></tr><tr><td>name2</td><td>1.1</td></tr>
049       * </table></td></tr><tr><td>setTwo</td><td><table _type="array"><tr><th>name
050       * </th><th>id</th></tr><tr><td>name1</td><td>1.2</td></tr><tr><td>name2</td><td>1.2
051       * </td></tr></table></td></tr></table></td></tr><tr><td>id</td><td>pojo</td></tr></table>
052       */
053      var htmlSerializer = HtmlSerializer.DEFAULT;
054      // Get a reference to a parser - converts that flat format back into the POJO
055      var htmlParser = HtmlParser.DEFAULT;
056
057      // Fill some data to a PojoComplex bean
058      Map<String,List<Pojo>> values = m();
059      List<Pojo> setOne = l();
060      setOne.add(new Pojo("1.1", "name1"));
061      setOne.add(new Pojo("1.1", "name2"));
062      List<Pojo> setTwo = l();
063      setTwo.add(new Pojo("1.2", "name1"));
064      setTwo.add(new Pojo("1.2", "name2"));
065      values.put("setOne", setOne);
066      values.put("setTwo", setTwo);
067      var pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
068
069      var flat = htmlSerializer.write(pojoc);
070
071      // Print out the created POJO in JSON format.
072      RichLogger.getLogger(HtmlComplexExample.class).info(flat);
073
074      var parse = htmlParser.read(flat, PojoComplex.class);
075
076      assert parse.getId().equals(pojoc.getId());
077      assert parse.getInnerPojo().getName().equals(pojoc.getInnerPojo().getName());
078      assert parse.getInnerPojo().getId().equals(pojoc.getInnerPojo().getId());
079
080      // The object above can be parsed thanks to the @BeanCtor(properties = id,name) annotation on Pojo
081      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
082   }
083}