|
| 1 | +package com.qiniu.convert; |
| 2 | + |
| 3 | +import com.qiniu.interfaces.KeyValuePair; |
| 4 | +import com.qiniu.util.FileUtils; |
| 5 | + |
| 6 | +public class IndentStringPair implements KeyValuePair<String, String> { |
| 7 | + |
| 8 | + private StringBuilder stringBuilder = new StringBuilder(); |
| 9 | + private StringBuilder parentPath = new StringBuilder(); |
| 10 | + private String separator; |
| 11 | + private int size; |
| 12 | + |
| 13 | + public IndentStringPair(String separator) { |
| 14 | + this.separator = separator; |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public void putKey(String key, String value) { |
| 19 | + value = value.replace("\n", "%0a").replace("\r", "%0d"); |
| 20 | + int num = value.split(FileUtils.pathSeparator).length; |
| 21 | + if (num > 1) { |
| 22 | + if (value.endsWith(FileUtils.pathSeparator)) { |
| 23 | + parentPath.append(value, 0, value.substring(0, value.length() - 1) |
| 24 | + .lastIndexOf(FileUtils.pathSeparator)); |
| 25 | + } else { |
| 26 | + parentPath.append(value, 0, value.lastIndexOf(FileUtils.pathSeparator)); |
| 27 | + } |
| 28 | + StringBuilder stringBuilder = new StringBuilder(); |
| 29 | + for (int j = 1; j < num; j++) stringBuilder.append("\t"); |
| 30 | + stringBuilder.append(value.replace(parentPath, "").substring(1)); |
| 31 | + stringBuilder.append(stringBuilder.toString()).append("\t"); |
| 32 | + parentPath.delete(0, parentPath.length()).append(value).append("\t|"); |
| 33 | + } else { |
| 34 | + stringBuilder.append(value).append("\t"); |
| 35 | + parentPath.append(value).append("\t|"); |
| 36 | + } |
| 37 | + stringBuilder.append(separator).append(value); |
| 38 | + size++; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void put(String key, String value) { |
| 43 | + stringBuilder.append(separator).append(value); |
| 44 | + size++; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void put(String key, Boolean value) { |
| 49 | + stringBuilder.append(separator).append(value); |
| 50 | + size++; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void put(String key, Integer value) { |
| 55 | + stringBuilder.append(separator).append(value); |
| 56 | + size++; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void put(String key, Long value) { |
| 61 | + stringBuilder.append(separator).append(value); |
| 62 | + size++; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String getProtoEntity() { |
| 67 | + return parentPath.append(stringBuilder.deleteCharAt(stringBuilder.length() - 1)).substring(1); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public int size() { |
| 72 | + return size; |
| 73 | + } |
| 74 | +} |
0 commit comments