1 34 package net.myvietnam.mvncore.filter; 35 36 public final class EnableMVNCodeFilter { 37 38 private EnableMVNCodeFilter() { } 40 41 static String [][] mvnCode = { 42 { "[b]", "<b>" }, 43 { "[/b]", "</b>" }, 44 { "[i]", "<i>" }, 45 { "[/i]", "</i>" }, 46 { "[u]", "<u>" }, 47 { "[/u]", "</u>" }, 48 { "[s]", "<s>" }, 49 { "[/s]", "</s>" }, 50 51 { "[hr]", "<hr>" }, 52 { "[br]", "<br>" }, 53 { "[p]", "<p>" }, 54 55 { "[h1]", "<h1>" }, 56 { "[/h1]", "</h1>" }, 57 { "[h2]", "<h2>" }, 58 { "[/h2]", "</h2>" }, 59 { "[h3]", "<h3>" }, 60 { "[/h3]", "</h3>" }, 61 { "[h4]", "<h4>" }, 62 { "[/h4]", "</h4>" }, 63 { "[h5]", "<h5>" }, 64 { "[/h5]", "</h5>" }, 65 { "[h6]", "<h6>" }, 66 { "[/h6]", "</h6>" }, 67 68 { "[/size]", "</font>" }, 69 { "[size=+1]", "<font size=\"+1\">" }, 70 { "[size=+2]", "<font size=\"+2\">" }, 71 { "[size=+3]", "<font size=\"+3\">" }, 72 { "[size=+4]", "<font size=\"+4\">" }, 73 { "[size=+5]", "<font size=\"+5\">" }, 74 { "[size=+6]", "<font size=\"+6\">" }, 75 76 { "[size=1]", "<font size=\"1\">" }, 77 { "[size=2]", "<font size=\"2\">" }, 78 { "[size=3]", "<font size=\"3\">" }, 79 { "[size=4]", "<font size=\"4\">" }, 80 { "[size=5]", "<font size=\"5\">" }, 81 { "[size=6]", "<font size=\"6\">" }, 82 83 { "[size=-1]", "<font size=\"-1\">" }, 84 { "[size=-2]", "<font size=\"-2\">" }, 85 { "[size=-3]", "<font size=\"-3\">" }, 86 { "[size=-4]", "<font size=\"-4\">" }, 87 { "[size=-5]", "<font size=\"-5\">" }, 88 { "[size=-6]", "<font size=\"-6\">" }, 89 90 { "[/font]", "</font>" }, 91 { "[font=arial]", "<font face=\"arial\">" }, 92 { "[font=times new roman]", "<font face=\"times new roman\">" }, 93 { "[font=courier new]", "<font face=\"courier new\">" }, 94 { "[font=century gothic]", "<font face=\"Century Gothic\">" }, 95 96 { "[/color]", "</span>" }, 97 { "[color=skyblue]", "<span style=\"color: skyblue\">" }, 98 { "[color=royalblue]", "<span style=\"color: royalblue\">" }, 99 { "[color=blue]", "<span style=\"color: blue\">" }, 100 { "[color=darkblue]", "<span style=\"color: darkblue\">" }, 101 { "[color=orange]", "<span style=\"color: orange\">" }, 102 { "[color=orangered]", "<span style=\"color: orangered\">" }, 103 { "[color=crimson]", "<span style=\"color: crimson\">" }, 104 { "[color=red]", "<span style=\"color: red\">" }, 105 { "[color=firebrick]", "<span style=\"color: firebrick\">" }, 106 { "[color=darkred]", "<span style=\"color: darkred\">" }, 107 { "[color=green]", "<span style=\"color: green\">" }, 108 { "[color=limegreen]", "<span style=\"color: limegreen\">" }, 109 { "[color=seagreen]", "<span style=\"color: seagreen\">" }, 110 { "[color=deeppink]", "<span style=\"color: deeppink\">" }, 111 { "[color=tomato]", "<span style=\"color: tomato\">" }, 112 { "[color=coral]", "<span style=\"color: coral\">" }, 113 { "[color=purple]", "<span style=\"color: purple\">" }, 114 { "[color=indigo]", "<span style=\"color: indigo\">" }, 115 { "[color=burlywood]", "<span style=\"color: burlywood\">" }, 116 { "[color=sandybrown]", "<span style=\"color: sandybrown\">" }, 117 { "[color=sienna]", "<span style=\"color: sienna\">" }, 118 { "[color=chocolate]", "<span style=\"color: chocolate\">" }, 119 { "[color=teal]", "<span style=\"color: teal\">" }, 120 { "[color=silver]", "<span style=\"color: silver\">" }, 121 { "[color=brown]", "<span style=\"color: brown\">" }, 122 { "[color=yellow]", "<span style=\"color: yellow\">" }, 123 { "[color=olive]", "<span style=\"color: olive\">" }, 124 { "[color=cyan]", "<span style=\"color: cyan\">" }, 125 { "[color=violet]", "<span style=\"color: violet\">" }, 126 { "[color=white]", "<span style=\"color: white\">" }, 127 { "[color=black]", "<span style=\"color: black\">" }, 128 { "[color=pink]", "<span style=\"color: pink\">" }, 129 { "[color=purple]", "<span style=\"color: purple\">" }, 130 { "[color=navy]", "<span style=\"color: navy\">" }, 131 { "[color=beige]", "<span style=\"color: beige\">" }, 132 133 { "[list]", "<ul>" }, 134 { "[/list]", "</ul>" }, 135 136 { "[list=1]", "<ul type=\"1\">" }, 137 { "[/list=1]", "</ul>" }, 138 139 { "[list=a]", "<ul type=\"a\">" }, 140 { "[/list=a]", "</ul>" }, 141 142 { "[list=A]", "<ul type=\"A\">" }, 143 { "[/list=A]", "</ul>" }, 144 145 { "[list=i]", "<ul type=\"i\">" }, 146 { "[/list=i]", "</ul>" }, 147 148 { "[list=I]", "<ul type=\"I\">" }, 149 { "[/list=I]", "</ul>" }, 150 151 { "[*]", "<li>" }, 152 153 { "[code]", "<pre>" }, 154 { "[/code]", "</pre>" }, 155 156 { "[quote]" , "<table width='96%' cellspacing='1' cellpadding='3' border='0' align='center'><tr><td>" + 157 " "+ 159 "</td></tr><tr><td class='quote'>"}, 160 { "[/quote]" , "</td></tr></table>"}, 161 }; 162 163 public static String filter(String input) { 164 int beginIndex = 0; 165 int currentBracketIndex = 0; 166 int inputLength = input.length(); 167 int mvnCodeLength = mvnCode.length; 168 StringBuffer output = new StringBuffer (inputLength * 2); 170 while(beginIndex < inputLength) { 171 currentBracketIndex = input.indexOf('[', beginIndex); 172 if (currentBracketIndex == -1) { String remain = input.substring(beginIndex, inputLength); output.append(remain); 175 break; 176 } 177 178 String remain = input.substring(beginIndex, currentBracketIndex); output.append(remain); 181 boolean matchFound = false; 182 183 for (int i = 0; i < mvnCodeLength; i++) { 185 String currentEmotion = mvnCode[i][0]; 186 int endIndex = currentBracketIndex + currentEmotion.length(); 187 if (endIndex > inputLength) continue; 188 String match = input.substring(currentBracketIndex, endIndex); if (currentEmotion.equals(match)) { 190 output.append(mvnCode[i][1]); 191 beginIndex = currentBracketIndex + currentEmotion.length(); 192 matchFound = true; 193 break; 194 } 195 } 197 if (matchFound == false) { 198 beginIndex = currentBracketIndex + 1; 199 output.append('['); 200 } 201 } 203 return output.toString(); 204 } 205 206 223 } 224 | Popular Tags |