001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.repository;
021
022import java.net.URI;
023import java.net.URISyntaxException;
024
025import org.nuxeo.ecm.core.api.DocumentLocation;
026import org.nuxeo.ecm.core.api.DocumentRef;
027import org.nuxeo.ecm.core.api.IdRef;
028import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
029import org.nuxeo.ecm.platform.url.DocumentViewImpl;
030import org.nuxeo.ecm.platform.url.api.DocumentView;
031
032/**
033 * @author Alexandre Russel
034 */
035public class URNDocumentViewTranslator {
036
037    public URI getNuxeoUrn(String server, String id) {
038        try {
039            return new URI("urn:nuxeo:" + server + ":" + id);
040        } catch (URISyntaxException e) {
041            throw new RuntimeException(e);
042        }
043    }
044
045    public URI getUriFromDocumentView(String serverName, DocumentRef docRef) {
046        try {
047            return new URI("urn:nuxeo:" + serverName + ":" + docRef);
048        } catch (URISyntaxException e) {
049            throw new RuntimeException(e);
050        }
051    }
052
053    public DocumentView getDocumentViewFromUri(URI uri) {
054        if (!isNuxeoUrn(uri)) {
055            return null;
056        }
057        String[] tokens = uri.toString().split(":");
058        DocumentRef ref = new IdRef(tokens[3]);
059        DocumentLocation dl = new DocumentLocationImpl(tokens[2], ref);
060        DocumentView view = null;
061        if (tokens.length < 5) {
062            view = new DocumentViewImpl(dl);
063        }
064        return view;
065    }
066
067    public boolean isNuxeoUrn(URI uri) {
068        return uri.toString().startsWith("urn:nuxeo");
069    }
070}