1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 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 ; 29 import org.w3c.dom.NamedNodeMap ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 33 38 public class SiteTreeNodeImpl implements SiteTreeNode { 39 private static Logger log = Logger.getLogger(SiteTreeNodeImpl.class); 40 public static final String ID_ATTRIBUTE_NAME = "id"; 41 public static final String VISIBLEINNAV_ATTRIBUTE_NAME="visibleinnav"; 42 public static final String HREF_ATTRIBUTE_NAME = "href"; 43 public static final String SUFFIX_ATTRIBUTE_NAME = "suffix"; 44 public static final String LINK_ATTRIBUTE_NAME = "link"; 45 public static final String LANGUAGE_ATTRIBUTE_NAME = "xml:lang"; 46 public static final String NODE_NAME = "node"; 47 public static final String LABEL_NAME = "label"; 48 49 private Node node = null; 50 private DefaultSiteTree sitetree = null; 51 52 57 public SiteTreeNodeImpl(Node node, DefaultSiteTree sitetree) { 58 this.node = node; 59 this.sitetree = sitetree; 60 } 61 62 66 public String getParentId() { 67 Node parent = node.getParentNode(); 68 String parentId = null; 69 70 if (parent == null) { 71 parentId = "/"; 72 } else { 73 synchronized(sitetree) { 74 NamedNodeMap attributes = parent.getAttributes(); 75 if (attributes == null) { 76 parentId = "/"; 77 } else { 78 Node 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 95 public String getId() { 96 String 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 111 public String getAbsoluteParentId() { 112 SiteTreeNode parent = this.getParent(); 113 return (parent == null) ? "" : parent.getAbsoluteId(); 114 } 115 116 119 public String getAbsoluteId() { 120 String absoluteId = ""; 121 Node currentNode = node; 122 123 NamedNodeMap attributes = null; 124 Node 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 150 public Label[] getLabels() { 151 ArrayList labels = new ArrayList (); 152 153 synchronized(sitetree) { 154 NodeList children = node.getChildNodes(); 155 156 for (int i = 0; i < children.getLength(); i++) { 157 Node child = children.item(i); 158 159 if ((child.getNodeType() == Node.ELEMENT_NODE) 160 && child.getNodeName().equals(LABEL_NAME)) { 161 String labelName = DocumentHelper.getSimpleElementText((Element ) child); 162 String labelLanguage = null; 163 Node 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 180 public Label getLabel(String xmlLanguage) { 181 Label label = null; 182 Label[] labels = getLabels(); 183 String language = null; 184 185 for (int i = 0; i < labels.length; i++) { 186 language = labels[i].getLanguage(); 187 188 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 205 public void addLabel(Label label) { 206 if (getLabel(label.getLanguage()) == null) { 207 209 NamespaceHelper helper = getNamespaceHelper(); 210 Element 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 222 public void removeLabel(Label label) { 223 if (getLabel(label.getLanguage()) != null) { 224 226 synchronized (sitetree) { 227 NodeList children = node.getChildNodes(); 228 229 for (int i = 0; i < children.getLength(); i++) { 230 Node 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 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 253 public boolean visibleInNav() { 254 boolean isVisibleInNav = true; 255 synchronized (sitetree) { 256 Node 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 268 public String getHref() { 269 String href = null; 270 synchronized (sitetree) { 271 Node attribute = node.getAttributes().getNamedItem(HREF_ATTRIBUTE_NAME); 272 273 if (attribute != null) { 274 href = attribute.getNodeValue(); 275 } 276 } 277 return href; 278 } 279 280 283 public String getSuffix() { 284 String suffix = null; 285 synchronized (sitetree) { 286 Node attribute = node.getAttributes().getNamedItem(SUFFIX_ATTRIBUTE_NAME); 287 288 if (attribute != null) { 289 suffix = attribute.getNodeValue(); 290 } 291 } 292 return suffix; 293 } 294 295 298 public boolean hasLink() { 299 boolean hasLink = false; 300 synchronized (sitetree) { 301 Node 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 312 public SiteTreeNode[] getChildren() { 313 List childElements = new ArrayList (); 314 synchronized (sitetree) { 315 NamespaceHelper helper = getNamespaceHelper(); 316 Element [] elements = helper.getChildren((Element ) 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 329 public SiteTreeNode[] removeChildren() { 330 List childElements = new ArrayList (); 331 synchronized (sitetree) { 332 NamespaceHelper helper = getNamespaceHelper(); 333 Element [] elements = helper.getChildren((Element ) 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 346 public void deleteChildren() { 347 synchronized (sitetree) { 348 NamespaceHelper helper = getNamespaceHelper(); 349 Element [] elements = helper.getChildren((Element ) node, SiteTreeNodeImpl.NODE_NAME); 350 for (int i = 0; i < elements.length; i++) { 351 node.removeChild(elements[i]); 352 } 353 } 354 } 355 356 359 public SiteTreeNode[] getNextSiblings() { 360 List siblingElements = new ArrayList (); 361 362 synchronized (sitetree) { 363 NamespaceHelper helper = getNamespaceHelper(); 364 Element [] elements = helper.getNextSiblings((Element ) 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 378 public String 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 390 public void accept(SiteTreeNodeVisitor visitor) throws DocumentException { 391 visitor.visitSiteTreeNode(this); 392 } 393 394 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 414 public void acceptReverseSubtree(SiteTreeNodeVisitor visitor) throws DocumentException { 415 List 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 425 public List postOrder() { 426 List list = new ArrayList (); 427 SiteTreeNode[] children = this.getChildren(); 428 for (int i = 0; i < children.length; i++) { 429 List orderedChildren = children[i].postOrder(); 430 list.addAll(orderedChildren); 431 } 432 list.add(this); 433 return list; 434 } 435 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 449 public void setNodeAttribute (String attributeName, String attributeValue) { 450 synchronized (sitetree) { 451 Element element = (Element ) node; 452 element.setAttribute(attributeName, attributeValue); 453 } 454 } 455 456 459 public SiteTreeNode[] getChildren(String language) { 460 SiteTreeNode[] children = getChildren(); 461 List languageChildren = new ArrayList (); 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 475 public SiteTreeNode getParent() { 476 SiteTreeNode parent = null; 477 478 synchronized (sitetree) { 479 Node 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 493 protected NamespaceHelper getNamespaceHelper() { 494 NamespaceHelper helper = 495 new NamespaceHelper(SiteTree.NAMESPACE_URI, "", node.getOwnerDocument()); 496 return helper; 497 } 498 499 502 public SiteTreeNode getParent(String 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 516 public List preOrder() { 517 List list = new ArrayList (); 518 list.add(this); 519 SiteTreeNode[] children = this.getChildren(); 520 for (int i = 0; i < children.length; i++) { 521 List 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 |