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