001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.juneau.examples.core.rdf; 020 021import org.apache.juneau.examples.core.pojo.Pojo; 022import org.apache.juneau.jena.*; 023 024/** 025 * Sample class which shows the simple usage of RdfXmlSerializer. 026 */ 027public class RdfExample { 028 029 /** 030 * Serializing Pojo bean into RDF XML format. 031 * @param args 032 * @throws Exception 033 */ 034 public static void main(String[] args) throws Exception { 035 Pojo pojo = new Pojo("rdf","This is RDF format."); 036 // this creates an RDF serializer with the default XML structure 037 /**Produces 038 * <rdf:RDF 039 * xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 040 * xmlns:j="http://www.apache.org/juneau/" 041 * xmlns:jp="http://www.apache.org/juneaubp/" > 042 * <rdf:Description rdf:nodeID="A0"> 043 * <jp:name>This is RDF format.</jp:name> 044 * <jp:id>rdf</jp:id> 045 * </rdf:Description> 046 * </rdf:RDF> 047 */ 048 RdfSerializer rdfSerializer = RdfXmlSerializer.DEFAULT; 049 // This will show the final output from the bean 050 System.out.println(rdfSerializer.serialize(pojo)); 051 052 /**Produces 053 * <rdf:RDF 054 * xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 055 * xmlns:j="http://www.apache.org/juneau/" 056 * xmlns:jp="http://www.apache.org/juneaubp/"> 057 * <rdf:Description> 058 * <jp:name>This is RDF format.</jp:name> 059 * <jp:id>rdf</jp:id> 060 * </rdf:Description> 061 * </rdf:RDF> 062 */ 063 String rdfXml = RdfXmlAbbrevSerializer.DEFAULT.serialize(pojo); 064 System.out.println(rdfXml); 065 066 // Deserialize back to Pojo instance type. 067 Pojo xmlAbParsed = RdfXmlParser.DEFAULT.parse(rdfXml,Pojo.class); 068 assert xmlAbParsed.getClass().equals(pojo.getClass()); 069 assert xmlAbParsed.getId().equals(pojo.getId()); 070 071 /**Produces 072 * @prefix jp: <http://www.apache.org/juneaubp/> . 073 * @prefix j: <http://www.apache.org/juneau/> . 074 * 075 * [] jp:id "rdf" ; 076 * jp:name "This is RDF format." . 077 */ 078 String rdfN3 = N3Serializer.DEFAULT.serialize(pojo); 079 System.out.println(rdfN3); 080 081 // Deserialize back to Pojo instance type. 082 Pojo n3parsed = N3Parser.DEFAULT.parse(rdfN3,Pojo.class); 083 assert n3parsed.getClass().equals(pojo.getClass()); 084 assert n3parsed.getId().equals(pojo.getId()); 085 086 /**Produces 087 *_:A5ecded4fX3aX167a62fdefeX3aXX2dX7ffc <http://www.apache.org/juneaubp/name> "This is RDF format." . 088 *_:A5ecded4fX3aX167a62fdefeX3aXX2dX7ffc <http://www.apache.org/juneaubp/id> "rdf" . 089 */ 090 String rdfNTriple = NTripleSerializer.DEFAULT.serialize(pojo); 091 System.out.println(rdfNTriple); 092 093 // Deserialize back to Pojo instance type. 094 Pojo nTripleparsed = NTripleParser.DEFAULT.parse(rdfNTriple,Pojo.class); 095 assert nTripleparsed.getClass().equals(pojo.getClass()); 096 assert nTripleparsed.getId().equals(pojo.getId()); 097 098 /** 099 * @prefix jp: <http://www.apache.org/juneaubp/> . 100 * @prefix j: <http://www.apache.org/juneau/> . 101 * 102 * [] jp:id "rdf" ; 103 * jp:name "This is RDF format." . 104 */ 105 String rdfTurtle = TurtleSerializer.DEFAULT.serialize(pojo); 106 System.out.println(rdfTurtle); 107 108 // Deserialize back to Pojo instance type. 109 Pojo turtleparsed = TurtleParser.DEFAULT.parse(rdfTurtle,Pojo.class); 110 assert turtleparsed.getClass().equals(pojo.getClass()); 111 assert turtleparsed.getId().equals(pojo.getId()); 112 113 114 } 115}