1 31 32 package org.opencms.jsp; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsProperty; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsResourceFilter; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 import org.opencms.main.OpenCms; 42 43 import java.util.ArrayList ; 44 import java.util.Collections ; 45 import java.util.Iterator ; 46 import java.util.List ; 47 48 import org.apache.commons.logging.Log; 49 50 70 public class CmsJspNavBuilder { 71 72 75 private static class ResourceTitleContainer implements Comparable { 76 77 78 protected CmsResource m_res; 79 80 81 protected String m_title; 82 83 87 ResourceTitleContainer(CmsObject cms, CmsResource res) { 88 89 m_res = res; 90 try { 91 cms.getRequestContext().saveSiteRoot(); 92 cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 93 m_title = cms.readPropertyObject( 94 res, 95 org.opencms.file.CmsPropertyDefinition.PROPERTY_TITLE, 96 false).getValue(); 97 cms.getRequestContext().restoreSiteRoot(); 98 } catch (Exception e) { 99 m_title = ""; 100 } 101 } 102 103 106 public int compareTo(Object obj) { 107 108 if (obj == this) { 109 return 0; 110 } 111 if (obj instanceof ResourceTitleContainer) { 112 if (m_title == null) { 113 return 1; 114 } 115 return (m_title.toLowerCase().compareTo(((ResourceTitleContainer)obj).m_title.toLowerCase())); 116 } 117 return 0; 118 } 119 } 120 121 122 private static final Log LOG = CmsLog.getLog(CmsJspNavBuilder.class); 123 124 private CmsObject m_cms; 126 private String m_requestUri; 127 private String m_requestUriFolder; 128 129 134 public CmsJspNavBuilder() { 135 136 } 138 139 144 public CmsJspNavBuilder(CmsObject cms) { 145 146 init(cms); 147 } 148 149 157 public static List getChannelSubFolders(CmsObject cms, String channel) { 158 159 if (!channel.startsWith("/")) { 160 channel = "/" + channel; 161 } 162 if (!channel.endsWith("/")) { 163 channel += "/"; 164 } 165 166 List subChannels = new ArrayList (); 168 cms.getRequestContext().saveSiteRoot(); 169 try { 170 cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 171 subChannels = cms.getSubFolders(channel); 172 } catch (CmsException e) { 173 if (LOG.isErrorEnabled()) { 174 LOG.error(e.getLocalizedMessage()); 176 } 177 } finally { 178 cms.getRequestContext().restoreSiteRoot(); 179 } 180 181 java.util.ArrayList list = new java.util.ArrayList (subChannels.size()); 183 list.addAll(subChannels); 184 return list; 185 } 186 187 197 public static List getChannelSubFolders(CmsObject cms, String parentChannel, String subChannel) { 198 199 String channel = null; 200 if (subChannel == null) { 201 subChannel = ""; 202 } else if (subChannel.startsWith("/")) { 203 subChannel = subChannel.substring(1); 204 } 205 if (parentChannel == null) { 206 parentChannel = ""; 207 } 208 if (parentChannel.endsWith("/")) { 209 channel = parentChannel + subChannel; 210 } else { 211 channel = parentChannel + "/" + subChannel; 212 } 213 return getChannelSubFolders(cms, channel); 214 } 215 216 226 public static List getChannelSubFoldersSortTitleAsc(CmsObject cms, String channel, String subChannel) { 227 228 List subChannels = getChannelSubFolders(cms, channel, subChannel); 229 ArrayList tmpList = new java.util.ArrayList (subChannels.size()); 231 for (int i = 0; i < subChannels.size(); i++) { 232 CmsResource res = (CmsResource)subChannels.get(i); 233 ResourceTitleContainer container = new ResourceTitleContainer(cms, res); 234 tmpList.add(container); 235 } 236 Collections.sort(tmpList); 237 java.util.ArrayList list = new java.util.ArrayList (subChannels.size()); 238 for (int i = 0; i < tmpList.size(); i++) { 239 ResourceTitleContainer container = (ResourceTitleContainer)tmpList.get(i); 240 list.add(container.m_res); 241 } 242 return list; 243 } 244 245 257 public static String getDefaultFile(CmsObject cms, String folder) { 258 259 if (folder.endsWith("/")) { 260 List defaultFolders = new ArrayList (); 261 try { 262 CmsProperty p = cms.readPropertyObject(folder, CmsPropertyDefinition.PROPERTY_DEFAULT_FILE, false); 263 defaultFolders.add(p.getValue()); 264 } catch (CmsException exc) { 265 } 267 268 defaultFolders.addAll(OpenCms.getDefaultFiles()); 269 270 for (Iterator i = defaultFolders.iterator(); i.hasNext();) { 271 String defaultName = (String )i.next(); 272 if (cms.existsResource(folder + defaultName)) { 273 return folder + defaultName; 274 } 275 } 276 277 return folder; 278 } 279 280 return null; 281 } 282 283 291 public static List getNavigationForFolder(CmsObject cms, String folder) { 292 293 folder = CmsResource.getFolderPath(folder); 294 List result = new ArrayList (); 295 296 List resources; 297 try { 298 resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT); 299 } catch (Exception e) { 300 return Collections.EMPTY_LIST; 301 } 302 303 for (int i = 0; i < resources.size(); i++) { 304 CmsResource r = (CmsResource)resources.get(i); 305 CmsJspNavElement element = getNavigationForResource(cms, cms.getSitePath(r)); 306 if ((element != null) && element.isInNavigation()) { 307 result.add(element); 308 } 309 } 310 Collections.sort(result); 311 return result; 312 } 313 314 327 public static List getNavigationForFolder(CmsObject cms, String folder, int level) { 328 329 folder = CmsResource.getFolderPath(folder); 330 if (level == 0) { 332 return getNavigationForFolder(cms, "/"); 333 } 334 String navfolder = CmsResource.getPathPart(folder, level); 335 if (navfolder != null) { 337 return getNavigationForFolder(cms, navfolder); 338 } 339 return Collections.EMPTY_LIST; 341 } 342 343 351 public static CmsJspNavElement getNavigationForResource(CmsObject cms, String resource) { 352 353 List properties; 354 try { 355 properties = cms.readPropertyObjects(resource, false); 356 } catch (Exception e) { 357 return null; 358 } 359 int level = CmsResource.getPathLevel(resource); 360 if (resource.endsWith("/")) { 361 level--; 362 } 363 return new CmsJspNavElement(resource, CmsProperty.toMap(properties), level); 364 } 365 366 380 public static List getNavigationTreeForFolder(CmsObject cms, String folder, int startlevel, int endlevel) { 381 382 folder = CmsResource.getFolderPath(folder); 383 if (endlevel < startlevel) { 385 return Collections.EMPTY_LIST; 386 } 387 int currentlevel = CmsResource.getPathLevel(folder); 388 if (currentlevel < endlevel) { 389 endlevel = currentlevel; 390 } 391 if (startlevel == endlevel) { 392 return getNavigationForFolder(cms, CmsResource.getPathPart(folder, startlevel), startlevel); 393 } 394 395 ArrayList result = new ArrayList (); 396 float parentcount = 0; 397 398 for (int i = startlevel; i <= endlevel; i++) { 399 String currentfolder = CmsResource.getPathPart(folder, i); 400 List entries = getNavigationForFolder(cms, currentfolder); 401 if (parentcount > 0) { 403 for (int it = 0; it < entries.size(); it++) { 404 CmsJspNavElement e = (CmsJspNavElement)entries.get(it); 405 e.setNavPosition(e.getNavPosition() + parentcount); 406 } 407 } 408 result.addAll(entries); 410 Collections.sort(result); 411 float pos = 0; 413 int count = 0; 414 String nextfolder = CmsResource.getPathPart(folder, i + 1); 415 parentcount = 0; 416 for (int it = 0; it < result.size(); it++) { 417 pos = 10000 * (++count); 418 CmsJspNavElement e = (CmsJspNavElement)result.get(it); 419 e.setNavPosition(pos); 420 if (e.getResourceName().startsWith(nextfolder)) { 421 parentcount = pos; 422 } 423 } 424 if (parentcount == 0) { 425 parentcount = pos; 426 } 427 } 428 return result; 429 } 430 431 444 public static List getSiteNavigation(CmsObject cms, String folder, int endLevel) { 445 446 boolean noLimit = false; 448 if (endLevel < 0) { 449 noLimit = true; 450 } 451 ArrayList list = new ArrayList (); 452 List curnav = getNavigationForFolder(cms, folder); 454 for (int i = 0; i < curnav.size(); i++) { 456 CmsJspNavElement ne = (CmsJspNavElement)curnav.get(i); 457 list.add(ne); 459 if (ne.isFolderLink() && (noLimit || (ne.getNavTreeLevel() < endLevel))) { 461 List subnav = getSiteNavigation(cms, ne.getResourceName(), endLevel); 462 list.addAll(subnav); 464 } 465 } 466 return list; 467 } 468 469 476 public List getChannelSubFolders(String channel) { 477 478 return getChannelSubFolders(m_cms, channel); 479 } 480 481 490 public List getChannelSubFolders(String parentChannel, String subChannel) { 491 492 return getChannelSubFolders(m_cms, parentChannel, subChannel); 493 } 494 495 504 public List getChannelSubFoldersSortTitleAsc(String channel, String subChannel) { 505 506 return getChannelSubFoldersSortTitleAsc(m_cms, channel, subChannel); 507 } 508 509 515 public List getNavigationBreadCrumb() { 516 517 return getNavigationBreadCrumb(m_requestUriFolder, 0, -1, true); 518 } 519 520 528 public List getNavigationBreadCrumb(int startlevel, boolean currentFolder) { 529 530 return getNavigationBreadCrumb(m_requestUriFolder, startlevel, -1, currentFolder); 531 } 532 533 541 public List getNavigationBreadCrumb(int startlevel, int endlevel) { 542 543 return getNavigationBreadCrumb(m_requestUriFolder, startlevel, endlevel, true); 544 } 545 546 562 public List getNavigationBreadCrumb(String folder, int startlevel, int endlevel, boolean currentFolder) { 563 564 ArrayList result = new ArrayList (); 565 566 int level = CmsResource.getPathLevel(folder); 567 if (!currentFolder) { 569 level -= 1; 570 } 571 if (level < endlevel || endlevel == -1) { 573 endlevel = level; 574 } 575 576 if (startlevel < 0) { 578 startlevel = endlevel + startlevel + 1; 579 if (startlevel < 0) { 580 startlevel = 0; 581 } 582 } 583 584 for (int i = startlevel; i <= endlevel; i++) { 586 String navFolder = CmsResource.getPathPart(folder, i); 587 CmsJspNavElement e = getNavigationForResource(navFolder); 588 result.add(e); 590 } 591 592 return result; 593 } 594 595 601 public List getNavigationForFolder() { 602 603 return getNavigationForFolder(m_cms, m_requestUriFolder); 604 } 605 606 617 public List getNavigationForFolder(int level) { 618 619 return getNavigationForFolder(m_cms, m_requestUriFolder, level); 620 } 621 622 629 public List getNavigationForFolder(String folder) { 630 631 return getNavigationForFolder(m_cms, folder); 632 } 633 634 646 public List getNavigationForFolder(String folder, int level) { 647 648 return getNavigationForFolder(m_cms, folder, level); 649 } 650 651 656 public CmsJspNavElement getNavigationForResource() { 657 658 return getNavigationForResource(m_cms, m_requestUri); 659 } 660 661 668 public CmsJspNavElement getNavigationForResource(String resource) { 669 670 return getNavigationForResource(m_cms, resource); 671 } 672 673 681 public List getNavigationTreeForFolder(int startlevel, int endlevel) { 682 683 return getNavigationTreeForFolder(m_cms, m_requestUriFolder, startlevel, endlevel); 684 } 685 686 695 public List getNavigationTreeForFolder(String folder, int startlevel, int endlevel) { 696 697 return getNavigationTreeForFolder(m_cms, folder, startlevel, endlevel); 698 } 699 700 707 public List getSiteNavigation() { 708 709 return getSiteNavigation(m_cms, "/", -1); 710 } 711 712 722 public List getSiteNavigation(String folder, int endLevel) { 723 724 return getSiteNavigation(m_cms, folder, endLevel); 725 } 726 727 732 public void init(CmsObject cms) { 733 734 m_cms = cms; 735 m_requestUri = m_cms.getRequestContext().getUri(); 736 m_requestUriFolder = CmsResource.getFolderPath(m_requestUri); 737 } 738 } 739 | Popular Tags |