001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.examples.core.html;
014
015import org.apache.juneau.examples.core.pojo.Pojo;
016import org.apache.juneau.html.HtmlDocSerializer;
017import org.apache.juneau.html.HtmlParser;
018import org.apache.juneau.html.HtmlSerializer;
019
020/**
021 * Sample class which shows the simple usage of HtmlSerializer and HtmlParser.
022 *
023 * <ul class='seealso'>
024 *    <li class='extlink'>{@source}
025 * </ul>
026 */
027public class HtmlSimpleExample {
028   /**
029    * Serializing Pojo bean into Html format and Deserialize back to Pojo instance type.
030    *
031    * @param args Unused.
032    * @throws Exception Unused.
033    */
034   public static void main(String[] args) throws Exception{
035      // Juneau provides static constants with the most commonly used configurations
036      // Get a reference to a serializer - converting POJO to flat format
037      // Produces
038      // <table><tr><td>name</td><td>name</td></tr><tr><td>id</td><td>id</td></tr></table>
039      HtmlSerializer htmlSerializer = HtmlSerializer.DEFAULT;
040      // Get a reference to a parser - converts that flat format back into the POJO
041      HtmlParser htmlParser = HtmlParser.DEFAULT;
042
043      Pojo pojo = new Pojo("id","name");
044
045      String flat = htmlSerializer.serialize(pojo);
046
047      // Print out the created POJO in JSON format.
048      System.out.println(flat);
049
050      Pojo parse = htmlParser.parse(flat, Pojo.class);
051
052      assert parse.getId().equals(pojo.getId());
053      assert parse.getName().equals(pojo.getName());
054
055      /**
056       *  Produces
057       *  <html><head><style></style><script></script></head><body><section><article><div class="outerdata">
058       *  <div class="data" id="data"><table><tr><td>name</td><td>name</td></tr><tr><td>id</td><td>id</td></tr>
059       *  </table></div></div></article></section></body></html>
060       */
061      String docSerialized = HtmlDocSerializer.DEFAULT.serialize(pojo);
062      System.out.println(docSerialized);
063
064      // The object above can be parsed thanks to the @Beanc(properties = id,name) annotation on Pojo
065      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
066   }
067}