summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/ioutils/format/IterationDirective.java
blob: e13cb1c53b48a35ce63583bc037869f8eb772340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package bjc.utils.ioutils.format;

import bjc.utils.esodata.Tape;

import java.util.IllegalFormatConversionException;
import java.util.regex.Matcher;

class IterationDirective implements Directive {

	@Override
	public void format(StringBuffer sb, Object item, CLModifiers mods, CLParameters arrParams, Tape<Object> tParams,
			Matcher dirMatcher, CLFormatter fmt) {
		CLFormatter.checkItem(item, '{');

		StringBuffer condBody = new StringBuffer();

		while(dirMatcher.find()) {
			/* Process a list of clauses. */
			String dirName = dirMatcher.group("name");

			if(dirName != null) {
				/* Append everything up to this directive. */
				dirMatcher.appendReplacement(condBody, "");

				if(dirName.equals("}")) {
					/* End the iteration. */
					break;
				}

				/* Not a special directive. */
				condBody.append(dirMatcher.group());
			}
		}

		String frmt = condBody.toString();
		Object iter = item;

		if(frmt.equals("")) {
			/* Grab an argument. */
			if(!(item instanceof String)) {
				throw new IllegalFormatConversionException('{', String.class);
			}

			frmt = (String) item;

			if(!tParams.right()) {
				throw new IllegalArgumentException("Not enough parameters to '{' directive");
			}

			iter = tParams.item();
		}

		int maxItr = Integer.MAX_VALUE;
		
		if(arrParams.length() > 0) {
			maxItr = arrParams.getInt(0, "maximum iterations", '{');
		}
		
		if(mods.atMod && mods.colonMod) {

		} else if(mods.atMod) {

		} else if(mods.colonMod) {
			if(!(item instanceof Iterable<?>)) {
				throw new IllegalFormatConversionException('{', item.getClass());
			}
		} else {
			if(!(item instanceof Iterable<?>)) {
				throw new IllegalFormatConversionException('{', item.getClass());
			}
		}

		tParams.right();
	}

}