Skip to content

Instantly share code, notes, and snippets.

@spyhunter99
Created May 12, 2017 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spyhunter99/9a4a802139011f2fc6eddc1829815b7c to your computer and use it in GitHub Desktop.
Save spyhunter99/9a4a802139011f2fc6eddc1829815b7c to your computer and use it in GitHub Desktop.
Using java, generates Java constants for all HTTP mime types
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class MimeGenerator {
public static void main(String[] args) throws Exception {
URL url = new URL("https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line = null;
int lineNumber=0;
while ((line = in.readLine()) != null) {
lineNumber++;
if (lineNumber > 15){
//skip the headers
line = line.trim();
if (line.startsWith("#"))
line = line.substring(1).trim();
if (line.contains("\t")) {
//with an extension
line = line.substring(0, line.indexOf("\t"));
}
System.out.println("public static String " + safeName(line) + " = \"" + line + "\";");
}
}
}
private static String safeName(String line) {
String text = line.toUpperCase();
return text.replace("/", "_").replace("-", "_DASH_").replace("+", "_PLUS_").replace(".", "_DOT_");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment