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