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