use of org.apache.cxf.tools.util.FileWriterUtil in project cxf by apache.
the class AbstractWSDLToProcessor method getOutputWriter.
protected Writer getOutputWriter(String newNameExt) throws ToolException {
Writer writer = null;
String newName = null;
String outputDir;
if (env.get(ToolConstants.CFG_OUTPUTFILE) != null) {
newName = (String) env.get(ToolConstants.CFG_OUTPUTFILE);
} else {
String oldName = (String) env.get(ToolConstants.CFG_WSDLURL);
int position = oldName.lastIndexOf("/");
if (position < 0) {
position = oldName.lastIndexOf("\\");
}
if (position >= 0) {
oldName = oldName.substring(position + 1, oldName.length());
}
if (oldName.toLowerCase().indexOf(WSDL_FILE_NAME_EXT) >= 0) {
newName = oldName.substring(0, oldName.length() - 5) + newNameExt + WSDL_FILE_NAME_EXT;
} else {
newName = oldName + newNameExt;
}
}
if (env.get(ToolConstants.CFG_OUTPUTDIR) != null) {
outputDir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
if (!("/".equals(outputDir.substring(outputDir.length() - 1)) || "\\".equals(outputDir.substring(outputDir.length() - 1)))) {
outputDir = outputDir + "/";
}
} else {
outputDir = "./";
}
FileWriterUtil fw = new FileWriterUtil(outputDir, env.get(OutputStreamCreator.class));
try {
writer = fw.getWriter("", newName);
} catch (IOException ioe) {
Message msg = new Message("FAIL_TO_WRITE_FILE", LOG, env.get(ToolConstants.CFG_OUTPUTDIR) + System.getProperty("file.seperator") + newName);
throw new ToolException(msg, ioe);
}
return writer;
}
use of org.apache.cxf.tools.util.FileWriterUtil in project cxf by apache.
the class AbstractGenerator method parseOutputName.
protected Writer parseOutputName(String packageName, String filename, String ext) throws ToolException {
FileWriterUtil fw = null;
Writer writer = null;
if (wantToKeep() && isCollision(packageName, filename, ext)) {
Message msg = new Message("SKIP_GEN", LOG, packageName + "." + filename + ext);
LOG.log(Level.INFO, msg.toString());
return null;
}
fw = new FileWriterUtil(getOutputDir(), env.get(OutputStreamCreator.class));
try {
if (".java".equals(ext)) {
writer = fw.getWriter(packageName, filename + ext, (String) getEnvironment().get(ToolConstants.CFG_ENCODING));
} else {
writer = fw.getWriter(packageName, filename + ext);
}
} catch (IOException ioe) {
Message msg = new Message("FAIL_TO_WRITE_FILE", LOG, packageName + "." + filename + ext);
throw new ToolException(msg, ioe);
}
return writer;
}
use of org.apache.cxf.tools.util.FileWriterUtil in project cxf by apache.
the class AbstractGeneratorTest method testOverwrite.
@Test
public void testOverwrite() throws Exception {
gen = new DummyGenerator();
util = new FileWriterUtil(output.toString(), null);
context = new ToolContext();
context.put(ToolConstants.CFG_OUTPUTDIR, output.toString());
gen.setEnvironment(context);
Writer writer = util.getWriter(packageName, className + ".java");
writer.write("hello world");
writer.flush();
writer.close();
assertNotNull(gen.parseOutputName(packageName, className));
}
use of org.apache.cxf.tools.util.FileWriterUtil in project cxf by apache.
the class BeanGenerator method generateAndCompile.
public void generateAndCompile(Collection<JavaClass> wrapperClasses, File dir) {
VelocityGenerator generator = new VelocityGenerator(false);
generator.setBaseDir(dir.toString());
List<File> generatedFiles = new ArrayList<>();
try {
for (JavaClass wrapperClass : wrapperClasses) {
generator.setCommonAttributes();
generator.setAttributes("bean", wrapperClass);
File file = generator.parseOutputName(wrapperClass.getPackageName(), wrapperClass.getName());
generatedFiles.add(file);
generator.doWrite(TEMPLATE, new FileWriterUtil(file.getParent(), getOutputStreamCreator()).getWriter(file, (String) getToolContext().get(ToolConstants.CFG_ENCODING)));
generator.clearAttributes();
}
// compile the classes
Compiler compiler = new Compiler();
compiler.setOutputDir(compileToDir);
List<String> files = new ArrayList<>(generatedFiles.size());
for (File file : generatedFiles) {
files.add(file.getAbsolutePath());
}
compiler.compileFiles(files.toArray(new String[files.size()]));
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.apache.cxf.tools.util.FileWriterUtil in project cxf by apache.
the class DateTypeCustomGenerator method generate.
public File generate(File outputdir) {
Class<?> dateType = getDateType();
File xjb = getJAXBCustFile(outputdir);
if (dateType != null) {
VelocityGenerator generator = new VelocityGenerator(false);
generator.setCommonAttributes();
generator.setAttributes("parseMethod", getAdapterMethod(dateType, ".parseDateTime"));
generator.setAttributes("printMethod", getAdapterMethod(dateType, ".printDateTime"));
generator.setAttributes("datetype", dateType.getName());
if (allowImports()) {
if (schemaFiles.isEmpty()) {
return null;
}
generator.setAttributes("schemaFiles", schemaFiles);
} else {
generator.setAttributes("wsdlName", wsdlName);
List<String> ns = getSchemaNamespaces();
if (ns.isEmpty()) {
return null;
}
generator.setAttributes("targetNamespaces", ns);
}
try {
generator.doWrite(getTemplate(), new FileWriterUtil(xjb.getParent(), getOutputStreamCreator()).getWriter(xjb, StandardCharsets.UTF_8.name()));
} catch (Exception e) {
e.printStackTrace();
}
generator.clearAttributes();
}
return xjb;
}
Aggregations