src/main/java/de/unixwork/uwproj/Main.java

changeset 39
3ca85da78515
parent 7
686991ae6e2f
child 42
becfbf7af928
--- a/src/main/java/de/unixwork/uwproj/Main.java	Sun Sep 03 12:43:44 2023 +0200
+++ b/src/main/java/de/unixwork/uwproj/Main.java	Wed Sep 06 21:08:04 2023 +0200
@@ -1,69 +1,44 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
 package de.unixwork.uwproj;
 
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.io.StringWriter;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
-import org.xml.sax.SAXException;
+
+import java.io.*;
+
+public class Main {
+
+    static Project loadProjectFile(String fileName) {
+        try {
+            return new Project(new File(fileName));
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
 
-/**
- *
- * @author olaf
- */
-public class Main {
+    static void writeConfigureScript(Writer out, String tplFileName, Project project) {
+        var context = new VelocityContext();
+        context.put("targets", project.getTargets());
+        context.put("namedDependencies", project.getNamedDependencies());
+        context.put("dependencies", project.getDependencies());
+        context.put("options", project.getOptions());
+        context.put("features", project.getFeatures());
+        context.put("project", project);
+        context.put("vars", project.getVars());
+        new VelocityEngine().getTemplate(tplFileName).merge(context, out);
+    }
+
     public static void main(String[] args){
-        File f = new File("make/project.xml");
-        Project p = null;
-        try {
-            p = new Project(f);
-        } catch (SAXException ex) {
-            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
-        } catch (IOException ex) {
-            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+        final var inFileName = "make/project.xml";
+        final var tplFileName = "make/configure.vm";
+        final var outFileName = "configure";
+
+        try (var out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFileName)))) {
+            writeConfigureScript(out, tplFileName, loadProjectFile(inFileName));
         } catch (Exception ex) {
-            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
-        }
-        if(p == null) {
+            ex.printStackTrace();
             System.exit(1);
         }
-        
-        VelocityEngine ve = new VelocityEngine();
-        Template t = ve.getTemplate("make/configure.vm");
-        VelocityContext context = new VelocityContext();
-        context.put("targets", p.getTargets());
-        context.put("namedDependencies", p.getNamedDependencies());
-        context.put("dependencies", p.getDependencies());
-        context.put("options", p.getOptions());
-        context.put("features", p.getFeatures());
-        context.put("project", p);
-        context.put("vars", p.getVars());
-        
-        int ret = 1;
-        try {
-            FileOutputStream fout = new FileOutputStream(new File("configure"));
-            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fout));
-            t.merge(context, out);
-            out.close();
-            ret = 0;
-            System.out.println("Out: configure");
-        } catch (FileNotFoundException ex) {
-            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
-        } catch (IOException ex) {
-            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
-        }
-        System.exit(ret);
+
+        System.out.println("Out: "+outFileName);
     }
 }

mercurial