001/* 002 * (C) Copyright 2006-2008 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 * Alexandre Russel 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.annotations.api; 023 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027import java.net.URI; 028import java.net.URISyntaxException; 029import java.util.ArrayList; 030import java.util.List; 031 032import org.nuxeo.ecm.core.api.NuxeoException; 033import org.nuxeo.ecm.platform.relations.api.Graph; 034import org.nuxeo.ecm.platform.relations.api.Node; 035import org.nuxeo.ecm.platform.relations.api.RelationManager; 036import org.nuxeo.ecm.platform.relations.api.Resource; 037import org.nuxeo.ecm.platform.relations.api.Statement; 038import org.nuxeo.ecm.platform.relations.api.impl.ResourceImpl; 039import org.nuxeo.runtime.api.Framework; 040 041/** 042 * @author Alexandre Russel 043 */ 044public class AnnotationManager { 045 046 private static final String TRANSIENT_GRAPH_TYPE = "jena"; 047 048 public void writeAnnotation(OutputStream os, Annotation annotation) { 049 Graph graph = getTransientGraph(); 050 graph.add(annotation.getStatements()); 051 try { 052 os.write("<?xml version='1.0'?>".getBytes()); 053 } catch (IOException e) { 054 throw new NuxeoException(e); 055 } 056 graph.write(os, null, null); 057 } 058 059 public Annotation translateAnnotationFromRepo(UriResolver resolver, String baseUrl, Annotation annotation) { 060 List<Statement> results = new ArrayList<Statement>(); 061 for (Statement statement : annotation.getStatements()) { 062 Node node = statement.getSubject(); 063 if (node instanceof Resource) { 064 Resource resource = getTranslatedResource(resolver, baseUrl, node); 065 statement.setSubject(resource); 066 } 067 node = statement.getObject(); 068 if (node instanceof Resource) { 069 Resource resource = getTranslatedResource(resolver, baseUrl, node); 070 statement.setObject(resource); 071 } 072 results.add(statement); 073 } 074 return getAnnotation(results); 075 } 076 077 private static Resource getTranslatedResource(UriResolver resolver, String baseUrl, Node node) { 078 String uri = ((Resource) node).getUri(); 079 Resource resource = null; 080 try { 081 URI newUri = resolver.translateFromGraphURI(new URI(uri), baseUrl); 082 resource = new ResourceImpl(newUri.toString()); 083 } catch (URISyntaxException e) { 084 throw new NuxeoException(e); 085 } 086 return resource; 087 } 088 089 public Annotation translateAnnotationToRepo(UriResolver resolver, Annotation annotation) { 090 List<Statement> results = new ArrayList<Statement>(); 091 for (Statement statement : annotation.getStatements()) { 092 Node node = statement.getSubject(); 093 if (node instanceof Resource) { 094 String uri = ((Resource) node).getUri(); 095 URI u; 096 try { 097 u = resolver.translateToGraphURI(new URI(uri)); 098 } catch (URISyntaxException e) { 099 throw new NuxeoException(e); 100 } 101 Resource resource = new ResourceImpl(u.toString()); 102 statement.setSubject(resource); 103 } 104 node = statement.getObject(); 105 if (node instanceof Resource) { 106 String uri = ((Resource) node).getUri(); 107 URI u; 108 try { 109 u = resolver.translateToGraphURI(new URI(uri)); 110 } catch (URISyntaxException e) { 111 throw new NuxeoException(e); 112 } 113 Resource resource = new ResourceImpl(u.toString()); 114 statement.setObject(resource); 115 } 116 results.add(statement); 117 } 118 return getAnnotation(results); 119 } 120 121 public Annotation getAnnotation(List<Statement> statements) { 122 AnnotationImpl annotation = new AnnotationImpl(); 123 Graph graph = getTransientGraph(); 124 graph.add(statements); 125 annotation.setGraph(graph); 126 return annotation; 127 } 128 129 public Annotation getAnnotation(InputStream is) { 130 Graph graph = getTransientGraph(); 131 graph.read(is, null, null); 132 AnnotationImpl annotation = new AnnotationImpl(); 133 annotation.setGraph(graph); 134 return annotation; 135 } 136 137 public Annotation getAnnotation(String is) { 138 Graph graph = getTransientGraph(); 139 graph.read(is, null, null); 140 AnnotationImpl annotation = new AnnotationImpl(); 141 annotation.setGraph(graph); 142 return annotation; 143 } 144 145 private static Graph getTransientGraph() { 146 return Framework.getService(RelationManager.class).getTransientGraph(TRANSIENT_GRAPH_TYPE); 147 } 148 149}