use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder in project mylyn.docs by eclipse.
the class CommonMarkAsserts method toComparisonValue.
private static String toComparisonValue(String html) {
if (html == null) {
return null;
}
try {
StringWriter out = new StringWriter();
DocumentBuilder builder = createDocumentBuilder(out);
HtmlParser.instance().parse(new InputSource(new StringReader(html)), builder);
return out.toString().trim();
} catch (IOException | SAXException e) {
throw new RuntimeException(html, e);
}
}
use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder in project mylyn.docs by eclipse.
the class InlineParser method toStringContent.
static String toStringContent(List<Inline> contents) {
final StringBuilder stringBuilder = new StringBuilder();
DocumentBuilder altDocumentBuilder = new NoOpDocumentBuilder() {
@Override
public void characters(String text) {
stringBuilder.append(text);
}
@Override
public void entityReference(String entity) {
stringBuilder.append(Objects.firstNonNull(EntityReferences.instance().equivalentString(entity), ""));
}
};
for (Inline inline : contents) {
inline.emit(altDocumentBuilder);
}
return stringBuilder.toString();
}
use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder 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);
}
}
use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder in project mylyn.docs by eclipse.
the class MultiplexingDocumentBuilderTest method flush.
@Test
public void flush() {
final AtomicBoolean flushed = new AtomicBoolean();
DocumentBuilder delegate = new NoOpDocumentBuilder() {
@Override
public void flush() {
flushed.set(true);
}
};
multiplexer = new MultiplexingDocumentBuilder(delegate);
multiplexer.flush();
assertTrue(flushed.get());
}
use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder in project mylyn.docs by eclipse.
the class MultiplexingDocumentBuilderTest method setLocator.
@Test
public void setLocator() {
DocumentBuilder delegateOne = new NoOpDocumentBuilder();
DocumentBuilder delegateTwo = new NoOpDocumentBuilder();
Locator locator = new ContentState();
multiplexer = new MultiplexingDocumentBuilder(delegateOne, delegateTwo);
multiplexer.setLocator(locator);
assertSame(locator, delegateOne.getLocator());
assertSame(locator, delegateTwo.getLocator());
}
Aggregations