Search in sources :

Example 46 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.

the class ServiceLocator method getMarkupLanguage.

/**
 * get a markup language by name
 *
 * @param languageName
 *            the {@link MarkupLanguage#getName() name} of the markup language, or the fully qualified name of the
 *            class that implements the language
 * @return the language implementation
 * @throws IllegalArgumentException
 *             if the provided language name is null or if no implementation is available for the given language
 */
public MarkupLanguage getMarkupLanguage(final String languageName) throws IllegalArgumentException {
    // $NON-NLS-1$
    checkArgument(!Strings.isNullOrEmpty(languageName), "Must provide a languageName");
    // $NON-NLS-1$
    Pattern classNamePattern = Pattern.compile("\\s*([^\\s#]+)?#?.*");
    // first try Java services (jar-based)
    final List<String> names = new ArrayList<>();
    final List<MarkupLanguage> languages = new ArrayList<>();
    final MarkupLanguage[] result = new MarkupLanguage[1];
    loadMarkupLanguages(new MarkupLanguageVisitor() {

        public boolean accept(MarkupLanguage language) {
            if (languageName.equals(language.getName())) {
                result[0] = language;
                return false;
            }
            languages.add(language);
            names.add(language.getName());
            return true;
        }
    });
    if (result[0] != null) {
        return result[0];
    }
    // next attempt to load the markup language as if the language name is a fully qualified name
    Matcher matcher = classNamePattern.matcher(languageName);
    if (matcher.matches()) {
        String className = matcher.group(1);
        if (className != null) {
            // classloader issues
            for (MarkupLanguage language : languages) {
                if (className.equals(language.getClass().getName())) {
                    return language;
                }
            }
            try {
                Class<?> clazz = Class.forName(className, true, classLoader);
                if (MarkupLanguage.class.isAssignableFrom(clazz)) {
                    MarkupLanguage instance = (MarkupLanguage) clazz.newInstance();
                    return instance;
                }
            } catch (Exception e) {
            // ignore
            }
        }
    }
    Collections.sort(names);
    // specified language not found.
    // create a useful error message
    StringBuilder buf = new StringBuilder();
    for (String name : names) {
        if (buf.length() != 0) {
            // $NON-NLS-1$
            buf.append(", ");
        }
        buf.append('\'');
        buf.append(name);
        buf.append('\'');
    }
    throw new IllegalArgumentException(// $NON-NLS-1$
    MessageFormat.format(// $NON-NLS-1$
    Messages.getString("ServiceLocator.4"), languageName, buf.length() == 0 ? // $NON-NLS-1$
    Messages.getString("ServiceLocator.5") : // $NON-NLS-1$
    Messages.getString("ServiceLocator.6") + buf));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)

Example 47 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.

the class DocumentLocalReferenceValidationRule method findProblems.

@Override
public List<ValidationProblem> findProblems(String markup, int offset, int length) {
    MarkupLanguage markupLanguage = createMarkupLanguage();
    OutlineParserExtension outlineParser = new OutlineParserExtension(offset, length);
    outlineParser.setMarkupLanguage(markupLanguage);
    outlineParser.parse(markup);
    List<ValidationProblem> problems = null;
    if (outlineParser.references != null) {
        Set<String> anchorNames = outlineParser.idGenerator.getAnchorNames();
        for (LocalReference reference : outlineParser.references) {
            if (!anchorNames.contains(reference.name)) {
                if (problems == null) {
                    problems = new ArrayList<ValidationProblem>();
                }
                problems.add(new ValidationProblem(Severity.ERROR, // $NON-NLS-1$
                MessageFormat.format(// $NON-NLS-1$
                Messages.getString("DocumentLocalReferenceValidationRule.0"), reference.name), reference.offset, reference.length));
            }
        }
    }
    if (problems == null) {
        return Collections.emptyList();
    }
    return problems;
}
Also used : MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)

Example 48 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.

the class OutlineParser method parse.

