blob: 37d166266fea2fe89ed00425f9f6140a31baf87f (
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
104
105
106
107
108
109
|
/*******************************************************************************
* Copyright (c) 2012, 2013 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.mylyn.internal.wikitext.markdown.core.block.NestableBlock;
import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;
import org.eclipse.mylyn.wikitext.core.parser.markup.ContentState;
/**
* Adapter {@link NestableBlock} for {@link ReadAheadBlock}s.
*
* @author Stefan Seelmann
*/
public class ReadAheadDispatcher extends NestableBlock {
private final LookAheadReader lookAheadReader;
private List<NestableBlock> blocks;
private NestableBlock dispatchedBlock;
public ReadAheadDispatcher(NestableBlock... blocks) {
this.blocks = cloneBlocks(Arrays.asList(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 (NestableBlock 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 (NestableBlock block : blocks) {
block.setState(state);
}
super.setState(state);
}
@Override
public void setParser(MarkupParser parser) {
for (NestableBlock block : blocks) {
block.setParser(parser);
}
super.setParser(parser);
}
@Override
public NestableBlock clone() {
ReadAheadDispatcher clone = (ReadAheadDispatcher) super.clone();
clone.blocks = cloneBlocks(blocks);
return clone;
}
private List<NestableBlock> cloneBlocks(List<NestableBlock> blocks) {
List<NestableBlock> clonedBlocks = new ArrayList<NestableBlock>();
for (NestableBlock block : blocks) {
clonedBlocks.add(block.clone());
}
return clonedBlocks;
}
}
|