[14627] | 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 |
|
---|
| 19 | import java.io.File;
|
---|
| 20 | import java.util.Vector;
|
---|
| 21 | import org.apache.tools.ant.util.StringUtils;
|
---|
| 22 | import org.apache.tools.ant.util.regexp.RegexpMatcher;
|
---|
| 23 | import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Parser that will parse an output line of MAudit and return an
|
---|
| 27 | * interpreted violation if any.
|
---|
| 28 | *
|
---|
| 29 | * <p>
|
---|
| 30 | * MAudit is supposed to be configured with -fullpath so that it can
|
---|
| 31 | * correctly locate the file and attribute violation to the appropriate
|
---|
| 32 | * file (there might be several classes with the same name in
|
---|
| 33 | * different packages)
|
---|
| 34 | * </p>
|
---|
| 35 | *
|
---|
| 36 | */
|
---|
| 37 | final class MAuditParser {
|
---|
| 38 |
|
---|
| 39 | /** pattern used by maudit to report the error for a file */
|
---|
| 40 | /** RE does not seems to support regexp pattern with comments so i'm stripping it*/
|
---|
| 41 | // (?:file:)?((?#filepath).+):((?#line)\\d+)\\s*:\\s+((?#message).*)
|
---|
| 42 | private static final String AUDIT_PATTERN = "(?:file:)?(.+):(\\d+)\\s*:\\s+(.*)";
|
---|
| 43 |
|
---|
| 44 | /** matcher that will be used to extract the info from the line */
|
---|
| 45 | private final RegexpMatcher matcher;
|
---|
| 46 |
|
---|
| 47 | MAuditParser() {
|
---|
| 48 | /** the matcher should be the Oro one. I don't know about the other one */
|
---|
| 49 | matcher = (new RegexpMatcherFactory()).newRegexpMatcher();
|
---|
| 50 | matcher.setPattern(AUDIT_PATTERN);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Parse a line obtained from MAudit.
|
---|
| 55 | * @param line a line obtained from the MAudit output.
|
---|
| 56 | * @return the violation corresponding to the displayed line
|
---|
| 57 | * or <tt>null</tt> if it could not parse it. (might be a
|
---|
| 58 | * message info or copyright or summary).
|
---|
| 59 | */
|
---|
| 60 | Violation parseLine(String line) {
|
---|
| 61 | Vector matches = matcher.getGroups(line);
|
---|
| 62 | if (matches == null) {
|
---|
| 63 | return null;
|
---|
| 64 | }
|
---|
| 65 | final String file = (String) matches.elementAt(1);
|
---|
| 66 | Violation violation = new Violation();
|
---|
| 67 | violation.file = file;
|
---|
| 68 | violation.line = (String) matches.elementAt(2);
|
---|
| 69 | violation.error = (String) matches.elementAt(3);
|
---|
| 70 | // remove the pathname from any messages and let the classname only.
|
---|
| 71 | final int pos = file.lastIndexOf(File.separatorChar);
|
---|
| 72 | if ((pos != -1) && (pos != file.length() - 1)) {
|
---|
| 73 | String filename = file.substring(pos + 1);
|
---|
| 74 | violation.error = StringUtils.replace(violation.error,
|
---|
| 75 | "file:" + file, filename);
|
---|
| 76 | }
|
---|
| 77 | return violation;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /** the inner class used to report violation information */
|
---|
| 81 | static final class Violation {
|
---|
| 82 | String file;
|
---|
| 83 | String line;
|
---|
| 84 | String error;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|