KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > URI2RepositoryMapping


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.beans.config;
14
15 import org.apache.commons.lang.StringUtils;
16
17
18 /**
19  * Describes a uri to repository mapping
20  * @author Philipp Bracher
21  * @version $Id: URI2RepositoryMapping.java 6341 2006-09-12 09:18:27Z philipp $
22  */

23 public class URI2RepositoryMapping {
24
25     /**
26      * The prefix which triggers this mapping
27      */

28     private String JavaDoc uriPrefix;
29
30     /**
31      * The repository used for this mapping
32      */

33     private String JavaDoc repository;
34
35     /**
36      * The prefix added to the uri to create a full handle
37      */

38     private String JavaDoc handlePrefix;
39
40     /**
41      * @param uriPrefix
42      * @param repository
43      * @param handlePrefix
44      */

45     public URI2RepositoryMapping(String JavaDoc uriPrefix, String JavaDoc repository, String JavaDoc handlePrefix) {
46         super();
47         this.uriPrefix = uriPrefix;
48         this.repository = repository;
49         this.handlePrefix = handlePrefix;
50     }
51
52     public URI2RepositoryMapping() {
53     }
54
55     /**
56      * True if this mapping can get applied to the specified uri
57      * @param uri
58      * @return
59      */

60     public boolean matches(String JavaDoc uri) {
61         return uri.startsWith(uriPrefix);
62     }
63
64     /**
65      * Create a node handle based on an uri
66      * @param uri
67      * @return
68      */

69     public String JavaDoc getHandle(String JavaDoc uri) {
70         String JavaDoc handle;
71         handle = StringUtils.removeStart(uri, this.uriPrefix);
72         if (StringUtils.isNotEmpty(this.handlePrefix)) {
73             StringUtils.removeStart(handle, "/");
74             handle = this.handlePrefix + "/" + handle;
75         }
76         return cleanHandle(handle);
77     }
78
79     /**
80      * Clean a handle. Remove double / and add allways a leading /
81      * @param handle
82      * @return
83      */

84     private String JavaDoc cleanHandle(String JavaDoc handle) {
85         if (!handle.startsWith("/")) {
86             handle = "/" + handle;
87         }
88         handle = StringUtils.replace(handle, "//", "/");
89         return handle;
90     }
91
92     /**
93      * Create a uri based on a handle
94      * @param handle
95      * @return
96      */

97     public String JavaDoc getURI(String JavaDoc handle) {
98         if (StringUtils.isNotEmpty(this.handlePrefix)) {
99             handle = StringUtils.removeStart(handle, this.handlePrefix);
100         }
101         if (StringUtils.isNotEmpty(this.uriPrefix)) {
102             handle = this.uriPrefix + "/" + handle;
103         }
104         return cleanHandle(handle);
105     }
106
107     public String JavaDoc getHandlePrefix() {
108         return handlePrefix;
109     }
110
111     public void setHandlePrefix(String JavaDoc handlePrefix) {
112         this.handlePrefix = handlePrefix;
113     }
114
115     public String JavaDoc getRepository() {
116         return repository;
117     }
118
119     public void setRepository(String JavaDoc repository) {
120         this.repository = repository;
121     }
122
123     public String JavaDoc getUriPrefix() {
124         return uriPrefix;
125     }
126
127     public void setUriPrefix(String JavaDoc uriPrefix) {
128         this.uriPrefix = uriPrefix;
129     }
130
131     public String JavaDoc toString() {
132         return this.uriPrefix + " --> " + repository + ":" + this.handlePrefix;
133     }
134 }
135
Popular Tags