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