01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.lenya.cms.linking;
19:
20: import org.apache.lenya.cms.publication.Document;
21: import org.apache.lenya.util.Assert;
22:
23: /**
24: * The target of a link.
25: */
26: public class LinkTarget {
27:
28: private Document doc;
29: private int revisionNumber = -1;
30:
31: /**
32: * Ctor.
33: * @param doc The document.
34: * @param revisionNumber The revision number.
35: */
36: protected LinkTarget(Document doc, int revisionNumber) {
37: this (doc);
38: this .revisionNumber = revisionNumber;
39: }
40:
41: /**
42: * Ctor.
43: * @param doc The document.
44: */
45: protected LinkTarget(Document doc) {
46: Assert.notNull("document", doc);
47: this .doc = doc;
48: }
49:
50: /**
51: * Ctor for non-existing targets.
52: */
53: protected LinkTarget() {
54: }
55:
56: /**
57: * @return The linked document.
58: * @throws LinkException if the target doesn't exist.
59: */
60: public Document getDocument() throws LinkException {
61: if (!exists()) {
62: throw new LinkException("The target doesn't exist!");
63: }
64: return this .doc;
65: }
66:
67: /**
68: * @return The revision number.
69: * @throws LinkException if the target doesn't exist or no revision number
70: * is specified in the link.
71: */
72: public int getRevisionNumber() throws LinkException {
73: if (!exists()) {
74: throw new LinkException("The target doesn't exist!");
75: }
76: if (this .revisionNumber == -1) {
77: throw new LinkException("No revision specified!");
78: }
79: return this .revisionNumber;
80: }
81:
82: /**
83: * @return if the revision is specified in the link.
84: */
85: public boolean isRevisionSpecified() {
86: return this .revisionNumber != -1;
87: }
88:
89: /**
90: * @return if the target exists.
91: */
92: public boolean exists() {
93: return this.doc != null;
94: }
95:
96: }
|