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.ArrayList;
021: import java.util.Arrays;
022: import java.util.Collections;
023: import java.util.HashSet;
024: import java.util.List;
025: import java.util.Set;
026:
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.avalon.framework.service.ServiceSelector;
029: import org.apache.lenya.cms.publication.Document;
030: import org.apache.lenya.cms.publication.DocumentException;
031: import org.apache.lenya.cms.publication.util.DocumentSet;
032:
033: /**
034: * A set containing nodes.
035: */
036: public class NodeSet {
037:
038: private ServiceManager manager;
039:
040: /**
041: * Ctor.
042: * @param manager The service manager.
043: */
044: public NodeSet(ServiceManager manager) {
045: this .manager = manager;
046: }
047:
048: /**
049: * Ctor.
050: * @param manager The service manager.
051: * @param _nodes The initial nodes.
052: */
053: public NodeSet(ServiceManager manager, SiteNode[] _nodes) {
054: this (manager);
055: for (int i = 0; i < _nodes.length; i++) {
056: add(_nodes[i]);
057: }
058: }
059:
060: /**
061: * Ctor.
062: * @param documents The corresponding documents to derive nodes from.
063: */
064: public NodeSet(DocumentSet documents) {
065: Document[] docs = documents.getDocuments();
066: for (int i = 0; i < docs.length; i++) {
067: SiteNode node;
068: try {
069: node = docs[i].getLink().getNode();
070: } catch (DocumentException e) {
071: throw new RuntimeException(e);
072: }
073: if (!contains(node)) {
074: add(node);
075: }
076: }
077: }
078:
079: /**
080: * @param node A node.
081: * @return If the node is contained.
082: */
083: public boolean contains(SiteNode node) {
084: return getSet().contains(node);
085: }
086:
087: private Set nodes = new HashSet();
088:
089: /**
090: * Returns the list object that stores the documents.
091: * @return A list.
092: */
093: protected Set getSet() {
094: return this .nodes;
095: }
096:
097: /**
098: * Returns the documents contained in this set.
099: *
100: * @return An array of documents.
101: */
102: public SiteNode[] getNodes() {
103: return (SiteNode[]) this .nodes.toArray(new SiteNode[this .nodes
104: .size()]);
105: }
106:
107: /**
108: * Adds a node to this set.
109: * @param node The node to add.
110: */
111: public void add(SiteNode node) {
112: assert node != null;
113: assert !this .nodes.contains(node);
114: this .nodes.add(node);
115: }
116:
117: /**
118: * Checks if this set is empty.
119: * @return A boolean value.
120: */
121: public boolean isEmpty() {
122: return getSet().isEmpty();
123: }
124:
125: /**
126: * Removes a node.
127: * @param resource The node.
128: */
129: public void remove(SiteNode resource) {
130: assert resource != null;
131: assert getSet().contains(resource);
132: getSet().remove(resource);
133: }
134:
135: /**
136: * Removes all nodes.
137: */
138: public void clear() {
139: getSet().clear();
140: }
141:
142: /**
143: * @return An iterator iterating in ascending order.
144: */
145: public NodeIterator ascending() {
146: SiteNode[] nodes = getNodesAscending();
147: return new NodeIterator(nodes);
148: }
149:
150: /**
151: * @return An iterator iterating in descending order.
152: */
153: public NodeIterator descending() {
154: SiteNode[] nodes = getNodesAscending();
155: List list = Arrays.asList(nodes);
156: Collections.reverse(list);
157: return new NodeIterator(list);
158: }
159:
160: protected SiteNode[] getNodesAscending() {
161: if (isEmpty()) {
162: return new SiteNode[0];
163: }
164:
165: SiteNode[] nodes;
166: ServiceSelector selector = null;
167: SiteManager siteManager = null;
168: try {
169: selector = (ServiceSelector) manager
170: .lookup(SiteManager.ROLE + "Selector");
171: siteManager = (SiteManager) selector.select(getNodes()[0]
172: .getStructure().getPublication()
173: .getSiteManagerHint());
174: nodes = siteManager.sortAscending(getNodes());
175: } catch (Exception e) {
176: throw new RuntimeException(e);
177: } finally {
178: if (selector != null) {
179: if (siteManager != null) {
180: selector.release(siteManager);
181: }
182: manager.release(selector);
183: }
184: }
185: return nodes;
186: }
187:
188: /**
189: * @return All documents referenced by this node set.
190: */
191: public Document[] getDocuments() {
192: List documents = new ArrayList();
193: for (NodeIterator i = ascending(); i.hasNext();) {
194: SiteNode node = i.next();
195: String[] langs = node.getLanguages();
196: for (int l = 0; l < langs.length; l++) {
197: try {
198: documents.add(node.getLink(langs[l]).getDocument());
199: } catch (SiteException e) {
200: throw new RuntimeException(e);
201: }
202: }
203: }
204: return (Document[]) documents.toArray(new Document[documents
205: .size()]);
206: }
207:
208: /**
209: * Adds all nodes from a node set to this.
210: * @param set The set.
211: */
212: public void addAll(NodeSet set) {
213: this.nodes.addAll(set.getSet());
214: }
215:
216: }
|