You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Utils.java 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package com.gitblit.markdown;
  2. import java.io.*;
  3. /**
  4. * Created by Yuriy Aizenberg
  5. */
  6. public class Utils {
  7. private static final String SPACES = " ";
  8. private static final String CODES = "%([abcdef]|\\d){2,2}";
  9. private static final String SPECIAL_CHARS = "[\\/?!:\\[\\]`.,()*\"';{}+=<>~\\$|#]";
  10. private static final String DASH = "-";
  11. private static final String EMPTY = "";
  12. public static boolean checkSourceFile(String fileName) {
  13. if (isEmpty(fileName)) return false;
  14. File sourceFile = new File(fileName);
  15. return sourceFile.exists() && sourceFile.canRead() && sourceFile.isFile();
  16. }
  17. public static boolean isEmpty(String string) {
  18. return string == null || string.isEmpty();
  19. }
  20. public static int getFileLines(String filePath, int defFaultValue) {
  21. LineNumberReader lineNumberReader = null;
  22. FileReader fileReader = null;
  23. try {
  24. fileReader = new FileReader(filePath);
  25. lineNumberReader = new LineNumberReader(fileReader);
  26. lineNumberReader.skip(Long.MAX_VALUE);
  27. return lineNumberReader.getLineNumber() + 1;
  28. } catch (IOException ignored) {
  29. } finally {
  30. closeStream(lineNumberReader, fileReader);
  31. }
  32. return defFaultValue;
  33. }
  34. public static void closeStream(Closeable... closeable) {
  35. for (Closeable c : closeable) {
  36. if (c != null) {
  37. try {
  38. c.close();
  39. } catch (IOException ignored) {
  40. }
  41. }
  42. }
  43. }
  44. public static String normalize(final String taintedURL) {
  45. return taintedURL
  46. .trim()
  47. .replaceAll(SPACES, DASH)
  48. .replaceAll(CODES, EMPTY)
  49. .replaceAll(SPECIAL_CHARS, EMPTY).toLowerCase();
  50. }
  51. }