Search in sources :

Example 1 with InvalidInputException

use of com.devonfw.cobigen.eclipse.common.exceptions.InvalidInputException in project cobigen by devonfw.

the class CobiGenWrapper method isValidInput.

/**
 * Checks if the selected items are supported by one or more {@link Trigger}s, and if they are supported by the same
 * {@link Trigger}s. Returns a boolean value, if all objects of the selection could be processed. If there are
 * objects, which are not yet supported as inputs for generation, or the selection in composed of valid objects in an
 * not yet supported way, an {@link InvalidInputException} will be thrown. Thus, getting a boolean value can be
 * interpreted as "selection supported, but currently not matching trigger".
 *
 * @param monitor to track progress
 * @return true, if all items are supported by the same trigger(s)<br>
 *         false, if they are not supported by any trigger at all
 * @throws InvalidInputException if the input could not be read as expected
 */
public boolean isValidInput(IProgressMonitor monitor) throws InvalidInputException {
    LOG.debug("Start checking selection validity for the use as Java input.");
    Iterator<?> it = this.inputs.iterator();
    List<String> firstTriggers = null;
    try {
        while (it.hasNext()) {
            Object tmp = it.next();
            monitor.subTask("Checking available triggers for " + tmp);
            monitor.worked(1);
            LOG.debug("Checking available triggers for {}", tmp);
            List<String> matchingTriggerIds = this.cobiGen.getMatchingTriggerIds(tmp);
            if (firstTriggers == null) {
                firstTriggers = matchingTriggerIds;
            } else if (!firstTriggers.equals(matchingTriggerIds)) {
                throw new InvalidInputException("You selected at least two inputs, which are not matching the same triggers. " + "For batch processing all inputs have to match the same triggers.");
            }
        }
    } finally {
        LOG.debug("Ended checking selection validity for the use as Java input.");
    }
    return firstTriggers != null && !firstTriggers.isEmpty();
}
Also used : InvalidInputException(com.devonfw.cobigen.eclipse.common.exceptions.InvalidInputException)

Example 2 with InvalidInputException

use of com.devonfw.cobigen.eclipse.common.exceptions.InvalidInputException in project cobigen by devonfw.

the class GeneratorWrapperFactory method extractValidEclipseInputs.

/**
 * Extracts a list of valid eclipse inputs. Therefore this method will throw an
 * {@link InvalidInputException},whenever<br>
 * <ul>
 * <li>the selection contains different content types</li>
 * <li>the selection contains a content type, which is currently not supported</li>
 * </ul>
 *
 * @param selection current {@link IStructuredSelection selection} of within the IDE
 * @return the {@link List} of selected objects, whereas all elements of the list are of the same content type
 * @throws InvalidInputException if the selection includes non supported input types or is composed in a non supported
 *         combination of inputs.
 */
private static List<Object> extractValidEclipseInputs(ISelection selection) throws InvalidInputException {
    LOG.info("Start extraction of valid inputs from selection...");
    int type = 0;
    boolean initialized = false;
    List<Object> inputObjects = Lists.newLinkedList();
    IJavaElement iJavaElem = null;
    // When the user is selecting text from the text editor
    if (selection instanceof ITextSelection) {
        IFileEditorInput iEditorInput = (IFileEditorInput) PlatformUIUtil.getActiveWorkbenchPage().getActiveEditor().getEditorInput();
        IFile iFile = iEditorInput.getFile();
        iJavaElem = JavaCore.create(iFile);
        if (iJavaElem instanceof ICompilationUnit) {
            inputObjects.add(iJavaElem);
        } else {
            inputObjects.add(iFile);
        }
    } else /*
     * Collect selected objects and check whether all selected objects are of the same type
     */
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        Iterator<?> it = structuredSelection.iterator();
        while (it.hasNext()) {
            Object o = it.next();
            switch(type) {
                case 0:
                    if (o instanceof ICompilationUnit) {
                        inputObjects.add(o);
                        initialized = true;
                    } else if (initialized) {
                        throw new InvalidInputException("Multiple different inputs have been selected of the following types: " + ICompilationUnit.class + ", " + o.getClass());
                    }
                    if (initialized) {
                        type = 0;
                        break;
                    }
                // $FALL-THROUGH$
                case 1:
                    if (o instanceof IPackageFragment) {
                        inputObjects.add(o);
                        initialized = true;
                    } else if (initialized) {
                        throw new InvalidInputException("Multiple different inputs have been selected of the following types: " + IPackageFragment.class + ", " + o.getClass());
                    }
                    if (initialized) {
                        type = 1;
                        break;
                    }
                // $FALL-THROUGH$
                case 2:
                    if (o instanceof IFile) {
                        inputObjects.add(o);
                        initialized = true;
                    } else if (initialized) {
                        throw new InvalidInputException("Multiple different inputs have been selected of the following types: " + IFile.class + ", " + o.getClass());
                    }
                    if (initialized) {
                        type = 2;
                        break;
                    }
                // $FALL-THROUGH$
                case 3:
                    if (o instanceof IResource) {
                        inputObjects.add(o);
                        initialized = true;
                    } else if (initialized) {
                        throw new InvalidInputException("Multiple different inputs have been selected of the following types: " + IFile.class + ", " + o.getClass());
                    }
                    if (initialized) {
                        type = 3;
                        break;
                    }
                // $FALL-THROUGH$
                default:
                    throw new InvalidInputException("Your selection contains an object of the type " + o.getClass().toString() + ", which is not yet supported to be treated as an input for generation.\n" + "Please adjust your selection to only contain supported objects like Java classes/packages or XML files.");
            }
        }
    }
    LOG.info("Finished extraction of inputs from selection successfully.");
    return inputObjects;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InvalidInputException(com.devonfw.cobigen.eclipse.common.exceptions.InvalidInputException) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IFile(org.eclipse.core.resources.IFile) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ITextSelection(org.eclipse.jface.text.ITextSelection) IFileEditorInput(org.eclipse.ui.IFileEditorInput) Iterator(java.util.Iterator) IResource(org.eclipse.core.resources.IResource)

Aggregations

InvalidInputException (com.devonfw.cobigen.eclipse.common.exceptions.InvalidInputException)2 Iterator (java.util.Iterator)1 IFile (org.eclipse.core.resources.IFile)1 IResource (org.eclipse.core.resources.IResource)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 IFileEditorInput (org.eclipse.ui.IFileEditorInput)1