I cant figure out how to load an file in my plugin to use as a string in the background of my plugin?
Hi,
I am making a Spring Boot generator plugin and I wanna load in some template files with Velocity and/or Java. Preferable Java.
Now, the problem is, that I just cant figure out how to read in a *.vm template file in Java and convert it to a String variable.
So, here in my screenshot, I wanna load my "java_dto.vm", for example in with Java so that its content get converted to a String.
This is how my Java_dto.vm file looks like:
------------------------------------------------------------------------------------
package $dtoPackage;
public class $entityName#[[Dto]]# {
public $entityName#[[Dto]]#() {
}
}
------------------------------------------------------------------------------------
I tried some things out in a normal Java class to load in a file which was successful.
But in the plugin itself now, I cant get this working.
So, this is NOT my plugin but the example I tried it in out.
With the code i tried all out and all worked.
-------------------------------------------------------
package com.tutorialspoint;
import org.apache.commons.text.StringSubstitutor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
try {
// Determine where the input file is; assuming it's in the same directory as the jar
String fileName = "java_dto.vm";
// C:\\Users\\fvandeney\\Documents\\Github\\samples\\untitled1\\out\\production\\java_dto.vm
File jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
// C:\\Users\\fvandeney\\Documents\\Github\\samples\\untitled1\\out\\production\\untitled1
System.out.println(jarFile.toString());
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
// C:\\Users\\fvandeney\\Documents\\Github\\samples\\untitled1\\out\\production\\untitled1
System.out.println(jarFile.getAbsolutePath());
System.out.println("string inpufilepath");
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
//String inputFilePath = jarFile.getParent() + File.separator + "untitled1\\com\\tutorialspoint\\" + fileName;
// String inputFilePath = jarFile.getParent() + File.separator + fileName;
// C:\\Users\\fvandeney\\Documents\\Github\\samples\\untitled1\\out\\production\\untitled1\\java_dto.vm
String inputFilePath = jarFile + File.separator + "com\\tutorialspoint\\templates\\" + fileName;
System.out.println(inputFilePath);
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
System.out.println("path1");
Path path = Paths.get(inputFilePath);
String stringFromFile = java.nio.file.Files.lines(path).collect(Collectors.joining());
System.out.println(stringFromFile);
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
System.out.println("path2");
// ok
Path path2 = Paths.get(inputFilePath);
String stringFromFile2 = new String(java.nio.file.Files.readAllBytes(path2));
System.out.println(stringFromFile2);
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
FileInputStream inStream = new FileInputStream(new File(inputFilePath));
try {
// Read in the contents of the input file in a single gulp
FileChannel fc = inStream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
// Do something with the read in data
System.out.println("-----------");
// hier wordt content uitgeprint
System.out.println(Charset.defaultCharset().decode(bb).toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
inStream.close();
}
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
//---------
try {
// Determine where the input file is; assuming it's in the same directory as the jar
String fileName = "java_dto.vm";
// C:\\Users\\fvandeney\\Documents\\Github\\samples\\untitled1\\out\\production\\java_dto.vm
File jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
// C:\\Users\\fvandeney\\Documents\\Github\\samples\\untitled1\\out\\production\\untitled1
System.out.println(jarFile.toString());
// C:\\Users\\fvandeney\\Documents\\Github\\samples\\untitled1\\out\\production\\untitled1
System.out.println(jarFile.getAbsolutePath());
System.out.println("string inpufilepath");
//String inputFilePath = jarFile.getParent() + File.separator + "untitled1\\com\\tutorialspoint\\" + fileName;
// String inputFilePath = jarFile.getParent() + File.separator + fileName;
// C:\\Users\\fvandeney\\Documents\\Github\\samples\\untitled1\\out\\production\\untitled1\\java_dto.vm
String inputFilePath = jarFile + File.separator + "com\\tutorialspoint\\templates\\" + fileName;
System.out.println(inputFilePath);
FileInputStream inStream = new FileInputStream(new File(inputFilePath));
try {
// Read in the contents of the input file in a single gulp
FileChannel fc = inStream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
// Do something with the read in data
System.out.println("-----------");
// hier wordt content uitgeprint
System.out.println(Charset.defaultCharset().decode(bb).toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
inStream.close();
}
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
//---------
try {
// Determine where the input file is; assuming it's in the same directory as the jar
String fileName = "java_dto.vm";
File jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
String inputFilePath = jarFile.getParent() + File.separator + "untitled1\\com\\tutorialspoint\\" + fileName;
FileInputStream inStream = new FileInputStream(new File(inputFilePath));
try {
// Read in the contents of the input file in a single gulp
FileChannel fc = inStream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
// Do something with the read in data
System.out.println("-----------");
// hier wordt content uitgeprint
System.out.println(Charset.defaultCharset().decode(bb).toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
inStream.close();
}
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
Map<String, String> valuesMap = new HashMap<String, String>();
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", "lazy dog");
String templateString = "The ${animal} jumped over the ${target}.";
System.out.println("templateString is: ");
System.out.println(templateString);
StringSubstitutor sub = new StringSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
System.out.println("resolvedstring is: ");
System.out.println(resolvedString);
//System.out.println(sub.replace(templateString, valuesMap));
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
String s = new StringBuilder()
.append("line1\n")
.append("line2\n")
.append("line3\n")
.toString();
System.out.println(s);
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
String ss = new StringBuilder()
.append("package $dtoPackage;")
.append("\n")
.append("\n")
.append("public class $entityName#[[Dto]]# {")
.append("\n")
.append("\n")
.append("public void haha() { ")
.append("\n")
.append("\n")
.append("}")
.append("\n")
.append("}").toString();
System.out.println(ss);
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
String java_test = "package ${dtoPackage};" +
"\n" +
"\n" +
"public class ${entityName}#[[Dto]]# {" +
"\n" +
"\n" +
" public void haha() { " +
"\n" +
" } " +
" \n " +
"}";
Map<String, String> valuesTest = new HashMap<String, String>();
valuesTest.put("dtoPackage", "com.filip.springboot.dto");
valuesTest.put("entityName", "Test");
System.out.println("templateStringTest is: ");
System.out.println(java_test);
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
StringSubstitutor subtest = new StringSubstitutor(valuesTest);
String resolvedStringTest = subtest.replace(java_test);
System.out.println("resolvedStringTest is: ");
System.out.println(resolvedStringTest);
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
}
}
---------------------------------------------------------------------------------------------------
Does anybody know this can be fixed/solved to use in a plugin for intellij idea? So no filechooser dialog, the name is already known and location also (in the templates folder).
Thanks already for your attention.
If something is not clear, let me know please.
Filip
Please sign in to leave a comment.
https://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/files.html