Search in sources :

Example 1 with IFormatterChangeLogContrib

use of org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib in project linuxtools by eclipse.

the class ChangeLogExtensionManager method getFormatterContributor.

/**
 * Fetches formatterName formatter from extension, but if there exists a inline
 * formatter for entryFileName, then it uses that inline formatter.
 * @param entryFilePath The full file name.
 * @param formatterName The formatter name.
 * @return The changelog formatter.
 */
public IFormatterChangeLogContrib getFormatterContributor(String entryFilePath, String formatterName) {
    // extract just file name;
    String fileName;
    int lastDir = entryFilePath.lastIndexOf('/');
    if ((lastDir >= 0) && (lastDir + 1 <= entryFilePath.length()))
        fileName = entryFilePath.substring(lastDir + 1, entryFilePath.length());
    else
        fileName = entryFilePath;
    // We don't yet know which formatter to use
    formatterConfigElementToUse = null;
    if (formatterExtensions != null) {
        IConfigurationElement[] elements = formatterExtensions.getConfigurationElements();
        // cache the in-file formatters on the first run
        if (cachedInFileFormateters == null) {
            List<IConfigurationElement> inFileFormatters = new LinkedList<>();
            for (int i = 0; i < elements.length; i++) {
                IConfigurationElement formatterConfigElement = elements[i];
                if (// $NON-NLS-1$
                formatterConfigElement.getName().equals("formatter") && // $NON-NLS-1$
                formatterConfigElement.getAttribute("inFile").equalsIgnoreCase("true")) {
                    // $NON-NLS-1$
                    inFileFormatters.add(elements[i]);
                }
            }
            cachedInFileFormateters = inFileFormatters.toArray(new IConfigurationElement[] {});
        }
        // edited file
        for (int i = 0; i < cachedInFileFormateters.length; i++) {
            IConfigurationElement formatterConfigElement = cachedInFileFormateters[i];
            IConfigurationElement[] patternElementTmp = formatterConfigElement.getChildren();
            // error check
            if (patternElementTmp == null)
                continue;
            IConfigurationElement patternElement = patternElementTmp[0];
            if (patternElement.getAttribute("pattern") == null) {
                // $NON-NLS-1$
                ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, // $NON-NLS-1$
                Messages.getString("ChangeLog.ErrNonPattern"), // $NON-NLS-1$
                new Exception(Messages.getString("ChangeLog.ErrNonPattern"))));
            } else {
                // $NON-NLS-1$
                String filePattern = patternElement.getAttribute("pattern");
                try {
                    Pattern pattern = Pattern.compile(filePattern);
                    Matcher fileMatcher = pattern.matcher(fileName);
                    // pattern then we're done
                    if (fileMatcher.matches()) {
                        formatterConfigElementToUse = formatterConfigElement;
                        break;
                    }
                } catch (PatternSyntaxException e) {
                    ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e));
                }
            }
        }
        // prefered formatter
        if (formatterConfigElementToUse == null) {
            // whenever it changes
            if (cachedPrefFormatter == null || !// $NON-NLS-1$
            cachedPrefFormatter.getAttribute("name").equals(formatterName)) {
                for (int i = 0; i < elements.length; i++) {
                    IConfigurationElement formatterConfigElement = elements[i];
                    if (formatterConfigElement.getName().equals("formatter") && formatterConfigElement.getAttribute("inFile").equalsIgnoreCase("false")) {
                        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        if (// $NON-NLS-1$
                        formatterConfigElement.getAttribute("name").equals(formatterName))
                            cachedPrefFormatter = formatterConfigElement;
                        break;
                    }
                }
            }
            formatterConfigElementToUse = cachedPrefFormatter;
            if (formatterConfigElementToUse == null) {
                ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, // $NON-NLS-1$
                Messages.getString("ChangeLog.ErrRetrieveFormatter"), // $NON-NLS-1$
                new Exception(Messages.getString("ChangeLog.ErrRetrieveFormatter"))));
                return null;
            }
        }
    }
    try {
        return (IFormatterChangeLogContrib) formatterConfigElementToUse.createExecutableExtension(// $NON-NLS-1$
        "class");
    } catch (CoreException e) {
        ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e));
        e.printStackTrace();
    }
    return null;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) LinkedList(java.util.LinkedList) PatternSyntaxException(java.util.regex.PatternSyntaxException) CoreException(org.eclipse.core.runtime.CoreException) CoreException(org.eclipse.core.runtime.CoreException) IFormatterChangeLogContrib(org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with IFormatterChangeLogContrib

use of org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib in project linuxtools by eclipse.

the class PrepareChangeLogAction method outputMultipleEntryChangeLog.

private void outputMultipleEntryChangeLog(PatchFile pf, String[] functionGuess) {
    String defaultContent = null;
    if (pf.isNewfile())
        // $NON-NLS-1$
        defaultContent = Messages.getString("ChangeLog.NewFile");
    else if (pf.isRemovedFile())
        // $NON-NLS-1$
        defaultContent = Messages.getString("ChangeLog.RemovedFile");
    IPath entryPath = pf.getPath();
    String entryFileName = entryPath.toOSString();
    ChangeLogWriter clw = new ChangeLogWriter();
    // load settings from extensions + user pref.
    loadPreferences();
    // get file path from target file
    clw.setEntryFilePath(entryPath.toOSString());
    if (defaultContent != null)
        clw.setDefaultContent(defaultContent);
    // err check. do nothing if no file is being open/edited
    if (clw.getEntryFilePath() == "") {
        return;
    }
    // Check if formatter is internal or inline..if inline, use the
    // current active editor part, otherwise, we must find the external
    // ChangeLog file.
    IEditorPart changelog = null;
    // Before accessing the getFormatterConfigElement, the getFormatContibutor
    // method must be called to initialize.
    extensionManager.getFormatterContributor(clw.getEntryFilePath(), pref_Formatter);
    IConfigurationElement formatterConfigElement = extensionManager.getFormatterConfigElement();
    if (// $NON-NLS-1$
    formatterConfigElement.getAttribute("inFile").equalsIgnoreCase("true")) {
        // $NON-NLS-1$
        try {
            changelog = openEditor((IFile) pf.getResource());
            clw.setFormatter(extensionManager.getFormatterContributor(clw.getEntryFilePath(), pref_Formatter));
        } catch (Exception e) {
        // do nothing changelog will be null
        }
    } else {
        // external changelog
        // get formatter
        clw.setFormatter(extensionManager.getFormatterContributor(entryFileName, pref_Formatter));
        if (pf.isRemovedFile())
            changelog = getChangelogForRemovePath(entryPath);
        else
            changelog = getChangelog(entryFileName);
        // one.
        if (createChangeLog && changelog == null)
            changelog = askChangeLogLocation(entryPath.toOSString());
        if (changelog == null) {
            createChangeLog = false;
            return;
        }
    }
    if ((changelog instanceof ChangeLogEditor) && (!this.newEntryWritten)) {
        ChangeLogEditor editor = (ChangeLogEditor) changelog;
        // saving), treat it as a change log modification
        if (editor.isDirty())
            this.changeLogModified = true;
        editor.setForceNewLogEntry(!this.changeLogModified);
        this.newEntryWritten = true;
    }
    // select changelog
    clw.setChangelog(changelog);
    // write to changelog
    IFormatterChangeLogContrib formatter = clw.getFormatter();
    clw.setDateLine(formatter.formatDateLine(pref_AuthorName, pref_AuthorEmail));
    clw.setChangelogLocation(getDocumentLocation(clw.getChangelog(), true));
    // print multiple changelog entries with different
    // function guess names.  We default to an empty guessed name
    // if we have zero function guess names.
    int numFuncs = 0;
    // $NON-NLS-1$
    clw.setGuessedFName("");
    if (functionGuess.length > 0) {
        for (String guess : functionGuess) {
            if (!guess.trim().equals("")) {
                // $NON-NLS-1$
                ++numFuncs;
                clw.setGuessedFName(guess);
                clw.writeChangeLog();
            }
        }
    }
    // function guesses.
    if (numFuncs == 0) {
        clw.writeChangeLog();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ChangeLogEditor(org.eclipse.linuxtools.internal.changelog.core.editors.ChangeLogEditor) ChangeLogWriter(org.eclipse.linuxtools.internal.changelog.core.ChangeLogWriter) IFormatterChangeLogContrib(org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib) IEditorPart(org.eclipse.ui.IEditorPart) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TeamException(org.eclipse.team.core.TeamException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with IFormatterChangeLogContrib

use of org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib in project linuxtools by eclipse.

the class ChangeLogWriterTest method testGetSetFormatter.

@Test
public void testGetSetFormatter() {
    IFormatterChangeLogContrib formatter = new GNUFormat();
    clogWriter.setFormatter(formatter);
    assertEquals(formatter, clogWriter.getFormatter());
}
Also used : IFormatterChangeLogContrib(org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib) GNUFormat(org.eclipse.linuxtools.internal.changelog.core.formatters.GNUFormat) Test(org.junit.Test)

Aggregations

IFormatterChangeLogContrib (org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib)3 CoreException (org.eclipse.core.runtime.CoreException)2 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 IFile (org.eclipse.core.resources.IFile)1 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)1 IPath (org.eclipse.core.runtime.IPath)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 ChangeLogWriter (org.eclipse.linuxtools.internal.changelog.core.ChangeLogWriter)1 ChangeLogEditor (org.eclipse.linuxtools.internal.changelog.core.editors.ChangeLogEditor)1 GNUFormat (org.eclipse.linuxtools.internal.changelog.core.formatters.GNUFormat)1 TeamException (org.eclipse.team.core.TeamException)1 IEditorPart (org.eclipse.ui.IEditorPart)1