use of org.eclipse.mylyn.wikitext.util.LocationTrackingReader in project mylyn.docs by eclipse.
the class AbstractMarkupLanguage method processContent.
@Override
public void processContent(MarkupParser parser, String markupContent, boolean asDocument) {
initializeSyntax(false);
initProcessors();
ContentState state = newContentState();
state.setMarkupContent(markupContent);
DocumentBuilder builder = parser.getBuilder();
builder.setLocator(state);
@SuppressWarnings("resource") LocationTrackingReader reader = new LocationTrackingReader(new StringReader(markupContent));
try {
if (asDocument) {
builder.beginDocument();
}
Stack<Block> nestedBlocks = null;
Stack<LineState> lineStates = null;
String line;
Block currentBlock = null;
try {
line = reader.readLine();
int lineOffset = 0;
while (line != null) {
state.setLineNumber(reader.getLineNumber() + 1);
state.setLineOffset(reader.getLineOffset());
state.setLineCharacterOffset(lineOffset);
state.setLineSegmentEndOffset(0);
state.setLineLength(line.length());
for (; ; ) {
popClosedBlocks(nestedBlocks);
if (nestedBlocks != null && !nestedBlocks.isEmpty()) {
Block nestedParent = nestedBlocks.peek();
int closeOffset = nestedParent.findCloseOffset(line, lineOffset);
if (closeOffset != -1) {
if (closeOffset > lineOffset) {
String truncatedLine = line.substring(0, closeOffset);
if (lineStates == null) {
lineStates = new Stack<LineState>();
}
lineStates.push(new LineState(line, closeOffset));
line = truncatedLine;
} else {
if (currentBlock != null) {
currentBlock.setClosed(true);
currentBlock = null;
}
currentBlock = nestedBlocks.pop();
lineOffset = closeOffset;
state.setLineCharacterOffset(lineOffset);
}
}
}
if (currentBlock == null) {
if (nestedBlocks != null && !nestedBlocks.isEmpty()) {
Block nestedParent = nestedBlocks.peek();
if (nestedParent.canResume(line, lineOffset)) {
currentBlock = nestedBlocks.pop();
}
}
if (currentBlock == null) {
currentBlock = startBlock(line, lineOffset);
if (currentBlock == null) {
break;
}
currentBlock.setMarkupLanguage(this);
currentBlock.setState(state);
currentBlock.setParser(parser);
}
}
lineOffset = currentBlock.processLineContent(line, lineOffset);
if (currentBlock.isClosed()) {
currentBlock = null;
} else if (currentBlock.beginNesting()) {
if (nestedBlocks == null) {
nestedBlocks = new Stack<Block>();
}
nestedBlocks.push(currentBlock);
currentBlock = null;
}
if (lineOffset < line.length() && lineOffset >= 0) {
if (currentBlock != null) {
throw new IllegalStateException(String.format(// $NON-NLS-1$
"if a block does not fully process a line then it must be closed, at or near line %s lineOffset %s, block %s", reader.getLineNumber(), lineOffset, currentBlock.getClass().getName()));
}
} else {
break;
}
}
if (lineStates != null && !lineStates.isEmpty()) {
LineState lineState = lineStates.pop();
line = lineState.line;
lineOffset = lineState.lineOffset;
} else {
lineOffset = 0;
line = reader.readLine();
}
}
state.setLineNumber(reader.getLineNumber() + 1);
state.setLineOffset(reader.getLineOffset());
state.setLineCharacterOffset(0);
state.setLineLength(0);
} catch (IOException e) {
throw new IllegalStateException(e);
}
if (currentBlock != null && !currentBlock.isClosed()) {
currentBlock.setClosed(true);
}
if (nestedBlocks != null) {
while (!nestedBlocks.isEmpty()) {
Block block = nestedBlocks.pop();
if (!block.isClosed()) {
block.setClosed(true);
}
}
nestedBlocks = null;
}
if (asDocument) {
builder.endDocument();
}
builder.flush();
} finally {
builder.setLocator(null);
}
}
Aggregations