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.xml;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022
023import org.apache.juneau.commons.logging.*;
024import org.apache.juneau.examples.core.pojo.*;
025import org.apache.juneau.marshall.xml.*;
026
027/**
028 * Xml configuration example.
029 *
030 */
031public class XmlConfigurationExample {
032
033   /**
034    * Examples on XML Serializers configured using properties
035    * defined in XmlSerializer class.
036    *
037    * @param args Unused.
038    * @throws Exception Unused.
039    */
040   public static void main(String[] args) throws Exception {
041
042      var aPojo = new Pojo("a", "<pojo>");
043
044      /**
045       * Xml Serializers can be configured using properties defined in XmlSerializer.
046       * Produces
047       * <object>
048       * <name>&lt;pojo&gt;</name>
049       * <id>a</id>
050       * </object>
051       */
052      var withWhitespace = XmlSerializer.create().ws().build().write(aPojo);
053      // the output will be padded with spaces after format characters.
054      RichLogger.getLogger(XmlConfigurationExample.class).info(withWhitespace);
055
056      Map<String,List<Pojo>> values = m();
057      var pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
058
059      //Produces
060      //<object><innerPojo><name>name0</name><id>1.0</id></innerPojo><id>pojo</id></object>
061      var mapescaped = XmlSerializer.create().trimEmptyMaps().build().write(pojoc);
062      // the output will have trimmed Empty maps.
063      RichLogger.getLogger(XmlConfigurationExample.class).info(mapescaped);
064
065      //Produces
066      //<object xmlns="http://www.apache.org/2013/Juneau"><name>&lt;pojo&gt;</name><id>a</id></object>
067      var nspaceToRoot = XmlSerializer.create().ns().addNamespaceUrisToRoot().build().write(aPojo);
068      // the output will add default name space to the xml document root.
069      RichLogger.getLogger(XmlConfigurationExample.class).info(nspaceToRoot);
070
071      var nPojo = new Pojo("a", null);
072
073      //Produces
074      //<object><id>a</id></object>
075      var nullescaped = XmlSerializer.create().build().write(nPojo);
076      // the output will have trimmed null properties.
077      RichLogger.getLogger(XmlConfigurationExample.class).info(nullescaped);
078
079      //Produces
080      //<object xmlns="http://www.pierobon.org/iis/review1.htm.html#one"><name>&lt;pojo&gt;</name><id>a</id></object>
081      var dNamsSpace = XmlSerializer.create().enableNamespaces().defaultNamespace(Namespace.create("http://www.pierobon.org" + "/iis/review1.htm.html#one")).addNamespaceUrisToRoot().build()
082         .write(aPojo);
083      // the output will have new default namespace added.
084      RichLogger.getLogger(XmlConfigurationExample.class).info(dNamsSpace);
085
086   }
087}