Search in sources :

Example 26 with DocumentBuilder

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) StringWriter(java.io.StringWriter) DocumentBuilder(org.eclipse.mylyn.wikitext.parser.DocumentBuilder) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SimplifiedHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder) StringReader(java.io.StringReader) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 27 with DocumentBuilder

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();
}
Also used : NoOpDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder) DocumentBuilder(org.eclipse.mylyn.wikitext.parser.DocumentBuilder) NoOpDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder)

Example 28 with DocumentBuilder

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);
    }
}
Also used : LocationTrackingReader(org.eclipse.mylyn.wikitext.util.LocationTrackingReader) IOException(java.io.IOException) Stack(java.util.Stack) DocumentBuilder(org.eclipse.mylyn.wikitext.parser.DocumentBuilder) StringReader(java.io.StringReader)

Example 29 with DocumentBuilder

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NoOpDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder) NoOpDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder) DocumentBuilder(org.eclipse.mylyn.wikitext.parser.DocumentBuilder) MultiplexingDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.MultiplexingDocumentBuilder) EventDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.EventDocumentBuilder) MultiplexingDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.MultiplexingDocumentBuilder) Test(org.junit.Test)

Example 30 with DocumentBuilder

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());
}
Also used : NoOpDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder) Locator(org.eclipse.mylyn.wikitext.parser.Locator) NoOpDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder) DocumentBuilder(org.eclipse.mylyn.wikitext.parser.DocumentBuilder) MultiplexingDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.MultiplexingDocumentBuilder) EventDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.EventDocumentBuilder) MultiplexingDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.MultiplexingDocumentBuilder) ContentState(org.eclipse.mylyn.wikitext.parser.markup.ContentState) Test(org.junit.Test)

Aggregations

DocumentBuilder (org.eclipse.mylyn.wikitext.parser.DocumentBuilder)31 StringWriter (java.io.StringWriter)20 Test (org.junit.Test)15 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)9 HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)9 EventDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.EventDocumentBuilder)7 Writer (java.io.Writer)6 NoOpDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder)6 HtmlLanguage (org.eclipse.mylyn.wikitext.html.HtmlLanguage)5 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)3 Line (org.eclipse.mylyn.wikitext.commonmark.internal.Line)2 ProcessingContext (org.eclipse.mylyn.wikitext.commonmark.internal.ProcessingContext)2 SimplifiedHtmlDocumentBuilder (org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder)2 Locator (org.eclipse.mylyn.wikitext.parser.Locator)2 MultiplexingDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.MultiplexingDocumentBuilder)2 InputSource (org.xml.sax.InputSource)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1