我正在制作一个Spring Boot生成器插件,我想用Velocity和/或Java加载一些模板文件。 首选Java。
现在的问题是,我无法弄清楚如何在Java中读取* .vm模板文件并将其转换为String变量。
因此,在我的屏幕快照中,我想加载“ java_dto.vm”(例如,使用Java加载),以便将其内容转换为String。
这是我的Java_dto.vm文件的样子:
package $dtoPackage;
public class $entityName#[[Dto]]# {
public $entityName#[[Dto]]#() {
}
}
我在普通的Java类中尝试了一些操作,以成功加载文件。 但是现在在插件本身中,我无法正常工作。
因此,这不是我的插件,而是我尝试过的示例。
有了代码,我全力以赴,一切正常。
`
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 valuesMap = new HashMap();
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 valuesTest = new HashMap();
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文件夹中)。
感谢您的关注。
如果不清楚,请告诉我。
菲利普