1 20 package org.openi.menu; 21 22 import org.apache.log4j.*; 23 import java.io.*; 24 import java.util.*; 25 26 27 36 public class MenuBuilder { 37 private static Logger logger = LogManager.getLogger(MenuBuilder.class); 38 39 42 public MenuBuilder() { 43 try { 44 jbInit(); 45 } catch (Exception ex) { 46 ex.printStackTrace(); 47 } 48 } 49 50 64 public Menu build(String baseDirName, List menuSubDirs) 65 throws IOException { 66 long start = System.currentTimeMillis(); 67 logger.debug("attempting to build menu using baseDir=" + baseDirName); 68 69 Menu root = new Menu(); 70 71 if ((baseDirName == null) || (menuSubDirs == null)) { 72 logger.error("baseDirName is null"); 73 } 74 75 File baseDir = new File(baseDirName); 76 77 if ((baseDir == null) || !baseDir.exists() || !baseDir.isDirectory()) { 78 throw new IOException( 79 "baseDir is null, or does not exist, or is not a directory: " 80 + baseDir); 81 } 82 83 Iterator dirs = menuSubDirs.iterator(); 85 logger.debug("building menu using: " + menuSubDirs); 86 87 while (dirs.hasNext()) { 88 String currentSubDir = (String ) dirs.next(); 89 90 recurse(root, baseDir, new File(baseDir, currentSubDir)); 92 } 93 94 logger.info("created menu tree in " 95 + String.valueOf(System.currentTimeMillis() - start) + "ms"); 96 97 return root; 98 } 99 100 108 private void recurse(Menu parent, File baseDir, File module) { 109 try { 110 if (module.isDirectory()) { 112 Menu menu = new Menu(); 115 menu.setDisplayName(constructDisplayName(module)); 116 parent.addSubMenu(menu); 117 118 File[] children = module.listFiles(); 119 java.util.Arrays.sort(children); 120 121 for (int i = 0; i < children.length; i++) { 122 recurse(menu, baseDir, children[i]); 123 } 124 } else { 125 handleFile(parent, baseDir, module); 126 } 127 } catch (IOException e) { 128 logger.error("trouble creating menu", e); 129 } 130 } 131 132 138 private void handleFile(Menu parent, File baseDir, File file) 139 throws IOException { 140 if (file.exists()) { 142 MenuItem menuItem = new MenuItem(); 143 menuItem.setDisplayName(constructDisplayName(file)); 144 menuItem.setUrl(constructUrl(baseDir, file)); 145 parent.addMenuItem(menuItem); 146 } 147 } 148 149 155 public static String constructDisplayName(File file) 156 throws IOException { 157 return constructDisplayName(file.getName()); 158 } 159 160 168 public static String constructDisplayName(String filename) 169 throws IOException { 170 String displayName = filename; 172 String extension = ""; 173 174 int dot = displayName.lastIndexOf("."); 176 177 if (dot >= 0) { 178 extension = displayName.substring(dot + 1); 180 displayName = displayName.substring(0, dot); 182 } 183 184 if ((extension.length() > 0) 186 && (!(extension.equalsIgnoreCase("analysis") 187 || extension.equalsIgnoreCase("html") 188 || extension.equalsIgnoreCase("htm") 189 || extension.equalsIgnoreCase("jsp")))) { 190 displayName = displayName + " (" + extension + ")"; 191 } 192 193 int lastSlash = displayName.lastIndexOf('/'); 194 if(lastSlash >=0 && lastSlash < (displayName.length()-1) ){ 195 displayName = displayName.substring(lastSlash+1, displayName.length()); 196 } 197 198 return displayName.replace('_', ' '); 200 } 201 202 208 private String constructUrl(File baseDir, File file) 209 throws IOException { 210 String relativeUrl = ""; 211 String extension = ""; 212 String url = ""; 213 214 int dot = file.getName().lastIndexOf("."); 216 217 if (dot >= 0) { 218 extension = file.getName().substring(dot + 1); 219 } 220 221 222 int len = baseDir.getCanonicalPath().length(); 224 relativeUrl = file.getCanonicalPath().substring(len); 225 226 if ((relativeUrl.charAt(0) == '/') || (relativeUrl.charAt(0) == '\\')) { 228 relativeUrl = relativeUrl.substring(1); 229 } 230 231 relativeUrl = relativeUrl.replace('\\', '/'); 233 234 return relativeUrl; 236 } 237 238 private void jbInit() throws Exception { 239 } 240 } 241 | Popular Tags |