List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot createPackageFragment
IPackageFragment createPackageFragment(String name, boolean force, IProgressMonitor monitor) throws JavaModelException;
From source file:com.hudson.hibernatesynchronizer.util.SynchronizerThread.java
License:GNU General Public License
public static void writeClass(String velocityTemplate, Context context, IPackageFragmentRoot fragmentRoot, String packageName, String className, boolean force) throws JavaModelException { IPackageFragment fragment = fragmentRoot.getPackageFragment(packageName); String fileName = className + ".java"; try {/*from ww w . jav a 2s.com*/ if (null != fragment) { if (!fragment.exists()) { fragment = fragmentRoot.createPackageFragment(packageName, false, null); } ICompilationUnit unit = fragment.getCompilationUnit(fileName); if (!unit.exists() && !force) { Template template = Constants.templateGenerator.getTemplate(velocityTemplate); StringWriter sw = new StringWriter(); template.merge(context, sw); try { unit = fragment.createCompilationUnit(fileName, addContent(sw.toString(), className), false, null); } catch (JavaModelException e) { } } else { if (force) { org.apache.velocity.Template template = Constants.templateGenerator .getTemplate(velocityTemplate); StringWriter sw = new StringWriter(); template.merge(context, sw); try { if (unit.exists()) { String existingContent = unit.getBuffer().getContents(); if (null != existingContent && existingContent.equals(sw.toString())) return; } unit = createCompilationUnit(fragment, unit, fileName, addContent(sw.toString(), className), true, null); } catch (JavaModelException e) { } } } } } catch (Exception e) { e.printStackTrace(); if (e instanceof JavaModelException) throw (JavaModelException) e; else MessageDialog.openWarning(null, "An error has occured: " + e.getClass(), e.getMessage()); } }
From source file:com.hudson.hibernatesynchronizer.util.SynchronizerThread.java
License:GNU General Public License
private static void writeProxyClass(String templateName, String contentTemplateName, Context context, IPackageFragmentRoot fragmentRoot, String packageName, String className) throws JavaModelException { IPackageFragment fragment = fragmentRoot.getPackageFragment(packageName); String fileName = className + ".java"; try {//from w w w .j a v a 2s . com if (null != fragment) { if (!fragment.exists()) { fragment = fragmentRoot.createPackageFragment(packageName, false, null); } ICompilationUnit unit = fragment.getCompilationUnit(fileName); if (unit.exists()) { String content = unit.getSource(); MarkerContents mc = HSUtil.getMarkerContents(content, "GENERATED CONTENT MARKER"); if (null != mc) { try { Template template = Constants.templateGenerator.getTemplate(contentTemplateName); StringWriter sw = new StringWriter(); template.merge(context, sw); if (null != mc.getContents() && mc.getContents().trim().equals(sw.toString().trim())) return; content = mc.getPreviousContents() + sw.toString() + mc.getPostContents(); createCompilationUnit(fragment, unit, fileName, content, true, null); } catch (JavaModelException e) { } } } else { try { Template template = Constants.templateGenerator.getTemplate(contentTemplateName); StringWriter sw = new StringWriter(); template.merge(context, sw); VelocityContext subContext = new VelocityContext(context); subContext.put("contents", sw.toString()); template = Constants.templateGenerator.getTemplate(templateName); sw = new StringWriter(); template.merge(subContext, sw); unit = fragment.createCompilationUnit(fileName, addContent(sw.toString(), className), true, null); } catch (JavaModelException e) { } } } } catch (Exception e) { if (e instanceof JavaModelException) throw (JavaModelException) e; else MessageDialog.openWarning(null, "An error has occured: " + e.getClass(), e.getMessage()); } }
From source file:com.hudson.hibernatesynchronizer.util.SynchronizerThread.java
License:GNU General Public License
private static boolean writeCustom(IProject project, IPackageFragmentRoot fragmentRoot, ProjectTemplate projectTemplate, HibernateClass hc, Context context, IFile syncFile, boolean force) { context.remove("custom_placeholder"); try {//from w w w .ja v a 2 s .co m if (projectTemplate.getTemplate().isJavaClass()) { StringWriter sw = new StringWriter(); Constants.customGenerator.evaluate(context, sw, Velocity.class.getName(), projectTemplate.getName()); String className = sw.toString(); String fileName = className + ".java"; context.put("className", className); sw = new StringWriter(); Constants.customGenerator.evaluate(context, sw, Velocity.class.getName(), projectTemplate.getLocation()); String packageName = sw.toString(); IPackageFragment fragment = fragmentRoot.getPackageFragment(packageName); if (null != fragment) { context.put("package", packageName); sw = new StringWriter(); Constants.customGenerator.evaluate(context, sw, Velocity.class.getName(), packageName); String location = sw.toString(); if (!fragment.exists()) { fragment = fragmentRoot.createPackageFragment(packageName, false, null); } ICompilationUnit unit = fragment.getCompilationUnit(fileName); sw = new StringWriter(); Constants.customGenerator.evaluate(context, sw, Velocity.class.getName(), projectTemplate.getTemplate().getContent()); try { String trimmed = sw.toString().trim(); if (NO_SAVE.equals(trimmed)) { return false; } else if (trimmed.startsWith(WARNING)) { String message = trimmed.substring(WARNING.length(), trimmed.length()); if (null != syncFile) { String key = WARNING + syncFile.getName() + message; if (null == context.get(key)) { context.put(key, Boolean.TRUE); EditorUtil.addWarningMarker(syncFile, message, 0); } } return false; } else if (trimmed.startsWith(ERROR)) { String message = trimmed.substring(ERROR.length(), trimmed.length()); if (null != syncFile) { String key = ERROR + syncFile.getName() + message; if (null == context.get(key)) { context.put(key, Boolean.TRUE); EditorUtil.addProblemMarker(syncFile, message, 0); } } return false; } else if (trimmed.startsWith(FATAL)) { String message = trimmed.substring(FATAL.length(), trimmed.length()); if (null != syncFile) { String key = FATAL + syncFile.getName() + message; if (null == context.get(key)) { context.put(key, Boolean.TRUE); EditorUtil.addProblemMarker(syncFile, message, 0); } } return true; } else { if (!unit.exists()) { unit = fragment.createCompilationUnit(fileName, addContent(sw.toString(), className), false, null); } else { String content = addContent(sw.toString(), className); if (force || projectTemplate.shouldOverride()) { unit = createCompilationUnit(fragment, unit, fileName, content, true, null); } } } } catch (JavaModelException e) { } context.remove("package"); } context.remove("className"); } else { StringWriter sw = new StringWriter(); Constants.customGenerator.evaluate(context, sw, Velocity.class.getName(), projectTemplate.getName()); String name = sw.toString(); sw = new StringWriter(); Constants.customGenerator.evaluate(context, sw, Velocity.class.getName(), projectTemplate.getLocation()); String location = sw.toString(); location = location.replace('\\', '/'); if (!location.startsWith("/")) { location = "/" + location; } if (location.endsWith("/")) { location = location.substring(0, location.length() - 1); } context.put("path", location); context.put("fileName", name); sw = new StringWriter(); Constants.customGenerator.evaluate(context, sw, Velocity.class.getName(), projectTemplate.getTemplate().getContent()); String content = sw.toString(); String trimmed = sw.toString().trim(); if (NO_SAVE.equals(trimmed)) { return false; } else if (trimmed.startsWith(WARNING)) { String message = trimmed.substring(WARNING.length(), trimmed.length()); if (null != syncFile) { String key = WARNING + syncFile.getName() + message; if (null == context.get(key)) { context.put(key, Boolean.TRUE); EditorUtil.addWarningMarker(syncFile, message, 0); } } return false; } else if (trimmed.startsWith(ERROR)) { String message = trimmed.substring(ERROR.length(), trimmed.length()); if (null != syncFile) { String key = ERROR + syncFile.getName() + message; if (null == context.get(key)) { context.put(key, Boolean.TRUE); EditorUtil.addProblemMarker(syncFile, message, 0); } } return false; } else if (trimmed.startsWith(FATAL)) { String message = trimmed.substring(FATAL.length(), trimmed.length()); if (null != syncFile) { String key = FATAL + syncFile.getName() + message; if (null == context.get(key)) { context.put(key, Boolean.TRUE); EditorUtil.addProblemMarker(syncFile, message, 0); } } return true; } else { IFile file = project.getFile(location + "/" + name); if (!file.exists()) { StringTokenizer st = new StringTokenizer(location, "/"); StringBuffer sb = new StringBuffer(); while (st.hasMoreTokens()) { if (sb.length() > 0) sb.append('/'); sb.append(st.nextToken()); IFolder folder = project.getFolder(sb.toString()); if (!folder.exists()) folder.create(false, true, null); } file.create(new ByteArrayInputStream(sw.toString().getBytes()), (projectTemplate.shouldOverride() || force), null); } else { if (force || projectTemplate.shouldOverride()) { try { if (file.exists()) { String existingContent = HSUtil.getStringFromStream(file.getContents()); if (null != existingContent && existingContent.equals(sw.toString())) return false; } } catch (JavaModelException jme) { } file.setContents(new ByteArrayInputStream(sw.toString().getBytes()), (projectTemplate.shouldOverride() || force), false, null); } } } context.remove("path"); } } catch (Exception e) { MessageDialog.openWarning(null, "An error has occured while creating custom template: " + projectTemplate.getTemplate().getName(), e.getMessage()); } return false; }
From source file:com.hudson.hibernatesynchronizer.util.SynchronizerThread.java
License:GNU General Public License
private static void removeCustom(IProject project, IPackageFragmentRoot fragmentRoot, ProjectTemplate projectTemplate, HibernateClass hc, Context context) { try {/*from ww w . j a v a 2 s . c o m*/ if (projectTemplate.getName().indexOf("$class") >= 0 || projectTemplate.getName().indexOf("${class") >= 0 || projectTemplate.getName().indexOf("$!class") >= 0 || projectTemplate.getName().indexOf("$!{class") >= 0) { StringWriter sw = new StringWriter(); Constants.customGenerator.evaluate(context, sw, Velocity.class.getName(), projectTemplate.getName()); String fileName = sw.toString(); sw = new StringWriter(); Constants.customGenerator.evaluate(context, sw, Velocity.class.getName(), projectTemplate.getLocation()); String location = sw.toString(); if (projectTemplate.getTemplate().isJavaClass()) { fileName = fileName + Constants.EXT_JAVA; IPackageFragment fragment = fragmentRoot.getPackageFragment(location); if (fragment.exists()) { fragment = fragmentRoot.createPackageFragment(location, false, null); ICompilationUnit unit = fragment.getCompilationUnit(fileName); if (unit.exists()) { unit.delete(true, null); } } } else { IFile file = project.getFile(location + "/" + fileName); if (file.exists()) { file.delete(true, true, null); } } } } catch (Exception e) { } }
From source file:com.iw.plugins.spindle.ui.wizards.factories.ClassFactory.java
License:Mozilla Public License
private void createType(IProgressMonitor monitor) throws CoreException, InterruptedException { if (monitor == null) { monitor = new NullProgressMonitor(); }/*from www . j a v a 2 s . c o m*/ monitor.beginTask(UIPlugin.getString("ClassFactory.operationdesc", fTypeName), 10); ICompilationUnit createdWorkingCopy = null; try { IPackageFragmentRoot root = fPackageFragmentyRoot; IPackageFragment pack = fPackageFragment; if (pack == null) { pack = root.getPackageFragment(""); //$NON-NLS-1$ } if (!pack.exists()) { String packName = pack.getElementName(); pack = root.createPackageFragment(packName, true, null); } monitor.worked(1); String clName = fTypeName; boolean isInnerClass = false; IType createdType; ImportsManager imports; int indent = 0; String lineDelimiter = System.getProperty("line.separator", "\n"); ICompilationUnit parentCU = pack.createCompilationUnit(clName + ".java", "", false, new SubProgressMonitor(monitor, 2)); // create a working copy with a new owner createdWorkingCopy = parentCU.getWorkingCopy(null); // use the compiler template a first time to read the imports String content = CodeGeneration.getCompilationUnitContent(createdWorkingCopy, null, "", lineDelimiter); //$NON-NLS-1$ if (content != null) createdWorkingCopy.getBuffer().setContents(content); imports = new ImportsManager(createdWorkingCopy); // add an import that will be removed again. Having this import solves // 14661 imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), fTypeName)); String typeContent = constructTypeStub(imports, lineDelimiter); String cuContent = constructCUContent(parentCU, typeContent, lineDelimiter); createdWorkingCopy.getBuffer().setContents(cuContent); createdType = createdWorkingCopy.getType(clName); if (monitor.isCanceled()) { throw new InterruptedException(); } // add imports for superclass/interfaces, so types can be resolved // correctly ICompilationUnit cu = createdType.getCompilationUnit(); boolean needsSave = !cu.isWorkingCopy(); imports.create(needsSave, new SubProgressMonitor(monitor, 1)); JavaModelUtil.reconcile(cu); if (monitor.isCanceled()) { throw new InterruptedException(); } // set up again imports = new ImportsManager(imports.getCompilationUnit(), imports.getAddedTypes()); createTypeMembers(createdType, imports, new SubProgressMonitor(monitor, 1)); // add imports imports.create(needsSave, new SubProgressMonitor(monitor, 1)); removeUnusedImports(cu, imports.getAddedTypes(), needsSave); JavaModelUtil.reconcile(cu); ISourceRange range = createdType.getSourceRange(); IBuffer buf = cu.getBuffer(); String originalContent = buf.getText(range.getOffset(), range.getLength()); String formattedContent = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, indent, null, lineDelimiter, pack.getJavaProject()); buf.replace(range.getOffset(), range.getLength(), formattedContent); // String fileComment = getFileComment(cu); // if (fileComment != null && fileComment.length() > 0) // { // buf.replace(0, 0, fileComment + lineDelimiter); // } cu.commitWorkingCopy(false, new SubProgressMonitor(monitor, 1)); if (createdWorkingCopy != null) { fCreatedType = (IType) createdType.getPrimaryElement(); } else { fCreatedType = createdType; } } finally { if (createdWorkingCopy != null) { createdWorkingCopy.discardWorkingCopy(); } monitor.done(); } }
From source file:com.iw.plugins.spindle.ui.wizards.factories.LibraryFactory.java
License:Mozilla Public License
/** * Method createLibrary./*ww w. j a v a2s . c o m*/ * * @param root * @param pack * @param appname * @param monitor * @return IFile */ public static IFile createLibrary(IPackageFragmentRoot root, IPackageFragment pack, String libraryName, IProgressMonitor monitor) throws CoreException, InterruptedException { monitor.beginTask(UIPlugin.getString("ApplicationFactory.operationdesc", libraryName), 10); if (pack == null) { pack = root.getPackageFragment(""); } if (!pack.exists()) { String packName = pack.getElementName(); pack = root.createPackageFragment(packName, true, null); pack.save(new SubProgressMonitor(monitor, 1), true); } monitor.worked(1); IContainer folder = (IContainer) pack.getUnderlyingResource(); IFile file = folder.getFile(new Path(libraryName + ".library")); InputStream contents = new ByteArrayInputStream(getLibraryContent().getBytes()); file.create(contents, false, new SubProgressMonitor(monitor, 1)); monitor.worked(1); monitor.done(); return file; }
From source file:com.iw.plugins.spindle.wizards.factories.ApplicationClassFactory.java
License:Mozilla Public License
static public IType createClass(IPackageFragmentRoot root, IPackageFragment pack, String classname, IType superClass, IMethodEvaluator methodEvaluator, IProgressMonitor monitor) throws CoreException, InterruptedException { IType createdType;//from w w w . ja v a2s . com ImportsStructure imports; int indent = 0; String superclassName = (superClass == null ? "java.lang.Object" : superClass.getElementName()); monitor.beginTask(MessageUtil.getFormattedString("ClassFactory.operationdesc", classname), 10); if (pack == null) { pack = root.getPackageFragment(""); } if (!pack.exists()) { String packName = pack.getElementName(); pack = root.createPackageFragment(packName, true, null); } monitor.worked(1); String lineDelimiter = null; ICompilationUnit parentCU = pack.getCompilationUnit(classname + ".java"); imports = getImports(parentCU); lineDelimiter = StubUtility.getLineDelimiterUsed(parentCU); String content = createClassBody(classname, superClass, imports, lineDelimiter); createdType = parentCU.createType(content, null, false, new SubProgressMonitor(monitor, 5)); // add imports for sclass, so the type can be parsed correctly if (imports != null) { imports.create(true, new SubProgressMonitor(monitor, 1)); } createAllNewMethods(methodEvaluator, createdType, imports, new SubProgressMonitor(monitor, 1)); monitor.worked(1); String formattedContent = StubUtility.codeFormat(createdType.getSource(), indent, lineDelimiter); save(createdType, formattedContent, monitor); monitor.done(); return createdType; }
From source file:com.iw.plugins.spindle.wizards.factories.ApplicationFactory.java
License:Mozilla Public License
static public IFile createApplication(IPackageFragmentRoot root, IPackageFragment pack, String appname, String qualifiedEngineClassname, IProgressMonitor monitor) throws CoreException, InterruptedException { monitor.beginTask(MessageUtil.getFormattedString("ApplicationFactory.operationdesc", appname), 10); if (pack == null) { pack = root.getPackageFragment(""); }/*w ww. j a v a2 s . c o m*/ if (!pack.exists()) { String packName = pack.getElementName(); pack = root.createPackageFragment(packName, true, null); pack.save(new SubProgressMonitor(monitor, 1), true); } monitor.worked(1); IContainer folder = (IContainer) pack.getUnderlyingResource(); IFile file = folder.getFile(new Path(appname + ".application")); InputStream contents = new ByteArrayInputStream( getApplicationContent(appname, qualifiedEngineClassname, pack.getElementName()).getBytes()); file.create(contents, false, new SubProgressMonitor(monitor, 1)); monitor.worked(1); monitor.done(); return file; }
From source file:com.iw.plugins.spindle.wizards.factories.ClassFactory.java
License:Mozilla Public License
public IType createClass(IPackageFragmentRoot root, IPackageFragment pack, String classname, IType superClass, IType[] interfaces, IMethodEvaluator methodEvaluator, IProgressMonitor monitor) throws CoreException, InterruptedException { IType createdType;/*from w w w .j av a2 s . c o m*/ ImportsStructure imports; int indent = 0; String superclassName = (superClass == null ? "java.lang.Object" : superClass.getElementName()); monitor.beginTask(MessageUtil.getFormattedString("ClassFactory.operationdesc", classname), 10); if (pack == null) { pack = root.getPackageFragment(""); } if (!pack.exists()) { String packName = pack.getElementName(); pack = root.createPackageFragment(packName, true, null); } monitor.worked(1); String lineDelimiter = null; ICompilationUnit parentCU = pack.getCompilationUnit(classname + ".java"); imports = getImports(parentCU); lineDelimiter = StubUtility.getLineDelimiterUsed(parentCU); String content = createClassBody(classname, superClass, interfaces, imports, lineDelimiter); createdType = parentCU.createType(content, null, false, new SubProgressMonitor(monitor, 5)); // add imports for sclass, so the type can be parsed correctly if (imports != null) { imports.create(true, new SubProgressMonitor(monitor, 1)); } createAllNewMethods(methodEvaluator, createdType, imports, new SubProgressMonitor(monitor, 1)); monitor.worked(1); String formattedContent = StubUtility.codeFormat(createdType.getSource(), indent, lineDelimiter); save(createdType, formattedContent, monitor); monitor.done(); return createdType; }
From source file:com.iw.plugins.spindle.wizards.factories.ComponentFactory.java
License:Mozilla Public License
public static IFile createComponent(IPackageFragmentRoot root, IPackageFragment pack, String componentName, IType specClass, IProgressMonitor monitor) throws CoreException, InterruptedException { monitor.beginTask(MessageUtil.getFormattedString("ApplicationFactory.operationdesc", componentName), 10); if (pack == null) { pack = root.getPackageFragment(""); }/*from w w w. ja v a 2s .com*/ if (!pack.exists()) { String packName = pack.getElementName(); pack = root.createPackageFragment(packName, true, null); pack.save(new SubProgressMonitor(monitor, 1), true); } monitor.worked(1); IContainer folder = (IContainer) pack.getUnderlyingResource(); IFile file = folder.getFile(new Path(componentName + ".jwc")); String qualifiedSpecClassname = specClass.getFullyQualifiedName(); InputStream contents = new ByteArrayInputStream(getComponentContent(qualifiedSpecClassname).getBytes()); file.create(contents, false, new SubProgressMonitor(monitor, 1)); monitor.worked(1); monitor.done(); return file; }