KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > SiteTreeNodeImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: SiteTreeNodeImpl.java 160543 2005-04-08 11:30:01Z andreas $ */
19
20 package org.apache.lenya.cms.publication;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.lenya.xml.DocumentHelper;
26 import org.apache.lenya.xml.NamespaceHelper;
27 import org.apache.log4j.Logger;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.NamedNodeMap JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * Concrete implementation of the <code>SiteTreeNode</code> interface.
35  *
36  * @see org.apache.lenya.cms.publication.SiteTreeNode
37  */

38 public class SiteTreeNodeImpl implements SiteTreeNode {
39     private static Logger log = Logger.getLogger(SiteTreeNodeImpl.class);
40     public static final String JavaDoc ID_ATTRIBUTE_NAME = "id";
41     public static final String JavaDoc VISIBLEINNAV_ATTRIBUTE_NAME="visibleinnav";
42     public static final String JavaDoc HREF_ATTRIBUTE_NAME = "href";
43     public static final String JavaDoc SUFFIX_ATTRIBUTE_NAME = "suffix";
44     public static final String JavaDoc LINK_ATTRIBUTE_NAME = "link";
45     public static final String JavaDoc LANGUAGE_ATTRIBUTE_NAME = "xml:lang";
46     public static final String JavaDoc NODE_NAME = "node";
47     public static final String JavaDoc LABEL_NAME = "label";
48
49     private Node JavaDoc node = null;
50     private DefaultSiteTree sitetree = null;
51
52     /**
53      * Creates a new SiteTreeNodeImpl object.
54      *
55      * @param node the node which is to be wrapped by this SiteTreeNode
56      */

57     public SiteTreeNodeImpl(Node JavaDoc node, DefaultSiteTree sitetree) {
58         this.node = node;
59         this.sitetree = sitetree;
60     }
61
62     /**
63      * @see org.apache.lenya.cms.publication.SiteTreeNode#getParentId()
64      * @deprecated use getParent().getId() instead
65      */

66     public String JavaDoc getParentId() {
67         Node JavaDoc parent = node.getParentNode();
68         String JavaDoc parentId = null;
69
70         if (parent == null) {
71             parentId = "/";
72         } else {
73             synchronized(sitetree) {
74                 NamedNodeMap JavaDoc attributes = parent.getAttributes();
75                 if (attributes == null) {
76                     parentId = "/";
77                 } else {
78                     Node JavaDoc idAttribute = attributes.getNamedItem(ID_ATTRIBUTE_NAME);
79
80                     if (idAttribute == null) {
81                         parentId = "/";
82                     } else {
83                         parentId = idAttribute.getNodeValue();
84                     }
85                     
86                 }
87             }
88         }
89         return parentId;
90     }
91
92     /**
93      * @see org.apache.lenya.cms.publication.SiteTreeNode#getId()
94      */

95     public String JavaDoc getId() {
96         String JavaDoc id = null;
97         synchronized (sitetree) {
98             if (node == node.getOwnerDocument().getDocumentElement()) {
99                 id = "";
100             } else {
101                 id = node.getAttributes().getNamedItem(ID_ATTRIBUTE_NAME).getNodeValue();
102             }
103         }
104         return id;
105     }
106
107     /**
108      * @see org.apache.lenya.cms.publication.SiteTreeNode#getAbsoluteParentId()
109      * @deprecated use getParent().getAbsoluteId() instead
110      */

111     public String JavaDoc getAbsoluteParentId() {
112         SiteTreeNode parent = this.getParent();
113         return (parent == null) ? "" : parent.getAbsoluteId();
114     }
115
116     /**
117      * @see org.apache.lenya.cms.publication.SiteTreeNode#getAbsoluteId()
118      */

119     public String JavaDoc getAbsoluteId() {
120         String JavaDoc absoluteId = "";
121         Node JavaDoc currentNode = node;
122         
123         NamedNodeMap JavaDoc attributes = null;
124         Node JavaDoc idAttribute = null;
125
126         synchronized (sitetree) {
127             while (currentNode != null) {
128                 attributes = currentNode.getAttributes();
129     
130                 if (attributes == null) {
131                     break;
132                 }
133     
134                 idAttribute = attributes.getNamedItem(ID_ATTRIBUTE_NAME);
135     
136                 if (idAttribute == null) {
137                     break;
138                 }
139     
140                 absoluteId = "/" + idAttribute.getNodeValue() + absoluteId;
141                 currentNode = currentNode.getParentNode();
142             }
143         }
144         return absoluteId;
145     }
146
147     /**
148      * @see org.apache.lenya.cms.publication.SiteTreeNode#getLabels()
149      */

150     public Label[] getLabels() {
151         ArrayList JavaDoc labels = new ArrayList JavaDoc();
152
153         synchronized(sitetree) {
154             NodeList JavaDoc children = node.getChildNodes();
155     
156             for (int i = 0; i < children.getLength(); i++) {
157                 Node JavaDoc child = children.item(i);
158     
159                 if ((child.getNodeType() == Node.ELEMENT_NODE)
160                     && child.getNodeName().equals(LABEL_NAME)) {
161                     String JavaDoc labelName = DocumentHelper.getSimpleElementText((Element JavaDoc) child);
162                     String JavaDoc labelLanguage = null;
163                     Node JavaDoc languageAttribute =
164                         child.getAttributes().getNamedItem(LANGUAGE_ATTRIBUTE_NAME);
165     
166                     if (languageAttribute != null) {
167                         labelLanguage = languageAttribute.getNodeValue();
168                     }
169     
170                     labels.add(new Label(labelName, labelLanguage));
171                 }
172             }
173         }
174         return (Label[]) labels.toArray(new Label[labels.size()]);
175     }
176
177     /**
178      * @see org.apache.lenya.cms.publication.SiteTreeNode#getLabel(java.lang.String)
179      */

180     public Label getLabel(String JavaDoc xmlLanguage) {
181         Label label = null;
182         Label[] labels = getLabels();
183         String JavaDoc language = null;
184
185         for (int i = 0; i < labels.length; i++) {
186             language = labels[i].getLanguage();
187
188             // FIXME: This expression is too complicated
189
// considering there can no longer be any labels with
190
// a null language, i.e. each label must have a language.
191
if ((((xmlLanguage == null) || (xmlLanguage.equals(""))) && (language == null))
192                 || ((language != null) && (language.equals(xmlLanguage)))) {
193                 label = labels[i];
194
195                 break;
196             }
197         }
198
199         return label;
200     }
201
202     /**
203      * @see org.apache.lenya.cms.publication.SiteTreeNode#addLabel(org.apache.lenya.cms.publication.Label)
204      */

205     public void addLabel(Label label) {
206         if (getLabel(label.getLanguage()) == null) {
207             // only add the label if there is no label with the same language yet.
208

209             NamespaceHelper helper = getNamespaceHelper();
210             Element JavaDoc labelElem = helper.createElement(SiteTreeNodeImpl.LABEL_NAME, label.getLabel());
211
212             labelElem.setAttribute(SiteTreeNodeImpl.LANGUAGE_ATTRIBUTE_NAME, label.getLanguage());
213             synchronized (sitetree) {
214                 node.appendChild(labelElem);
215             }
216         }
217     }
218
219     /**
220      * @see org.apache.lenya.cms.publication.SiteTreeNode#removeLabel(org.apache.lenya.cms.publication.Label)
221      */

222     public void removeLabel(Label label) {
223         if (getLabel(label.getLanguage()) != null) {
224             // this node doesn't contain this label
225

226             synchronized (sitetree) {
227                 NodeList JavaDoc children = node.getChildNodes();
228
229                 for (int i = 0; i < children.getLength(); i++) {
230                     Node JavaDoc child = children.item(i);
231     
232                     if ((child.getNodeType() == Node.ELEMENT_NODE)
233                         && child.getNodeName().equals(LABEL_NAME)
234                         && child.getFirstChild().getNodeValue().equals(label.getLabel())) {
235     
236                         Node JavaDoc languageAttribute =
237                             child.getAttributes().getNamedItem(LANGUAGE_ATTRIBUTE_NAME);
238     
239                         if (languageAttribute != null
240                             && languageAttribute.getNodeValue().equals(label.getLanguage())) {
241                             node.removeChild(child);
242                             break;
243                         }
244                     }
245                 }
246             }
247         }
248     }
249  
250     /**
251      * @see org.apache.lenya.cms.publication.SiteTreeNode#hasLink()
252      */

253     public boolean visibleInNav() {
254         boolean isVisibleInNav = true;
255         synchronized (sitetree) {
256             Node JavaDoc attribute = node.getAttributes().getNamedItem(VISIBLEINNAV_ATTRIBUTE_NAME);
257     
258             if (attribute != null) {
259                 isVisibleInNav = attribute.getNodeValue().equals("true");
260             }
261         }
262         return isVisibleInNav;
263     }
264  
265     /**
266      * @see org.apache.lenya.cms.publication.SiteTreeNode#getHref()
267      */

268     public String JavaDoc getHref() {
269         String JavaDoc href = null;
270         synchronized (sitetree) {
271             Node JavaDoc attribute = node.getAttributes().getNamedItem(HREF_ATTRIBUTE_NAME);
272
273             if (attribute != null) {
274                 href = attribute.getNodeValue();
275             }
276         }
277         return href;
278     }
279    
280     /**
281      * @see org.apache.lenya.cms.publication.SiteTreeNode#getSuffix()
282      */

283     public String JavaDoc getSuffix() {
284         String JavaDoc suffix = null;
285         synchronized (sitetree) {
286             Node JavaDoc attribute = node.getAttributes().getNamedItem(SUFFIX_ATTRIBUTE_NAME);
287
288             if (attribute != null) {
289                 suffix = attribute.getNodeValue();
290             }
291         }
292         return suffix;
293     }
294
295     /**
296      * @see org.apache.lenya.cms.publication.SiteTreeNode#hasLink()
297      */

298     public boolean hasLink() {
299         boolean hasLink = false;
300         synchronized (sitetree) {
301             Node JavaDoc attribute = node.getAttributes().getNamedItem(LINK_ATTRIBUTE_NAME);
302     
303             if (attribute != null) {
304                 hasLink = attribute.getNodeValue().equals("true");
305             }
306         }
307         return hasLink;
308     }
309     /**
310      * @see org.apache.lenya.cms.publication.SiteTreeNode#getChildren()
311      */

312     public SiteTreeNode[] getChildren() {
313         List JavaDoc childElements = new ArrayList JavaDoc();
314         synchronized (sitetree) {
315             NamespaceHelper helper = getNamespaceHelper();
316             Element JavaDoc[] elements = helper.getChildren((Element JavaDoc) node, SiteTreeNodeImpl.NODE_NAME);
317     
318             for (int i = 0; i < elements.length; i++) {
319                 SiteTreeNode newNode = new SiteTreeNodeImpl(elements[i], sitetree);
320                 childElements.add(newNode);
321             }
322         }
323         return (SiteTreeNode[]) childElements.toArray(new SiteTreeNode[childElements.size()]);
324     }
325
326     /**
327      * @see org.apache.lenya.cms.publication.SiteTreeNode#removeChildren()
328      */

329     public SiteTreeNode[] removeChildren() {
330         List JavaDoc childElements = new ArrayList JavaDoc();
331         synchronized (sitetree) {
332             NamespaceHelper helper = getNamespaceHelper();
333             Element JavaDoc[] elements = helper.getChildren((Element JavaDoc) node, SiteTreeNodeImpl.NODE_NAME);
334             for (int i = 0; i < elements.length; i++) {
335                 node.removeChild(elements[i]);
336                 SiteTreeNode newNode = new SiteTreeNodeImpl(elements[i], sitetree);
337                 childElements.add(newNode);
338             }
339         }
340         return (SiteTreeNode[]) childElements.toArray(new SiteTreeNode[childElements.size()]);
341     }
342
343     /* (non-Javadoc)
344      * @see org.apache.lenya.cms.publication.SiteTreeNode#deleteChildren()
345      */

346     public void deleteChildren() {
347         synchronized (sitetree) {
348             NamespaceHelper helper = getNamespaceHelper();
349             Element JavaDoc[] elements = helper.getChildren((Element JavaDoc) node, SiteTreeNodeImpl.NODE_NAME);
350             for (int i = 0; i < elements.length; i++) {
351                 node.removeChild(elements[i]);
352             }
353         }
354     }
355     
356     /**
357      * @see org.apache.lenya.cms.publication.SiteTreeNode#getChildren()
358      */

359     public SiteTreeNode[] getNextSiblings() {
360         List JavaDoc siblingElements = new ArrayList JavaDoc();
361
362         synchronized (sitetree) {
363             NamespaceHelper helper = getNamespaceHelper();
364             Element JavaDoc[] elements = helper.getNextSiblings((Element JavaDoc) node, SiteTreeNodeImpl.NODE_NAME);
365     
366             for (int i = 0; i < elements.length; i++) {
367                 SiteTreeNode newNode = new SiteTreeNodeImpl(elements[i], sitetree);
368                 siblingElements.add(newNode);
369             }
370         }
371
372         return (SiteTreeNode[]) siblingElements.toArray(new SiteTreeNode[siblingElements.size()]);
373     }
374
375     /**
376      * @see org.apache.lenya.cms.publication.SiteTreeNode#getNextSiblingDocumentId()
377      */

378     public String JavaDoc getNextSiblingDocumentId() {
379         SiteTreeNode[] siblings = getNextSiblings();
380         if (siblings != null && siblings.length > 0) {
381             return siblings[0].getAbsoluteId();
382         } else {
383             return null;
384         }
385     }
386
387     /**
388      * @see org.apache.lenya.cms.publication.SiteTreeNode#accept(org.apache.lenya.cms.publication.SiteTreeNodeVisitor)
389      */

390     public void accept(SiteTreeNodeVisitor visitor) throws DocumentException {
391         visitor.visitSiteTreeNode(this);
392     }
393
394     /**
395      * (non-Javadoc)
396      * @see org.apache.lenya.cms.publication.SiteTreeNode#acceptSubtree(org.apache.lenya.cms.publication.SiteTreeNodeVisitor)
397      */

398     public void acceptSubtree(SiteTreeNodeVisitor visitor) throws DocumentException {
399         this.accept(visitor);
400         SiteTreeNode[] children = this.getChildren();
401         if (children == null) {
402             log.info("The node " + this.getId() + " has no children");
403             return;
404         } else {
405             for (int i = 0; i < children.length; i++) {
406                 children[i].acceptSubtree(visitor);
407             }
408         }
409     }
410
411     /**
412      * @see org.apache.lenya.cms.publication.SiteTreeNode#acceptSubtree(org.apache.lenya.cms.publication.SiteTreeNodeVisitor)
413      */

414     public void acceptReverseSubtree(SiteTreeNodeVisitor visitor) throws DocumentException {
415         List JavaDoc orderedNodes = this.postOrder();
416         for (int i = 0; i < orderedNodes.size(); i++) {
417             SiteTreeNode node = (SiteTreeNode) orderedNodes.get(i);
418             node.accept(visitor);
419         }
420     }
421
422     /**
423      * @see org.apache.lenya.cms.publication.SiteTreeNode#postOrder()
424      */

425     public List JavaDoc postOrder() {
426         List JavaDoc list = new ArrayList JavaDoc();
427         SiteTreeNode[] children = this.getChildren();
428         for (int i = 0; i < children.length; i++) {
429             List JavaDoc orderedChildren = children[i].postOrder();
430             list.addAll(orderedChildren);
431         }
432         list.add(this);
433         return list;
434     }
435     /**
436      * @see org.apache.lenya.cms.publication.SiteTreeNode#setLabel(org.apache.lenya.cms.publication.Label)
437      */

438     public void setLabel(Label label) {
439         Label existingLabel = getLabel(label.getLanguage());
440         if (existingLabel != null) {
441             removeLabel(existingLabel);
442         }
443         addLabel(label);
444     }
445
446     /**
447      * @see org.apache.lenya.cms.publication.SiteTreeNode#setLabel(org.apache.lenya.cms.publication.Label)
448      */

449     public void setNodeAttribute (String JavaDoc attributeName, String JavaDoc attributeValue) {
450         synchronized (sitetree) {
451             Element JavaDoc element = (Element JavaDoc) node;
452             element.setAttribute(attributeName, attributeValue);
453         }
454     }
455     
456     /**
457      * @see org.apache.lenya.cms.publication.SiteTreeNode#getChildren(java.lang.String)
458      */

459     public SiteTreeNode[] getChildren(String JavaDoc language) {
460         SiteTreeNode[] children = getChildren();
461         List JavaDoc languageChildren = new ArrayList JavaDoc();
462
463         for (int i = 0; i < children.length; i++) {
464             if (children[i].getLabel(language) != null) {
465                 languageChildren.add(children[i]);
466             }
467         }
468
469         return (SiteTreeNode[]) languageChildren.toArray(new SiteTreeNode[languageChildren.size()]);
470     }
471
472     /**
473      * @see org.apache.lenya.cms.publication.SiteTreeNode#getParent()
474      */

475     public SiteTreeNode getParent() {
476         SiteTreeNode parent = null;
477
478         synchronized (sitetree) {
479             Node JavaDoc parentNode = node.getParentNode();
480             if (parentNode.getNodeType() == Node.ELEMENT_NODE
481                 && parentNode.getLocalName().equals(NODE_NAME)) {
482                 parent = new SiteTreeNodeImpl(parentNode, sitetree);
483             }
484         }
485
486         return parent;
487     }
488
489     /**
490      * Returns the namespace helper of the sitetree XML document.
491      * @return A namespace helper.
492      */

493     protected NamespaceHelper getNamespaceHelper() {
494         NamespaceHelper helper =
495             new NamespaceHelper(SiteTree.NAMESPACE_URI, "", node.getOwnerDocument());
496         return helper;
497     }
498
499     /**
500      * @see org.apache.lenya.cms.publication.SiteTreeNode#getParent(java.lang.String)
501      */

502     public SiteTreeNode getParent(String JavaDoc language) {
503         SiteTreeNode parent = getParent();
504
505         Label label = parent.getLabel(language);
506         if (label == null) {
507             parent = null;
508         }
509
510         return parent;
511     }
512
513     /**
514      * @see org.apache.lenya.cms.publication.SiteTreeNode#preOrder()
515      */

516     public List JavaDoc preOrder() {
517         List JavaDoc list = new ArrayList JavaDoc();
518         list.add(this);
519         SiteTreeNode[] children = this.getChildren();
520         for (int i = 0; i < children.length; i++) {
521             List JavaDoc orderedChildren = children[i].preOrder();
522             list.addAll(orderedChildren);
523         }
524         return list;
525     }
526     
527     protected DefaultSiteTree getDefaultSiteTree() {
528         return this.sitetree;
529     }
530 }
531
Popular Tags