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));
}
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;
}
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;
}
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;
}
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();
}
Aggregations