001/*
002 * ModeShape (http://www.modeshape.org)
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 */
016package org.modeshape.web.client;
017
018import java.io.Serializable;
019
020/**
021 * Represents fully qualified node path including repository name and workspace.
022 * 
023 * @author kulikov
024 */
025public class JcrURL implements Serializable {
026    private static final long serialVersionUID = 1L;
027    private String context;
028    private String repository;
029    private String workspace;
030    private String path;
031        
032    public JcrURL() {
033    }
034    
035    /**
036     * Creates locator.
037     * 
038     * @param repository the jndi name of the repository.
039     * @param workspace the name of the workspace.
040     * @param path path to the node;
041     */
042    public JcrURL(String repository, String workspace, String path) {
043        this.repository = repository;
044        this.workspace = workspace;
045        this.path = path;
046    } 
047    
048    public String getContext() {
049        return context;
050    }
051    
052    /**
053     * Gets repository name encoded in this locator.
054     * 
055     * @return the name of repository.
056     */
057    public String getRepository() {
058        return repository;
059    }
060    
061    public void setRepository(String repository) {
062        this.repository = repository;
063    }
064    /**
065     * Gets workspace name encoded in this locator.
066     * 
067     * @return the name of workspace.
068     */
069    public String getWorkspace() {
070        return workspace;
071    }
072    
073    public void setWorkspace(String workspace) {
074        this.workspace = workspace;
075    }
076    
077    /**
078     * Gets node path encoded in this locator.
079     * 
080     * @return the path of the node
081     */
082    public String getPath() {
083        return path == null ? "/" : path;
084    }
085    
086    /**
087     * Modify node path.
088     * 
089     * @param path the new path value.
090     */
091    public void setPath(String path) {
092        this.path = path;
093    }
094    
095    /**
096     * Parses given URL and extracts repository name, workspace and path.
097     * 
098     * @param uri URL started with http prefix.
099     */
100    public void parse2(String uri) {
101        String path = uri;
102        path = path.replace("http://", "");
103        int pos = path.indexOf("/");
104        path = path.substring(pos);
105        parse(path);
106    }
107    
108    /**
109     * Parses given URL and extracts repository name, workspace and path.
110     * 
111     * @param uri URL started with prefix.
112     */
113    public void parse(String uri) {
114        if (uri.startsWith("/")) {
115            uri = uri.substring(1);
116        }
117        
118        int pos = uri.indexOf("/");
119        if (pos == -1) {
120            context = uri;
121            repository = "";
122            workspace = "";
123            path="/";
124            return;
125        }
126        
127        if (pos > 0) {
128            context = uri.substring(0, pos);
129            uri = uri.substring(pos + 1);
130        }
131        
132        //expecting tree here
133         uri = uri.substring(5);
134        
135         pos = uri.indexOf("/ws-");
136         repository = uri.substring(0, pos);
137         
138         uri = uri.substring(repository.length() + 1);
139         pos = uri.indexOf("/");
140         
141         workspace = uri.substring(3, pos);
142         path = uri.substring(workspace.length() + 3);
143    }
144    
145    @Override
146    public String toString() {
147        return "/" + context + "/tree/" + repository + "/ws-" + workspace + path;
148    }
149}