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