1 16 package org.outerj.daisy.navigation.impl; 17 18 import org.outerj.daisy.repository.RepositoryException; 19 import org.xml.sax.ContentHandler ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.helpers.AttributesImpl ; 22 23 import java.util.Map ; 24 import java.util.List ; 25 import java.util.Iterator ; 26 27 public class GroupNode extends AbstractParentNode { 28 private final String id; 29 private final String label; 30 private final NodeVisibility nodeVisibility; 31 32 36 public GroupNode(String id, String label, NodeVisibility nodeVisibility) { 37 this.id = id; 38 this.label = label; 39 this.nodeVisibility = nodeVisibility; 40 } 41 42 public boolean checkId(String id, long branchId, long languageId) { 43 return this.id.equals(id); 44 } 45 46 public boolean isExpandable() { 47 return false; 48 } 49 50 public void populateNodeLookupMap(Map map, String path) throws RepositoryException { 51 path = path + "/" + id; 52 super.populateNodeLookupMap(map, path); 53 } 54 55 public void generateXml(ContentHandler contentHandler, int depth, String path, long userId, long[] roleIds) throws RepositoryException, SAXException { 56 if (!isVisible(userId, roleIds, null, -1)) 57 return; 58 59 path = path + "/" + id; 60 AttributesImpl attrs = new AttributesImpl (); 61 attrs.addAttribute("", "label", "label", "CDATA", label); 62 attrs.addAttribute("", "path", "path", "CDATA", path); 63 attrs.addAttribute("", "id", "id", "CDATA", id); 64 contentHandler.startElement(NAVIGATION_NS, "group", "group", attrs); 65 if (depth != -1 && depth - 1 > 0) 66 super.generateXml(contentHandler, depth - 1, path, userId, roleIds); 67 else if (depth == -1) 68 super.generateXml(contentHandler, -1, path, userId, roleIds); 69 contentHandler.endElement(NAVIGATION_NS, "group", "group"); 70 } 71 72 public void generateXml(ContentHandler contentHandler, Node[] activeNodePath, int pos, boolean includeOnlyActivePath, 73 String path, long userId, long[] roleIds) throws RepositoryException, SAXException { 74 if (!isVisible(userId, roleIds, activeNodePath, pos)) 75 return; 76 77 path = path + "/" + id; 78 AttributesImpl attrs = new AttributesImpl (); 79 attrs.addAttribute("", "label", "label", "CDATA", label); 80 attrs.addAttribute("", "id", "id", "CDATA", id); 81 attrs.addAttribute("", "path", "path", "CDATA", path); 82 if (pos < activeNodePath.length && activeNodePath[pos] == this) 83 attrs.addAttribute("", "selected", "selected", "CDATA", "true"); 84 if (pos == activeNodePath.length -1 && activeNodePath[pos] == this) 85 attrs.addAttribute("", "active", "active", "CDATA", "true"); 86 contentHandler.startElement(NAVIGATION_NS, "group", "group", attrs); 87 if ((includeOnlyActivePath && pos < activeNodePath.length && activeNodePath[pos] == this) || (!includeOnlyActivePath)) 88 super.generateXml(contentHandler, activeNodePath, pos + 1, includeOnlyActivePath, path, userId, roleIds); 89 contentHandler.endElement(NAVIGATION_NS, "group", "group"); 90 } 91 92 public boolean isVisible(long userId, long[] roleIds, Node[] activeNodePath, int activeNodePathPos) throws RepositoryException { 93 if (nodeVisibility == NodeVisibility.HIDDEN) { 94 return false; 95 } else if (nodeVisibility == NodeVisibility.WHEN_ACTIVE 96 && !(activeNodePath != null && activeNodePathPos < activeNodePath.length && activeNodePath[activeNodePathPos] == this)) { 97 return false; 98 } 99 100 List children = getExpandedChildList(); 101 Iterator childrenIt = children.iterator(); 102 activeNodePathPos++; 103 while (childrenIt.hasNext()) { 104 Node child = (Node)childrenIt.next(); 105 if (child.isVisible(userId, roleIds, activeNodePath, activeNodePathPos)) 106 return true; 107 } 108 return false; 109 } 110 111 public boolean isIdentifiable() { 112 return true; 113 } 114 115 public String getId() { 116 return id; 117 } 118 119 public String findFirstDocumentNode(String path, long userId, long[] roleIds) throws RepositoryException { 120 List children = getExpandedChildList(); 121 Iterator childrenIt = children.iterator(); 122 while (childrenIt.hasNext()) { 123 Object child = childrenIt.next(); 124 if (child instanceof GroupNode) { 125 GroupNode groupChild = (GroupNode)child; 126 String node = groupChild.findFirstDocumentNode(path + "/" + groupChild.getId(), userId, roleIds); 127 if (node != null) 128 return node; 129 } else if (child instanceof DocumentRepresentingNode && ((Node)child).isVisible(userId, roleIds, null, -1)) { 130 return path + "/" + ((Node)child).getId(); 131 } 132 } 134 return null; 135 } 136 } 137 | Popular Tags |