1 31 32 package org.opencms.util; 33 34 import org.opencms.configuration.CmsConfigurationManager; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsRequestContext; 37 import org.opencms.file.CmsResource; 38 import org.opencms.flex.CmsFlexCache; 39 import org.opencms.i18n.CmsEncoder; 40 import org.opencms.main.CmsException; 41 import org.opencms.main.CmsIllegalArgumentException; 42 import org.opencms.main.CmsLog; 43 import org.opencms.main.CmsSystemInfo; 44 import org.opencms.staticexport.CmsLinkManager; 45 46 import java.io.ByteArrayInputStream ; 47 import java.io.ByteArrayOutputStream ; 48 import java.io.File ; 49 import java.io.FileFilter ; 50 import java.io.FileInputStream ; 51 import java.io.FileNotFoundException ; 52 import java.io.FileOutputStream ; 53 import java.io.IOException ; 54 import java.io.InputStream ; 55 import java.io.OutputStreamWriter ; 56 import java.net.URL ; 57 import java.net.URLClassLoader ; 58 import java.util.ArrayList ; 59 import java.util.Arrays ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.ListIterator ; 64 import java.util.Locale ; 65 66 import org.apache.commons.logging.Log; 67 68 77 public final class CmsFileUtil { 78 79 80 private static final Log LOG = CmsLog.getLog(CmsFileUtil.class); 81 82 85 private CmsFileUtil() { 86 87 } 89 90 98 public static void checkResources(CmsObject cms, List resources) throws CmsIllegalArgumentException { 99 100 StringBuffer result = new StringBuffer (128); 101 ListIterator it = resources.listIterator(); 102 while (it.hasNext()) { 103 String resourcePath = (String )it.next(); 104 try { 105 CmsResource resource = cms.readResource(resourcePath); 106 if (resource.isFolder() && !resourcePath.endsWith("/")) { 108 it.set(resourcePath + "/"); 109 } 110 } catch (CmsException e) { 111 result.append(resourcePath); 112 result.append('\n'); 113 } 114 } 115 if (result.length() > 0) { 116 throw new CmsIllegalArgumentException(Messages.get().container( 117 Messages.ERR_MISSING_RESOURCES_1, 118 result.toString())); 119 } 120 } 121 122 129 public static void copy(String fromFile, String toFile) throws IOException { 130 131 File inputFile = new File (fromFile); 132 File outputFile = new File (toFile); 133 134 FileInputStream in = new FileInputStream (inputFile); 135 FileOutputStream out = new FileOutputStream (outputFile); 136 137 byte[] buf = new byte[1024]; 139 int len; 140 while ((len = in.read(buf)) > 0) { 141 out.write(buf, 0, len); 142 } 143 in.close(); 144 out.close(); 145 } 146 147 156 public static String formatFilesize(long filesize, Locale locale) { 157 158 String result; 159 filesize = Math.abs(filesize); 160 161 if (Math.abs(filesize) < 1024) { 162 result = Messages.get().getBundle(locale).key(Messages.GUI_FILEUTIL_FILESIZE_BYTES_1, new Long (filesize)); 163 } else if (Math.abs(filesize) < 1048576) { 164 result = Messages.get().getBundle(locale).key( 166 Messages.GUI_FILEUTIL_FILESIZE_KBYTES_1, 167 new Double (filesize / 1024.0)); 168 } else if (Math.abs(filesize) < 1073741824) { 169 result = Messages.get().getBundle(locale).key( 171 Messages.GUI_FILEUTIL_FILESIZE_MBYTES_1, 172 new Double (filesize / 1048576.0)); 173 } else { 174 result = Messages.get().getBundle(locale).key( 175 Messages.GUI_FILEUTIL_FILESIZE_GBYTES_1, 176 new Double (filesize / 1073741824.0)); 177 } 178 return result; 179 } 180 181 190 public static String formatResourceNames(CmsRequestContext context, List resources) { 191 192 if (resources == null) { 193 return null; 194 } 195 StringBuffer result = new StringBuffer (128); 196 Iterator i = resources.iterator(); 197 while (i.hasNext()) { 198 CmsResource res = (CmsResource)i.next(); 199 String path = res.getRootPath(); 200 if (context != null) { 201 path = context.removeSiteRoot(path); 202 } 203 result.append(path); 204 if (i.hasNext()) { 205 result.append(", "); 206 } 207 } 208 return result.toString(); 209 } 210 211 225 public static String getFileExtension(String filename) { 226 227 int pos = filename.lastIndexOf('.'); 228 return (pos >= 0) ? filename.substring(pos).toLowerCase() : ""; 229 } 230 231 246 public static List getFiles(String name, FileFilter filter, boolean includeSubtree) { 247 248 List ret = new ArrayList (); 249 250 File file = new File (name); 251 if (!file.isDirectory()) { 252 file = new File (file.getParent()); 253 if (!file.isDirectory()) { 254 return ret; 255 } 256 } 257 File [] dirContent = file.listFiles(); 258 for (int i = 0; i < dirContent.length; i++) { 259 File f = dirContent[i]; 260 if (filter.accept(f)) { 261 ret.add(f); 262 } 263 if (includeSubtree && f.isDirectory()) { 264 ret.addAll(getFiles(f.getAbsolutePath(), filter, true)); 265 } 266 } 267 268 return ret; 269 } 270 271 282 public static String getRepositoryName(String repository, String vfspath, boolean online) { 283 284 StringBuffer result = new StringBuffer (64); 285 result.append(repository); 286 result.append(online ? CmsFlexCache.REPOSITORY_ONLINE : CmsFlexCache.REPOSITORY_OFFLINE); 287 result.append(vfspath); 288 return result.toString(); 289 } 290 291 313 public static String getResourcePathFromClassloader(String fileName) { 314 315 boolean isFolder = CmsResource.isFolder(fileName); 316 String result = ""; 317 URL inputUrl = Thread.currentThread().getContextClassLoader().getResource(fileName); 318 if (inputUrl != null) { 319 result = normalizePath(inputUrl); 321 if (isFolder && !CmsResource.isFolder(result)) { 322 result = result + '/'; 323 } 324 } else { 325 if (LOG.isErrorEnabled()) { 326 try { 327 URLClassLoader cl = (URLClassLoader )Thread.currentThread().getContextClassLoader(); 328 URL [] paths = cl.getURLs(); 329 LOG.error(Messages.get().getBundle().key( 330 Messages.ERR_MISSING_CLASSLOADER_RESOURCE_2, 331 fileName, 332 Arrays.asList(paths))); 333 } catch (Throwable t) { 334 LOG.error(Messages.get().getBundle().key(Messages.ERR_MISSING_CLASSLOADER_RESOURCE_1, fileName)); 335 } 336 } 337 } 338 return result; 339 } 340 341 356 public static String getRfsPath(String filename, String extension, String parameters) { 357 358 StringBuffer buf = new StringBuffer (128); 359 buf.append(filename); 360 buf.append('_'); 361 int h = parameters.hashCode(); 362 buf.append(h > 0 ? h : -h); 364 buf.append(extension); 365 return buf.toString(); 366 } 367 368 378 public static String normalizePath(String path) { 379 380 return normalizePath(path, File.separatorChar); 381 } 382 383 395 public static String normalizePath(String path, char separatorChar) { 396 397 if (CmsStringUtil.isNotEmpty(path)) { 398 path = path.replace('\\', '/'); 400 String drive = null; 401 if ((path.length() > 1) && (path.charAt(1) == ':')) { 402 drive = path.substring(0, 2); 404 path = path.substring(2); 405 } else if ((path.length() > 1) && (path.charAt(0) == '/') && (path.charAt(1) == '/')) { 406 drive = path.substring(0, 2); 408 path = path.substring(2); 409 } 410 if (path.charAt(0) == '/') { 411 path = '.' + path; 413 } 414 path = CmsLinkManager.getAbsoluteUri(path, "/"); 416 path = CmsStringUtil.substitute(path, "//", "/"); 418 if (drive != null) { 420 path = drive.concat(path); 421 } 422 if (separatorChar != '/') { 424 path = path.replace('/', separatorChar); 425 } 426 } 427 return path; 428 } 429 430 442 public static String normalizePath(URL url) { 443 444 return normalizePath(url, File.separatorChar); 445 } 446 447 458 public static String normalizePath(URL url, char separatorChar) { 459 460 String path = new File (url.getPath()).getAbsolutePath(); 462 String systemEncoding = (new OutputStreamWriter (new ByteArrayOutputStream ())).getEncoding(); 464 return CmsFileUtil.normalizePath(CmsEncoder.decode(path, systemEncoding), separatorChar); 466 } 467 468 473 public static void purgeDirectory(File directory) { 474 475 if (directory.canRead() && directory.isDirectory()) { 476 File [] files = directory.listFiles(); 477 for (int i = 0; i < files.length; i++) { 478 File f = files[i]; 479 if (f.isDirectory()) { 480 purgeDirectory(f); 481 } 482 if (f.canWrite()) { 483 f.delete(); 484 } 485 } 486 directory.delete(); 487 } 488 } 489 490 498 public static byte[] readFile(File file) throws IOException { 499 500 FileInputStream in = new FileInputStream (file); 502 503 return readFully(in, (int)file.length()); 505 } 506 507 515 public static byte[] readFile(String filename) throws IOException { 516 517 InputStream in = CmsFileUtil.class.getClassLoader().getResourceAsStream(filename); 519 if (in == null) { 520 throw new FileNotFoundException (filename); 521 } 522 523 return readFully(in); 524 } 525 526 534 public static String readFile(String filename, String encoding) throws IOException { 535 536 return new String (readFile(filename), encoding); 537 } 538 539 548 public static byte[] readFully(InputStream in) throws IOException { 549 550 return readFully(in, true); 551 } 552 553 563 public static byte[] readFully(InputStream in, boolean closeInputStream) throws IOException { 564 565 if (in instanceof ByteArrayInputStream ) { 566 return readFully(in, in.available(), closeInputStream); 568 } 569 570 byte[] xfer = new byte[2048]; 572 ByteArrayOutputStream out = new ByteArrayOutputStream (xfer.length); 574 575 for (int bytesRead = in.read(xfer, 0, xfer.length); bytesRead >= 0; bytesRead = in.read(xfer, 0, xfer.length)) { 577 if (bytesRead > 0) { 578 out.write(xfer, 0, bytesRead); 579 } 580 } 581 if (closeInputStream) { 582 in.close(); 583 } 584 out.close(); 585 return out.toByteArray(); 586 } 587 588 599 public static byte[] readFully(InputStream in, int size) throws IOException { 600 601 return readFully(in, size, true); 602 } 603 604 616 public static byte[] readFully(InputStream in, int size, boolean closeStream) throws IOException { 617 618 byte[] bytes = new byte[size]; 620 621 int offset = 0; 623 int numRead = 0; 624 while (offset < size) { 625 numRead = in.read(bytes, offset, size - offset); 626 if (numRead >= 0) { 627 offset += numRead; 628 } else { 629 break; 630 } 631 } 632 633 if (closeStream) { 635 in.close(); 636 } 637 638 if (offset < bytes.length) { 640 throw new IOException ("Could not read requested " + size + " bytes from input stream"); 641 } 642 643 return bytes; 644 } 645 646 647 659 public static List removeRedundancies(List resourcenames) { 660 661 if ((resourcenames == null) || (resourcenames.isEmpty())) { 662 return new ArrayList (); 663 } 664 if (resourcenames.size() == 1) { 665 return new ArrayList (resourcenames); 667 } 668 List result = new ArrayList (resourcenames.size()); 670 List base = new ArrayList (resourcenames); 671 Collections.sort(base); 672 Iterator i = base.iterator(); 673 while (i.hasNext()) { 674 String resourcename = (String )i.next(); 676 if (CmsStringUtil.isEmptyOrWhitespaceOnly(resourcename)) { 677 continue; 679 } 680 boolean valid = true; 681 for (int j = (result.size() - 1); j >= 0; j--) { 682 String check = (String )result.get(j); 684 if (resourcename.startsWith(check)) { 685 valid = false; 686 break; 687 } 688 } 689 if (valid) { 690 result.add(resourcename); 692 } 693 } 694 return result; 695 } 696 697 709 public static List removeRedundantResources(List resources) { 710 711 if ((resources == null) || (resources.isEmpty())) { 712 return new ArrayList (); 713 } 714 if (resources.size() == 1) { 715 return new ArrayList (resources); 717 } 718 List result = new ArrayList (resources.size()); 720 List base = new ArrayList (resources); 721 Collections.sort(base); 722 Iterator i = base.iterator(); 723 while (i.hasNext()) { 724 CmsResource resource = (CmsResource)i.next(); 726 boolean valid = true; 727 for (int j = (result.size() - 1); j >= 0; j--) { 728 String check = ((CmsResource)result.get(j)).getRootPath(); 730 if (resource.getRootPath().startsWith(check)) { 731 valid = false; 732 break; 733 } 734 } 735 if (valid) { 736 result.add(resource); 738 } 739 } 740 return result; 741 } 742 743 751 public static String searchWebInfFolder(String startFolder) { 752 753 if (CmsStringUtil.isEmpty(startFolder)) { 754 return null; 755 } 756 757 File f = new File (startFolder); 758 if (!f.exists() || !f.isDirectory()) { 759 return null; 760 } 761 762 File configFile = new File (f, CmsSystemInfo.FOLDER_CONFIG + CmsConfigurationManager.DEFAULT_XML_FILE_NAME); 763 if (configFile.exists() && configFile.isFile()) { 764 return f.getAbsolutePath(); 765 } 766 767 String webInfFolder = null; 768 File [] subFiles = f.listFiles(); 769 for (int i = 0; i < subFiles.length; i++) { 770 if (subFiles[i].isDirectory()) { 771 webInfFolder = searchWebInfFolder(subFiles[i].getAbsolutePath()); 772 if (webInfFolder != null) { 773 break; 774 } 775 } 776 } 777 778 return webInfFolder; 779 } 780 } | Popular Tags |