Search in sources :

Example 1 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class EclipseShellCallback method getSpecificSourceFolder.

private IPackageFragmentRoot getSpecificSourceFolder(IJavaProject javaProject, String targetProject) throws ShellException {
    try {
        Path path = new Path("/" + targetProject);
        IPackageFragmentRoot pfr = javaProject.findPackageFragmentRoot(path);
        if (pfr == null) {
            StringBuffer sb = new StringBuffer();
            sb.append("Cannot find source folder ");
            sb.append(targetProject);
            throw new ShellException(sb.toString());
        }
        return pfr;
    } catch (CoreException e) {
        throw new ShellException(e.getStatus().getMessage(), e);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) CoreException(org.eclipse.core.runtime.CoreException) ShellException(org.mybatis.generator.exception.ShellException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 2 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class EclipseShellCallback method getDirectory.

/*
     * (non-Javadoc)
     * @see org.mybatis.generator.api.ShellCallback#getDirectory(java.lang.String, java.lang.String)
     */
public File getDirectory(String targetProject, String targetPackage) throws ShellException {
    if (targetProject.startsWith("/") || targetProject.startsWith("\\")) {
        StringBuffer sb = new StringBuffer();
        sb.append("targetProject ");
        sb.append(targetProject);
        sb.append(" is invalid - it cannot start with / or \\");
        throw new ShellException(sb.toString());
    }
    IFolder folder = getFolder(targetProject, targetPackage);
    return folder.getRawLocation().toFile();
}
Also used : ShellException(org.mybatis.generator.exception.ShellException) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class EclipseShellCallback method getJavaProject.

private IJavaProject getJavaProject(String javaProjectName) throws ShellException {
    IJavaProject javaProject = projects.get(javaProjectName);
    if (javaProject == null) {
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IProject project = root.getProject(javaProjectName);
        if (project.exists()) {
            boolean isJavaProject;
            try {
                isJavaProject = project.hasNature(JavaCore.NATURE_ID);
            } catch (CoreException e) {
                throw new ShellException(e.getStatus().getMessage(), e);
            }
            if (isJavaProject) {
                javaProject = JavaCore.create(project);
            } else {
                StringBuffer sb = new StringBuffer();
                sb.append("Project ");
                sb.append(javaProjectName);
                sb.append(" is not a Java project");
                throw new ShellException(sb.toString());
            }
        } else {
            StringBuffer sb = new StringBuffer();
            sb.append("Project ");
            sb.append(javaProjectName);
            sb.append(" does not exist");
            throw new ShellException(sb.toString());
        }
        projects.put(javaProjectName, javaProject);
    }
    return javaProject;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) ShellException(org.mybatis.generator.exception.ShellException) IProject(org.eclipse.core.resources.IProject)

Example 4 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class MyBatisGenerator method writeGeneratedXmlFile.

private void writeGeneratedXmlFile(GeneratedXmlFile gxf, ProgressCallback callback) throws InterruptedException, IOException {
    File targetFile;
    String source;
    try {
        File directory = shellCallback.getDirectory(gxf.getTargetProject(), gxf.getTargetPackage());
        targetFile = new File(directory, gxf.getFileName());
        if (targetFile.exists()) {
            if (gxf.isMergeable()) {
                source = XmlFileMergerJaxp.getMergedSource(gxf, targetFile);
            } else if (shellCallback.isOverwriteEnabled()) {
                source = gxf.getFormattedContent();
                warnings.add(getString(//$NON-NLS-1$
                "Warning.11", targetFile.getAbsolutePath()));
            } else {
                source = gxf.getFormattedContent();
                targetFile = getUniqueFileName(directory, gxf.getFileName());
                warnings.add(getString("Warning.2", //$NON-NLS-1$
                targetFile.getAbsolutePath()));
            }
        } else {
            source = gxf.getFormattedContent();
        }
        callback.checkCancel();
        callback.startTask(getString("Progress.15", //$NON-NLS-1$
        targetFile.getName()));
        //$NON-NLS-1$
        writeFile(targetFile, source, "UTF-8");
    } catch (ShellException e) {
        warnings.add(e.getMessage());
    }
}
Also used : Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) File(java.io.File) ShellException(org.mybatis.generator.exception.ShellException)

Example 5 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class MyBatisGenerator method writeGeneratedJavaFile.

private void writeGeneratedJavaFile(GeneratedJavaFile gjf, ProgressCallback callback) throws InterruptedException, IOException {
    File targetFile;
    String source;
    try {
        File directory = shellCallback.getDirectory(gjf.getTargetProject(), gjf.getTargetPackage());
        targetFile = new File(directory, gjf.getFileName());
        if (targetFile.exists()) {
            if (shellCallback.isMergeSupported()) {
                source = shellCallback.mergeJavaFile(gjf.getFormattedContent(), targetFile, MergeConstants.OLD_ELEMENT_TAGS, gjf.getFileEncoding());
            } else if (shellCallback.isOverwriteEnabled()) {
                source = gjf.getFormattedContent();
                warnings.add(getString(//$NON-NLS-1$
                "Warning.11", targetFile.getAbsolutePath()));
            } else {
                source = gjf.getFormattedContent();
                targetFile = getUniqueFileName(directory, gjf.getFileName());
                warnings.add(getString("Warning.2", //$NON-NLS-1$
                targetFile.getAbsolutePath()));
            }
        } else {
            source = gjf.getFormattedContent();
        }
        callback.checkCancel();
        callback.startTask(getString("Progress.15", //$NON-NLS-1$
        targetFile.getName()));
        writeFile(targetFile, source, gjf.getFileEncoding());
    } catch (ShellException e) {
        warnings.add(e.getMessage());
    }
}
Also used : Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) File(java.io.File) ShellException(org.mybatis.generator.exception.ShellException)

Aggregations

ShellException (org.mybatis.generator.exception.ShellException)12 File (java.io.File)4 CoreException (org.eclipse.core.runtime.CoreException)4 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)3 StringTokenizer (java.util.StringTokenizer)2 IFolder (org.eclipse.core.resources.IFolder)2 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 IProject (org.eclipse.core.resources.IProject)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 Path (org.eclipse.core.runtime.Path)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1