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.commons.logging.*; 020import org.apache.juneau.examples.core.pojo.*; 021import org.apache.juneau.marshall.html.*; 022 023/** 024 * Sample class which shows the simple usage of HtmlSerializer and HtmlParser. 025 * 026 */ 027public class HtmlSimpleExample { 028 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 var htmlSerializer = HtmlSerializer.DEFAULT; 041 // Get a reference to a parser - converts that flat format back into the POJO 042 var htmlParser = HtmlParser.DEFAULT; 043 044 var pojo = new Pojo("id", "name"); 045 046 var flat = htmlSerializer.write(pojo); 047 048 // Print out the created POJO in JSON format. 049 RichLogger.getLogger(HtmlSimpleExample.class).info(flat); 050 051 var parse = htmlParser.read(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 var docSerialized = HtmlDocSerializer.DEFAULT.write(pojo); 063 RichLogger.getLogger(HtmlSimpleExample.class).info(docSerialized); 064 065 // The object above can be parsed thanks to the @BeanCtor(properties = id,name) annotation on Pojo 066 // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them. 067 } 068}