001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.linking;
019:
020: import java.net.MalformedURLException;
021: import java.util.StringTokenizer;
022:
023: import org.apache.lenya.util.Query;
024:
025: /**
026: * A link to a document.
027: */
028: public class Link {
029:
030: protected static final String PAIR_DELIMITER = ",";
031: protected static final String KEY_VALUE_DELIMITER = "=";
032:
033: private String uuid;
034: private String language;
035: private String revision;
036: private String area;
037: private String pubId;
038:
039: /**
040: * Ctor.
041: */
042: public Link() {
043: }
044:
045: /**
046: * Ctor.
047: * @param linkUri The link URI.
048: * @throws MalformedURLException if the URI doesn't represent a link.
049: */
050: public Link(String linkUri) throws MalformedURLException {
051:
052: if (!linkUri.startsWith(LinkResolver.SCHEME + ":")) {
053: throw new MalformedURLException("The string [" + linkUri
054: + "] is not a valid link URI!");
055: }
056:
057: StringTokenizer schemeAndPath = new StringTokenizer(linkUri,
058: ":");
059: schemeAndPath.nextToken();
060: String path = schemeAndPath.nextToken();
061:
062: if (path.indexOf(PAIR_DELIMITER) > -1) {
063: int firstDelimiterIndex = path.indexOf(PAIR_DELIMITER);
064: this .uuid = path.substring(0, firstDelimiterIndex);
065: String pathQueryString = path
066: .substring(firstDelimiterIndex + 1);
067: Query query = new Query(pathQueryString, PAIR_DELIMITER,
068: KEY_VALUE_DELIMITER);
069: this .pubId = query.getValue("pub");
070: this .area = query.getValue("area");
071: this .language = query.getValue("lang");
072: this .revision = query.getValue("rev");
073: } else {
074: this .uuid = path;
075: }
076: }
077:
078: /**
079: * @return The area.
080: */
081: public String getArea() {
082: return area;
083: }
084:
085: /**
086: * @param area The area.
087: */
088: public void setArea(String area) {
089: this .area = area;
090: }
091:
092: /**
093: * @return The language.
094: */
095: public String getLanguage() {
096: return language;
097: }
098:
099: /**
100: * @param language The language.
101: */
102: public void setLanguage(String language) {
103: this .language = language;
104: }
105:
106: /**
107: * @return The publication ID.
108: */
109: public String getPubId() {
110: return pubId;
111: }
112:
113: /**
114: * @param pubId The publication ID.
115: */
116: public void setPubId(String pubId) {
117: this .pubId = pubId;
118: }
119:
120: /**
121: * @return The revision.
122: */
123: public String getRevision() {
124: return revision;
125: }
126:
127: /**
128: * @param revision The revision.
129: */
130: public void setRevision(String revision) {
131: this .revision = revision;
132: }
133:
134: /**
135: * @return The UUID.
136: */
137: public String getUuid() {
138: return uuid;
139: }
140:
141: /**
142: * @param uuid The UUID.
143: */
144: public void setUuid(String uuid) {
145: this .uuid = uuid;
146: }
147:
148: /**
149: * @return The link URI.
150: */
151: public String getUri() {
152: String uri = LinkResolver.SCHEME + ":";
153: if (this .uuid != null) {
154: uri = uri + this .uuid;
155: }
156: if (this .language != null) {
157: uri = uri + ",lang=" + this .language;
158: }
159: if (this .area != null) {
160: uri = uri + ",area=" + this .area;
161: }
162: if (this .pubId != null) {
163: uri = uri + ",pub=" + this .pubId;
164: }
165: if (this .revision != null) {
166: uri = uri + ",rev=" + this .revision;
167: }
168: return uri;
169: }
170:
171: public boolean equals(Object obj) {
172: if (!getClass().isInstance(obj)) {
173: return false;
174: }
175: return ((Link) obj).getUri().equals(getUri());
176: }
177:
178: public int hashCode() {
179: return getUri().hashCode();
180: }
181:
182: public String toString() {
183: return getUri();
184: }
185:
186: }
|