我正在制作一个Spring Boot生成器插件,我想用Velocity和/或Java加载一些模板文件。首选Java。
现在,问题是,我无法弄清楚如何用Java读取* .vm模板文件并将其转换为String变量。
因此,在我的屏幕快照中,我想加载“ java_dto.vm”(例如,使用Java加载),以便将其内容转换为字符串。
Folder structure and files of plugin I am developing
这是我的Java_dto.vm文件的样子:
package $dtoPackage;
public class $entityName#[[Dto]]# {
public $entityName#[[Dto]]#() {
}
}
我在普通的Java类中尝试了一些操作,以成功加载到文件中。 但是现在在插件本身中,我无法正常工作。
所以,这不是我的插件,而是我尝试过的示例。
Project structure of tryout code to load in a file
我使用代码尝试了全部并全部成功。
`
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";
File jarFile = new File(Main.class.getProtectionDomain()
.getCodeSource().getLocation()
.toURI().getPath());
String inputFilePath = jarFile + File.separator +
"com\\tutorialspoint\\templates\\" + fileName;
Path path = Paths.get(inputFilePath);
String stringFromFile =
java.nio.file.Files.lines(path).collect(Collectors.joining());
Path path2 = Paths.get(inputFilePath);
String stringFromFile2 = new
String(java.nio.file.Files.readAllBytes(path2));
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());
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 + File.separator +
"com\\tutorialspoint\\templates\\" + 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());
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);
String s = new StringBuilder()
.append("line1\n")
.append("line2\n")
.append("line3\n")
.toString();
System.out.println(s);
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);
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");
StringSubstitutor subtest = new StringSubstitutor(valuesTest);
String resolvedStringTest = subtest.replace(java_test);
}
}
`
有人知道这可以解决/解决在intellij想法的插件中使用吗?因此,没有filechooser对话框,名称已经知道并且位置也位于(在templates文件夹中)。
已经感谢您的关注。
如果不清楚,请告诉我。
Filip