ソースを参照

Feature: adding output format and hardware destination to web assembler

Klaas, Wilfried 4 年 前
コミット
e62a4f7589

+ 2 - 1
spsassembler/buildDocker.cmd

@@ -1,4 +1,5 @@
-docker image build -t spsassembler:1.0 -t spsassembler:latest .
+call mvn package
+docker image build -t spsassembler:1.1 -t spsassembler:latest .
 docker run --restart=always -d -p8180:8080 -p8181:8081 --name spsassembler spsassembler
 docker save -o="spsassembler.tar" spsassembler
 copy spsassembler.tar \\192.168.178.14\Docker\dockerbuild\spsassembler\

+ 6 - 1
spsassembler/src/main/java/de/mcs/tools/sps/HEXTextOutputter.java

@@ -1,9 +1,9 @@
 package de.mcs.tools.sps;
+
 /**
  * 
  */
 
-
 import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -55,4 +55,9 @@ public class HEXTextOutputter implements Outputter {
     return ".txt";
   }
 
+  @Override
+  public String getMimeType() {
+    return "text/plain";
+  }
+
 }

+ 7 - 2
spsassembler/src/main/java/de/mcs/tools/sps/IntelHEXOutputter.java

@@ -1,9 +1,9 @@
 package de.mcs.tools.sps;
+
 /**
  * 
  */
 
-
 import java.io.OutputStream;
 import java.util.List;
 
@@ -15,7 +15,7 @@ import de.mcs.tools.sps.utils.IntelHex;
  * @author w.klaas
  *
  */
-@SPSOutputter(name = "HEX")
+@SPSOutputter(name = "INTELHEX")
 public class IntelHEXOutputter implements Outputter {
 
   /**
@@ -45,4 +45,9 @@ public class IntelHEXOutputter implements Outputter {
     return ".hex";
   }
 
+  @Override
+  public String getMimeType() {
+    return "text/plain";
+  }
+
 }

+ 53 - 0
spsassembler/src/main/java/de/mcs/tools/sps/OutputFactory.java

@@ -0,0 +1,53 @@
+package de.mcs.tools.sps;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.reflections.Reflections;
+import org.reflections.util.ClasspathHelper;
+import org.reflections.util.ConfigurationBuilder;
+
+import de.mcs.tools.sps.annotations.SPSOutputter;
+
+public class OutputFactory {
+
+  private static final String DEFAULT_PACKAGE_FILTER = "de.mcs.tools.sps"; //$NON-NLS-1$
+
+  private static Map<String, Outputter> outputters = new HashMap<>();
+
+  static {
+    Set<Class<?>> outputClasses = searchOutputter(SPSOutputter.class);
+    for (Class<?> outClass : outputClasses) {
+      SPSOutputter annotation = outClass.getAnnotation(SPSOutputter.class);
+      try {
+        Outputter outputter = (Outputter) outClass.getConstructor().newInstance();
+        outputters.put(annotation.name().toUpperCase(), outputter);
+      } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
+          | NoSuchMethodException | SecurityException e) {
+        e.printStackTrace();
+      }
+    }
+
+  }
+
+  private static Set<Class<?>> searchOutputter(Class<? extends Annotation> annotationClass) {
+    ConfigurationBuilder builder = new ConfigurationBuilder();
+    builder.addUrls(ClasspathHelper.forPackage(DEFAULT_PACKAGE_FILTER));
+    Reflections reflections = new Reflections(builder);
+    Set<Class<?>> classes = new HashSet<Class<?>>();
+    classes.addAll(reflections.getTypesAnnotatedWith(annotationClass));
+    return classes;
+  }
+
+  public static Outputter getOutputter(String outputformat) throws Exception {
+    Outputter outputter = outputters.get(outputformat.toUpperCase());
+    if (outputter == null) {
+      throw new Exception(String.format("can't find outputter for format: %s", outputformat));
+    }
+    return outputter;
+  }
+}

+ 1 - 0
spsassembler/src/main/java/de/mcs/tools/sps/Outputter.java

@@ -18,4 +18,5 @@ public interface Outputter {
 
   String getDefaultExtension();
 
+  String getMimeType();
 }

+ 6 - 1
spsassembler/src/main/java/de/mcs/tools/sps/TPSTextOutputter.java

@@ -1,9 +1,9 @@
 package de.mcs.tools.sps;
+
 /**
  * 
  */
 
-
 import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -65,4 +65,9 @@ public class TPSTextOutputter implements Outputter {
     return ".txt";
   }
 
+  @Override
+  public String getMimeType() {
+    return "text/plain";
+  }
+
 }

+ 9 - 3
spsassembler/src/main/java/de/mcs/tools/sps/service/AsmResource.java

@@ -12,9 +12,11 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
 
+import org.apache.commons.lang3.StringUtils;
+
 import com.codahale.metrics.annotation.Timed;
 
