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.xml;
014
015import org.apache.juneau.examples.core.pojo.Pojo;
016import org.apache.juneau.examples.core.pojo.PojoComplex;
017import org.apache.juneau.xml.*;
018
019import java.util.HashMap;
020import java.util.List;
021
022/**
023 * Xml configuration example.
024 *
025 * <h5 class='section'>See Also:</h5><ul>
026 * </ul>
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      Pojo 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      String 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      HashMap<String, List<Pojo>> values = new HashMap<>();
054      PojoComplex 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      String 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      String 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      Pojo nPojo = new Pojo("a",null);
069
070      //Produces
071      //<object><id>a</id></object>
072      String 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      String dNamsSpace = XmlSerializer.create().enableNamespaces().defaultNamespace(Namespace.create("http://www.pierobon.org" +
079         "/iis/review1.htm.html#one")).addNamespaceUrisToRoot().build()
080         .serialize(aPojo);
081      // the output will have new default namespace added.
082      System.out.println(dNamsSpace);
083
084
085   }
086}