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 java.util.*;
020
021import org.apache.juneau.examples.core.pojo.*;
022import org.apache.juneau.html.*;
023
024/**
025 * Sample class which shows the complex usage of HtmlSerializer and HtmlParser.
026 *
027 */
028public class HtmlComplexExample {
029
030   /**
031    * Serializing PojoComplex bean into Html type
032    * and Deserialize back to PojoComplex instance type.
033    *
034    * @param args Unused.
035    * @throws Exception Unused.
036    */
037   public static void main(String[] args) throws Exception {
038      // Juneau provides static constants with the most commonly used configurations
039      // Get a reference to a serializer - converting POJO to flat format
040      /**
041       * Produces
042       * <table><tr><td>innerPojo</td><td><table><tr><td>name</td><td>name0</td></tr>
043       * <tr><td>id</td><td>1.0</td></tr></table></td></tr><tr><td>values</td><td><table>
044       * <tr><td>setOne</td><td><table _type="array"><tr><th>name</th><th>id</th></tr>
045       * <tr><td>name1</td><td>1.1</td></tr><tr><td>name2</td><td>1.1</td></tr>
046       * </table></td></tr><tr><td>setTwo</td><td><table _type="array"><tr><th>name
047       * </th><th>id</th></tr><tr><td>name1</td><td>1.2</td></tr><tr><td>name2</td><td>1.2
048       * </td></tr></table></td></tr></table></td></tr><tr><td>id</td><td>pojo</td></tr></table>
049       */
050      var htmlSerializer = HtmlSerializer.DEFAULT;
051      // Get a reference to a parser - converts that flat format back into the POJO
052      var htmlParser = HtmlParser.DEFAULT;
053
054      // Fill some data to a PojoComplex bean
055      var values = new HashMap<String,List<Pojo>>();
056      var setOne = new ArrayList<Pojo>();
057      setOne.add(new Pojo("1.1", "name1"));
058      setOne.add(new Pojo("1.1", "name2"));
059      var setTwo = new ArrayList<Pojo>();
060      setTwo.add(new Pojo("1.2", "name1"));
061      setTwo.add(new Pojo("1.2", "name2"));
062      values.put("setOne", setOne);
063      values.put("setTwo", setTwo);
064      var pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
065
066      var flat = htmlSerializer.serialize(pojoc);
067
068      // Print out the created POJO in JSON format.
069      System.out.println(flat);
070
071      var parse = htmlParser.parse(flat, PojoComplex.class);
072
073      assert parse.getId().equals(pojoc.getId());
074      assert parse.getInnerPojo().getName().equals(pojoc.getInnerPojo().getName());
075      assert parse.getInnerPojo().getId().equals(pojoc.getInnerPojo().getId());
076
077      // The object above can be parsed thanks to the @Beanc(properties = id,name) annotation on Pojo
078      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
079   }
080}