001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * <a href="mailto:[email protected]">Anahide Tchertchian</a> 018 * 019 * $Id: IORelationGraphHelper.java 25081 2007-09-18 14:57:22Z atchertchian $ 020 */ 021 022package org.nuxeo.ecm.platform.relations.io; 023 024import java.io.InputStream; 025import java.io.OutputStream; 026import java.util.List; 027import java.util.Map; 028 029import org.nuxeo.ecm.platform.relations.api.Graph; 030import org.nuxeo.ecm.platform.relations.api.Statement; 031import org.nuxeo.ecm.platform.relations.jena.JenaGraph; 032 033/** 034 * Relation graph importer/exporter. 035 * <p> 036 * relies on Jena memory graphs to perform serialization and deserialization of memory graphs. 037 * 038 * @author <a href="mailto:[email protected]">Anahide Tchertchian</a> 039 */ 040public class IORelationGraphHelper { 041 042 protected final Map<String, String> namespaces; 043 044 protected List<Statement> statements; 045 046 public IORelationGraphHelper(Map<String, String> namespaces, List<Statement> statements) { 047 this.namespaces = namespaces; 048 this.statements = statements; 049 } 050 051 public List<Statement> getStatements() { 052 return statements; 053 } 054 055 protected Graph createMemoryGraph() { 056 JenaGraph graph = new JenaGraph(); 057 graph.setNamespaces(namespaces); 058 return graph; 059 } 060 061 public void write(OutputStream out) { 062 Graph graph = createMemoryGraph(); 063 if (statements != null) { 064 graph.add(statements); 065 } 066 graph.write(out, null, null); 067 } 068 069 public void read(InputStream in) { 070 Graph graph = createMemoryGraph(); 071 if (statements != null) { 072 graph.add(statements); 073 } 074 graph.read(in, null, null); 075 // update current statements with new ones 076 statements = graph.getStatements(); 077 } 078 079 public Graph getGraph() { 080 Graph graph = createMemoryGraph(); 081 if (statements != null) { 082 graph.add(statements); 083 } 084 return graph; 085 } 086 087}