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