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 TriGSerializer} and {@link TriGParser} 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 TriG (Turtle with named graphs)</jc>
031 *    String <jv>trig</jv> = TriG.<jsm>of</jsm>(<jv>myBean</jv>);
032 *
033 *    <jc>// Parse TriG into a bean or map</jc>
034 *    MyPojo <jv>parsed</jv> = TriG.<jsm>to</jsm>(<jv>trig</jv>, MyPojo.<jk>class</jk>);
035 * </p>
036 * <p class='bjava'>
037 *    <jc>// Using instance methods</jc>
038 *    TriG <jv>m</jv> = TriG.<jsf>DEFAULT</jsf>;
039 *    <jv>trig</jv> = <jv>m</jv>.write(<jv>myBean</jv>);
040 *    <jv>parsed</jv> = <jv>m</jv>.read(<jv>trig</jv>, MyPojo.<jk>class</jk>);
041 * </p>
042 *
043 * <h5 class='figure'>Example output (bean with name/age, Turtle with named graph):</h5>
044 * <p class='bcode'>
045 *    @prefix j: &lt;...&gt; .
046 *    &lt;...&gt; { &lt;...&gt; a j:Person ; j:name "Alice" ; j:age 30 . }
047 * </p>
048 *
049 * <h5 class='figure'>Complex (nested address + array):</h5>
050 * <p class='bcode'>
051 *    @prefix j: &lt;...&gt; .
052 *    &lt;...&gt; { &lt;...&gt; a j:Person ; j:name "Alice" ; j:age 30 ;
053 *       j:address &lt;.../address&gt; . }
054 *    &lt;...&gt; { &lt;.../address&gt; a j:Address ; j:street "123 Main St" ;
055 *       j:city "Boston" ; j:state "MA" . }
056 * </p>
057 *
058 * <p class='bjava'>
059 * <jc>// Using static shortcuts.</jc>
060 *    MyPojo <jv>myPojo</jv> = TriG.<jsm>to</jsm>(<jv>string</jv>, MyPojo.<jk>class</jk>);
061 *    String <jv>string</jv> = TriG.<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 TriG extends CharMarshaller {
070
071   /** Default reusable instance.*/
072   public static final TriG DEFAULT = new TriG();
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 TriG() {
123      this(TriGSerializer.DEFAULT, TriGParser.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 TriG(TriGSerializer s, TriGParser p) {
135      super(s, p);
136   }
137}