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.site;
019:
020: import java.util.Arrays;
021:
022: import org.apache.avalon.framework.container.ContainerUtil;
023: import org.apache.avalon.framework.logger.AbstractLogEnabled;
024: import org.apache.avalon.framework.logger.Logger;
025: import org.apache.lenya.cms.publication.Publication;
026: import org.apache.lenya.util.Assert;
027:
028: /**
029: * Abstract site node implementation.
030: */
031: public abstract class AbstractSiteNode extends AbstractLogEnabled
032: implements SiteNode {
033:
034: private String path;
035: private SiteStructure structure;
036: private String uuid;
037:
038: protected AbstractSiteNode(Publication publication,
039: SiteStructure structure, String path, String uuid,
040: Logger logger) {
041:
042: ContainerUtil.enableLogging(this , logger);
043:
044: Assert.notNull("structure", structure);
045: this .structure = structure;
046:
047: Assert.notNull("path", path);
048: Assert.isTrue("path starts with /", path.startsWith("/"));
049: this .path = path;
050:
051: Assert.notNull("uuid", uuid);
052: this .uuid = uuid;
053: }
054:
055: public String getPath() {
056: return path;
057: }
058:
059: public SiteStructure getStructure() {
060: return this .structure;
061: }
062:
063: public boolean equals(Object obj) {
064: if (obj == null || !(obj instanceof SiteNode)) {
065: return false;
066: }
067: String this Key = getKey(getStructure().getPublication(),
068: getStructure().getArea(), getPath());
069: SiteNode node = (SiteNode) obj;
070: String nodeKey = getKey(node.getStructure().getPublication(),
071: node.getStructure().getArea(), node.getPath());
072: return this Key.equals(nodeKey);
073: }
074:
075: public int hashCode() {
076: return getKey(getStructure().getPublication(),
077: getStructure().getArea(), getPath()).hashCode();
078: }
079:
080: protected static String getKey(Publication pub, String area,
081: String docId) {
082: return pub.getId() + ":" + area + ":" + docId;
083: }
084:
085: public SiteNode getParent() throws SiteException {
086: String id = getPath().substring(1);
087: String[] steps = id.split("/");
088: if (steps.length == 1) {
089: throw new SiteException("The node [" + getPath()
090: + "] is a top-level node.");
091: } else {
092: int lastIndex = id.lastIndexOf("/");
093: String parentId = id.substring(0, lastIndex);
094: return getStructure().getNode("/" + parentId);
095: }
096: }
097:
098: public boolean isTopLevel() {
099: return getPath().lastIndexOf("/") == 0;
100: }
101:
102: public String getUuid() {
103: return this .uuid;
104: }
105:
106: public boolean hasLink(String language) {
107: return Arrays.asList(getLanguages()).contains(language);
108: }
109:
110: }
|