Search in sources :

Example 1 with ValidationRule

use of org.eclipse.mylyn.wikitext.validation.ValidationRule in project mylyn.docs by eclipse.

the class WikiTextExtensionPointReader method initializeValidationRules.

private void initializeValidationRules() {
    initializeMarkupLanguages();
    synchronized (this) {
        if (validationRulesByLanguageName == null) {
            Map<String, ValidationRules> validationRulesByLanguageName = new HashMap<>();
            IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(getExtensionPointNamespace(), EXTENSION_VALIDATION_RULES);
            if (extensionPoint != null) {
                IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements();
                for (IConfigurationElement element : configurationElements) {
                    try {
                        // $NON-NLS-1$
                        String markupLanguage = element.getAttribute("markupLanguage");
                        if (markupLanguage == null || markupLanguage.length() == 0) {
                            // $NON-NLS-1$
                            throw new Exception(Messages.getString("WikiTextExtensionPointReader.4"));
                        }
                        if (!languageByName.containsKey(markupLanguage)) {
                            throw new Exception(MessageFormat.format(Messages.getString("WikiTextExtensionPointReader.5"), // $NON-NLS-1$
                            languageByName));
                        }
                        Object extension;
                        try {
                            // $NON-NLS-1$
                            extension = element.createExecutableExtension("class");
                        } catch (CoreException e) {
                            getLog().log(e.getStatus());
                            continue;
                        }
                        if (!(extension instanceof ValidationRule)) {
                            throw new Exception(MessageFormat.format(Messages.getString("WikiTextExtensionPointReader.7"), // $NON-NLS-1$
                            extension.getClass().getName()));
                        }
                        ValidationRules rules = validationRulesByLanguageName.get(markupLanguage);
                        if (rules == null) {
                            rules = new ValidationRules();
                            validationRulesByLanguageName.put(markupLanguage, rules);
                        }
                        rules.addValidationRule((ValidationRule) extension);
                    } catch (Exception e) {
                        log(IStatus.ERROR, // $NON-NLS-1$
                        MessageFormat.format(// $NON-NLS-1$
                        Messages.getString("WikiTextExtensionPointReader.8"), element.getDeclaringExtension().getContributor().getName(), EXTENSION_VALIDATION_RULES, e.getMessage()), e);
                    }
                }
            }
            // now that we have the basic validation rules, check for language extensions and connect the hierarchy
            // first ensure that all language names have templates defined
            Set<String> languageNames = getMarkupLanguageNames();
            for (String languageName : languageNames) {
                ValidationRules rules = validationRulesByLanguageName.get(languageName);
                if (rules == null) {
                    rules = new ValidationRules();
                    validationRulesByLanguageName.put(languageName, rules);
                }
            }
            // next connect the hierarchy
            for (String languageName : languageNames) {
                MarkupLanguage markupLanguage = getMarkupLanguage(languageName);
                if (markupLanguage != null && markupLanguage.getExtendsLanguage() != null) {
                    ValidationRules languageRules = validationRulesByLanguageName.get(languageName);
                    ValidationRules parentLanguageRules = validationRulesByLanguageName.get(markupLanguage.getExtendsLanguage());
                    languageRules.setParent(parentLanguageRules);
                }
            }
            this.validationRulesByLanguageName = validationRulesByLanguageName;
        }
    }
}
Also used : HashMap(java.util.HashMap) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) CoreException(org.eclipse.core.runtime.CoreException) ValidationRule(org.eclipse.mylyn.wikitext.validation.ValidationRule) ValidationRules(org.eclipse.mylyn.wikitext.validation.ValidationRules) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) CoreException(org.eclipse.core.runtime.CoreException) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)

Aggregations

HashMap (java.util.HashMap)1 CoreException (org.eclipse.core.runtime.CoreException)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)1 MarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)1 ValidationRule (org.eclipse.mylyn.wikitext.validation.ValidationRule)1 ValidationRules (org.eclipse.mylyn.wikitext.validation.ValidationRules)1