PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/carrot-jdk6-jnlp-macosx/src/common/share/classes/com/sun/deploy/net/proxy/RemoveCommentReader.java

https://github.com/carrot-garden/carrot-jnlper
Java | 121 lines | 83 code | 13 blank | 25 comment | 9 complexity | 3a37b4d359cbfbc206f42364fc6d2f16 MD5 | raw file
  1. /*
  2. * @(#)RemoveCommentReader.java 1.15 10/03/24
  3. *
  4. * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
  5. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.deploy.net.proxy;
  8. import java.io.FilterReader;
  9. import java.io.Reader;
  10. import java.io.IOException;
  11. /**
  12. * RemoveCommentReader is a simple FilterReader that strips comment out of a
  13. * stream of characters. It will strip the '//' comment and the '/*' comment.
  14. */
  15. public final class RemoveCommentReader extends FilterReader {
  16. /** A trivial constructor. Just initialize our superclass */
  17. public RemoveCommentReader(Reader in) {
  18. super(in);
  19. }
  20. // Use to remember whether we are "inside" the '//'
  21. boolean inComment1 = false;
  22. // Use to remember whether we are "inside" the '/*'
  23. boolean inComment2 = false;
  24. // Use to remember whether we are "inside" the quote (", ')
  25. boolean inQuote = false;
  26. /**
  27. * This is the implementation of the no-op read() method of FilterReader.
  28. * It calls in.read() to get a buffer full of characters, then strips
  29. * out the comments.
  30. **/
  31. public int read(char[] buf, int from, int len) throws IOException {
  32. int numchars = 0; // how many characters have been read
  33. // Loop, because we might read a bunch of characters, then strip them
  34. // all out, leaving us with zero characters to return.
  35. while (numchars == 0) {
  36. numchars = in.read(buf, from, len); // Read characters
  37. if (numchars == -1) // Check if EOF
  38. return -1;
  39. // Loop through the characters we read, stripping out comments.
  40. // Characters not in tags are copied over any previous comment in
  41. // the buffer.
  42. int last = from; // Index of last non-comment character
  43. for (int i=from; i < from + numchars; i++) {
  44. if (!inComment1 && !inComment2) {
  45. // Not in comment
  46. if (buf[i] == '"' || buf[i] == '\'') {
  47. // found a quote
  48. inQuote = !inQuote;
  49. }
  50. else if (!inQuote && buf[i] == '/') {
  51. if (buf[i+1] == '/') { // Comment '//'
  52. inComment1 = true;
  53. i++;
  54. continue;
  55. }
  56. else if (buf[i+1] == '*') { // Comment '/*'
  57. inComment2 = true;
  58. i++;
  59. continue;
  60. }
  61. }
  62. if (Character.isWhitespace(buf[i])) // Skip whitespaces
  63. buf[last++] = ' ';
  64. else
  65. buf[last++] = buf[i];
  66. }
  67. else
  68. {
  69. // inComment1 || inComment2
  70. if (inComment1) { // End Comment '//'
  71. if (buf[i] == '\n')
  72. inComment1 = false;
  73. }
  74. else if (inComment2)
  75. {
  76. if (buf[i] == '*' && buf[i+1] == '/') // End Comment '/*'
  77. {
  78. inComment2 = false;
  79. i++;
  80. }
  81. }
  82. }
  83. }
  84. numchars = last - from; // Figure out how many characters remain
  85. // And if it is more than zero characters
  86. // Then return that number
  87. }
  88. return numchars;
  89. }
  90. /**
  91. * This is another no-op read() method we have to implement. We implement
  92. * it in terms of the method above. Our superclass implements the
  93. * remaining read() methods in terms of these two.
  94. **/
  95. public int read() throws IOException {
  96. char[] buf = new char[1];
  97. int result = read(buf, 0, 1);
  98. if (result == -1)
  99. return -1;
  100. else
  101. return (int)buf[0];
  102. }
  103. }