/*******************************************************************************
 * Copyright (c) 2007, 2008 David Green and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     David Green - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.internal.wikitext.twiki.core.validation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.mylyn.wikitext.core.validation.ValidationProblem;
import org.eclipse.mylyn.wikitext.core.validation.ValidationRule;
import org.eclipse.mylyn.wikitext.core.validation.ValidationProblem.Severity;

/**
 * TWiki lists markup must have spaces in multiples of 3 preceding the 
 * markup (*,i,I,1,a,A) 
 *  
 * @author David Green
 */
public class ListWhitespaceValidationRule extends ValidationRule {

	private static final Pattern almostListPattern = Pattern.compile("^((?: |\t)*)(\\*|((i|I|a|A|1)\\.))(\\S)?",Pattern.MULTILINE);
	
	public ListWhitespaceValidationRule() {
	}

	@Override
	public ValidationProblem findProblem(String markup, int offset, int length) {
		Matcher matcher = almostListPattern.matcher(markup);
		if (offset > 0 || length != markup.length()) {
			matcher.region(offset, offset+length);
		}
		while (matcher.find()) {
			String spaces = matcher.group(1);
			if (spaces == null || spaces.length() == 0 || (spaces.length()%3) != 0 || containsNonSpace(spaces)) {
				int problemOffset = matcher.start();
				int problemLength = Math.max(2,problemOffset-matcher.end(2));
				return new ValidationProblem(Severity.WARNING,"Lists must be indented with spaces in multiples of three",problemOffset,problemLength);
			}
			String after = matcher.group(5);
			if (after != null) {
				int problemOffset = matcher.start();
				int problemLength = Math.max(2,problemOffset-matcher.end(2));
				return new ValidationProblem(Severity.WARNING,"List item markup must be followed by whitespace",problemOffset,problemLength);
			}
		}
		return null;
	}

	private boolean containsNonSpace(String spaces) {
		for (int x = 0;x<spaces.length();++x) {
			if (spaces.charAt(x) != ' ') {
				return true;
			}
		}
		return false;
	}
}

@@ -948,20 +948,16 @@
 ing the 
-%0A * 
 markup (
@@ -972,14 +972,12 @@
 a,A)
- 
 %0A * 
- 
 %0A * 
@@ -1177,16 +1177,20 @@
 (%5C%5CS)?%22,
+%0A%09%09%09
 Pattern.
@@ -1201,17 +1201,16 @@
 ILINE);%0A
-%09
 %0A%09public
@@ -1473,17 +1473,19 @@
 , offset
-+
+ + 
 length);
@@ -1622,9 +1622,11 @@
 th()
-%25
+ %25 
 3) !
@@ -1740,36 +1740,39 @@
 x(2,
-problemOffset-matcher.end(2)
+ matcher.end(2) - problemOffset
 );%0A%09
@@ -1816,24 +1816,31 @@
 ity.WARNING,
+%0A%09%09%09%09%09%09
 %22Lists must 
@@ -1886,16 +1886,17 @@
  three%22,
+ 
 problemO
@@ -1893,32 +1893,33 @@
 , problemOffset,
+ 
 problemLength);%0A
@@ -2063,36 +2063,39 @@
 x(2,
-problemOffset-matcher.end(2)
+ matcher.end(2) - problemOffset
 );%0A%09
@@ -2143,16 +2143,17 @@
 WARNING,
+ 
 %22List it
@@ -2194,16 +2194,23 @@
 espace%22,
+%0A%09%09%09%09%09%09
 problemO
@@ -2215,16 +2215,17 @@
 mOffset,
+ 
 problemL
@@ -2332,10 +2332,13 @@
 = 0;
-x%3C
+ x %3C 
 spac
@@ -2349,16 +2349,17 @@
 ength();
+ 
 ++x) %7B%0A%09

fix to length calculation