1. /*
  2. * Copyright 2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.metamata;
  18. import java.io.File;
  19. import java.util.Vector;
  20. import org.apache.tools.ant.util.StringUtils;
  21. import org.apache.tools.ant.util.regexp.RegexpMatcher;
  22. import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
  23. /**
  24. * Parser that will parse an output line of MAudit and return an
  25. * interpreted violation if any.
  26. *
  27. * <p>
  28. * MAudit is supposed to be configured with -fullpath so that it can
  29. * correctly locate the file and attribute violation to the appropriate
  30. * file (there might be several classes with the same name in
  31. * different packages)
  32. * </p>
  33. *
  34. */
  35. final class MAuditParser {
  36. /** pattern used by maudit to report the error for a file */
  37. /** RE does not seems to support regexp pattern with comments so i'm stripping it*/
  38. // (?:file:)?((?#filepath).+):((?#line)\\d+)\\s*:\\s+((?#message).*)
  39. private static final String AUDIT_PATTERN = "(?:file:)?(.+):(\\d+)\\s*:\\s+(.*)";
  40. /** matcher that will be used to extract the info from the line */
  41. private final RegexpMatcher matcher;
  42. MAuditParser() {
  43. /** the matcher should be the Oro one. I don't know about the other one */
  44. matcher = (new RegexpMatcherFactory()).newRegexpMatcher();
  45. matcher.setPattern(AUDIT_PATTERN);
  46. }
  47. /**
  48. * Parse a line obtained from MAudit.
  49. * @param line a line obtained from the MAudit output.
  50. * @return the violation corresponding to the displayed line
  51. * or <tt>null</tt> if it could not parse it. (might be a
  52. * message info or copyright or summary).
  53. */
  54. Violation parseLine(String line) {
  55. Vector matches = matcher.getGroups(line);
  56. if (matches == null) {
  57. return null;
  58. }
  59. final String file = (String) matches.elementAt(1);
  60. Violation violation = new Violation();
  61. violation.file = file;
  62. violation.line = (String) matches.elementAt(2);
  63. violation.error = (String) matches.elementAt(3);
  64. // remove the pathname from any messages and let the classname only.
  65. final int pos = file.lastIndexOf(File.separatorChar);
  66. if ((pos != -1) && (pos != file.length() - 1)) {
  67. String filename = file.substring(pos + 1);
  68. violation.error = StringUtils.replace(violation.error,
  69. "file:" + file, filename);
  70. }
  71. return violation;
  72. }
  73. /** the inner class used to report violation information */
  74. static final class Violation {
  75. String file;
  76. String line;
  77. String error;
  78. }
  79. }