Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8891a96fb775d15dd3702a24c74f13ac5d8b9603 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2012 Stefan Seelmann 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:
 *     Stefan Seelmann - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.wikitext.markdown.core.util;

import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;
import org.eclipse.mylyn.wikitext.core.parser.markup.Block;
import org.eclipse.mylyn.wikitext.core.parser.markup.ContentState;

/**
 * Adapter {@link Block} for {@link ReadAheadBlock}s.
 * 
 * @author Stefan Seelmann
 */
public class ReadAheadDispatcher extends Block {

	private final LookAheadReader lookAheadReader;

	private Block[] blocks;

	private Block dispatchedBlock;

	public ReadAheadDispatcher(Block... blocks) {
		this.blocks = blocks;
		this.lookAheadReader = new LookAheadReader();
	}

	@Override
	public boolean canStart(String line, int lineOffset) {
		dispatchedBlock = null;
		return true;
	}

	@Override
	protected int processLineContent(String line, int offset) {
		if (dispatchedBlock == null) {
			lookAheadReader.setContentState(getState());
			for (Block block : blocks) {
				if (block instanceof ReadAheadBlock) {
					ReadAheadBlock raBlock = ReadAheadBlock.class.cast(block);
					if (raBlock.canStart(line, offset, lookAheadReader)) {
						dispatchedBlock = block;
						break;
					}
				} else {
					if (block.canStart(line, offset)) {
						dispatchedBlock = block;
						break;
					}
				}
			}
		}

		int result = dispatchedBlock.processLine(line, offset);
		if (dispatchedBlock.isClosed()) {
			setClosed(true);
		}
		return result;
	}

	@Override
	public void setClosed(boolean closed) {
		dispatchedBlock.setClosed(closed);
		super.setClosed(closed);
	}

	@Override
	public void setState(ContentState state) {
		for (Block block : blocks) {
			block.setState(state);
		}
		super.setState(state);
	}

	@Override
	public void setParser(MarkupParser parser) {
		for (Block block : blocks) {
			block.setParser(parser);
		}
		super.setParser(parser);
	}

	@Override
	public Block clone() {
		ReadAheadDispatcher clone = (ReadAheadDispatcher) super.clone();
		Block[] clonedBlocks = new Block[blocks.length];
		int i = 0;
		for (Block block : blocks) {
			clonedBlocks[i++] = block.clone();
		}
		clone.blocks = clonedBlocks;
		return clone;
	}

}

Back to the top