| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.report; |
| 34 | | | |
| 35 | | | import java.io.BufferedReader; |
| 36 | | | import java.io.File; |
| 37 | | | import java.io.FileInputStream; |
| 38 | | | import java.io.IOException; |
| 39 | | | import java.io.InputStreamReader; |
| 40 | | | import java.io.LineNumberReader; |
| 41 | | | import java.io.Reader; |
| 42 | | | import java.io.StringReader; |
| 43 | | | import java.nio.charset.Charset; |
| 44 | | | import java.util.logging.Logger; |
| 45 | | | |
| 46 | | | import javax.swing.text.Segment; |
| 47 | | | |
| 48 | | | import org.gjt.sp.jedit.Mode; |
| 49 | | | import org.gjt.sp.jedit.syntax.DefaultTokenHandler; |
| 50 | | | import org.gjt.sp.jedit.syntax.ModeProvider; |
| 51 | | | import org.gjt.sp.jedit.syntax.ParserRuleSet; |
| 52 | | | import org.gjt.sp.jedit.syntax.Token; |
| 53 | | | import org.gjt.sp.jedit.syntax.TokenMarker; |
| 54 | | | import org.gjt.sp.jedit.syntax.TokenMarker.LineContext; |
| 55 | | | import org.jcoderz.commons.util.Assert; |
| 56 | | | import org.jcoderz.commons.util.IoUtil; |
| 57 | | | |
| 58 | | | |
| 59 | | | |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | @author |
| 67 | | | |
| 68 | | | public class Syntax |
| 69 | | | { |
| 70 | | | private static final int MAX_RATIO_ILLEGAL_CHARACTERS = 10; |
| 71 | | | private static final int MAX_AVERAGE_LINE_LENGTH = 200; |
| 72 | | | private static final int BINARY_TEST_PROBE_CHARACTERS = 1024; |
| 73 | 0 | | private static final String CLASSNAME = Syntax.class.getName(); |
| 74 | 0 | | private static final Logger LOGGER = Logger.getLogger(CLASSNAME); |
| 75 | | | |
| 76 | | | private final Charset mSourceCharset; |
| 77 | | | private final int mTabWidth; |
| 78 | | | private final char[] mFileContent; |
| 79 | | | private int mFileContentPos; |
| 80 | | (1) | |
| 81 | 0 | | private Token mToken = emptyToken(); |
| 82 | | | private int mCurrentLineNumber; |
| 83 | | | private int mCurrentLinePos; |
| 84 | | | private Segment mCurrentLine; |
| 85 | | | private int mNumberOfLines; |
| 86 | | | |
| 87 | | | private final TokenMarker mTokenMarker; |
| 88 | 0 | | private final DefaultTokenHandler mTokenHandler |
| 89 | | | = new DefaultTokenHandler(); |
| 90 | 0 | | private LineContext mLineContext = null; |
| 91 | | | |
| 92 | | | private String mFirstLine; |
| 93 | | | |
| 94 | | | static |
| 95 | | | { |
| 96 | 0 | | SyntaxModeCatalogHandler.loadModes(); |
| 97 | 0 | | } |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | @param |
| 103 | | | @param |
| 104 | | | |
| 105 | | | @param |
| 106 | | | |
| 107 | | | @throws |
| 108 | | | |
| 109 | | | public Syntax (File in, Charset charSet, int tabWidth) |
| 110 | | | throws IOException |
| 111 | 0 | | { |
| 112 | 0 | | Assert.notNull(in, "in"); |
| 113 | 0 | | mSourceCharset = charSet == null ? Charset.defaultCharset() : charSet; |
| 114 | 0 | (2) | mTabWidth = tabWidth; |
| 115 | 0 | | mFileContent = readFile(in).toCharArray(); |
| 116 | 0 | | mFileContentPos = 0; |
| 117 | 0 | | mCurrentLineNumber = 0; |
| 118 | 0 | | mCurrentLine = null; |
| 119 | 0 | (3) | final Mode mode |
| 120 | | | = ModeProvider.instance.getModeForFile(in.getName(), mFirstLine); |
| 121 | 0 | | if (mode == null) |
| 122 | | | { |
| 123 | 0 | | if (isBinary(in.getAbsolutePath(), mFileContent)) |
| 124 | | | { |
| 125 | 0 | | throw new RuntimeException("No html view for binary file '" |
| 126 | | | + in.getAbsolutePath() + "'."); |
| 127 | | | } |
| 128 | | | |
| 129 | 0 | | LOGGER.fine("Could not find mode file for '" + in.getName() |
| 130 | | | + "'. Is the jedit-syntax.jar on the classpath?"); |
| 131 | 0 | | mTokenMarker = new TokenMarker(); |
| 132 | 0 | | mTokenMarker.addRuleSet(new ParserRuleSet("text", "MAIN")); |
| 133 | | | } |
| 134 | | | else |
| 135 | | | { |
| 136 | 0 | | mTokenMarker = mode.getTokenMarker(); |
| 137 | | | } |
| 138 | 0 | | } |
| 139 | | | |
| 140 | | | |
| 141 | | | |
| 142 | | | |
| 143 | | | @return |
| 144 | | | |
| 145 | | | public int getNumberOfLines () |
| 146 | | | { |
| 147 | 0 | | return mNumberOfLines; |
| 148 | | | } |
| 149 | | | |
| 150 | | | |
| 151 | | | |
| 152 | | | |
| 153 | | | {@link } |
| 154 | | | @return |
| 155 | | | |
| 156 | | | public int getCurrentLineNumber () |
| 157 | | | { |
| 158 | 0 | | return mCurrentLineNumber; |
| 159 | | | } |
| 160 | | | |
| 161 | | | |
| 162 | | | |
| 163 | | | @return |
| 164 | | | |
| 165 | | | public int getCurrentLinePos () |
| 166 | | | { |
| 167 | 0 | | return mCurrentLinePos; |
| 168 | | | } |
| 169 | | | |
| 170 | | | |
| 171 | | | |
| 172 | | | |
| 173 | | | {@link } |
| 174 | | | {@link } |
| 175 | | | @return |
| 176 | | | |
| 177 | | | public String getCurrentTokenType () |
| 178 | | | { |
| 179 | | | final String result; |
| 180 | 0 | | if (mToken.id == Token.END) |
| 181 | | | { |
| 182 | 0 | | result = null; |
| 183 | | | } |
| 184 | | | else |
| 185 | | | { |
| 186 | 0 | | result = Token.tokenToString(mToken.id); |
| 187 | | | } |
| 188 | 0 | | return result; |
| 189 | | | } |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | @return |
| 194 | | | |
| 195 | | | public int getCurrentTokenLength () |
| 196 | | | { |
| 197 | 0 | | return mToken.length; |
| 198 | | | } |
| 199 | | | |
| 200 | | | |
| 201 | | | |
| 202 | | | @return |
| 203 | | | |
| 204 | | | public String nextToken () |
| 205 | | | { |
| 206 | 0 | | if (mCurrentLine == null |
| 207 | | | || mToken.id == Token.END) |
| 208 | | | { |
| 209 | 0 | | nextLine(); |
| 210 | | | } |
| 211 | | | else |
| 212 | | | { |
| 213 | 0 | | mCurrentLinePos += mToken.length; |
| 214 | 0 | | mToken = mToken.next; |
| 215 | | | } |
| 216 | | | final String result; |
| 217 | 0 | | if (mCurrentLine.count == 0) |
| 218 | | | { |
| 219 | 0 | | mToken = emptyToken(); |
| 220 | 0 | | result = ""; |
| 221 | | | } |
| 222 | | | else |
| 223 | | | { |
| 224 | 0 | | if (mToken != null) |
| 225 | | | { |
| 226 | 0 | | result |
| 227 | | | = new String(mFileContent, |
| 228 | | | mCurrentLine.offset + mToken.offset, mToken.length); |
| 229 | | | } |
| 230 | | | else |
| 231 | | | { |
| 232 | 0 | | result = ""; |
| 233 | 0 | | mToken = emptyToken(); |
| 234 | | | } |
| 235 | | | } |
| 236 | 0 | | return result; |
| 237 | | | } |
| 238 | | | |
| 239 | | | |
| 240 | | | |
| 241 | | | |
| 242 | | | |
| 243 | | (4) | private void nextLine () |
| 244 | | | { |
| 245 | 0 | | if (mFileContentPos > mFileContent.length) |
| 246 | | | { |
| 247 | 0 | | mCurrentLine = null; |
| 248 | 0 | | mCurrentLineNumber = mNumberOfLines + 1; |
| 249 | | | } |
| 250 | | | else |
| 251 | | | { |
| 252 | 0 | | int pos = mFileContentPos; |
| 253 | | | while (pos < mFileContent.length |
| 254 | | | && mFileContent[pos] != '\n' |
| 255 | 0 | | && mFileContent[pos] != '\r') |
| 256 | | | { |
| 257 | 0 | | pos++; |
| 258 | | | } |
| 259 | 0 | | final int currentLineEnd = pos; |
| 260 | 0 | | if (pos < mFileContent.length |
| 261 | | | && (mFileContent[pos] == '\n' |
| 262 | | | || mFileContent[pos] == '\r')) |
| 263 | | | { |
| 264 | 0 | | pos++; |
| 265 | | | } |
| 266 | 0 | | if (pos < mFileContent.length |
| 267 | | | && mFileContent[pos - 1] != mFileContent[pos] |
| 268 | | | && (mFileContent[pos] == '\n' |
| 269 | | | || mFileContent[pos] == '\r')) |
| 270 | | | { |
| 271 | 0 | | pos++; |
| 272 | | | } |
| 273 | 0 | | mCurrentLine |
| 274 | | | = new Segment(mFileContent, |
| 275 | | | mFileContentPos, currentLineEnd - mFileContentPos); |
| 276 | 0 | | mCurrentLineNumber++; |
| 277 | 0 | | mFileContentPos = pos; |
| 278 | 0 | | mCurrentLinePos = 1; |
| 279 | | | |
| 280 | 0 | | if (mCurrentLine.count > 0) |
| 281 | | | { |
| 282 | 0 | | mTokenHandler.init(); |
| 283 | 0 | | mLineContext |
| 284 | | | = mTokenMarker.markTokens( |
| 285 | | | mLineContext, mTokenHandler, mCurrentLine); |
| 286 | 0 | | mToken = mTokenHandler.getTokens(); |
| 287 | | | } |
| 288 | | | else |
| 289 | | | { |
| 290 | 0 | | mToken = emptyToken(); |
| 291 | | | } |
| 292 | | | } |
| 293 | 0 | | } |
| 294 | | | |
| 295 | | | private String readFile (File in) |
| 296 | | | throws IOException |
| 297 | | | { |
| 298 | 0 | | String result = ""; |
| 299 | 0 | | final FileInputStream fis = new FileInputStream(in); |
| 300 | 0 | | Reader reader = null; |
| 301 | 0 | | LineNumberReader lnr = null; |
| 302 | | | try |
| 303 | | | { |
| 304 | 0 | | reader = new InputStreamReader(fis, mSourceCharset); |
| 305 | 0 | | lnr = new LineNumberReader(reader); |
| 306 | 0 | | result = IoUtil.readFully(lnr); |
| 307 | 0 | | mNumberOfLines = lnr.getLineNumber(); |
| 308 | 0 | | mFirstLine |
| 309 | | | = new BufferedReader(new StringReader(result)).readLine(); |
| 310 | | | } |
| 311 | | | finally |
| 312 | | | { |
| 313 | 0 | | IoUtil.close(lnr); |
| 314 | 0 | | IoUtil.close(reader); |
| 315 | 0 | | IoUtil.close(fis); |
| 316 | 0 | | } |
| 317 | 0 | | return result; |
| 318 | | | } |
| 319 | | | |
| 320 | | | private static Token emptyToken () |
| 321 | | | { |
| 322 | 0 | | return new Token(Token.END, 0, 0, null); |
| 323 | | | } |
| 324 | | | |
| 325 | | | static boolean isBinary (String name, char[] fileContent) |
| 326 | | | { |
| 327 | 0 | | int newLines = 0; |
| 328 | 0 | | int chars = 0; |
| 329 | 0 | | int illegal = 0; |
| 330 | | | int i; |
| 331 | 0 | | for (i = 0; i < fileContent.length |
| 332 | 0 | | && i < BINARY_TEST_PROBE_CHARACTERS; i++) |
| 333 | | | { |
| 334 | 0 | | final char c = fileContent[i]; |
| 335 | 0 | | if (c == '\n' || c == '\r') |
| 336 | | | { |
| 337 | 0 | | newLines++; |
| 338 | | | } |
| 339 | 0 | | else if (Character.isWhitespace(c)) |
| 340 | | | { |
| 341 | 0 | | chars++; |
| 342 | | | } |
| 343 | 0 | | else if (Character.isISOControl(c)) |
| 344 | | | { |
| 345 | 0 | | illegal++; |
| 346 | | | } |
| 347 | 0 | | else if (Character.isDefined(c)) |
| 348 | | | { |
| 349 | 0 | | chars++; |
| 350 | | | } |
| 351 | | | else |
| 352 | | | { |
| 353 | 0 | | illegal++; |
| 354 | | | } |
| 355 | | | } |
| 356 | 0 | | boolean result = false; |
| 357 | | | |
| 358 | 0 | | if (((newLines + 1) * MAX_AVERAGE_LINE_LENGTH) < i) |
| 359 | | | { |
| 360 | 0 | | result = true; |
| 361 | | | } |
| 362 | | | |
| 363 | 0 | | else if (illegal * MAX_RATIO_ILLEGAL_CHARACTERS > chars) |
| 364 | | | { |
| 365 | 0 | | result = true; |
| 366 | | | } |
| 367 | 0 | | LOGGER.finest("For file " + name + " tested " + i + " chars with " |
| 368 | | | + newLines + " newlines, " + chars + " legal chars, " |
| 369 | | | + illegal + " illegal chars. -> " |
| 370 | | | + (result ? "isBinary" : "isNotBinary")); |
| 371 | 0 | | return result; |
| 372 | | | } |
| 373 | | | |
| 374 | | | } |