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.marshall.jena.marshaller; 018 019import org.apache.juneau.marshall.jena.*; 020import org.apache.juneau.marshall.marshaller.*; 021import java.lang.reflect.*; 022import org.apache.juneau.marshall.parser.*; 023import org.apache.juneau.marshall.serializer.*; 024 025/** 026 * A pairing of a {@link N3Serializer} and {@link N3Parser} into a single class with convenience to/of methods. 027 * 028 * <p> 029 * The general idea is to combine a single serializer and parser inside a simplified API for serializing and parsing POJOs. 030 * 031 * <h5 class='figure'>Examples:</h5> 032 * <p class='bjava'> 033 * <jc>// Using instance.</jc> 034 * N3 <jv>n3</jv> = <jk>new</jk> N3(); 035 * MyPojo <jv>myPojo</jv> = <jv>n3</jv>.read(<jv>string</jv>, MyPojo.<jk>class</jk>); 036 * String <jv>string</jv> = <jv>n3</jv>.write(<jv>myPojo</jv>); 037 * </p> 038 * <p class='bjava'> 039 * <jc>// Using DEFAULT instance.</jc> 040 * MyPojo <jv>myPojo</jv> = N3.<jsf>DEFAULT</jsf>.read(<jv>string</jv>, MyPojo.<jk>class</jk>); 041 * String <jv>string</jv> = N3.<jsf>DEFAULT</jsf>.write(<jv>myPojo</jv>); 042 * </p> 043 * 044 * <h5 class='figure'>Example output (bean with name/age, N3/Turtle-like):</h5> 045 * <p class='bcode'> 046 * @prefix j: <...> . 047 * <...> a j:Person ; j:name "Alice" ; j:age 30 . 048 * </p> 049 * 050 * <h5 class='figure'>Complex (nested address + array):</h5> 051 * <p class='bcode'> 052 * @prefix j: <...> . 053 * <...> a j:Person ; j:name "Alice" ; j:age 30 ; 054 * j:address <.../address> ; j:tags <.../tags> . 055 * <.../address> a j:Address ; j:street "123 Main St" ; 056 * j:city "Boston" ; j:state "MA" . 057 * </p> 058 * 059 * <p class='bjava'> 060 * <jc>// Using static shortcuts.</jc> 061 * MyPojo <jv>myPojo</jv> = N3.<jsm>to</jsm>(<jv>string</jv>, MyPojo.<jk>class</jk>); 062 * String <jv>string</jv> = N3.<jsm>of</jsm>(<jv>myPojo</jv>); 063 * </p> 064 * 065 * <h5 class='section'>See Also:</h5><ul> 066 * <li class='link'>{doc jmr.RdfDetails} 067 * <li class='link'>{doc jm.Marshallers} 068 * </ul> 069 */ 070public class N3 extends CharMarshaller { 071 072 /** 073 * Default reusable instance. 074 */ 075 public static final N3 DEFAULT = new N3(); 076 077 /** 078 * Serializes a POJO to a <c>String</c> using the {@link #DEFAULT} marshaller. 079 * 080 * <p> 081 * A shortcut for calling <c><jsf>DEFAULT</jsf>.write(<jv>object</jv>)</c>. 082 * 083 * @param object The object to serialize. 084 * @return The serialized object. 085 * @throws SerializeException If a problem occurred trying to convert the output. 086 */ 087 public static String of(Object object) throws SerializeException { 088 return DEFAULT.write(object); 089 } 090 091 /** 092 * Parses an input into the specified object type using the {@link #DEFAULT} marshaller. 093 * 094 * <p> 095 * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>)</c>. 096 * 097 * @param <T> The class type of the object being created. 098 * @param input The input. 099 * @param type The object type to create. 100 * @return The parsed object. 101 * @throws ParseException Malformed input encountered. 102 */ 103 public static <T> T to(String input, Class<T> type) throws ParseException { 104 return DEFAULT.read(input, type); 105 } 106 107 /** 108 * Parses an input into the specified parameterized object type using the {@link #DEFAULT} marshaller. 109 * 110 * <p> 111 * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>, <jv>args</jv>)</c>. 112 * 113 * @param <T> The class type of the object to create. 114 * @param input The input. 115 * @param type The object type to create. 116 * @param args The type arguments of the class if it's a collection or map. 117 * @return The parsed object. 118 * @throws ParseException Malformed input encountered. 119 */ 120 public static <T> T to(String input, Type type, Type... args) throws ParseException { 121 return DEFAULT.read(input, type, args); 122 } 123 124 /** 125 * Constructor. 126 * 127 * <p> 128 * Uses {@link N3Serializer#DEFAULT} and {@link N3Parser#DEFAULT}. 129 */ 130 public N3() { 131 this(N3Serializer.DEFAULT, N3Parser.DEFAULT); 132 } 133 134 /** 135 * Constructor. 136 * 137 * @param s 138 * The serializer to use for serializing output. 139 * <br>Must not be <jk>null</jk>. 140 * @param p 141 * The parser to use for parsing input. 142 * <br>Must not be <jk>null</jk>. 143 */ 144 public N3(N3Serializer s, N3Parser p) { 145 super(s, p); 146 } 147}