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