-import de.mcs.tools.sps.IntelHEXOutputter;
+import de.mcs.tools.sps.OutputFactory;
 import de.mcs.tools.sps.Outputter;
 import de.mcs.tools.sps.SPSAssembler;
 import de.mcs.tools.sps.exceptions.HardwareException;
@@ -35,7 +37,9 @@ public class AsmResource {
   public AsmModel assemble(AsmModel model) throws HardwareException {
     try {
       HARDWARE destination = HARDWARE.HOLTEK;
-
+      if (StringUtils.isEmpty(model.getOutputformat())) {
+        model.setOutputformat("INTELHEX");
+      }
       try {
         destination = HARDWARE.valueOf((model.getHardware().toUpperCase()));
       } catch (IllegalArgumentException e) {
@@ -49,10 +53,12 @@ public class AsmResource {
       List<Mnemonic> mnemonics = spsAssembler.getMnemonics();
       ByteArrayOutputStream output = new ByteArrayOutputStream();
 
-      Outputter outputter = new IntelHEXOutputter();
+      Outputter outputter = OutputFactory.getOutputter(model.getOutputformat());
       outputter.output(mnemonics, output);
 
       model.setHexfile(new String(output.toByteArray()));
+      model.setMimetype(outputter.getMimeType());
+      model.setExtension(outputter.getDefaultExtension());
 
       return model;
     } catch (Exception e) {

+ 30 - 0
spsassembler/src/main/java/de/mcs/tools/sps/service/model/AsmModel.java

@@ -15,9 +15,15 @@ public class AsmModel {
   @JsonProperty
   private String hardware;
   @JsonProperty
+  private String outputformat;
+  @JsonProperty
   private String content;
   @JsonProperty
   private String hexfile;
+  @JsonProperty
+  private String mimetype;
+  @JsonProperty
+  private String extension;
 
   public String getName() {
     return name;
@@ -51,4 +57,28 @@ public class AsmModel {
     this.hexfile = hexfile;
   }
 
+  public String getOutputformat() {
+    return outputformat;
+  }
+
+  public void setOutputformat(String outputformat) {
+    this.outputformat = outputformat;
+  }
+
+  public String getMimetype() {
+    return mimetype;
+  }
+
+  public void setMimetype(String mimetype) {
+    this.mimetype = mimetype;
+  }
+
+  public String getExtension() {
+    return extension;
+  }
+
+  public void setExtension(String extension) {
+    this.extension = extension;
+  }
+
 }

+ 7 - 4
spsassembler/src/main/php/ass.php

@@ -2,13 +2,15 @@
 
 $source = $_POST["source"];
 $filename = $_POST["name"];
+$dest = $_POST["dest"];
+$outputformat = $_POST["output"];
 $name = pathinfo($filename, PATHINFO_FILENAME);
 $url = "http://192.168.178.14:8180/assemble";
 
 $source = str_replace("\r", "", $source);
 $source = str_replace("\n", "\\n", $source);
 
-$content = "{\"name\":\"$filename\",\"hardware\":\"arduinosps\",\"content\":\"$source\"}";
+$content = "{\"name\":\"$filename\",\"hardware\":\"$dest\",\"outputformat\":\"$outputformat\",\"content\":\"$source\"}";
 
 $data = $content;
 
@@ -32,9 +34,10 @@ $code = $info["http_code"];
 $result = json_decode($response);
 
 if ($code == 200) {
-
-	header('Content-Type: text/simple');
-	header("Content-Disposition: attachment; filename=\"$name.hex\"");
+	$mime = $result->mimetype;
+	$ext = $result->extension;
+	header("Content-Type: $mime");
+	header("Content-Disposition: attachment; filename=\"$name$ext\"");
 
 	echo $result->hexfile;
 } else {

+ 12 - 1
spsassembler/src/main/php/index.php

@@ -56,7 +56,18 @@
 			<button type="submit">load</button> 
 		</form>
 		<form  method="post" action="ass.php" target="_blank">
-			Name: <input id="name" name="name">
+			<label for="name">Name:</label><input id="name" name="name"><br/>
+			<label for="dest">Destination:</label><select id="dest" name="dest">
+			<option value="HOLTEK">Holtek</option>
+			<option value="ATMEGA8">ATMega 8</option>
+			<option value="ARDUINOSPS">Arduino SPS</option>
+			<option value="TINYSPS">ATtiny84 SPS</option> 
+			</select>
+			<label for="output">Outputformat:</label><select id="output" name="output">
+			<option value="INTELHEX">Intel HEX Code</option>
+			<option value="TPSTXT">SPS Emulator Source</option>
+			<option value="HEXTXT">HEX Text</option>
+			</select><br/>
 			<textarea class="lined" rows="20" cols="60" name="source"></textarea>
 			<button type="submit">Assemble</button> 
 		</form>