Search in sources :

Example 86 with Problem

use of com.liferay.blade.api.Problem in project liferay-ide by liferay.

the class FindBreakingChangesPage method _createColumns.

private void _createColumns(final TableViewer problemsViewer) {
    final String[] titles = { "Resolved", "Line", "Problem" };
    TableViewerColumn col0 = _createTableViewerColumn(titles[0], problemsViewer);
    col0.getColumn().pack();
    col0.setEditingSupport(new EditingSupport(problemsViewer) {

        @Override
        protected boolean canEdit(Object element) {
            return true;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return new CheckboxCellEditor(problemsViewer.getTable());
        }

        @Override
        protected Object getValue(Object element) {
            return ((Problem) element).getStatus() == Problem.STATUS_RESOLVED;
        }

        @Override
        protected void setValue(Object element, Object value) {
            if (value == Boolean.TRUE) {
                new MarkDoneAction().run((Problem) element, problemsViewer);
            } else {
                new MarkUndoneAction().run((Problem) element, problemsViewer);
            }
        }
    });
    col0.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public Image getImage(Object element) {
            Problem p = (Problem) element;
            if (p.getStatus() == Problem.STATUS_RESOLVED) {
                return _imageChecked;
            } else {
                return _imageUnchecked;
            }
        }

        @Override
        public String getText(Object element) {
            return null;
        }
    });
    TableViewerColumn col1 = _createTableViewerColumn(titles[1], problemsViewer);
    col1.getColumn().pack();
    col1.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            Problem p = (Problem) element;
            return p.lineNumber > -1 ? (p.lineNumber + "") : "";
        }
    });
    TableViewerColumn col2 = _createTableViewerColumn(titles[2], problemsViewer);
    col2.getColumn().pack();
    col2.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            Problem p = (Problem) element;
            return p.title;
        }

        @Override
        public void update(ViewerCell cell) {
            super.update(cell);
            Table table = problemsViewer.getTable();
            table.getColumn(2).pack();
        }
    });
    TableColumnLayout tableLayout = new TableColumnLayout();
    TableColumn column0 = col0.getColumn();
    TableColumn column1 = col1.getColumn();
    TableColumn column2 = col2.getColumn();
    tableLayout.setColumnData(column0, new ColumnWeightData(50, column0.getWidth()));
    tableLayout.setColumnData(column1, new ColumnWeightData(50, column1.getWidth()));
    tableLayout.setColumnData(column2, new ColumnWeightData(100, column2.getWidth()));
    Table table = problemsViewer.getTable();
    table.getParent().setLayout(tableLayout);
}
Also used : ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) MarkDoneAction(com.liferay.ide.project.ui.migration.MarkDoneAction) Table(org.eclipse.swt.widgets.Table) CheckboxCellEditor(org.eclipse.jface.viewers.CheckboxCellEditor) CellEditor(org.eclipse.jface.viewers.CellEditor) CheckboxCellEditor(org.eclipse.jface.viewers.CheckboxCellEditor) EditingSupport(org.eclipse.jface.viewers.EditingSupport) MarkUndoneAction(com.liferay.ide.project.ui.migration.MarkUndoneAction) Image(org.eclipse.swt.graphics.Image) ViewerCell(org.eclipse.jface.viewers.ViewerCell) TableColumn(org.eclipse.swt.widgets.TableColumn) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) Problem(com.liferay.blade.api.Problem) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn)

Example 87 with Problem

use of com.liferay.blade.api.Problem in project liferay-ide by liferay.

the class ProjectMigrationService method reportProblems.

