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 * 027 * <ul class='seealso'> 028 * <li class='extlink'>{@source} 029 * </ul> 030 */ 031public class RdfExample { 032 033 /** 034 * Serializing Pojo 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 Pojo pojo = new Pojo("rdf","This is RDF format."); 041 // this creates an RDF serializer with the default XML structure 042 /**Produces 043 * <rdf:RDF 044 * xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 045 * xmlns:j="http://www.apache.org/juneau/" 046 * xmlns:jp="http://www.apache.org/juneaubp/" > 047 * <rdf:Description rdf:nodeID="A0"> 048 * <jp:name>This is RDF format.</jp:name> 049 * <jp:id>rdf</jp:id> 050 * </rdf:Description> 051 * </rdf:RDF> 052 */ 053 RdfSerializer rdfSerializer = RdfXmlSerializer.DEFAULT; 054 // This will show the final output from the bean 055 System.out.println(rdfSerializer.serialize(pojo)); 056 057 /**Produces 058 * <rdf:RDF 059 * xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 060 * xmlns:j="http://www.apache.org/juneau/" 061 * xmlns:jp="http://www.apache.org/juneaubp/"> 062 * <rdf:Description> 063 * <jp:name>This is RDF format.</jp:name> 064 * <jp:id>rdf</jp:id> 065 * </rdf:Description> 066 * </rdf:RDF> 067 */ 068 String rdfXml = RdfXmlAbbrevSerializer.DEFAULT.serialize(pojo); 069 System.out.println(rdfXml); 070 071 // Deserialize back to Pojo instance type. 072 Pojo xmlAbParsed = RdfXmlParser.DEFAULT.parse(rdfXml,Pojo.class); 073 assert xmlAbParsed.getClass().equals(pojo.getClass()); 074 assert xmlAbParsed.getId().equals(pojo.getId()); 075 076 /**Produces 077 * @prefix jp: <http://www.apache.org/juneaubp/> . 078 * @prefix j: <http://www.apache.org/juneau/> . 079 * 080 * [] jp:id "rdf" ; 081 * jp:name "This is RDF format." . 082 */ 083 String rdfN3 = N3Serializer.DEFAULT.serialize(pojo); 084 System.out.println(rdfN3); 085 086 // Deserialize back to Pojo instance type. 087 Pojo n3parsed = N3Parser.DEFAULT.parse(rdfN3,Pojo.class); 088 assert n3parsed.getClass().equals(pojo.getClass()); 089 assert n3parsed.getId().equals(pojo.getId()); 090 091 /**Produces 092 *_:A5ecded4fX3aX167a62fdefeX3aXX2dX7ffc <http://www.apache.org/juneaubp/name> "This is RDF format." . 093 *_:A5ecded4fX3aX167a62fdefeX3aXX2dX7ffc <http://www.apache.org/juneaubp/id> "rdf" . 094 */ 095 String rdfNTriple = NTripleSerializer.DEFAULT.serialize(pojo); 096 System.out.println(rdfNTriple); 097 098 // Deserialize back to Pojo instance type. 099 Pojo nTripleparsed = NTripleParser.DEFAULT.parse(rdfNTriple,Pojo.class); 100 assert nTripleparsed.getClass().equals(pojo.getClass()); 101 assert nTripleparsed.getId().equals(pojo.getId()); 102 103 /** 104 * @prefix jp: <http://www.apache.org/juneaubp/> . 105 * @prefix j: <http://www.apache.org/juneau/> . 106 * 107 * [] jp:id "rdf" ; 108 * jp:name "This is RDF format." . 109 */ 110 String rdfTurtle = TurtleSerializer.DEFAULT.serialize(pojo); 111 System.out.println(rdfTurtle); 112 113 // Deserialize back to Pojo instance type. 114 Pojo turtleparsed = TurtleParser.DEFAULT.parse(rdfTurtle,Pojo.class); 115 assert turtleparsed.getClass().equals(pojo.getClass()); 116 assert turtleparsed.getId().equals(pojo.getId()); 117 118 119 } 120}