Java Code Examples for org.openide.loaders.DataObject#createFromTemplate()
The following examples show how to use
org.openide.loaders.DataObject#createFromTemplate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptingCreateFromTemplateTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testCreateFromTemplateEncodingProperty() throws Exception { FileObject root = FileUtil.createMemoryFileSystem().getRoot(); FileObject fo = FileUtil.createData(root, "simpleObject.txt"); OutputStream os = fo.getOutputStream(); os.write("${encoding}".getBytes()); os.close(); assertEquals("content/unknown", fo.getMIMEType()); fo.setAttribute ("template", Boolean.TRUE); assertEquals("content/unknown", fo.getMIMEType()); fo.setAttribute("javax.script.ScriptEngine", "freemarker"); assertEquals("text/x-freemarker", fo.getMIMEType()); DataObject obj = DataObject.find(fo); DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); Map<String,String> parameters = Collections.emptyMap(); DataObject inst = obj.createFromTemplate(folder, "complex", parameters); FileObject instFO = inst.getPrimaryFile(); Charset targetEnc = FileEncodingQuery.getEncoding(instFO); assertNotNull("Template encoding is null", targetEnc); assertEquals("Encoding in template doesn't match", targetEnc.name(), instFO.asText()); }
Example 2
Source File: ReactRepoWizardIterator.java From nb-springboot with Apache License 2.0 | 6 votes |
@Override public Set<?> instantiate() throws IOException { // Create file from template String targetName = Templates.getTargetName(wizard); FileObject foDir = Templates.getTargetFolder(wizard); FileObject foTemplate = Templates.getTemplate(wizard); DataObject doTemplate = DataObject.find(foTemplate); DataFolder df = DataFolder.findFolder(foDir); Map<String, Object> props = new HashMap<>(); props.put(WIZ_BASE_INTERF, wizard.getProperty(WIZ_BASE_INTERF)); props.put(WIZ_ENTITY_CLASS, wizard.getProperty(WIZ_ENTITY_CLASS)); props.put(WIZ_ID_CLASS, wizard.getProperty(WIZ_ID_CLASS)); DataObject doCreated = doTemplate.createFromTemplate(df, targetName, props); FileObject foCreated = doCreated.getPrimaryFile(); return Collections.singleton(foCreated); }
Example 3
Source File: DTDWizardIterator.java From netbeans with Apache License 2.0 | 6 votes |
/** * This is where, the schema gets instantiated from the template. */ public Set instantiate (TemplateWizard wizard) throws IOException { FileObject dir = Templates.getTargetFolder( wizard ); DataFolder df = DataFolder.findFolder( dir ); FileObject template = Templates.getTemplate( wizard ); DataObject dTemplate = DataObject.find( template ); DataObject dobj = dTemplate.createFromTemplate(df, Templates.getTargetName(wizard)); if (dobj == null) return Collections.emptySet(); encoding = EncodingUtil.getProjectEncoding(df.getPrimaryFile()); if(!EncodingUtil.isValidEncoding(encoding)) encoding = "UTF-8"; //NOI18N EditCookie edit = dobj.getCookie(EditCookie.class); if (edit != null) { EditorCookie editorCookie = dobj.getCookie(EditorCookie.class); Document doc = (Document)editorCookie.openDocument(); fixEncoding(doc, encoding); SaveCookie save = dobj.getCookie(SaveCookie.class); if (save!=null) save.save(); } return Collections.singleton(dobj.getPrimaryFile()); }
Example 4
Source File: TemplateOperation.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Set<FileObject> execute() { FileObject template = FileUtil.getConfigFile(templateName); if (template != null) { String targetName = target.getName(); try { FileObject targetParent = FileUtil.createFolder(target.getParentFile()); DataFolder targetFolder = DataFolder.findFolder(targetParent); DataObject o = DataObject.find(template); DataObject newData = o.createFromTemplate(targetFolder,targetName, tokens); return important ? Collections.singleton(newData.getPrimaryFile()) : null; } catch (IOException ex) { } } return null; }
Example 5
Source File: JerseyClientWizardIterator.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Set<DataObject> instantiate() throws IOException { FileObject template = Templates.getTemplate(wizard); template.setAttribute("REST_RESOURCE_NAME", bottomPanel.getResourceName()); DataObject dTemplate = DataObject.find( template ); org.openide.filesystems.FileObject dir = Templates.getTargetFolder(wizard); DataFolder df = DataFolder.findFolder( dir ); final DataObject dobj = dTemplate.createFromTemplate(df, Templates.getTargetName(wizard )); // generating client RequestProcessor.getDefault().post(new Runnable() { @Override public void run() { ClientJavaSourceHelper.generateJerseyClient( bottomPanel.getResourceNode(), dobj.getPrimaryFile(), null, bottomPanel.getSecurity()); } }); return Collections.<DataObject>singleton(dobj); }
Example 6
Source File: CompositeComponentWizardIterator.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Set<DataObject> instantiate(TemplateWizard wiz) throws IOException { DataObject result = null; String targetName = Templates.getTargetName(wizard); FileObject targetDir = Templates.getTargetFolder(wizard); DataFolder df = DataFolder.findFolder(targetDir); FileObject template = Templates.getTemplate( wizard ); DataObject dTemplate = DataObject.find(template); HashMap<String, Object> templateProperties = new HashMap<String, Object>(); if (selectedText != null) { templateProperties.put("implementation", selectedText); //NOI18N } Project project = Templates.getProject(wizard); WebModule webModule = WebModule.getWebModule(project.getProjectDirectory()); if (webModule != null) { JSFVersion version = JSFVersion.forWebModule(webModule); if (version != null && version.isAtLeast(JSFVersion.JSF_2_2)) { templateProperties.put("isJSF22", Boolean.TRUE); //NOI18N } } result = dTemplate.createFromTemplate(df,targetName,templateProperties); return Collections.singleton(result); }
Example 7
Source File: DefaultCreationTest.java From netbeans with Apache License 2.0 | 6 votes |
/** * Checks that FileEntry.Format properly formats the template. Part of parameters is provided * by the DataLoader while part is provided by the API caller. * * @throws Exception */ public void testCreateDefaultFormat() throws Exception { MockServices.setServices(Pool.class); FileObject root = FileUtil.createMemoryFileSystem().getRoot(); FileObject templ = FileUtil.createData(root, "simpleTemplate.prim"); String txt = "{a}{b}"; OutputStream os = templ.getOutputStream(); os.write(txt.getBytes()); os.close(); DataObject obj = DataObject.find(templ); DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); Map m = new HashMap(); m.put("a", "eeee"); DataObject x = obj.createFromTemplate(folder, "nue", m); assertEquals("eeeexxxx\n", x.getPrimaryFile().asText()); }
Example 8
Source File: CISupport.java From nb-ci-plugin with GNU General Public License v2.0 | 5 votes |
public static void generateAutoCompletionFile(PhpModule pm) { Project project = PhpProjectSupport.getProject(pm.getSourceDirectory()); FileObject projectDirectory = project.getProjectDirectory(); FileObject nbProjectDirectory = PhpProjectSupport.getNbProjectDirectory(projectDirectory); if (nbProjectDirectory != null) { FileObject autoCompleteTemplateFile = FileUtil.getConfigFile(AUTO_COMPLETE_TEMPLATE_PATH); if (autoCompleteTemplateFile != null) { Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("annotations", getClassPropertyAnnotations(pm)); // NOI18N parameters.put("ciVersion", CIPreferences.getVersion(pm)); // NOI18N try { FileObject autoCompleteDirectory = getAutoCompleteDirectory(nbProjectDirectory); File autoCompleteFile = new File(autoCompleteDirectory.getPath(), AUTO_COMPLETE_FILE); if (autoCompleteFile.exists()) { autoCompleteFile.delete(); } DataFolder autoCompleteDirectoryDataFolder = DataFolder.findFolder(autoCompleteDirectory); DataObject autoCompleteTemplateDataObject = DataObject.find(autoCompleteTemplateFile); autoCompleteTemplateDataObject.createFromTemplate(autoCompleteDirectoryDataFolder, AUTO_COMPLETE_FILE_NAME, parameters); } catch (IOException ex) { Exceptions.printStackTrace(ex); } } } }
Example 9
Source File: PrimefacesImplementation.java From netbeans with Apache License 2.0 | 5 votes |
/** * Generates PrimeFaces's welcome page in the webmodule. */ private static FileObject generateWelcomePage(WebModule webModule) throws IOException { FileObject templateFO = FileUtil.getConfigFile("Templates/Other/welcomePrimefaces.xhtml"); //NOI18N DataObject templateDO = DataObject.find(templateFO); DataObject generated = templateDO.createFromTemplate( DataFolder.findFolder(webModule.getDocumentBase()), "welcomePrimefaces"); //NOI18N JsfComponentUtils.reformat(generated); // update and reformat index page updateIndexPage(webModule); return generated.getPrimaryFile(); }
Example 10
Source File: NewKarmaConfWizardIterator.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Set instantiate() throws IOException { FileObject dir = Templates.getTargetFolder(wizard); FileObject template = Templates.getTemplate(wizard); DataFolder dataFolder = DataFolder.findFolder(dir); DataObject dataTemplate = DataObject.find(template); DataObject createdFile = dataTemplate.createFromTemplate(dataFolder, Templates.getTargetName(wizard)); return Collections.singleton(createdFile.getPrimaryFile()); }
Example 11
Source File: SCFTHandlerTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testBasePropertiesAlwaysPresent() throws Exception { FileObject root = FileUtil.createMemoryFileSystem().getRoot(); FileObject fo = FileUtil.createData(root, "simpleObject.txt"); OutputStream os = fo.getOutputStream(); String txt = "<html><h1>${name}</h1>" + "<h2>${date}</h2>" + "<h3>${time}</h3>" + "<h4>${user}</h4>" + "<h4>${dateTime?string(\"yy\")}</h4>" + "</html>"; os.write(txt.getBytes()); os.close(); fo.setAttribute("javax.script.ScriptEngine", "freemarker"); DataObject obj = DataObject.find(fo); DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); Map<String,String> parameters = Collections.singletonMap("title", "Nazdar"); DataObject n = obj.createFromTemplate(folder, "complex", parameters); assertEquals("Created in right place", folder, n.getFolder()); assertEquals("Created with right name", "complex.txt", n.getName()); String res = readFile(n.getPrimaryFile()); if (res.indexOf("date") >= 0) fail(res); if (res.indexOf("time") >= 0) fail(res); if (res.indexOf("user") >= 0) fail(res); if (res.indexOf("name") >= 0) fail(res); if (res.indexOf("dateTime") >= 0) fail(res); }
Example 12
Source File: Richfaces4Implementation.java From netbeans with Apache License 2.0 | 5 votes |
private static FileObject generateWelcomePage(WebModule webModule) throws IOException { FileObject templateFO = FileUtil.getConfigFile("Templates/Other/welcomeRichfaces.xhtml"); //NOI18N DataObject templateDO = DataObject.find(templateFO); DataObject generated = templateDO.createFromTemplate( DataFolder.findFolder(webModule.getDocumentBase()), "welcomeRichfaces"); //NOI18N JsfComponentUtils.reformat(generated); // update and reformat index page updateIndexPage(webModule); return generated.getPrimaryFile(); }
Example 13
Source File: ActivatorIterator.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Set<DataObject> instantiate () throws IOException/*, IllegalStateException*/ { // Here is the default plain behavior. Simply takes the selected // template (you need to have included the standard second panel // in createPanels(), or at least set the properties targetName and // targetFolder correctly), instantiates it in the provided // position, and returns the result. // More advanced wizards can create multiple objects from template // (return them all in the result of this method), populate file // contents on the fly, etc. org.openide.filesystems.FileObject dir = Templates.getTargetFolder( wiz ); DataFolder df = DataFolder.findFolder( dir ); FileObject template = Templates.getTemplate( wiz ); DataObject dTemplate = DataObject.find( template ); final DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wiz ) ); //this part might be turned pluggable once we have also ant based osgi projects. if.. Project project = Templates.getProject( wiz ); ClassPath cp = ClassPath.getClassPath(dobj.getPrimaryFile(), ClassPath.SOURCE); final String path = cp.getResourceName(dobj.getPrimaryFile(), '.', false); final NbMavenProject prj = project.getLookup().lookup(NbMavenProject.class); if (prj != null) { Utilities.performPOMModelOperations(project.getProjectDirectory().getFileObject("pom.xml"), Collections.<ModelOperation<POMModel>>singletonList( new ModelOperation<POMModel>() { @Override public void performOperation(POMModel model) { addActivator(prj, model, path); } } )); } return Collections.singleton(dobj); }
Example 14
Source File: ActEndpointWizardIterator.java From nb-springboot with Apache License 2.0 | 5 votes |
@Override public Set<?> instantiate() throws IOException { // Create file from template String targetName = Templates.getTargetName(wizard); FileObject foDir = Templates.getTargetFolder(wizard); FileObject foTemplate = Templates.getTemplate(wizard); DataObject doTemplate = DataObject.find(foTemplate); DataFolder df = DataFolder.findFolder(foDir); Map<String, Object> props = new HashMap<>(); props.put(WIZ_TECHNOLOGY, wizard.getProperty(WIZ_TECHNOLOGY)); props.put(WIZ_SAMPLE_OPS, wizard.getProperty(WIZ_SAMPLE_OPS)); DataObject doCreated = doTemplate.createFromTemplate(df, targetName, props); FileObject foCreated = doCreated.getPrimaryFile(); return Collections.singleton(foCreated); }
Example 15
Source File: DefaultFileObjectFromTemplateCreator.java From netbeans with Apache License 2.0 | 5 votes |
@Override public FileObject create(FileObject template, FileObject folder, String name) throws IOException { DataObject templateDobj = DataObject.find(template); if (templateDobj == null || !templateDobj.isTemplate()) { return FileUtil.createData(folder, name); } DataFolder target = DataFolder.findFolder(folder); String simpleName = FileObjects.stripExtension(name); DataObject newDobj = templateDobj.createFromTemplate(target, simpleName); return newDobj.getPrimaryFile(); }
Example 16
Source File: InterceptorGenerator.java From netbeans with Apache License 2.0 | 5 votes |
private void createInterceptor( InterceptorPanel panel, FileObject targetFolder ) { FileObject templateFileObject = FileUtil.getConfigFile( "Templates/Classes/Class.java"); // NOI18N try { DataObject templateDataObject = DataObject .find(templateFileObject); DataFolder dataFolder = DataFolder.findFolder(targetFolder); DataObject createdDataObject = templateDataObject.createFromTemplate( dataFolder,panel.getInterceptorName(), Collections.<String, Object> emptyMap()); modifyClass( createdDataObject.getPrimaryFile() , getType(myBindingFileObject, ElementKind.ANNOTATION_TYPE)); Project project = FileOwnerQuery.getOwner(myBindingFileObject); if ( project != null ){ CdiUtil logger = project.getLookup().lookup(CdiUtil.class); if ( logger != null ){ logger.log("USG_CDI_GENERATE_INTERCEPTOR", // NOI18N InterceptorGenerator.class, new Object[]{project.getClass().getName()}); } } } catch (IOException e) { LOG.log(Level.WARNING , null , e ); } }
Example 17
Source File: ClientSideProjectWizardIterator.java From netbeans with Apache License 2.0 | 4 votes |
private void createIndexFile(FileObject siteRoot) throws IOException { FileObject indexTemplate = FileUtil.getConfigFile("Templates/Other/html.html"); // NOI18N DataFolder dataFolder = DataFolder.findFolder(siteRoot); DataObject dataIndex = DataObject.find(indexTemplate); dataIndex.createFromTemplate(dataFolder, "index"); // NOI18N }
Example 18
Source File: SelectorUtils.java From netbeans with Apache License 2.0 | 4 votes |
/** Instantiate a template object. * Asks user for the target file's folder and creates the file. * @param project the project the template should be instantiated in * @param refFile the file for which bundle is created * @param template the template to use * @return the generated DataObject * @exception UserCancelException if the user cancels the action * @exception IOException on I/O error * @see DataObject#createFromTemplate */ public static DataObject instantiateTemplate(Project project, FileObject refFile, DataObject template) throws IOException { // Create component for for file name input. ObjectNameInputPanel panel = new ObjectNameInputPanel(); Node repositoryNode = SelectorUtils.bundlesNode(project, refFile, false); // Selects one folder from data systems. DataFolder dataFolder = NodeOperation.getDefault().select ( I18nUtil.getBundle().getString ("CTL_Template_Dialog_Title"), I18nUtil.getBundle().getString ("CTL_Template_Dialog_RootTitle"), repositoryNode, new NodeAcceptor() { public boolean acceptNodes(Node[] nodes) { if(nodes == null || nodes.length != 1) { return false; } DataFolder cookie = nodes[0].getCookie(DataFolder.class); return (cookie != null && cookie.getPrimaryFile().canWrite()); } }, panel )[0].getCookie(DataFolder.class); String name = panel.getText(); DataObject newObject; if(name.equals ("")) { // NOI18N newObject = template.createFromTemplate(dataFolder); } else { newObject = template.createFromTemplate(dataFolder, name); } try { return newObject; } catch(ClassCastException cce) { throw new UserCancelException(); } }
Example 19
Source File: SCFTHandlerTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testCreateWithNameAndExtForForm() throws Exception { LocalFileSystem lfs = new LocalFileSystem(); lfs.setRootDirectory(getWorkDir()); FileObject root = lfs.getRoot(); FileObject fo = FileUtil.createData(root, "j.java"); OutputStream os = fo.getOutputStream(); String txt = "print('<html><h1>', nameAndExt, '</h1></html>')"; os.write(txt.getBytes()); os.close(); fo.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); FileObject fo2 = FileUtil.createData(root, "j.form"); OutputStream os2 = fo2.getOutputStream(); String txt2 = "print('<html><h2>', nameAndExt, '</h2></html>')"; os2.write(txt2.getBytes()); os2.close(); fo2.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); DataObject obj = DataObject.find(fo); assertEquals("Both files", 2, obj.files().size()); DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); Map<String,String> parameters = Collections.emptyMap(); DataObject n = obj.createFromTemplate(folder, "complex", parameters); assertEquals("Two files", 2, n.files().size()); FileObject newForm = FileUtil.findBrother(n.getPrimaryFile(), "form"); assertEquals("Primary file is java", "java", n.getPrimaryFile().getExt()); assertNotNull("Form copied", newForm); DataObject frm = DataObject.find(newForm); assertSame("Form belongs to java", n, frm); assertEquals("Created in right place", folder, n.getFolder()); assertEquals("Created with right name", "complex", n.getName()); String exp = "<html><h1> complex.java </h1></html>\n"; assertEquals("Primary file" + n.getPrimaryFile(), exp, readFile(n.getPrimaryFile())); String exp2 = "<html><h2> complex.form </h2></html>\n"; assertEquals(exp2, readFile(newForm)); }
Example 20
Source File: SCFTHandlerTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testCreateWithNameAndExtForForm() throws Exception { LocalFileSystem lfs = new LocalFileSystem(); lfs.setRootDirectory(getWorkDir()); FileObject root = lfs.getRoot(); FileObject fo = FileUtil.createData(root, "j.java"); OutputStream os = fo.getOutputStream(); String txt = "<html><h1>${nameAndExt}</h1></html>"; os.write(txt.getBytes()); os.close(); fo.setAttribute("javax.script.ScriptEngine", "freemarker"); FileObject fo2 = FileUtil.createData(root, "j.form"); OutputStream os2 = fo2.getOutputStream(); String txt2 = "<html><h2>${nameAndExt}</h2></html>"; os2.write(txt2.getBytes()); os2.close(); fo2.setAttribute("javax.script.ScriptEngine", "freemarker"); DataObject obj = DataObject.find(fo); assertEquals("Both files", 2, obj.files().size()); DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); Map<String,String> parameters = Collections.emptyMap(); DataObject n = obj.createFromTemplate(folder, "complex", parameters); assertEquals("Two files", 2, n.files().size()); FileObject newForm = FileUtil.findBrother(n.getPrimaryFile(), "form"); assertEquals("Primary file is java", "java", n.getPrimaryFile().getExt()); assertNotNull("Form copied", newForm); DataObject frm = DataObject.find(newForm); assertSame("Form belongs to java", n, frm); assertEquals("Created in right place", folder, n.getFolder()); assertEquals("Created with right name", "complex", n.getName()); String exp = "<html><h1>complex.java</h1></html>"; assertEquals("Primary file" + n.getPrimaryFile(), exp, readFile(n.getPrimaryFile())); String exp2 = "<html><h2>complex.form</h2></html>"; assertEquals(exp2, readFile(newForm)); }