@Override
public void reportProblems(List<Problem> problems, int detail, String format, Object... args) {
    Reporter reporter = null;
    try {
        Collection<ServiceReference<Reporter>> references = _context.getServiceReferences(Reporter.class, "(format=" + format + ")");
        if (ListUtil.isNotEmpty(references)) {
            reporter = _context.getService(references.iterator().next());
        } else {
            ServiceReference<Reporter> sr = _context.getServiceReference(Reporter.class);
            reporter = _context.getService(sr);
        }
    } catch (InvalidSyntaxException ise) {
        ise.printStackTrace();
    }
    OutputStream fos = null;
    try {
        if (ListUtil.isNotEmpty(args)) {
            if (args[0] instanceof File) {
                File outputFile = (File) args[0];
                outputFile.getParentFile().mkdirs();
                outputFile.createNewFile();
                fos = Files.newOutputStream(outputFile.toPath());
            } else if (args[0] instanceof OutputStream) {
                fos = (OutputStream) args[0];
            }
        }
        if (ListUtil.isNotEmpty(problems)) {
            reporter.beginReporting(detail, fos);
            for (Problem problem : problems) {
                reporter.report(problem);
            }
            reporter.endReporting();
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
Also used : Reporter(com.liferay.blade.api.Reporter) OutputStream(java.io.OutputStream) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Problem(com.liferay.blade.api.Problem) IOException(java.io.IOException) File(java.io.File) ServiceReference(org.osgi.framework.ServiceReference)

Example 88 with Problem

use of com.liferay.blade.api.Problem in project liferay-ide by liferay.

the class ProjectMigrationService method analyzeFile.

protected FileVisitResult analyzeFile(File file, final List<Problem> problems, ProgressMonitor monitor) {
    Path path = file.toPath();
    String fileName = path.getFileName().toString();
    String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
    monitor.setTaskName("Analyzing file " + _count + "/" + _total + " " + fileName);
    ServiceReference<FileMigrator>[] fileMigrators = _fileMigratorTracker.getServiceReferences();
    if (ListUtil.isNotEmpty(fileMigrators)) {
        Stream<ServiceReference<FileMigrator>> serviceStream = Arrays.asList(fileMigrators).stream();
        List<ServiceReference<FileMigrator>> fileMigratorList = serviceStream.filter(predicate -> {
            String fileExtensionString = (String) predicate.getProperty("file.extensions");
            List<String> extensionList = Arrays.asList(fileExtensionString.split(","));
            return extensionList.contains(extension);
        }).collect(Collectors.toList());
        if (ListUtil.isNotEmpty(fileMigratorList)) {
            try {
                Stream<ServiceReference<FileMigrator>> migratorStream = fileMigratorList.stream();
                migratorStream.map(_context::getService).parallel().forEach(fm -> {
                    List<Problem> fileProlbems = fm.analyze(file);
                    if (ListUtil.isNotEmpty(fileProlbems)) {
                        problems.addAll(fileProlbems);
                    }
                });
            } catch (Exception e) {
            }
        }
        _count++;
    }
    return FileVisitResult.CONTINUE;
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) ProgressMonitor(com.liferay.blade.api.ProgressMonitor) Migration(com.liferay.blade.api.Migration) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) ListUtil(com.liferay.ide.core.util.ListUtil) Activate(org.osgi.service.component.annotations.Activate) MigrationListener(com.liferay.blade.api.MigrationListener) Path(java.nio.file.Path) ServiceReference(org.osgi.framework.ServiceReference) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) OutputStream(java.io.OutputStream) FileVisitor(java.nio.file.FileVisitor) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Files(java.nio.file.Files) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Collectors(java.util.stream.Collectors) FileMigrator(com.liferay.blade.api.FileMigrator) File(java.io.File) BundleContext(org.osgi.framework.BundleContext) FileVisitResult(java.nio.file.FileVisitResult) Problem(com.liferay.blade.api.Problem) List(java.util.List) Stream(java.util.stream.Stream) Reporter(com.liferay.blade.api.Reporter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Collections(java.util.Collections) ArrayList(java.util.ArrayList) List(java.util.List) Problem(com.liferay.blade.api.Problem) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 89 with Problem

use of com.liferay.blade.api.Problem in project liferay-ide by liferay.

the class AbstractFileMigrator method analyze.

@Override
public List<Problem> analyze(File file) {
    List<Problem> problems = new ArrayList<>();
    String fileExtension = new Path(file.getAbsolutePath()).getFileExtension();
    List<SearchResult> searchResults = searchFile(file, createFileChecker(type, file, fileExtension));
    if (ListUtil.isNotEmpty(searchResults)) {
        String sectionHtml = MarkdownParser.getSection("BREAKING_CHANGES.markdown", sectionKey);
        if ((sectionHtml != null) && sectionHtml.equals("#legacy")) {
            sectionHtml = problemSummary;
        }
        for (SearchResult searchResult : searchResults) {
            if (searchResult != null) {
                problems.add(new Problem(problemTitle, problemSummary, fileExtension, problemTickets, file, searchResult.startLine, searchResult.startOffset, searchResult.endOffset, sectionHtml, searchResult.autoCorrectContext, Problem.STATUS_NOT_RESOLVED, Problem.DEFAULT_MARKER_ID, Problem.MARKER_ERROR));
            }
        }
    }
    return problems;
}
Also used : Path(org.eclipse.core.runtime.Path) ArrayList(java.util.ArrayList) Problem(com.liferay.blade.api.Problem) SearchResult(com.liferay.blade.api.SearchResult)

Example 90 with Problem

use of com.liferay.blade.api.Problem in project liferay-ide by liferay.

the class JSPTagMigrator method correctProblems.

@Override
public int correctProblems(File file, List<Problem> problems) throws AutoMigrateException {
    int corrected = 0;
    List<Integer> autoCorrectTagOffsets = new ArrayList<>();
    Stream<Problem> stream = problems.stream();
    Class<? extends JSPTagMigrator> class1 = getClass();
    String autoCorrectContext = "jsptag:" + class1.getName();
    stream.filter(p -> p.autoCorrectContext.equals(autoCorrectContext)).map(p -> p.getStartOffset()).sorted();
    for (Problem problem : problems) {
        if ((problem.autoCorrectContext != null) && problem.autoCorrectContext.equals("jsptag:" + class1.getName())) {
            autoCorrectTagOffsets.add(problem.getStartOffset());
        }
    }
    Collections.sort(autoCorrectTagOffsets, new Comparator<Integer>() {

        @Override
        public int compare(Integer i1, Integer i2) {
            return i2.compareTo(i1);
        }
    });
    IFile jspFile = getJSPFile(file);
    if (ListUtil.isNotEmpty(autoCorrectTagOffsets)) {
        IDOMModel domModel = null;
        try {
            domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(jspFile);
            List<IDOMElement> elementsToCorrect = new ArrayList<>();
            for (int startOffset : autoCorrectTagOffsets) {
                IndexedRegion region = domModel.getIndexedRegion(startOffset);
                if (region instanceof IDOMElement) {
                    IDOMElement element = (IDOMElement) region;
                    elementsToCorrect.add(element);
                }
            }
            for (IDOMElement element : elementsToCorrect) {
                domModel.aboutToChangeModel();
                if (_newAttrValues.length == 1) {
                    element.setAttribute(_attrNames[0], _newAttrValues[0]);
                    corrected++;
                } else if (_newAttrNames.length == 1) {
                    String value = element.getAttribute(_attrNames[0]);
                    element.removeAttribute(_attrNames[0]);
                    element.setAttribute(_newAttrNames[0], value);
                    corrected++;
                } else if (ListUtil.isNotEmpty(_newTagNames)) {
                    String tagName = element.getTagName();
                    NamedNodeMap attributes = element.getAttributes();
                    NodeList childNodes = element.getChildNodes();
                    String nodeValue = element.getNodeValue();
                    String newTagName = "";
                    for (int i = 0; i < _tagNames.length; i++) {
                        if (_tagNames[i].equals(tagName)) {
                            newTagName = _newTagNames[i];
                            break;
                        }
                    }
                    if (newTagName.equals("")) {
                        continue;
                    }
                    Element newNode = element.getOwnerDocument().createElement(newTagName);
                    if (nodeValue != null) {
                        newNode.setNodeValue(nodeValue);
                    }
                    for (int i = 0; i < attributes.getLength(); i++) {
                        Node attribute = attributes.item(i);
                        newNode.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
                    }
                    for (int i = 0; i < childNodes.getLength(); i++) {
                        Node childNode = childNodes.item(i);
                        newNode.appendChild(childNode.cloneNode(true));
                    }
                    element.getParentNode().replaceChild(newNode, element);
                    corrected++;
                }
                domModel.changedModel();
                domModel.save();
            }
        } catch (Exception e) {
            throw new AutoMigrateException("Unable to auto-correct", e);
        } finally {
            if (domModel != null) {
                domModel.releaseFromEdit();
            }
        }
        IPath location = jspFile.getLocation();
        if ((corrected > 0) && !location.toFile().equals(file)) {
            try (InputStream jspFileContent = jspFile.getContents()) {
                Files.copy(jspFileContent, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
            } catch (Exception e) {
                throw new AutoMigrateException("Error writing corrected file.", e);
            }
        }
    }
    return corrected;
}
Also used : SearchResult(com.liferay.blade.api.SearchResult) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) IPath(org.eclipse.core.runtime.IPath) Node(org.w3c.dom.Node) ListUtil(com.liferay.ide.core.util.ListUtil) IFile(org.eclipse.core.resources.IFile) NamedNodeMap(org.w3c.dom.NamedNodeMap) JSPFile(com.liferay.blade.api.JSPFile) NodeList(org.w3c.dom.NodeList) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Files(java.nio.file.Files) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) File(java.io.File) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) Problem(com.liferay.blade.api.Problem) List(java.util.List) Stream(java.util.stream.Stream) Element(org.w3c.dom.Element) AutoMigrator(com.liferay.blade.api.AutoMigrator) StructuredModelManager(org.eclipse.wst.sse.core.StructuredModelManager) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) IFile(org.eclipse.core.resources.IFile) NamedNodeMap(org.w3c.dom.NamedNodeMap) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) IPath(org.eclipse.core.runtime.IPath) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) Problem(com.liferay.blade.api.Problem)

Aggregations

Problem (com.liferay.blade.api.Problem)92 Test (org.junit.Test)58 File (java.io.File)42 FileMigrator (com.liferay.blade.api.FileMigrator)31 Migration (com.liferay.blade.api.Migration)29 NullProgressMonitor (com.liferay.blade.util.NullProgressMonitor)26 ArrayList (java.util.ArrayList)16 FileProblems (com.liferay.ide.project.core.upgrade.FileProblems)9 IOException (java.io.IOException)9 ServiceReference (org.osgi.framework.ServiceReference)9 AutoMigrator (com.liferay.blade.api.AutoMigrator)8 IFile (org.eclipse.core.resources.IFile)8 List (java.util.List)6 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)6 AutoMigrateException (com.liferay.blade.api.AutoMigrateException)5 SearchResult (com.liferay.blade.api.SearchResult)5 MigrationProblems (com.liferay.ide.project.core.upgrade.MigrationProblems)5 MigrationProblemsContainer (com.liferay.ide.project.core.upgrade.MigrationProblemsContainer)5 HashSet (java.util.HashSet)5 CoreException (org.eclipse.core.runtime.CoreException)5