Skip to content

Commit

Permalink
Regex pattern used for parsing the CruiseControl command output is th…
Browse files Browse the repository at this point in the history
…readsafe (according to Javadoc), so I am making the Pattern a final static variable to try to improve the performance a bit more.
  • Loading branch information
pvince committed Nov 11, 2016
1 parent 0d73ca3 commit e835e86
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/main/java/hudson/scm/SurroundSCMChangeLogParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@

public class SurroundSCMChangeLogParser extends ChangeLogParser {

/**
* Standard format for a cruisecontrol output is:
* {@code <Mainline/Repository/Path/To/File><Filename.txt><21><Check in><20161103144114><Comment for the action><LastName, FirstName><email@address.com> }
*/
private Pattern changeRegex = null;
private final static String CC_LINE_PATTERN = "<(.*)><(.*)><(.*)><(.*)><(.*)><(.*)><(.*)><(.*)>";

private final static Pattern CRUISECONTROL_PATTERN = Pattern.compile(CC_LINE_PATTERN);

/**
* Parses the Surround SCM changelogFile, which is generated by the Surround SCM CLI's cruisecontrol command.
* {@inheritDoc}
Expand All @@ -25,7 +22,6 @@ public class SurroundSCMChangeLogParser extends ChangeLogParser {
public ChangeLogSet<? extends Entry> parse(Run build, RepositoryBrowser<?> browser, File changelogFile) {
//open the changelog File
SurroundSCMChangeLogSet cls = new SurroundSCMChangeLogSet(build, browser);
changeRegex = Pattern.compile(CC_LINE_PATTERN);
commonParseChangeLog(cls, changelogFile);
return cls;
}
Expand All @@ -36,17 +32,23 @@ public ChangeLogSet<? extends Entry> parse(AbstractBuild build,
File changelogFile) {
//open the changelog File
SurroundSCMChangeLogSet cls = new SurroundSCMChangeLogSet(build);
changeRegex = Pattern.compile(CC_LINE_PATTERN);
commonParseChangeLog(cls, changelogFile);

return cls;
}

public void commonParseChangeLog(SurroundSCMChangeLogSet cls, File changelogFile) {
/**
* Handles the dirty work of actually parsing the changelog file.
*
* @param cls What changelogset to add newly parsed entries too.
* @param changelogFile What changelog file to use as a source for the parsing.
*/
private void commonParseChangeLog(SurroundSCMChangeLogSet cls, File changelogFile) {
String line;
BufferedReader br = null;

InputStreamReader is = null;
InputStreamReader is;

try{
is = new InputStreamReader(new FileInputStream(changelogFile), "UTF-8");

Expand Down Expand Up @@ -79,11 +81,16 @@ public void commonParseChangeLog(SurroundSCMChangeLogSet cls, File changelogFile
* @param cls SurroundSCMChangeLogSet this entry will be added to.
* @param cruiseControlLine Line to parse
* @return Returns a new {@link hudson.scm.SurroundSCMChangeLogSet.SurroundSCMChangeLogSetEntry} created based on the
* passed in line.
* passed in line. This could return a 'null' result if we fail to parse the line.
*/
public SurroundSCMChangeLogSet.SurroundSCMChangeLogSetEntry parseCCLine(SurroundSCMChangeLogSet cls, String cruiseControlLine ) {
Matcher changeMatcher = changeRegex.matcher(cruiseControlLine);

private SurroundSCMChangeLogSet.SurroundSCMChangeLogSetEntry parseCCLine(SurroundSCMChangeLogSet cls, String cruiseControlLine ) {
Matcher changeMatcher = CRUISECONTROL_PATTERN.matcher(cruiseControlLine);

// The SSCM 'cruisecontrol' output is supposed to output 8 blocks of "<....>" per line. However, due to a defect
// (which will hopefully be fixed soon) there is a possibility that the contents of hte "<...>" block could themselves
// contain "<>" characters. This breaks the parsing of the line and could result in garbage output.
// To try to address that situation in a small way we are going to check to make sure we found exactly 8 'groups'.
// If we found more // less then we will consider it a parsing failure, and return a 'null' entry.
if(changeMatcher.find() && changeMatcher.groupCount() == 8) {
String repository = changeMatcher.group(1);
String filename = changeMatcher.group(2);
Expand Down

0 comments on commit e835e86

Please sign in to comment.