http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfCollectionFormat.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfCollectionFormat.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfCollectionFormat.java new file mode 100755 index 0000000..5b865a1 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfCollectionFormat.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * Note to U.S. Government Users Restricted Rights: Use, + * duplication or disclosure restricted by GSA ADP Schedule + * Contract with IBM Corp. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import com.ibm.juno.core.jena.annotation.*; + +/** + * Used in conjunction with the {@link Rdf#collectionFormat() @Rdf.collectionFormat()} annotation to fine-tune how + * classes, beans, and bean properties are serialized, particularly collections. + * <p> + * + * @author James Bognar ([email protected]) + */ +public enum RdfCollectionFormat { + + /** + * Default formatting (default). + * <p> + * Inherit formatting from parent class or parent package. + * If no formatting specified at any level, default is {@link #SEQ}. + */ + DEFAULT, + + /** + * Causes collections and arrays to be rendered as RDF sequences. + */ + SEQ, + + /** + * Causes collections and arrays to be rendered as RDF bags. + */ + BAG, + + /** + * Causes collections and arrays to be rendered as RDF lists. + */ + LIST, + + /** + * Causes collections and arrays to be rendered as multi-valued RDF properties instead of sequences. + * <p> + * Note that enabling this setting will cause order of elements in the collection to be lost. + */ + MULTI_VALUED; + +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$N3.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$N3.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$N3.class new file mode 100755 index 0000000..9170a84 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$N3.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$NTriple.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$NTriple.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$NTriple.class new file mode 100755 index 0000000..4a64589 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$NTriple.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$Turtle.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$Turtle.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$Turtle.class new file mode 100755 index 0000000..39cc7e4 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$Turtle.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$Xml.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$Xml.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$Xml.class new file mode 100755 index 0000000..3398700 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser$Xml.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser.class new file mode 100755 index 0000000..8f78101 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser.java new file mode 100755 index 0000000..50fec99 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParser.java @@ -0,0 +1,527 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import static com.ibm.juno.core.jena.Constants.*; +import static com.ibm.juno.core.jena.RdfProperties.*; +import static com.ibm.juno.core.utils.StringUtils.*; +import static com.ibm.juno.core.xml.XmlUtils.*; + +import java.io.*; +import java.lang.reflect.*; +import java.util.*; + +import com.hp.hpl.jena.rdf.model.*; +import com.hp.hpl.jena.util.iterator.*; +import com.ibm.juno.core.*; +import com.ibm.juno.core.annotation.*; +import com.ibm.juno.core.filter.*; +import com.ibm.juno.core.parser.*; +import com.ibm.juno.core.utils.*; +import com.ibm.juno.core.xml.*; + +/** + * Parses RDF into POJOs. + * + * + * <h6 class='topic'>Configurable properties</h6> + * <p> + * Refer to <a class='doclink' href='package-summary.html#ParserConfigurableProperties'>Configurable Properties</a> + * for the entire list of configurable properties. + * + * + * <h6 class='topic'>Behavior-specific subclasses</h6> + * <p> + * The following direct subclasses are provided for language-specific parsers: + * <ul> + * <li>{@link RdfParser.Xml} - RDF/XML and RDF/XML-ABBREV. + * <li>{@link RdfParser.NTriple} - N-TRIPLE. + * <li>{@link RdfParser.Turtle} - TURTLE. + * <li>{@link RdfParser.N3} - N3. + * </ul> + * + * + * <h6 class='topic'>Additional Information</h6> + * <p> + * See <a class='doclink' href='package-summary.html#TOC'>RDF Overview</a> for an overview of RDF support in Juno. + * + * + * @author James Bognar ([email protected]) + */ +@Consumes(value="text/xml+rdf") +public class RdfParser extends ReaderParser { + + /** Default XML parser, all default settings.*/ + public static final RdfParser DEFAULT_XML = new RdfParser.Xml().lock(); + + /** Default Turtle parser, all default settings.*/ + public static final RdfParser DEFAULT_TURTLE = new RdfParser.Turtle().lock(); + + /** Default N-Triple parser, all default settings.*/ + public static final RdfParser DEFAULT_NTRIPLE = new RdfParser.NTriple().lock(); + + /** Default N3 parser, all default settings.*/ + public static final RdfParser DEFAULT_N3 = new RdfParser.N3().lock(); + + + /** Consumes RDF/XML input */ + @Consumes("text/xml+rdf") + public static class Xml extends RdfParser { + /** Constructor */ + public Xml() { + setProperty(RDF_language, LANG_RDF_XML); + } + } + + /** Consumes N-Triple input */ + @Consumes(value="text/n-triple") + public static class NTriple extends RdfParser { + /** Constructor */ + public NTriple() { + setProperty(RDF_language, LANG_NTRIPLE); + } + } + + /** Consumes Turtle input */ + @Consumes(value="text/turtle") + public static class Turtle extends RdfParser { + /** Constructor */ + public Turtle() { + setProperty(RDF_language, LANG_TURTLE); + } + } + + /** Consumes N3 input */ + @Consumes(value="text/n3") + public static class N3 extends RdfParser { + /** Constructor */ + public N3() { + setProperty(RDF_language, LANG_N3); + } + } + + + /** Jena parser properties currently set on this parser. */ + protected transient RdfParserProperties rpp = new RdfParserProperties(); + + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Override /* ReaderParser */ + protected <T> T doParse(Reader in, int estimatedSize, ClassMeta<T> type, ParserContext ctx) throws ParseException, IOException { + + in = IOUtils.getBufferedReader(in, estimatedSize); + + RdfParserContext rctx = (RdfParserContext)ctx; + + type = ctx.getBeanContext().normalizeClassMeta(type); + + Model model = rctx.model; + RDFReader r = rctx.rdfReader; + r.read(model, in, null); + BeanContext bc = ctx.getBeanContext(); + + List<Resource> roots = getRoots(model, rctx); + + try { + // Special case where we're parsing a loose collection of resources. + if (rctx.isLooseCollection() && (type.isCollection() || type.isArray())) { + Collection c = null; + if (type.isArray()) + c = new ArrayList(); + else + c = (type.canCreateNewInstance(ctx.getOuter()) ? (Collection<?>)type.newInstance(ctx.getOuter()) : new ObjectList(bc)); + for (Resource resource : roots) + c.add(parseAnything(type.getElementType(), resource, null, rctx, ctx.getOuter(), null)); + + if (type.isArray()) + return (T)bc.toArray(type, c); + return (T)c; + } + + if (roots.isEmpty()) + return null; + if (roots.size() > 1) + throw new ParseException("Too many root nodes found in model: {0}", roots.size()); + Resource resource = roots.get(0); + + return parseAnything(type, resource, null, rctx, ctx.getOuter(), null); + + } catch (ParseException e) { + throw e; + } catch (Exception e) { + throw new ParseException(e); + } + } + + /* + * Finds the roots in the model using either the "root" property to identify it, + * or by resorting to scanning the model for all nodes with no incoming predicates. + */ + private List<Resource> getRoots(Model m, RdfParserContext ctx) { + List<Resource> l = new LinkedList<Resource>(); + + // First try to find the root using the "http://www.ibm.com/juno/root" property. + Property root = m.createProperty(ctx.junoNs.getUri(), RDF_junoNs_ROOT); + for (ResIterator i = m.listResourcesWithProperty(root); i.hasNext();) + l.add(i.next()); + + if (! l.isEmpty()) + return l; + + // Otherwise, we need to find all resources that aren't objects. + // We want to explicitly ignore statements where the subject + // and object are the same node. + Set<RDFNode> objects = new HashSet<RDFNode>(); + for (StmtIterator i = m.listStatements(); i.hasNext();) { + Statement st = i.next(); + RDFNode subject = st.getSubject(); + RDFNode object = st.getObject(); + if (object.isResource() && ! object.equals(subject)) + objects.add(object); + } + for (ResIterator i = m.listSubjects(); i.hasNext();) { + Resource r = i.next(); + if (! objects.contains(r)) + l.add(r); + } + return l; + } + + private <T> BeanMap<T> parseIntoBeanMap(Resource r2, BeanMap<T> m, RdfParserContext ctx) throws ParseException { + BeanMeta<T> bm = m.getMeta(); + if (bm.hasBeanUriProperty() && r2.getURI() != null) + m.putBeanUri(r2.getURI()); + Property subTypeIdProperty = null; + BeanPropertyMeta<T> stp = bm.getSubTypeIdProperty(); + if (stp != null) { + subTypeIdProperty = ctx.getProperty(stp.getName()); + Statement st = r2.getProperty(subTypeIdProperty); + if (st == null) + throw new ParseException("Could not find subtype ID property for bean of type ''{0}''", m.getClassMeta()); + String subTypeId = st.getLiteral().getString(); + stp.set(m, subTypeId); + } + for (StmtIterator i = r2.listProperties(); i.hasNext();) { + Statement st = i.next(); + Property p = st.getPredicate(); + if (p.equals(subTypeIdProperty)) + continue; + String key = XmlUtils.decode(p.getLocalName()); + BeanPropertyMeta<T> pMeta = m.getPropertyMeta(key); + if (pMeta != null) { + RDFNode o = st.getObject(); + ClassMeta<?> cm = pMeta.getClassMeta(); + if ((cm.isArray() || cm.isCollection()) && isMultiValuedCollections(ctx, pMeta)) { + Object val = parseAnything(cm.getElementType(), o, pMeta, ctx, m.getBean(false), key); + pMeta.add(m, val); + } else { + Object val = parseAnything(cm, o, pMeta, ctx, m.getBean(false), key); + pMeta.set(m, val); + } + } else if (! (p.equals(ctx.pRoot) || p.equals(ctx.pClass) || p.equals(subTypeIdProperty))) { + if (bm.isSubTyped()) { + RDFNode o = st.getObject(); + Object val = parseAnything(object(), o, null, ctx, m.getBean(false), key); + m.put(key, val); + } else { + onUnknownProperty(ctx, key, m, -1, -1); + } + } + } + return m; + } + + private boolean isMultiValuedCollections(RdfParserContext ctx, BeanPropertyMeta<?> pMeta) { + if (pMeta != null && pMeta.getRdfMeta().getCollectionFormat() != RdfCollectionFormat.DEFAULT) + return pMeta.getRdfMeta().getCollectionFormat() == RdfCollectionFormat.MULTI_VALUED; + return ctx.getCollectionFormat() == RdfCollectionFormat.MULTI_VALUED; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private <T> T parseAnything(ClassMeta<T> nt, RDFNode n, BeanPropertyMeta<?> p, RdfParserContext ctx, Object outer, Object name) throws ParseException { + + BeanContext bc = ctx.getBeanContext(); + + if (nt == null) + nt = (ClassMeta<T>)object(); + PojoFilter<T,Object> filter = (PojoFilter<T,Object>)nt.getPojoFilter(); + ClassMeta<?> ft = nt.getFilteredClassMeta(); + + if (! ft.canCreateNewInstance(outer)) { + if (n.isResource()) { + Statement st = n.asResource().getProperty(ctx.pClass); + if (st != null) { + String c = st.getLiteral().getString(); + ft = nt = (ClassMeta<T>)bc.getClassMetaFromString(c); + } + } + } + + try { + + Object o = null; + if (n.isResource() && n.asResource().getURI() != null && n.asResource().getURI().equals(RDF_NIL)) { + // Do nothing. Leave o == null. + } else if (ft.isObject()) { + if (n.isLiteral()) { + o = n.asLiteral().getValue(); + if (o instanceof String) { + String s = o.toString(); + s = decode(s); + if (ctx.trimWhitespace) + s = s.trim(); + o = s; + } + } + else if (n.isResource()) { + Resource r = n.asResource(); + if (ctx.wasAlreadyProcessed(r)) + o = r.getURI(); + else if (r.getProperty(ctx.pValue) != null) { + o = parseAnything(object(), n.asResource().getProperty(ctx.pValue).getObject(), null, ctx, outer, null); + } else if (isSeq(r, ctx)) { + o = new ObjectList(bc); + parseIntoCollection(r.as(Seq.class), (Collection)o, ft.getElementType(), ctx); + } else if (isBag(r, ctx)) { + o = new ObjectList(bc); + parseIntoCollection(r.as(Bag.class), (Collection)o, ft.getElementType(), ctx); + } else if (r.canAs(RDFList.class)) { + o = new ObjectList(bc); + parseIntoCollection(r.as(RDFList.class), (Collection)o, ft.getElementType(), ctx); + } else { + // If it has a URI and no child properties, we interpret this as an + // external resource, and convert it to just a URL. + String uri = r.getURI(); + if (uri != null && ! r.listProperties().hasNext()) { + o = r.getURI(); + } else { + o = new ObjectMap(bc); + parseIntoMap(r, (Map)o, null, null, ctx); + } + } + } else { + throw new ParseException("Unrecognized node type ''{0}'' for object", n); + } + } else if (ft.isBoolean()) { + o = bc.convertToType(getValue(n, ctx, outer), boolean.class); + } else if (ft.isCharSequence()) { + String s = decode(getValue(n, ctx, outer).toString()); + if (ctx.trimWhitespace) + s = s.trim(); + o = s; + } else if (ft.isChar()) { + o = decode(getValue(n, ctx, outer).toString()).charAt(0); + } else if (ft.isNumber()) { + o = parseNumber(getValue(n, ctx, outer).toString(), (Class<? extends Number>)ft.getInnerClass()); + } else if (ft.isMap()) { + Resource r = n.asResource(); + if (ctx.wasAlreadyProcessed(r)) + return null; + Map m = (ft.canCreateNewInstance(outer) ? (Map)ft.newInstance(outer) : new ObjectMap(bc)); + o = parseIntoMap(r, m, nt.getKeyType(), nt.getValueType(), ctx); + } else if (ft.isCollection() || ft.isArray()) { + if (ft.isArray()) + o = new ArrayList(); + else + o = (ft.canCreateNewInstance(outer) ? (Collection<?>)ft.newInstance(outer) : new ObjectList(bc)); + Resource r = n.asResource(); + if (ctx.wasAlreadyProcessed(r)) + return null; + if (isSeq(r, ctx)) { + parseIntoCollection(r.as(Seq.class), (Collection)o, ft.getElementType(), ctx); + } else if (isBag(r, ctx)) { + parseIntoCollection(r.as(Bag.class), (Collection)o, ft.getElementType(), ctx); + } else if (r.canAs(RDFList.class)) { + parseIntoCollection(r.as(RDFList.class), (Collection)o, ft.getElementType(), ctx); + } else { + throw new ParseException("Unrecognized node type ''{0}'' for collection", n); + } + if (ft.isArray()) + o = bc.toArray(ft, (Collection)o); + } else if (ft.canCreateNewInstanceFromObjectMap(outer)) { + Resource r = n.asResource(); + if (ctx.wasAlreadyProcessed(r)) + return null; + Map m = new ObjectMap(bc); + parseIntoMap(r, m, nt.getKeyType(), nt.getValueType(), ctx); + o = ft.newInstanceFromObjectMap(outer, (ObjectMap)m); + } else if (ft.canCreateNewBean(outer)) { + Resource r = n.asResource(); + if (ctx.wasAlreadyProcessed(r)) + return null; + BeanMap<?> bm = bc.newBeanMap(outer, ft.getInnerClass()); + o = parseIntoBeanMap(r, bm, ctx).getBean(); + } else if (ft.isUri() && n.isResource()) { + o = ft.newInstanceFromString(outer, decode(n.asResource().getURI())); + } else if (ft.canCreateNewInstanceFromString(outer)) { + o = ft.newInstanceFromString(outer, decode(getValue(n, ctx, outer).toString())); + } else { + throw new ParseException("Class ''{0}'' could not be instantiated. Reason: ''{1}''", ft.getInnerClass().getName(), ft.getNotABeanReason()); + } + + if (filter != null && o != null) + o = filter.unfilter(o, nt); + + if (outer != null) + setParent(nt, o, outer); + + if (name != null) + setName(nt, o, name); + + return (T)o; + + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + if (p == null) + throw new ParseException("Error occurred trying to parse into class ''{0}''", ft).initCause(e); + throw new ParseException("Error occurred trying to parse value for bean property ''{0}'' on class ''{1}''", + p.getName(), p.getBeanMeta().getClassMeta() + ).initCause(e); + } + } + + private boolean isSeq(RDFNode n, RdfParserContext ctx) { + if (n.isResource()) { + Statement st = n.asResource().getProperty(ctx.pType); + if (st != null) + return RDF_SEQ.equals(st.getResource().getURI()); + } + return false; + } + + private boolean isBag(RDFNode n, RdfParserContext ctx) { + if (n.isResource()) { + Statement st = n.asResource().getProperty(ctx.pType); + if (st != null) + return RDF_BAG.equals(st.getResource().getURI()); + } + return false; + } + + private Object getValue(RDFNode n, RdfParserContext ctx, Object outer) throws ParseException { + if (n.isLiteral()) + return n.asLiteral().getValue(); + if (n.isResource()) { + Statement st = n.asResource().getProperty(ctx.pValue); + if (st != null) { + n = st.getObject(); + if (n.isLiteral()) + return n.asLiteral().getValue(); + return parseAnything(object(), st.getObject(), null, ctx, outer, null); + } + } + throw new ParseException("Unknown value type for node ''{0}''", n); + } + + private <K,V> Map<K,V> parseIntoMap(Resource r, Map<K,V> m, ClassMeta<K> keyType, ClassMeta<V> valueType, RdfParserContext ctx) throws ParseException { + // Add URI as "uri" to generic maps. + if (r.getURI() != null) { + K uri = convertAttrToType(m, "uri", keyType); + V value = convertAttrToType(m, r.getURI(), valueType); + m.put(uri, value); + } + for (StmtIterator i = r.listProperties(); i.hasNext();) { + Statement st = i.next(); + Property p = st.getPredicate(); + String key = p.getLocalName(); + if (! (key.equals("root") && p.getURI().equals(ctx.junoNs.getUri()))) { + key = decode(key); + RDFNode o = st.getObject(); + K key2 = convertAttrToType(m, key, keyType); + V value = parseAnything(valueType, o, null, ctx, m, key); + m.put(key2, value); + } + + } + // TODO Auto-generated method stub + return m; + } + + private <E> Collection<E> parseIntoCollection(Container c, Collection<E> l, ClassMeta<E> et, RdfParserContext ctx) throws ParseException { + for (NodeIterator ni = c.iterator(); ni.hasNext();) { + E e = parseAnything(et, ni.next(), null, ctx, l, null); + l.add(e); + } + return l; + } + + private <E> Collection<E> parseIntoCollection(RDFList list, Collection<E> l, ClassMeta<E> et, RdfParserContext ctx) throws ParseException { + for (ExtendedIterator<RDFNode> ni = list.iterator(); ni.hasNext();) { + E e = parseAnything(et, ni.next(), null, ctx, l, null); + l.add(e); + } + return l; + } + + //-------------------------------------------------------------------------------- + // Overridden methods + //-------------------------------------------------------------------------------- + + @Override /* Parser */ + public RdfParserContext createContext(ObjectMap op, Method javaMethod, Object outer) { + return new RdfParserContext(getBeanContext(), pp, rpp, op, javaMethod, outer); + } + + @Override /* CoreApi */ + public RdfParser setProperty(String property, Object value) throws LockedException { + checkLock(); + if (! rpp.setProperty(property, value)) + super.setProperty(property, value); + return this; + } + + @Override /* CoreApi */ + public RdfParser setProperties(ObjectMap properties) throws LockedException { + for (Map.Entry<String,Object> e : properties.entrySet()) + setProperty(e.getKey(), e.getValue()); + return this; + } + + @Override /* CoreApi */ + public RdfParser addNotBeanClasses(Class<?>...classes) throws LockedException { + super.addNotBeanClasses(classes); + return this; + } + + @Override /* CoreApi */ + public RdfParser addFilters(Class<?>...classes) throws LockedException { + super.addFilters(classes); + return this; + } + + @Override /* CoreApi */ + public <T> RdfParser addImplClass(Class<T> interfaceClass, Class<? extends T> implClass) throws LockedException { + super.addImplClass(interfaceClass, implClass); + return this; + } + + @Override /* CoreApi */ + public RdfParser setClassLoader(ClassLoader classLoader) throws LockedException { + super.setClassLoader(classLoader); + return this; + } + + @Override /* Lockable */ + public RdfParser lock() { + super.lock(); + return this; + } + + @Override /* Lockable */ + public RdfParser clone() { + try { + RdfParser c = (RdfParser)super.clone(); + c.rpp = rpp.clone(); + return c; + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); // Shouldn't happen + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserContext.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserContext.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserContext.class new file mode 100755 index 0000000..32ba6c5 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserContext.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserContext.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserContext.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserContext.java new file mode 100755 index 0000000..f85e31f --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserContext.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import static com.ibm.juno.core.jena.Constants.*; +import static com.ibm.juno.core.jena.RdfProperties.*; + +import java.lang.reflect.*; +import java.util.*; + +import com.hp.hpl.jena.rdf.model.*; +import com.ibm.juno.core.*; +import com.ibm.juno.core.parser.*; +import com.ibm.juno.core.xml.*; + +/** + * Context object that lives for the duration of a single parse of {@link RdfParser}. + * <p> + * See {@link ParserContext} for details. + * + * @author James Bognar ([email protected]) + */ +public class RdfParserContext extends ParserContext { + + final String rdfLanguage; + final Namespace junoNs, junoBpNs; + final Property pRoot, pValue, pClass, pType; + final Model model; + final boolean trimWhitespace, looseCollection; + final RDFReader rdfReader; + final Set<Resource> urisVisited = new HashSet<Resource>(); + final RdfCollectionFormat collectionFormat; + + /** + * Constructor. + * + * @param beanContext The bean context being used by the parser. + * @param pp Default general parser properties. + * @param rpp Default Jena parser properties. + * @param op Override properties. + * @param javaMethod The java method that called this parser, usually the method in a REST servlet. + * @param outer The outer object for instantiating top-level non-static inner classes. + */ + protected RdfParserContext(BeanContext beanContext, ParserProperties pp, RdfParserProperties rpp, ObjectMap op, Method javaMethod, Object outer) { + super(beanContext, pp, op, javaMethod, outer); + ObjectMap jenaSettings = new ObjectMap(); + jenaSettings.putAll(rpp.jenaSettings); + if (op == null || op.isEmpty()) { + this.rdfLanguage = rpp.rdfLanguage; + this.junoNs = rpp.junoNs; + this.junoBpNs = rpp.junoBpNs; + this.trimWhitespace = rpp.trimWhitespace; + this.collectionFormat = rpp.collectionFormat; + this.looseCollection = rpp.looseCollection; + } else { + this.rdfLanguage = op.getString(RDF_language, rpp.rdfLanguage); + this.junoNs = (op.containsKey(RDF_junoNs) ? NamespaceFactory.parseNamespace(op.get(RDF_junoNs)) : rpp.junoNs); + this.junoBpNs = (op.containsKey(RDF_junoBpNs) ? NamespaceFactory.parseNamespace(op.get(RDF_junoBpNs)) : rpp.junoBpNs); + this.trimWhitespace = op.getBoolean(RdfParserProperties.RDF_trimWhitespace, rpp.trimWhitespace); + this.collectionFormat = RdfCollectionFormat.valueOf(op.getString(RDF_collectionFormat, "DEFAULT")); + this.looseCollection = op.getBoolean(RDF_looseCollection, rpp.looseCollection); + } + this.model = ModelFactory.createDefaultModel(); + addModelPrefix(junoNs); + addModelPrefix(junoBpNs); + this.pRoot = model.createProperty(junoNs.getUri(), RDF_junoNs_ROOT); + this.pValue = model.createProperty(junoNs.getUri(), RDF_junoNs_VALUE); + this.pClass = model.createProperty(junoNs.getUri(), RDF_junoNs_CLASS); + this.pType = model.createProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); + rdfReader = model.getReader(rdfLanguage); + + // Note: NTripleReader throws an exception if you try to set any properties on it. + if (! rdfLanguage.equals(LANG_NTRIPLE)) { + for (Map.Entry<String,Object> e : jenaSettings.entrySet()) + rdfReader.setProperty(e.getKey(), e.getValue()); + } + } + + boolean wasAlreadyProcessed(Resource r) { + return ! urisVisited.add(r); + } + + /** + * Adds the specified namespace as a model prefix. + * + * @param ns The XML namespace. + */ + public void addModelPrefix(Namespace ns) { + model.setNsPrefix(ns.getName(), ns.getUri()); + } + + /** + * Constructs a <code>Property</code> in the specified namespace in this mode. + * + * @param namespaceUri The namespace URI. + * @param name The property name. + * @return The new property object. + */ + public Property getProperty(String namespaceUri, String name) { + return model.createProperty(namespaceUri, name); + } + + /** + * Constructs a <code>Property</code> in the Juno Bean namespace in this mode. + * + * @param name The property name. + * @return The new property object. + */ + public Property getProperty(String name) { + return model.createProperty(junoBpNs.getUri(), name); + } + + /** + * Returns the format for serializing collections. + * + * @return The format for serializing collections. + */ + protected RdfCollectionFormat getCollectionFormat() { + return collectionFormat; + } + + /** + * Returns the {@link RdfProperties#RDF_looseCollection} property value. + * + * @return The {@link RdfProperties#RDF_looseCollection} property value. + */ + protected boolean isLooseCollection() { + return looseCollection; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserProperties.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserProperties.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserProperties.class new file mode 100755 index 0000000..c475e4b Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserProperties.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserProperties.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserProperties.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserProperties.java new file mode 100755 index 0000000..b6ea048 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfParserProperties.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import com.ibm.juno.core.*; +import com.ibm.juno.core.parser.*; + +/** + * Configurable properties on the {@link RdfParser} class. + * <p> + * Use the {@link RdfParser#setProperty(String, Object)} method to set property values. + * <p> + * In addition to these properties, the following properties are also applicable for {@link RdfParser}. + * <ul> + * <li>{@link RdfProperties} + * <li>{@link ParserProperties} + * <li>{@link BeanContextProperties} + * </ul> + * + * @author James Bognar ([email protected]) + */ +public final class RdfParserProperties extends RdfProperties implements Cloneable { + + + /** + * Trim whitespace from text elements ({@link Boolean}, default=<jk>false</jk>). + * <p> + * If <jk>true</jk>, whitespace in text elements will be automatically trimmed. + */ + public static final String RDF_trimWhitespace = "RdfParser.trimWhitespace"; + + boolean trimWhitespace = false; + + /** + * Sets the specified property value. + * @param property The property name. + * @param value The property value. + * @return <jk>true</jk> if property name was valid and property was set. + */ + @Override /* RdfProperties */ + public boolean setProperty(String property, Object value) { + if (property.equals(RDF_trimWhitespace)) + trimWhitespace = BeanContext.DEFAULT.convertToType(value, boolean.class); + else + return super.setProperty(property, value); + return true; + } + + + //-------------------------------------------------------------------------------- + // Overridden methods + //-------------------------------------------------------------------------------- + + @Override /* Cloneable */ + public RdfParserProperties clone() { + try { + return (RdfParserProperties)super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); // Shouldn't happen. + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties$1.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties$1.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties$1.class new file mode 100755 index 0000000..b05e79d Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties$1.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties.class new file mode 100755 index 0000000..804022f Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties.java new file mode 100755 index 0000000..8994948 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfProperties.java @@ -0,0 +1,415 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import java.util.*; + +import com.ibm.juno.core.annotation.*; +import com.ibm.juno.core.jena.annotation.*; +import com.ibm.juno.core.xml.*; +import com.ibm.juno.core.xml.annotation.*; + +/** + * Configurable properties common to both the {@link RdfSerializer} and {@link RdfParser} classes. + * + * @author James Bognar ([email protected]) + */ +@SuppressWarnings("serial") +public class RdfProperties { + + /** + * Maps RDF writer names to property prefixes that apply to them. + */ + final static Map<String,String> LANG_PROP_MAP = new HashMap<String,String>() {{ + put("RDF/XML","rdfXml."); + put("RDF/XML-ABBREV","rdfXml."); + put("N3","n3."); + put("N3-PP","n3."); + put("N3-PLAIN","n3."); + put("N3-TRIPLES","n3."); + put("TURTLE","n3."); + put("N-TRIPLE","ntriple."); + }}; + + /** + * The RDF language to serialize to ({@link String}, default=<js>"RDF/XML-ABBREV"</js>). + * <p> + * Can be any of the following: + * <ul> + * <li><js>"RDF/XML"</js> + * <li><js>"RDF/XML-ABBREV"</js> + * <li><js>"N-TRIPLE"</js> + * <li><js>"N3"</js> - General name for the N3 writer. + * Will make a decision on exactly which writer to use (pretty writer, plain writer or simple writer) when created. + * Default is the pretty writer but can be overridden with system property <code>com.hp.hpl.jena.n3.N3JenaWriter.writer</code>. + * <li><js>"N3-PP"</js> - Name of the N3 pretty writer. + * The pretty writer uses a frame-like layout, with prefixing, clustering like properties and embedding one-referenced bNodes. + * <li><js>"N3-PLAIN"</js> - Name of the N3 plain writer. + * The plain writer writes records by subject. + * <li><js>"N3-TRIPLES"</js> - Name of the N3 triples writer. + * This writer writes one line per statement, like N-Triples, but does N3-style prefixing. + * <li><js>"TURTLE"</js> - Turtle writer. + * http://www.dajobe.org/2004/01/turtle/ + * </ul> + */ + public static final String RDF_language = "Rdf.language"; + + /** + * The XML namespace for Juno properties ({@link Namespace}, default=<js>{j:'http://www.ibm.com/juno/'}</js>). + */ + public static final String RDF_junoNs = "Rdf.junoNs"; + + /** + * The default XML namespace for bean properties ({@link Namespace}, default=<js>{j:'http://www.ibm.com/junobp/'}</js>). + */ + public static final String RDF_junoBpNs = "Rdf.junoBpNs"; + + /** + * Reuse XML namespaces when RDF namespaces not specified ({@link Boolean}, default=<jk>true</jk>). + * <p> + * When specified, namespaces defined using {@link XmlNs} and {@link Xml} will be inherited by the RDF serializers. + * Otherwise, namespaces will be defined using {@link RdfNs} and {@link Rdf}. + */ + public static final String RDF_useXmlNamespaces = "Rdf.useXmlNamespaces"; + + /** + * RDF/XML property: <code>iri_rules</code> ({@link String}, default=<js>"lax"</js>). + * <p> + * Set the engine for checking and resolving. + * <p> + * Possible values: + * <ul> + * <li><js>"lax"</js> - The rules for RDF URI references only, which does permit spaces although the use of spaces is not good practice. + * <li><js>"strict"</js> - Sets the IRI engine with rules for valid IRIs, XLink and RDF; it does not permit spaces in IRIs. + * <li><js>"iri"</js> - Sets the IRI engine to IRI (<a href='http://www.ietf.org/rfc/rfc3986.txt'>RFC 3986</a>, <a href='http://www.ietf.org/rfc/rfc3987.txt'>RFC 3987</a>). + * </ul> + */ + public static final String RDF_arp_iriRules = "Rdf.jena.rdfXml.iri-rules"; + + /** + * RDF/XML ARP property: <code>error-mode</code> ({@link String}, default=<js>"lax"</js>). + * <p> + * This allows a coarse-grained approach to control of error handling. + * <p> + * Possible values: + * <ul> + * <li><js>"default"</js> + * <li><js>"lax"</js> + * <li><js>"strict"</js> + * <li><js>"strict-ignore"</js> + * <li><js>"strict-warning"</js> + * <li><js>"strict-error"</js> + * <li><js>"strict-fatal"</js> + * </ul> + * <p> + * See also: + * <ul> + * <li><a href='http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/arp/ARPOptions.html#setDefaultErrorMode()'>ARPOptions.setDefaultErrorMode()</a> + * <li><a href='http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/arp/ARPOptions.html#setLaxErrorMode()'>ARPOptions.setLaxErrorMode()</a> + * <li><a href='http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/arp/ARPOptions.html#setStrictErrorMode()'>ARPOptions.setStrictErrorMode()</a> + * <li><a href='http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/arp/ARPOptions.html#setStrictErrorMode(int)'>ARPOptions.setStrictErrorMode(int)</a> + * </ul> + */ + public static final String RDF_arp_errorMode = "Rdf.jena.rdfXml.error-mode"; + + /** + * RDF/XML ARP property: <code>embedding</code> ({@link Boolean}, default=<jk>false</jk>). + * <p> + * Sets ARP to look for RDF embedded within an enclosing XML document. + * <p> + * See also: + * <ul> + * <li><a href='http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/arp/ARPOptions.html#setEmbedding(boolean)'>ARPOptions.setEmbedding(boolean)</a> + * </ul> + */ + public static final String RDF_arp_embedding = "Rdf.jena.rdfXml.embedding"; + + /** + * RDF/XML ARP property: <code>ERR_xxx</code> ({@link String}). + * <p> + * Provides fine-grained control over detected error conditions. + * <p> + * Possible values: + * <ul> + * <li><js>"EM_IGNORE"</js> + * <li><js>"EM_WARNING"</js> + * <li><js>"EM_ERROR"</js> + * <li><js>"EM_FATAL"</js> + * </ul> + * <p> + * See also: + * <ul> + * <li><a href='http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/arp/ARPErrorNumbers.html'>ARPErrorNumbers</a> + * <li><a href='http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/arp/ARPOptions.html#setErrorMode(int,%20int)'>ARPOptions.setErrorMode(int, int)</a> + * </ul> + */ + public static final String RDF_arp_err_ = "Rdf.jena.rdfXml.ERR_"; + + /** + * RDF/XML ARP property: <code>WARN_xxx</code> ({@link String}). + * <p> + * See {@link #RDF_arp_err_} for details. + */ + public static final String RDF_arp_warn_ = "Rdf.jena.rdfXml.WARN_"; + + /** + * RDF/XML ARP property: <code>IGN_xxx</code> ({@link String}). + * <p> + * See {@link #RDF_arp_err_} for details. + */ + public static final String RDF_arp_ign_ = "Rdf.jena.rdfXml.IGN_"; + + /** + * RDF/XML property: <code>xmlbase</code> ({@link String}, default=<jk>null</jk>). + * <p> + * The value to be included for an <xa>xml:base</xa> attribute on the root element in the file. + */ + public static final String RDF_rdfxml_xmlBase = "Rdf.jena.rdfXml.xmlbase"; + + /** + * RDF/XML property: <code>longId</code> ({@link Boolean}, default=<jk>false</jk>). + * <p> + * Whether to use long ID's for anon resources. + * Short ID's are easier to read, but can run out of memory on very large models. + */ + public static final String RDF_rdfxml_longId = "Rdf.jena.rdfXml.longId"; + + /** + * RDF/XML property: <code>allowBadURIs</code> ({@link Boolean}, default=<jk>false</jk>). + * <p> + * URIs in the graph are, by default, checked prior to serialization. + */ + public static final String RDF_rdfxml_allowBadUris = "Rdf.jena.rdfXml.allowBadURIs"; + + /** + * RDF/XML property: <code>relativeURIs</code> ({@link String}). + * <p> + * What sort of relative URIs should be used. + * <p> + * A comma separate list of options: + * <ul> + * <li><js>"same-document"</js> - Same-document references (e.g. <js>""</js> or <js>"#foo"</js>) + * <li><js>"network"</js> - Network paths (e.g. <js>"//example.org/foo"</js> omitting the URI scheme) + * <li><js>"absolute"</js> - Absolute paths (e.g. <js>"/foo"</js> omitting the scheme and authority) + * <li><js>"relative"</js> - Relative path not begining in <js>"../"</js> + * <li><js>"parent"</js> - Relative path begining in <js>"../"</js> + * <li><js>"grandparent"</js> - Relative path begining in <js>"../../"</js> + * </ul> + * <p> + * The default value is <js>"same-document, absolute, relative, parent"</js>. + * To switch off relative URIs use the value <js>""</js>. + * Relative URIs of any of these types are output where possible if and only if the option has been specified. + */ + public static final String RDF_rdfxml_relativeUris = "Rdf.jena.rdfXml.relativeURIs"; + + /** + * RDF/XML property: <code>showXmlDeclaration</code> ({@link String}, default=<js>"default"</js>). + * <p> + * Possible values: + * <ul> + * <li><js>"true"</js> - Add XML Declaration to the output. + * <li><js>"false"</js> - Don't add XML Declaration to the output. + * <li><js>"default"</js> - Only add an XML Declaration when asked to write to an <code>OutputStreamWriter</code> that uses some encoding other than <code>UTF-8</code> or <code>UTF-16</code>. + * In this case the encoding is shown in the XML declaration. + * </ul> + */ + public static final String RDF_rdfxml_showXmlDeclaration = "Rdf.jena.rdfXml.showXmlDeclaration"; + + /** + * RDF/XML property: <code>showDoctypeDeclaration</code> ({@link Boolean}, default=<jk>true</jk>). + * <p> + * If true, an XML Doctype declaration is included in the output. + * This declaration includes a <code>!ENTITY</code> declaration for each prefix mapping in the model, and any attribute value that starts with the URI of that mapping is written as starting with the corresponding entity invocation. + */ + public static final String RDF_rdfxml_showDoctypeDeclaration = "Rdf.jena.rdfXml.showDoctypeDeclaration"; + + /** + * RDF/XML property: <code>tab</code> ({@link Integer}, default=<code>2</code>). + * <p> + * The number of spaces with which to indent XML child elements. + */ + public static final String RDF_rdfxml_tab = "Rdf.jena.rdfXml.tab"; + + /** + * RDF/XML property: <code>attributeQuoteChar</code> ({@link Character}, default=<js>'"'</js>). + * <p> + * The XML attribute quote character. + */ + public static final String RDF_rdfxml_attributeQuoteChar = "Rdf.jena.rdfXml.attributeQuoteChar"; + + /** + * RDF/XML property: <code>blockRules</code> ({@link String}, default=<js>""</js>). + * <p> + * A list of <code>Resource</code> or a <code>String</code> being a comma separated list of fragment IDs from <a href='http://www.w3.org/TR/rdf-syntax-grammar'>RDF Syntax Grammar</a> indicating grammar rules that will not be used. + */ + public static final String RDF_rdfxml_blockRules = "Rdf.jena.rdfXml.blockRules"; + + /** + * N3/Turtle property: <code>minGap</code> ({@link Integer}, default=<code>1</code>). + * <p> + * Minimum gap between items on a line. + */ + public static final String RDF_n3_minGap = "Rdf.jena.n3.minGap"; + + /** + * N3/Turtle property: <code>objectLists</code> ({@link Boolean}, default=<jk>true</jk>). + * <p> + * Print object lists as comma separated lists. + */ + public static final String RDF_n3_objectLists = "Rdf.jena.n3.objectLists"; + + /** + * N3/Turtle property: <code>subjectColumn</code> ({@link Integer}, default=indentProperty). + * <p> + * If the subject is shorter than this value, the first property may go on the same line. + */ + public static final String RDF_n3_subjectColumn = "Rdf.jena.n3.subjectColumn"; + + /** + * N3/Turtle property: <code>propertyColumn</code> ({@link Integer}, default=<code>8</code>). + * <p> + * Width of the property column. + */ + public static final String RDF_n3_propertyColumn = "Rdf.jena.n3.propertyColumn"; + + /** + * N3/Turtle property: <code>indentProperty</code> ({@link Integer}, default=<code>6</code>). + * <p> + * Width to indent properties. + */ + public static final String RDF_n3_indentProperty = "Rdf.jena.n3.indentProperty"; + + /** + * N3/Turtle property: <code>widePropertyLen</code> ({@link Integer}, default=<code>20</code>). + * <p> + * Width of the property column. + * Must be longer than <code>propertyColumn</code>. + */ + public static final String RDF_n3_widePropertyLen = "Rdf.jena.n3.widePropertyLen"; + + /** + * N3/Turtle property: <code>abbrevBaseURI</code> ({@link Boolean}, default=<jk>true</jk>). + * <p> + * Control whether to use abbreviations <code><></code> or <code><#></code>. + */ + public static final String RDF_n3_abbrevBaseUri = "Rdf.jena.n3.abbrevBaseURI"; + + /** + * N3/Turtle property: <code>usePropertySymbols</code> ({@link Boolean}, default=<jk>true</jk>). + * <p> + * Control whether to use <code>a</code>, <code>=</code> and <code>=></code> in output + */ + public static final String RDF_n3_usePropertySymbols = "Rdf.jena.n3.usePropertySymbols"; + + /** + * N3/Turtle property: <code>useTripleQuotedStrings</code> ({@link Boolean}, default=<jk>true</jk>). + * <p> + * Allow the use of <code>"""</code> to delimit long strings. + */ + public static final String RDF_n3_useTripleQuotedStrings = "Rdf.jena.n3.useTripleQuotedStrings"; + + /** + * N3/Turtle property: <code>useDoubles</code> ({@link Boolean}, default=<jk>true</jk>). + * <p> + * Allow the use doubles as <code>123.456</code>. + */ + public static final String RDF_n3_useDoubles = "Rdf.jena.n3.useDoubles"; + + /** + * The RDF format for representing collections and arrays. ({@link String}, default=<js>"DEFAULT"</js>). + * <p> + * Possible values: + * <ul> + * <li><js>"DEFAULT"</js> - Default format. The default is an RDF Sequence container. + * <li><js>"SEQ"</js> - RDF Sequence container. + * <li><js>"BAG"</js> - RDF Bag container. + * <li><js>"LIST"</js> - RDF List container. + * <li><js>"MULTI_VALUED"</js> - Multi-valued properties. + * </ul> + * </p> + * <p> + * Important Note: If you use <js>"BAG"</js> or <js>"MULTI_VALUED"</js>, the order of the elements + * in the collection will get lost. + */ + public static final String RDF_collectionFormat = "Rdf.collectionFormat"; + RdfCollectionFormat collectionFormat = RdfCollectionFormat.DEFAULT; + + /** + * Specifies that collections should be serialized and parsed as loose collections. ({@link Boolean}, default=<jk>false</jk>). + * <p> + * When specified, collections of resources are handled as loose collections of resources in RDF instead of + * resources that are children of an RDF collection (e.g. Sequence, Bag). + * <p> + * Note that this setting is specialized for RDF syntax, and is incompatible with the concept of + * losslessly representing POJO models, since the tree structure of these POJO models are lost + * when serialized as loose collections. + * <p> + * This setting is typically only useful if the beans being parsed into do not have a bean property + * annotated with {@link BeanProperty#beanUri @BeanProperty(beanUri=true)}. + * + * <dl> + * <dt>Example:</dt> + * <dd> + * <p class='bcode'> + * WriterSerializer s = <jk>new</jk> RdfSerializer.XmlAbbrev().setProperty(<jsf>RDF_looseCollection</jsf>, <jk>true</jk>); + * ReaderParser p = <jk>new</jk> RdfParser.Xml().setProperty(<jsf>RDF_looseCollection</jsf>, <jk>true</jk>); + * + * List<MyBean> l = createListOfMyBeans(); + * + * <jc>// Serialize to RDF/XML as loose resources</jc> + * String rdfXml = s.serialize(l); + * + * <jc>// Parse back into a Java collection</jc> + * l = p.parseCollection(rdfXml, LinkedList.<jk>class</jk>, MyBean.<jk>class</jk>); + * + * MyBean[] b = createArrayOfMyBeans(); + * + * <jc>// Serialize to RDF/XML as loose resources</jc> + * String rdfXml = s.serialize(b); + * + * <jc>// Parse back into a bean array</jc> + * b = p.parse(rdfXml, MyBean[].<jk>class</jk>); + * </p> + * </dd> + * </dl> + */ + public static final String RDF_looseCollection = "Rdf.looseCollection"; + + String rdfLanguage = "RDF/XML-ABBREV"; + Namespace junoNs = NamespaceFactory.get("j", "http://www.ibm.com/juno/"); + Namespace junoBpNs = NamespaceFactory.get("jp", "http://www.ibm.com/junobp/"); + boolean useXmlNamespaces = true, looseCollection = false; + Map<String,Object> jenaSettings = new HashMap<String,Object>(); + + /** + * Sets the specified property value. + * @param property The property name. + * @param value The property value. + * @return <jk>true</jk> if property name was valid and property was set. + */ + public boolean setProperty(String property, Object value) { + if (property.equals(RDF_language)) + rdfLanguage = value.toString(); + else if (property.equals(RDF_junoNs)) + junoNs = NamespaceFactory.parseNamespace(value); + else if (property.equals(RDF_junoBpNs)) + junoBpNs = NamespaceFactory.parseNamespace(value); + else if (property.startsWith("Rdf.jena.")) + jenaSettings.put(property.substring(9), value); + else if (property.equals(RDF_collectionFormat)) + collectionFormat = RdfCollectionFormat.valueOf(value.toString()); + else if (property.equals(RDF_useXmlNamespaces)) + useXmlNamespaces = Boolean.valueOf(value.toString()); + else if (property.equals(RDF_looseCollection)) + looseCollection = Boolean.valueOf(value.toString()); + else + return false; + return true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$1.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$1.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$1.class new file mode 100755 index 0000000..4ac3006 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$1.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$N3.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$N3.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$N3.class new file mode 100755 index 0000000..e90348b Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$N3.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$NTriple.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$NTriple.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$NTriple.class new file mode 100755 index 0000000..0314bf4 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$NTriple.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$Turtle.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$Turtle.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$Turtle.class new file mode 100755 index 0000000..8bde941 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$Turtle.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$Xml.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$Xml.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$Xml.class new file mode 100755 index 0000000..79dc12d Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$Xml.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$XmlAbbrev.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$XmlAbbrev.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$XmlAbbrev.class new file mode 100755 index 0000000..8c176f9 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer$XmlAbbrev.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer.class new file mode 100755 index 0000000..2af16f2 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer.java new file mode 100755 index 0000000..4acb7cd --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializer.java @@ -0,0 +1,466 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import static com.ibm.juno.core.jena.Constants.*; +import static com.ibm.juno.core.jena.RdfProperties.*; +import static com.ibm.juno.core.xml.XmlUtils.*; + +import java.io.*; +import java.lang.reflect.*; +import java.util.*; + +import com.hp.hpl.jena.rdf.model.*; +import com.ibm.juno.core.*; +import com.ibm.juno.core.annotation.*; +import com.ibm.juno.core.filter.*; +import com.ibm.juno.core.serializer.*; +import com.ibm.juno.core.utils.*; +import com.ibm.juno.core.xml.*; + +/** + * Serializes POJOs to RDF. + * + * + * <h6 class='topic'>Configurable properties</h6> + * <p> + * Refer to <a class='doclink' href='package-summary.html#SerializerConfigurableProperties'>Configurable Properties</a> + * for the entire list of configurable properties. + * + * + * <h6 class='topic'>Behavior-specific subclasses</h6> + * <p> + * The following direct subclasses are provided for language-specific serializers: + * <ul> + * <li>{@link RdfSerializer.Xml} - RDF/XML. + * <li>{@link RdfSerializer.XmlAbbrev} - RDF/XML-ABBREV. + * <li>{@link RdfSerializer.NTriple} - N-TRIPLE. + * <li>{@link RdfSerializer.Turtle} - TURTLE. + * <li>{@link RdfSerializer.N3} - N3. + * </ul> + * + * + * <h6 class='topic'>Additional Information</h6> + * <p> + * See <a class='doclink' href='package-summary.html#TOC'>RDF Overview</a> for an overview of RDF support in Juno. + * + * + * @author James Bognar ([email protected]) + */ +@SuppressWarnings({ "rawtypes", "unchecked" }) +@Produces(value="text/xml+rdf+abbrev", contentType="text/xml+rdf") +public class RdfSerializer extends WriterSerializer { + + /** Default RDF/XML serializer, all default settings.*/ + public static final RdfSerializer DEFAULT_XML = new RdfSerializer.Xml().lock(); + + /** Default Abbreviated RDF/XML serializer, all default settings.*/ + public static final RdfSerializer DEFAULT_XMLABBREV = new RdfSerializer.XmlAbbrev().lock(); + + /** Default Turtle serializer, all default settings.*/ + public static final RdfSerializer DEFAULT_TURTLE = new RdfSerializer.Turtle().lock(); + + /** Default N-Triple serializer, all default settings.*/ + public static final RdfSerializer DEFAULT_NTRIPLE = new RdfSerializer.NTriple().lock(); + + /** Default N3 serializer, all default settings.*/ + public static final RdfSerializer DEFAULT_N3 = new RdfSerializer.N3().lock(); + + + /** Produces RDF/XML output */ + @Produces("text/xml+rdf") + public static class Xml extends RdfSerializer { + /** Constructor */ + public Xml() { + setProperty(RDF_language, LANG_RDF_XML); + } + } + + /** Produces Abbreviated RDF/XML output */ + @Produces(value="text/xml+rdf+abbrev", contentType="text/xml+rdf") + public static class XmlAbbrev extends RdfSerializer { + /** Constructor */ + public XmlAbbrev() { + setProperty(RDF_language, LANG_RDF_XML_ABBREV); + } + } + + /** Produces N-Triple output */ + @Produces("text/n-triple") + public static class NTriple extends RdfSerializer { + /** Constructor */ + public NTriple() { + setProperty(RDF_language, LANG_NTRIPLE); + } + } + + /** Produces Turtle output */ + @Produces("text/turtle") + public static class Turtle extends RdfSerializer { + /** Constructor */ + public Turtle() { + setProperty(RDF_language, LANG_TURTLE); + } + } + + /** Produces N3 output */ + @Produces("text/n3") + public static class N3 extends RdfSerializer { + /** Constructor */ + public N3() { + setProperty(RDF_language, LANG_N3); + } + } + + + /** Jena serializer properties currently set on this serializer. */ + protected transient RdfSerializerProperties rsp = new RdfSerializerProperties(); + + /** Xml serializer properties currently set on this serializer. */ + protected transient XmlSerializerProperties xsp = new XmlSerializerProperties(); + + + @Override /* Serializer */ + protected void doSerialize(Object o, Writer out, SerializerContext ctx) throws IOException, SerializeException { + + RdfSerializerContext rctx = (RdfSerializerContext)ctx; + + Model model = rctx.model; + Resource r = null; + + ClassMeta<?> cm = rctx.getBeanContext().getClassMetaForObject(o); + if (rctx.isLooseCollection() && cm != null && (cm.isCollection() || cm.isArray())) { + Collection c = sort(ctx, cm.isCollection() ? (Collection)o : toList(cm.getInnerClass(), o)); + for (Object o2 : c) + serializeAnything(o2, false, object(), rctx, "root", null, null); + } else { + RDFNode n = serializeAnything(o, false, object(), rctx, "root", null, null); + if (n.isLiteral()) { + r = model.createResource(); + r.addProperty(rctx.pValue, n); + } else { + r = n.asResource(); + } + + if (rctx.addRootProperty) + r.addProperty(rctx.pRoot, "true"); + } + + rctx.writer.write(model, out, "http://unknown/"); + } + + private RDFNode serializeAnything(Object o, boolean isURI, ClassMeta<?> eType, RdfSerializerContext ctx, String attrName, BeanPropertyMeta bpm, Resource parentResource) throws SerializeException { + Model m = ctx.model; + BeanContext bc = ctx.getBeanContext(); + + ClassMeta<?> aType = null; // The actual type + ClassMeta<?> wType = null; // The wrapped type + ClassMeta<?> gType = object(); // The generic type + + aType = ctx.push(attrName, o, eType); + + if (eType == null) + eType = object(); + + // Handle recursion + if (aType == null) { + o = null; + aType = object(); + } + + if (o != null) { + + if (aType.isDelegate()) { + wType = aType; + aType = ((Delegate)o).getClassMeta(); + } + + gType = aType.getFilteredClassMeta(); + + // Filter if necessary + PojoFilter filter = aType.getPojoFilter(); + if (filter != null) { + o = filter.filter(o); + + // If the filter's getFilteredClass() method returns Object, we need to figure out + // the actual type now. + if (gType.isObject()) + gType = bc.getClassMetaForObject(o); + } + } else { + gType = eType.getFilteredClassMeta(); + } + + RDFNode n = null; + + if (o == null || gType.isChar() && ((Character)o).charValue() == 0) { + if (bpm != null) { + if (! ctx.isTrimNulls()) { + n = m.createResource(RDF_NIL); + } + } else { + n = m.createResource(RDF_NIL); + } + + } else if (gType.isUri() || isURI) { + n = m.createResource(getUri(o, null, ctx)); + + } else if (gType.isCharSequence() || gType.isChar()) { + n = m.createLiteral(encodeTextInvalidChars(o)); + + } else if (gType.isNumber() || gType.isBoolean()) { + if (! ctx.addLiteralTypes) + n = m.createLiteral(o.toString()); + else + n = m.createTypedLiteral(o); + + } else if (gType.isMap() || (wType != null && wType.isMap())) { + if (o instanceof BeanMap) { + BeanMap bm = (BeanMap)o; + String uri = getUri(bm.getBeanUri(), null, ctx); + n = m.createResource(uri); + serializeBeanMap(bm, (Resource)n, ctx); + } else { + Map m2 = (Map)o; + n = m.createResource(); + serializeMap(m2, (Resource)n, gType, ctx); + } + + } else if (gType.hasToObjectMapMethod()) { + Map m2 = gType.toObjectMap(o); + n = m.createResource(); + serializeMap(m2, (Resource)n, gType, ctx); + + } else if (gType.isBean()) { + BeanMap bm = bc.forBean(o); + String uri = getUri(bm.getBeanUri(), null, ctx); + n = m.createResource(uri); + serializeBeanMap(bm, (Resource)n, ctx); + + } else if (gType.isCollection() || gType.isArray() || (wType != null && wType.isCollection())) { + Collection c = sort(ctx, gType.isCollection() ? (Collection)o : toList(gType.getInnerClass(), o)); + RdfCollectionFormat f = ctx.getCollectionFormat(); + if (gType.getRdfMeta().getCollectionFormat() != RdfCollectionFormat.DEFAULT) + f = gType.getRdfMeta().getCollectionFormat(); + if (bpm != null && bpm.getRdfMeta().getCollectionFormat() != RdfCollectionFormat.DEFAULT) + f = bpm.getRdfMeta().getCollectionFormat(); + switch (f) { + case BAG: n = serializeToContainer(c, gType, m.createBag(), ctx); break; + case LIST: n = serializeToList(c, gType, ctx); break; + case MULTI_VALUED: serializeToMultiProperties(c, gType, bpm, ctx, attrName, parentResource); break; + default: n = serializeToContainer(c, gType, m.createSeq(), ctx); + } + } else { + n = m.createLiteral(encodeTextInvalidChars(o)); + } + + if (ctx.isAddClassAttrs() && n != null && n.isResource()) { + if (o != null && ! eType.equals(aType)) + n.asResource().addProperty(ctx.pClass, aType.toString()); + else if (o == null) + n.asResource().addProperty(ctx.pClass, eType.toString()); + } + + ctx.pop(); + + return n; + } + + private String getUri(Object uri, Object uri2, RdfSerializerContext ctx) { + String s = null; + if (uri != null) + s = uri.toString(); + if ((s == null || s.isEmpty()) && uri2 != null) + s = uri2.toString(); + if (s == null) + return null; + if (s.indexOf("://") == -1) { + String aUri = ctx.getAbsolutePathUriBase(); + String rUri = ctx.getRelativeUriBase(); + if (StringUtils.startsWith(s, '/')) { + if (aUri != null) + return aUri + s; + } else { + if (rUri != null) { + if (rUri.equals("/")) + return '/' + s; + return rUri + '/' + s; + } + } + } + return s; + } + + private void serializeMap(Map m, Resource r, ClassMeta<?> type, RdfSerializerContext ctx) throws SerializeException { + + m = sort(ctx, m); + + ClassMeta<?> keyType = type.getKeyType(), valueType = type.getValueType(); + + ArrayList<Map.Entry<Object,Object>> l = new ArrayList<Map.Entry<Object,Object>>(m.entrySet()); + Collections.reverse(l); + for (Map.Entry<Object,Object> me : l) { + Object value = me.getValue(); + + Object key = generalize(ctx, me.getKey(), keyType); + + Namespace ns = ctx.junoBpNs; + Model model = ctx.model; + Property p = model.createProperty(ns.getUri(), encodeElementName(key)); + RDFNode n = serializeAnything(value, false, valueType, ctx, key == null ? null : key.toString(), null, r); + if (n != null) + r.addProperty(p, n); + } + } + + private void serializeBeanMap(BeanMap m, Resource r, RdfSerializerContext ctx) throws SerializeException { + ArrayList<BeanMapEntry> l = new ArrayList<BeanMapEntry>(m.entrySet()); + Collections.reverse(l); + for (BeanMapEntry bme : l) { + BeanPropertyMeta pMeta = bme.getMeta(); + ClassMeta<?> cm = pMeta.getClassMeta(); + + if (pMeta.isBeanUri()) + continue; + + String key = bme.getKey(); + Object value = null; + try { + value = bme.getValue(); + } catch (StackOverflowError e) { + throw e; + } catch (Throwable t) { + ctx.addBeanGetterWarning(pMeta, t); + } + + if (canIgnoreValue(ctx, cm, key, value)) + continue; + + BeanPropertyMeta bpm = bme.getMeta(); + Namespace ns = bpm.getRdfMeta().getNamespace(); + if (ns == null && ctx.isUseXmlNamespaces()) + ns = bpm.getXmlMeta().getNamespace(); + if (ns == null) + ns = ctx.junoBpNs; + else if (ctx.isAutoDetectNamespaces()) + ctx.addModelPrefix(ns); + + Property p = ctx.model.createProperty(ns.getUri(), encodeElementName(key)); + RDFNode n = serializeAnything(value, pMeta.isUri(), cm, ctx, key, pMeta, r); + if (n != null) + r.addProperty(p, n); + } + } + + + private Container serializeToContainer(Collection c, ClassMeta<?> type, Container list, RdfSerializerContext ctx) throws SerializeException { + + ClassMeta<?> elementType = type.getElementType(); + for (Object e : c) { + RDFNode n = serializeAnything(e, false, elementType, ctx, null, null, null); + list = list.add(n); + } + return list; + } + + private RDFList serializeToList(Collection c, ClassMeta<?> type, RdfSerializerContext ctx) throws SerializeException { + ClassMeta<?> elementType = type.getElementType(); + List<RDFNode> l = new ArrayList<RDFNode>(c.size()); + for (Object e : c) { + l.add(serializeAnything(e, false, elementType, ctx, null, null, null)); + } + return ctx.model.createList(l.iterator()); + } + + private void serializeToMultiProperties(Collection c, ClassMeta<?> gType, BeanPropertyMeta bpm, RdfSerializerContext ctx, String attrName, Resource parentResource) throws SerializeException { + ClassMeta<?> elementType = gType.getElementType(); + for (Object e : c) { + Namespace ns = null; + if (bpm != null) { + ns = bpm.getRdfMeta().getNamespace(); + if (ns == null && ctx.isUseXmlNamespaces()) + ns = bpm.getXmlMeta().getNamespace(); + } + if (ns == null) + ns = ctx.junoBpNs; + else if (ctx.isAutoDetectNamespaces()) + ctx.addModelPrefix(ns); + RDFNode n2 = serializeAnything(e, false, elementType, ctx, null, null, null); + Property p = ctx.model.createProperty(ns.getUri(), encodeElementName(attrName)); + parentResource.addProperty(p, n2); + } + + } + + //-------------------------------------------------------------------------------- + // Overridden methods + //-------------------------------------------------------------------------------- + + @Override /* Serializer */ + public RdfSerializerContext createContext(ObjectMap properties, Method javaMethod) { + return new RdfSerializerContext(getBeanContext(), sp, xsp, rsp, properties, javaMethod); + } + + @Override /* CoreApi */ + public RdfSerializer setProperty(String property, Object value) throws LockedException { + checkLock(); + if (! rsp.setProperty(property, value)) + if (! xsp.setProperty(property, value)) + super.setProperty(property, value); + return this; + } + + @Override /* CoreApi */ + public RdfSerializer setProperties(ObjectMap properties) throws LockedException { + for (Map.Entry<String,Object> e : properties.entrySet()) + setProperty(e.getKey(), e.getValue()); + return this; + } + + @Override /* CoreApi */ + public RdfSerializer addNotBeanClasses(Class<?>...classes) throws LockedException { + super.addNotBeanClasses(classes); + return this; + } + + @Override /* CoreApi */ + public RdfSerializer addFilters(Class<?>...classes) throws LockedException { + super.addFilters(classes); + return this; + } + + @Override /* CoreApi */ + public <T> RdfSerializer addImplClass(Class<T> interfaceClass, Class<? extends T> implClass) throws LockedException { + super.addImplClass(interfaceClass, implClass); + return this; + } + + @Override /* CoreApi */ + public RdfSerializer setClassLoader(ClassLoader classLoader) throws LockedException { + super.setClassLoader(classLoader); + return this; + } + + @Override /* Lockable */ + public RdfSerializer lock() { + super.lock(); + return this; + } + + @Override /* Lockable */ + public RdfSerializer clone() { + try { + RdfSerializer c = (RdfSerializer)super.clone(); + c.rsp = rsp.clone(); + return c; + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); // Shouldn't happen + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerContext.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerContext.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerContext.class new file mode 100755 index 0000000..6d3e9ae Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerContext.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerContext.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerContext.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerContext.java new file mode 100755 index 0000000..82d95c5 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerContext.java @@ -0,0 +1,135 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import static com.ibm.juno.core.jena.Constants.*; +import static com.ibm.juno.core.jena.RdfProperties.*; +import static com.ibm.juno.core.jena.RdfSerializerProperties.*; + +import java.lang.reflect.*; +import java.util.*; + +import com.hp.hpl.jena.rdf.model.*; +import com.ibm.juno.core.*; +import com.ibm.juno.core.serializer.*; +import com.ibm.juno.core.xml.*; + +/** + * Context object that lives for the duration of a single serialization of {@link RdfSerializer}. + * <p> + * See {@link SerializerContext} for details. + * + * @author James Bognar ([email protected]) + */ +public final class RdfSerializerContext extends XmlSerializerContext { + + final String rdfLanguage; + final Namespace junoNs, junoBpNs; + final boolean addLiteralTypes, addRootProperty, useXmlNamespaces, looseCollection; + final Property pRoot, pValue, pClass; + final Model model; + final RDFWriter writer; + final RdfCollectionFormat collectionFormat; + + /** + * Constructor. + * @param beanContext The bean context being used by the serializer. + * @param sp Default general serializer properties. + * @param xsp Default XML serializer properties. + * @param jsp Default Jena serializer properties. + * @param op Override properties. + * @param javaMethod Java method that invoked this serializer. + * When using the REST API, this is the Java method invoked by the REST call. + * Can be used to access annotations defined on the method or class. + */ + protected RdfSerializerContext(BeanContext beanContext, SerializerProperties sp, XmlSerializerProperties xsp, RdfSerializerProperties jsp, ObjectMap op, Method javaMethod) { + super(beanContext, sp, xsp, op, javaMethod); + ObjectMap jenaSettings = new ObjectMap(); + jenaSettings.put("rdfXml.tab", isUseIndentation() ? 2 : 0); + jenaSettings.put("rdfXml.attributeQuoteChar", Character.toString(getQuoteChar())); + jenaSettings.putAll(jsp.jenaSettings); + if (op == null || op.isEmpty()) { + this.rdfLanguage = jsp.rdfLanguage; + this.junoNs = jsp.junoNs; + this.junoBpNs = jsp.junoBpNs; + this.addLiteralTypes = jsp.addLiteralTypes; + this.addRootProperty = jsp.addRootProperty; + this.collectionFormat = jsp.collectionFormat; + this.looseCollection = jsp.looseCollection; + this.useXmlNamespaces = jsp.useXmlNamespaces; + } else { + this.rdfLanguage = op.getString(RDF_language, jsp.rdfLanguage); + this.junoNs = (op.containsKey(RDF_junoNs) ? NamespaceFactory.parseNamespace(op.get(RDF_junoNs)) : jsp.junoNs); + this.junoBpNs = (op.containsKey(RDF_junoBpNs) ? NamespaceFactory.parseNamespace(op.get(RDF_junoBpNs)) : jsp.junoBpNs); + this.addLiteralTypes = op.getBoolean(RDF_addLiteralTypes, jsp.addLiteralTypes); + this.addRootProperty = op.getBoolean(RDF_addRootProperty, jsp.addRootProperty); + for (Map.Entry<String,Object> e : op.entrySet()) { + String key = e.getKey(); + if (key.startsWith("Rdf.jena.")) + jenaSettings.put(key.substring(9), e.getValue()); + } + this.collectionFormat = RdfCollectionFormat.valueOf(op.getString(RDF_collectionFormat, "DEFAULT")); + this.looseCollection = op.getBoolean(RDF_looseCollection, jsp.looseCollection); + this.useXmlNamespaces = op.getBoolean(RDF_useXmlNamespaces, jsp.useXmlNamespaces); + } + this.model = ModelFactory.createDefaultModel(); + addModelPrefix(junoNs); + addModelPrefix(junoBpNs); + for (Namespace ns : this.getNamespaces()) + addModelPrefix(ns); + this.pRoot = model.createProperty(junoNs.getUri(), RDF_junoNs_ROOT); + this.pValue = model.createProperty(junoNs.getUri(), RDF_junoNs_VALUE); + this.pClass = model.createProperty(junoNs.getUri(), RDF_junoNs_CLASS); + writer = model.getWriter(rdfLanguage); + + // Only apply properties with this prefix! + String propPrefix = RdfProperties.LANG_PROP_MAP.get(rdfLanguage); + if (propPrefix == null) + throw new RuntimeException("Unknown RDF language encountered: '"+rdfLanguage+"'"); + + for (Map.Entry<String,Object> e : jenaSettings.entrySet()) + if (e.getKey().startsWith(propPrefix)) + writer.setProperty(e.getKey().substring(propPrefix.length()), e.getValue()); + } + + /** + * Adds the specified namespace as a model prefix. + * @param ns The XML namespace. + */ + public void addModelPrefix(Namespace ns) { + model.setNsPrefix(ns.getName(), ns.getUri()); + } + + /** + * Returns the format for serializing collections. + * + * @return The format for serializing collections. + */ + protected RdfCollectionFormat getCollectionFormat() { + return collectionFormat; + } + + /** + * Returns <jk>true</jk> if RDF serializer should use XML serializer namespaces. + * + * @return <jk>true</jk> if RDF serializer should use XML serializer namespaces. + */ + protected boolean isUseXmlNamespaces() { + return useXmlNamespaces; + } + + /** + * Returns the {@link RdfProperties#RDF_looseCollection} property value. + * + * @return The {@link RdfProperties#RDF_looseCollection} property value. + */ + protected boolean isLooseCollection() { + return looseCollection; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerProperties.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerProperties.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerProperties.class new file mode 100755 index 0000000..e5fb1bc Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerProperties.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerProperties.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerProperties.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerProperties.java new file mode 100755 index 0000000..fd5ffbc --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfSerializerProperties.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import com.ibm.juno.core.*; +import com.ibm.juno.core.serializer.*; + +/** + * Configurable properties on the {@link RdfSerializer} class. + * <p> + * Use the {@link RdfSerializer#setProperty(String, Object)} method to set property values. + * <p> + * In addition to these properties, the following properties are also applicable for {@link RdfSerializer}. + * <ul> + * <li>{@link RdfProperties} + * <li>{@link SerializerProperties} + * <li>{@link BeanContextProperties} + * </ul> + * + * @author James Bognar ([email protected]) + */ +public final class RdfSerializerProperties extends RdfProperties implements Cloneable { + + /** + * Add XSI data types to non-<code>String</code> literals ({@link Boolean}, default=<jk>false</jk>). + */ + public static final String RDF_addLiteralTypes = "RdfSerializer.addLiteralTypes"; + + /** + * Add RDF root identifier property to root node ({@link Boolean}, default=<jk>false</jk>). + * <p> + * When enabled an RDF property <code>http://www.ibm.com/juno/root</code> is added with a value of <js>"true"</js> + * to identify the root node in the graph. + * This helps locate the root node during parsing. + * <p> + * If disabled, the parser has to search through the model to find any resources without + * incoming predicates to identify root notes, which can introduce a considerable performance + * degradation. + */ + public static final String RDF_addRootProperty = "RdfSerializer.addRootProperty"; + + boolean addLiteralTypes = false, addRootProperty = false; + + /** + * Sets the specified property value. + * @param property The property name. + * @param value The property value. + * @return <jk>true</jk> if property name was valid and property was set. + */ + @Override /* RdfProperties */ + public boolean setProperty(String property, Object value) { + if (property.equals(RDF_addLiteralTypes)) + addLiteralTypes = BeanContext.DEFAULT.convertToType(value, boolean.class); + else if (property.equals(RDF_addRootProperty)) + addRootProperty = BeanContext.DEFAULT.convertToType(value, boolean.class); + else + return super.setProperty(property, value); + return true; + } + + + //-------------------------------------------------------------------------------- + // Overridden methods + //-------------------------------------------------------------------------------- + + @Override /* Cloneable */ + public RdfSerializerProperties clone() { + try { + return (RdfSerializerProperties)super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); // Shouldn't happen. + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfUtils.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfUtils.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfUtils.class new file mode 100755 index 0000000..c20f2c9 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfUtils.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfUtils.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfUtils.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfUtils.java new file mode 100755 index 0000000..2e34b81 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfUtils.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * Note to U.S. Government Users Restricted Rights: Use, + * duplication or disclosure restricted by GSA ADP Schedule + * Contract with IBM Corp. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import java.util.*; + +import com.ibm.juno.core.*; +import com.ibm.juno.core.jena.annotation.*; +import com.ibm.juno.core.xml.*; + +/** + * Utility classes. + */ +public class RdfUtils { + + /** + * Find the namespace given a list of <ja>@Rdf</ja> and <ja>@RdfSchema</ja> annotations. + * The annotations should be a child-to-parent ordering of annotations found on + * a class or method. + * + * @param rdfs The <code>@Rdf</code> annotations to search. + * @param schemas The list of known RDF schemas. + * @return The resolved namespace, or <jk>null</jk> if the namespace could not be resolved. + */ + public static Namespace findNamespace(List<Rdf> rdfs, List<RdfSchema> schemas) { + + for (Rdf rdf : rdfs) { + Namespace ns = findNamespace(rdf.prefix(), rdf.namespace(), rdfs, schemas); + if (ns != null) + return ns; + } + + for (RdfSchema schema : schemas) { + Namespace ns = findNamespace(schema.prefix(), schema.namespace(), null, schemas); + if (ns != null) + return ns; + } + + return null; + } + + private static Namespace findNamespace(String prefix, String ns, List<Rdf> rdfs, List<RdfSchema> schemas) { + + // If both prefix and namespace specified, use that Namespace mapping. + if (! (prefix.isEmpty() || ns.isEmpty())) + return NamespaceFactory.get(prefix, ns); + + // If only prefix specified, need to search for namespaceURI. + if (! prefix.isEmpty()) { + if (rdfs != null) + for (Rdf rdf2 : rdfs) + if (rdf2.prefix().equals(prefix) && ! rdf2.namespace().isEmpty()) + return NamespaceFactory.get(prefix, rdf2.namespace()); + for (RdfSchema schema : schemas) { + if (schema.prefix().equals(prefix) && ! schema.namespace().isEmpty()) + return NamespaceFactory.get(prefix, schema.namespace()); + for (RdfNs rdfNs : schema.rdfNs()) + if (rdfNs.prefix().equals(prefix)) + return NamespaceFactory.get(prefix, rdfNs.namespaceURI()); + } + throw new BeanRuntimeException("Found @Rdf.prefix annotation with no matching URI. prefix='"+prefix+"'"); + } + + // If only namespaceURI specified, need to search for prefix. + if (! ns.isEmpty()) { + if (rdfs != null) + for (Rdf rdf2 : rdfs) + if (rdf2.namespace().equals(ns) && ! rdf2.prefix().isEmpty()) + return NamespaceFactory.get(rdf2.prefix(), ns); + for (RdfSchema schema : schemas) { + if (schema.namespace().equals(ns) && ! schema.prefix().isEmpty()) + return NamespaceFactory.get(schema.prefix(), ns); + for (RdfNs rdfNs : schema.rdfNs()) + if (rdfNs.namespaceURI().equals(ns)) + return NamespaceFactory.get(rdfNs.prefix(), ns); + } + } + + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/annotation/Rdf.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/annotation/Rdf.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/annotation/Rdf.class new file mode 100755 index 0000000..afb35fb Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/annotation/Rdf.class differ
