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 org.apache.juneau.examples.core.pojo.*;
020import org.apache.juneau.html.*;
021
022/**
023 * Sample class which shows the simple usage of HtmlSerializer and HtmlParser.
024 *
025 */
026public class HtmlSimpleExample {
027   /**
028    * Serializing Pojo bean into Html format and Deserialize back to Pojo instance type.
029    *
030    * @param args Unused.
031    * @throws Exception Unused.
032    */
033   public static void main(String[] args) throws Exception {
034      // Juneau provides static constants with the most commonly used configurations
035      // Get a reference to a serializer - converting POJO to flat format
036      // Produces
037      // <table><tr><td>name</td><td>name</td></tr><tr><td>id</td><td>id</td></tr></table>
038      var htmlSerializer = HtmlSerializer.DEFAULT;
039      // Get a reference to a parser - converts that flat format back into the POJO
040      var htmlParser = HtmlParser.DEFAULT;
041
042      var pojo = new Pojo("id", "name");
043
044      var flat = htmlSerializer.serialize(pojo);
045
046      // Print out the created POJO in JSON format.
047      System.out.println(flat);
048
049      var parse = htmlParser.parse(flat, Pojo.class);
050
051      assert parse.getId().equals(pojo.getId());
052      assert parse.getName().equals(pojo.getName());
053
054      /**
055       *  Produces
056       *  <html><head><style></style><script></script></head><body><section><article><div class="outerdata">
057       *  <div class="data" id="data"><table><tr><td>name</td><td>name</td></tr><tr><td>id</td><td>id</td></tr>
058       *  </table></div></div></article></section></body></html>
059       */
060      var docSerialized = HtmlDocSerializer.DEFAULT.serialize(pojo);
061      System.out.println(docSerialized);
062
063      // The object above can be parsed thanks to the @Beanc(properties = id,name) annotation on Pojo
064      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
065   }
066}