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 NQuadsSerializer} and {@link NQuadsParser} into a single class with convenience to/of methods. 027 * 028 * <h5 class='figure'>Examples:</h5> 029 * <p class='bjava'> 030 * <jc>// Serialize a bean or map to N-Quads</jc> 031 * String <jv>nquads</jv> = NQuads.<jsm>of</jsm>(<jv>myBean</jv>); 032 * 033 * <jc>// Parse N-Quads into a bean or map</jc> 034 * MyPojo <jv>parsed</jv> = NQuads.<jsm>to</jsm>(<jv>nquads</jv>, MyPojo.<jk>class</jk>); 035 * </p> 036 * <p class='bjava'> 037 * <jc>// Using instance methods</jc> 038 * NQuads <jv>m</jv> = NQuads.<jsf>DEFAULT</jsf>; 039 * <jv>nquads</jv> = <jv>m</jv>.write(<jv>myBean</jv>); 040 * <jv>parsed</jv> = <jv>m</jv>.read(<jv>nquads</jv>, MyPojo.<jk>class</jk>); 041 * </p> 042 * 043 * <h5 class='figure'>Example output (bean with name/age, N-Triples with graph):</h5> 044 * <p class='bcode'> 045 * <...> <.../name> "Alice" <...> . 046 * <...> <.../age> "30"^^<http://www.w3.org/2001/XMLSchema#int> <...> . 047 * </p> 048 * 049 * <h5 class='figure'>Complex (nested address + array):</h5> 050 * <p class='bcode'> 051 * <...> <.../name> "Alice" <graph> . 052 * <...> <.../address> <.../address> <graph> . 053 * <.../address> <.../street> "123 Main St" <graph> . 054 * <.../address> <.../city> "Boston" <graph> . 055 * <.../address> <.../state> "MA" <graph> . 056 * </p> 057 * 058 * <p class='bjava'> 059 * <jc>// Using static shortcuts.</jc> 060 * MyPojo <jv>myPojo</jv> = NQuads.<jsm>to</jsm>(<jv>string</jv>, MyPojo.<jk>class</jk>); 061 * String <jv>string</jv> = NQuads.<jsm>of</jsm>(<jv>myPojo</jv>); 062 * </p> 063 * 064 * <h5 class='section'>See Also:</h5><ul> 065 * <li class='link'>{doc jmr.RdfDetails} 066 * <li class='link'>{doc jm.Marshallers} 067 * </ul> 068 */ 069public class NQuads extends CharMarshaller { 070 071 /** Default reusable instance.*/ 072 public static final NQuads DEFAULT = new NQuads(); 073 074 /** 075 * Serializes a POJO to a <c>String</c> using the {@link #DEFAULT} marshaller. 076 * 077 * <p> 078 * A shortcut for calling <c><jsf>DEFAULT</jsf>.write(<jv>object</jv>)</c>. 079 * 080 * @param object The object to serialize. 081 * @return The serialized object. 082 * @throws SerializeException If a problem occurred trying to convert the output. 083 */ 084 public static String of(Object object) throws SerializeException { 085 return DEFAULT.write(object); 086 } 087 088 /** 089 * Parses an input into the specified object type using the {@link #DEFAULT} marshaller. 090 * 091 * <p> 092 * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>)</c>. 093 * 094 * @param <T> The class type of the object being created. 095 * @param input The input. 096 * @param type The object type to create. 097 * @return The parsed object. 098 * @throws ParseException Malformed input encountered. 099 */ 100 public static <T> T to(String input, Class<T> type) throws ParseException { 101 return DEFAULT.read(input, type); 102 } 103 104 /** 105 * Parses an input into the specified parameterized object type using the {@link #DEFAULT} marshaller. 106 * 107 * <p> 108 * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>, <jv>args</jv>)</c>. 109 * 110 * @param <T> The class type of the object to create. 111 * @param input The input. 112 * @param type The object type to create. 113 * @param args The type arguments of the class if it's a collection or map. 114 * @return The parsed object. 115 * @throws ParseException Malformed input encountered. 116 */ 117 public static <T> T to(String input, Type type, Type... args) throws ParseException { 118 return DEFAULT.read(input, type, args); 119 } 120 121 /** Constructor using defaults.*/ 122 public NQuads() { 123 this(NQuadsSerializer.DEFAULT, NQuadsParser.DEFAULT); 124 } 125 126 /** 127 * Constructor with serializer and parser. 128 * 129 * @param s The serializer to use. 130 * <br>Must not be <jk>null</jk>. 131 * @param p The parser to use. 132 * <br>Must not be <jk>null</jk>. 133 */ 134 public NQuads(NQuadsSerializer s, NQuadsParser p) { 135 super(s, p); 136 } 137}