public OutlineItem parse(OutlineItem root, String markup) {
    if (markup == null || markup.length() == 0 || markupLanguage == null) {
        root.setLength(markup == null ? 0 : markup.length());
        return root;
    }
    root.setLength(markup.length());
    MarkupLanguage markupLanguage = this.markupLanguage.clone();
    if (markupLanguage instanceof AbstractMarkupLanguage) {
        AbstractMarkupLanguage language = (AbstractMarkupLanguage) markupLanguage;
        language.setFilterGenerativeContents(true);
        language.setBlocksOnly(isBlocksOnly());
    }
    OutlineBuilder outlineBuilder = (OutlineBuilder) createOutlineUpdater(root);
    outlineBuilder.idGenerator.setGenerationStrategy(markupLanguage.getIdGenerationStrategy());
    MarkupParser markupParser = new MarkupParser();
    markupParser.setBuilder(outlineBuilder);
    markupParser.setMarkupLanguage(markupLanguage);
    markupParser.parse(markup);
    return root;
}
Also used : AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 49 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.

the class MarkupEditor method getMarkupLanguage.

public MarkupLanguage getMarkupLanguage() {
    IDocument document = getDocumentProvider().getDocument(getEditorInput());
    IDocumentPartitioner partitioner = document.getDocumentPartitioner();
    MarkupLanguage markupLanguage = null;
    if (partitioner instanceof FastMarkupPartitioner) {
        markupLanguage = ((FastMarkupPartitioner) partitioner).getMarkupLanguage();
    }
    return markupLanguage;
}
Also used : FastMarkupPartitioner(org.eclipse.mylyn.internal.wikitext.ui.editor.syntax.FastMarkupPartitioner) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) IDocument(org.eclipse.jface.text.IDocument)

Example 50 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.

the class MarkupEditor method updateOutline.

private void updateOutline() {
    if (!outlineDirty) {
        return;
    }
    if (!isSourceViewerValid()) {
        return;
    }
    // we maintain the outline even if the outline page is not in use, which allows us to use the outline for
    // content assist and other things
    MarkupLanguage markupLanguage = getMarkupLanguage();
    if (markupLanguage == null) {
        return;
    }
    final MarkupLanguage language = markupLanguage.clone();
    final Display display = getSourceViewer().getTextWidget().getDisplay();
    final String content = document.get();
    final int contentGeneration;
    synchronized (MarkupEditor.this) {
        contentGeneration = documentGeneration;
    }
    // we parse the outline in another thread so that the UI remains responsive
    Job parseOutlineJob = new // $NON-NLS-1$
    Job(// $NON-NLS-1$
    MarkupEditor.class.getSimpleName() + "#updateOutline") {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            outlineParser.setMarkupLanguage(language);
            if (shouldCancel()) {
                return Status.CANCEL_STATUS;
            }
            final OutlineItem rootItem = outlineParser.parse(content);
            if (shouldCancel()) {
                return Status.CANCEL_STATUS;
            }
            display.asyncExec(new Runnable() {

                public void run() {
                    updateOutline(contentGeneration, rootItem);
                }
            });
            return Status.OK_STATUS;
        }

        private boolean shouldCancel() {
            synchronized (MarkupEditor.this) {
                if (contentGeneration != documentGeneration) {
                    return true;
                }
            }
            return false;
        }
    };
    parseOutlineJob.setPriority(Job.INTERACTIVE);
    parseOutlineJob.setSystem(true);
    parseOutlineJob.schedule();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) UIJob(org.eclipse.ui.progress.UIJob) Job(org.eclipse.core.runtime.jobs.Job) Point(org.eclipse.swt.graphics.Point) Display(org.eclipse.swt.widgets.Display) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)

Aggregations

MarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)60 Test (org.junit.Test)18 AbstractMarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage)15 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)8 File (java.io.File)7 BuildException (org.apache.tools.ant.BuildException)7 CoreException (org.eclipse.core.runtime.CoreException)7 MockMarkupLanguage (org.eclipse.mylyn.internal.wikitext.MockMarkupLanguage)7 StringWriter (java.io.StringWriter)6 HashMap (java.util.HashMap)5 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)5 FileSet (org.apache.tools.ant.types.FileSet)5 OutlineItem (org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)5 Point (org.eclipse.swt.graphics.Point)5 IOException (java.io.IOException)4 IFile (org.eclipse.core.resources.IFile)4 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)4 TreeMap (java.util.TreeMap)3 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3