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.rdf;
014
015import org.apache.juneau.examples.core.pojo.*;
016import org.apache.juneau.jena.RdfSerializer;
017import org.apache.juneau.jena.RdfXmlAbbrevSerializer;
018import org.apache.juneau.jena.RdfXmlSerializer;
019
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023
024/**
025 * Sample class which shows the complex usage of RdfXmlSerializer.
026 *
027 * <ul class='seealso'>
028 *    <li class='extlink'>{@source}
029 * </ul>
030 */
031public class RdfComplexExample {
032
033   /**
034    * Serializing PojoComplex bean into RDF XML format.
035    *
036    * @param args Unused.
037    * @throws Exception Unused.
038    */
039   public static void main(String[] args) throws Exception {
040
041      // Fill some data to a PojoComplex bean
042      HashMap<String, List<Pojo>> values = new HashMap<>();
043      ArrayList<Pojo> setOne = new ArrayList<>();
044      setOne.add(new Pojo("1.1", "name1"));
045      setOne.add(new Pojo("1.1", "name2"));
046      ArrayList<Pojo> setTwo = new ArrayList<>();
047      setTwo.add(new Pojo("1.2", "name1"));
048      setTwo.add(new Pojo("1.2", "name2"));
049      values.put("setOne", setOne);
050      values.put("setTwo", setTwo);
051      PojoComplex pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
052
053      // this creates an RDF serializer with the default XML structure
054      /**Produces
055       * <rdf:RDF
056       * xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
057       * xmlns:j="http://www.apache.org/juneau/"
058       * xmlns:jp="http://www.apache.org/juneaubp/" >
059       * <rdf:Description rdf:nodeID="A0">
060       * <jp:name>name1</jp:name>
061       * <jp:id>1.1</jp:id>
062       * </rdf:Description>
063       * <rdf:Description rdf:nodeID="A1">
064       * <jp:innerPojo rdf:nodeID="A2"/>
065       * <jp:values rdf:nodeID="A3"/>
066       * <jp:id>pojo</jp:id>
067       * </rdf:Description>
068       * <rdf:Description rdf:nodeID="A3">
069       * <jp:setOne rdf:nodeID="A4"/>
070       * <jp:setTwo rdf:nodeID="A5"/>
071       * </rdf:Description>
072       * <rdf:Description rdf:nodeID="A6">
073       * <jp:name>name2</jp:name>
074       * <jp:id>1.1</jp:id>
075       * </rdf:Description>
076       * <rdf:Description rdf:nodeID="A2">
077       * <jp:name>name0</jp:name>
078       * <jp:id>1.0</jp:id>
079       * </rdf:Description>
080       * <rdf:Description rdf:nodeID="A7">
081       * <jp:name>name2</jp:name>
082       * <jp:id>1.2</jp:id>
083       * </rdf:Description>
084       * <rdf:Description rdf:nodeID="A4">
085       * <rdf:_2 rdf:nodeID="A6"/>
086       * <rdf:_1 rdf:nodeID="A0"/>
087       * <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq"/>
088       * </rdf:Description>
089       * <rdf:Description rdf:nodeID="A5">
090       * <rdf:_2 rdf:nodeID="A7"/>
091       * <rdf:_1 rdf:nodeID="A8"/>
092       * <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq"/>
093       * </rdf:Description>
094       * <rdf:Description rdf:nodeID="A8">
095       * <jp:name>name1</jp:name>
096       * <jp:id>1.2</jp:id>
097       * </rdf:Description>
098       * </rdf:RDF>
099       */
100      RdfSerializer rdfSerializer = RdfXmlSerializer.DEFAULT;
101      // This will show the final output from the bean
102      System.out.println(rdfSerializer.serialize(pojoc));
103
104      //Usage of RdfXmlAbbrevSerializer.
105      String rdfXml = RdfXmlAbbrevSerializer.DEFAULT.serialize(pojoc);
106      System.out.println(rdfXml);
107   }
108}