001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.kernel.api.utils; 019 020import static org.apache.jena.graph.NodeFactory.createURI; 021 022import org.apache.jena.graph.Node; 023import org.apache.jena.graph.Triple; 024 025/** 026 * Utility for remapping subjects in rdf triples. 027 * 028 * @author bbpennel 029 */ 030public class SubjectMappingUtil { 031 032 private SubjectMappingUtil() { 033 // Empty constructor for static utility class 034 } 035 036 /** 037 * Maps the subject of t from resourceUri to destinationUri to produce a new Triple. 038 * If the triple does not have the subject resourceUri, then the triple is unchanged. 039 * 040 * @param t triple to be remapped. 041 * @param resourceUri resource subject uri to be remapped. 042 * @param destinationUri subject uri for the resultant triple. 043 * @return triple with subject remapped to destinationUri or the original subject. 044 */ 045 public static Triple mapSubject(final Triple t, final String resourceUri, final String destinationUri) { 046 final Node destinationNode = createURI(destinationUri); 047 return mapSubject(t, resourceUri, destinationNode); 048 } 049 050 /** 051 * Maps the subject of t from resourceUri to destinationNode to produce a new Triple. 052 * If the triple does not have the subject resourceUri, then the triple is unchanged. 053 * 054 * @param t triple to be remapped. 055 * @param resourceUri resource subject uri to be remapped. 056 * @param destinationNode subject node for the resultant triple. 057 * @return triple with subject remapped to destinationNode or the original subject. 058 */ 059 public static Triple mapSubject(final Triple t, final String resourceUri, final Node destinationNode) { 060 final Node tripleSubj = t.getSubject(); 061 final String tripleSubjUri = tripleSubj.getURI(); 062 final Node subject; 063 if (tripleSubjUri.equals(resourceUri)) { 064 subject = destinationNode; 065 } else if (tripleSubjUri.startsWith(resourceUri)) { 066 // If the subject begins with the originating resource uri, such as a hash uri, then rebase 067 // the portions of the subject after the resource uri to the destination uri. 068 final String suffix = tripleSubjUri.substring(resourceUri.length()); 069 subject = createURI(destinationNode.getURI() + suffix); 070 } else { 071 subject = t.getSubject(); 072 } 073 return new Triple(subject, t.getPredicate(), t.getObject()); 074 } 075}