Added MimeTypeUtil class

This commit is contained in:
Ziver Koc 2018-10-10 22:22:47 +02:00
parent 8b4217c1d3
commit 62923e7f60
3 changed files with 579 additions and 98 deletions

108
src/zutil/MimeTypeUtil.java Normal file
View file

@ -0,0 +1,108 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil;
import zutil.io.file.FileUtil;
import zutil.parser.DataNode;
import zutil.parser.json.JSONParser;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;
/**
* Utility class for MIME type definitions
*/
public class MimeTypeUtil {
// Define mime types
static {
try {
readMimeFile("zutil/data/mime.json");
} catch (IOException e) {
e.printStackTrace();
}
}
// Static variables
private static final ArrayList<MimeType> mimes = new ArrayList<MimeType>();
private static void readMimeFile(String path) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(FileUtil.find(path)));
DataNode json = new JSONParser(in).read();
for (Iterator<String> it = json.keyIterator(); it.hasNext(); ) {
String primaryType = it.next();
for (Iterator<String> it2 = json.get(primaryType).keyIterator(); it2.hasNext(); ) {
String subType = it2.next();
DataNode mimeJson = json.get(primaryType).get(subType);
addMimeType(new MimeType(
primaryType,
subType,
mimeJson.getString("description"),
mimeJson.getList("extensions")));
}
}
}
private static void addMimeType(MimeType mime){
mimes.add(mime);
}
public static class MimeType{
private final String primaryType;
private final String subType;
private final String description;
private final String[] extensions;
private MimeType(String primary, String subType, String description, List extensions) {
this.primaryType = primary;
this.subType = subType;
this.description = description;
this.extensions = (String[]) extensions.toArray(new String[0]);
}
public String getPrimaryType() {
return primaryType;
}
public String getSubType() {
return subType;
}
public String toString() {
return primaryType + "/" + subType;
}
}
}

242
src/zutil/data/mime.json Normal file
View file

@ -0,0 +1,242 @@
{
"audio": {
"aac": {
"description": "AAC audio",
"extensions": ["aac"]
},
"midi": {
"description": "Musical Instrument Digital Interface (MIDI)",
"extensions": ["mid", "midi"]
},
"ogg": {
"description": "OGG audio",
"extensions": ["oga"]
},
"wav": {
"description": "Waveform Audio Format",
"extensions": ["wav"]
},
"webm": {
"description": "WEBM audio",
"extensions": ["weba"]
}
},
"application": {
"octet-stream": {
"description": "Binary data",
"extensions": ["arc", "bin"]
},
"vnd.amazon.ebook": {
"description": "Amazon Kindle eBook format",
"extensions": ["azw"]
},
"x-bzip": {
"description": "BZip archive",
"extensions": ["bz"]
},
"x-bzip2": {
"description": "BZip2 archive",
"extensions": ["bz2"]
},
"x-csh": {
"description": "C-Shell script",
"extensions": ["csh"]
},
"msword": {
"description": "Microsoft Word",
"extensions": ["doc"]
},
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": {
"description": "Microsoft Word (OpenXML)",
"extensions": ["docx"]
},
"epub+zip": {
"description": "Electronic publication (EPUB)",
"extensions": ["epub"]
},
"java-archive": {
"description": "Java Archive (JAR)",
"extensions": ["jar"]
},
"json": {
"description": "JSON format",
"extensions": ["json"]
},
"vnd.apple.installer+xml": {
"description": "Apple Installer Package",
"extensions": ["mpkg"]
},
"vnd.oasis.opendocument.presentation": {
"description": "OpenDocument presentation document",
"extensions": ["odp"]
},
"vnd.oasis.opendocument.spreadsheet": {
"description": "OpenDocument spreadsheet document",
"extensions": ["ods"]
},
"vnd.oasis.opendocument.text": {
"description": "OpenDocument text document",
"extensions": ["odt"]
},
"ogg": {
"description": "OGG",
"extensions": ["ogx"]
},
"pdf": {
"description": "Adobe Portable Document Format (PDF)",
"extensions": ["pdf"]
},
"vnd.ms-powerpoint": {
"description": "Microsoft PowerPoint",
"extensions": ["ppt"]
},
"vnd.openxmlformats-officedocument.presentationml.presentation": {
"description": "Microsoft PowerPoint (OpenXML)",
"extensions": ["pptx"]
},
"x-rar-compressed": {
"description": "RAR archive",
"extensions": ["rar"]
},
"rtf": {
"description": "Rich Text Format (RTF)",
"extensions": ["rtf"]
},
"x-sh": {
"description": "Bourne shell script",
"extensions": ["sh"]
},
"x-shockwave-flash": {
"description": "Small web format (SWF) or Adobe Flash document",
"extensions": ["swf"]
},
"x-tar": {
"description": "Tape Archive (TAR)",
"extensions": ["tar"]
},
"typescript": {
"description": "Typescript file",
"extensions": ["ts"]
},
"xhtml+xml": {
"description": "XHTML",
"extensions": ["xhtml"]
},
"vnd.ms-excel": {
"description": "Microsoft Excel",
"extensions": ["xls"]
},
"vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
"description": "Microsoft Excel (OpenXML)",
"extensions": ["xlsx"]
},
"xml": {
"description": "XML",
"extensions": ["xml"]
},
"zip": {
"description": "ZIP archive",
"extensions": ["zip"]
},
"x-7z-compressed": {
"description": "7-zip archive",
"extensions": ["7z"]
}
},
"font": {
"otf": {
"description": "OpenType font",
"extensions": ["otf"]
},
"ttf": {
"description": "TrueType Font",
"extensions": ["ttf"]
},
"woff": {
"description": "Web Open Font Format (WOFF)",
"extensions": ["woff"]
},
"woff2": {
"description": "Web Open Font Format (WOFF)",
"extensions": ["woff2"]
}
},
"video": {
"x-msvideo": {
"description": "AVI: Audio Video Interleave",
"extensions": ["avi"]
},
"mpeg": {
"description": "MPEG Video",
"extensions": ["mpeg"]
},
"ogg": {
"description": "OGG video",
"extensions": ["ogv"]
},
"webm": {
"description": "WEBM video",
"extensions": ["webm"]
}
},
"image": {
"bmp": {
"description": "Windows OS/2 Bitmap Graphics",
"extensions": ["bmp"]
},
"gif": {
"description": "Graphics Interchange Format (GIF)",
"extensions": ["gif"]
},
"x-icon": {
"description": "Icon format",
"extensions": ["ico"]
},
"jpeg": {
"description": "JPEG image",
"extensions": ["jpeg", "jpg"]
},
"png": {
"description": "Portable Network Graphics",
"extensions": ["png"]
},
"svg+xml": {
"description": "Scalable Vector Graphics (SVG)",
"extensions": ["svg"]
},
"tiff": {
"description": "Tagged Image File Format (TIFF)",
"extensions": ["tif", "tiff"]
},
"webp": {
"description": "WEBP image",
"extensions": ["webp"]
}
},
"text":{
"css": {
"description": "Cascading Style Sheets (CSS)",
"extensions": ["css"]
},
"csv": {
"description": "Comma-separated values (CSV)",
"extensions": ["csv"]
},
"html": {
"description": "HyperText Markup Language (HTML)",
"extensions": ["htm", "html"]
},
"calendar": {
"description": "iCalendar format",
"extensions": ["ics"]
},
"javascript": {
"description": "JavaScript",
"extensions": ["js"]
},
"plain": {
"description": "Text, (generally ASCII or ISO 8859-n)",
"extensions": ["txt"]
}
}
}

View file

@ -36,6 +36,7 @@ public class DataNode implements Iterable<DataNode>{
public enum DataType {
Map, List, String, Number, Boolean
}
private Map<String, DataNode> map = null;
private List<DataNode> list = null;
private String value = null;
@ -49,6 +50,7 @@ public class DataNode implements Iterable<DataNode>{
this.type = DataType.Boolean;
this.value = "" + value;
}
/**
* Creates an instance with an int value
*/
@ -56,6 +58,7 @@ public class DataNode implements Iterable<DataNode>{
this.type = DataType.Number;
this.value = "" + value;
}
/**
* Creates an instance with an double value
*/
@ -63,6 +66,7 @@ public class DataNode implements Iterable<DataNode>{
this.type = DataType.Number;
this.value = "" + value;
}
/**
* Creates an instance with an long value
*/
@ -70,6 +74,7 @@ public class DataNode implements Iterable<DataNode>{
this.type = DataType.Number;
this.value = "" + value;
}
/**
* Creates an instance with an String value
*/
@ -77,6 +82,7 @@ public class DataNode implements Iterable<DataNode>{
this.type = DataType.String;
this.value = value;
}
/**
* Creates an instance with a specific type
*/
@ -84,9 +90,11 @@ public class DataNode implements Iterable<DataNode>{
this.type = type;
switch (type) {
case Map:
map = new HashMap<>(); break;
map = new HashMap<>();
break;
case List:
list = new LinkedList<>(); break;
list = new LinkedList<>();
break;
default:
break;
}
@ -103,6 +111,7 @@ public class DataNode implements Iterable<DataNode>{
return list.get(index);
return null;
}
/**
* @param index is the key in the Map
* @return an JSONNode that contains the next level of the Map
@ -123,6 +132,7 @@ public class DataNode implements Iterable<DataNode>{
return list.iterator();
return null;
}
/**
* @return a iterator for the keys in the Map or null if the node contains a value or List
*/
@ -131,6 +141,7 @@ public class DataNode implements Iterable<DataNode>{
return map.keySet().iterator();
return null;
}
/**
* @return the size of the Map or List or -1 if it is a value
*/
@ -149,84 +160,111 @@ public class DataNode implements Iterable<DataNode>{
public void add(DataNode node) {
list.add(node);
}
public void add(boolean value) {
list.add(new DataNode(value));
}
public void add(int value) {
list.add(new DataNode(value));
}
public void add(double value) {
list.add(new DataNode(value));
}
public void add(long value) {
list.add(new DataNode(value));
}
public void add(String value) {
list.add(new DataNode(value));
}
/**
* Adds a node to the Map
*/
public void set(String key, DataNode node) {
map.put(key, node);
}
public void set(String key, boolean value) {
map.put(key, new DataNode(value));
}
public void set(String key, int value) {
map.put(key, new DataNode(value));
}
public void set(String key, double value) {
map.put(key, new DataNode(value));
}
public void set(String key, long value) {
map.put(key, new DataNode(value));
}
public void set(String key, String value) {
map.put(key, new DataNode(value));
}
/**
* Sets the value of the node
* @exception NullPointerException if the node is setup as anything other than a DataType.Number
*
* @throws NullPointerException if the node is setup as anything other than a DataType.Number
*/
public void set(int value) {
if (this.type != DataType.Number)
throw new NullPointerException("The node is not setup as a DataType.Number");
this.value = "" + value;
}
/**
* Sets the value of the node
* @exception NullPointerException if the node is setup as anything other than a DataType.Number
*
* @throws NullPointerException if the node is setup as anything other than a DataType.Number
*/
public void set(double value) {
if (this.type != DataType.Number)
throw new NullPointerException("The node is not setup as a DataType.Number");
this.value = "" + value;
}
/**
* Sets the value of the node
* @exception NullPointerException if the node is setup as anything other than a DataType.Boolean
*
* @throws NullPointerException if the node is setup as anything other than a DataType.Boolean
*/
public void set(boolean value) {
if (this.type != DataType.Boolean)
throw new NullPointerException("The node is not setup as a DataType.Boolean");
this.value = "" + value;
}
/**
* Sets the value of the node, but only
* @exception NullPointerException if the node is setup as anything other than a DataType.Number
*
* @throws NullPointerException if the node is setup as anything other than a DataType.Number
*/
public void set(long value) {
if (this.type != DataType.Number)
throw new NullPointerException("The node is not setup as a DataType.Number");
this.value = "" + value;
}
/**
* Sets the value of the node
* @exception NullPointerException if the method DataType.isValue() returns false
*
* @throws NullPointerException if the method DataType.isValue() returns false
*/
public void set(String value) {
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value node");
if (!this.isValue())
throw new NullPointerException("The node is not setup as a value node");
this.value = value;
}
@ -237,18 +275,21 @@ public class DataNode implements Iterable<DataNode>{
public boolean isMap() {
return type == DataType.Map;
}
/**
* @return if this node contains an List
*/
public boolean isList() {
return type == DataType.List;
}
/**
* @return if this node contains an value
*/
public boolean isValue() {
return type != DataType.Map && type != DataType.List;
}
/**
* @return the type of the node
*/
@ -258,47 +299,92 @@ public class DataNode implements Iterable<DataNode>{
/**
* @return the String value in this map
* @return the String value in this map or null or if key does not exist
* @throws NullPointerException if the node is not of type map
*/
public String getString(String key) {
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if (!this.isMap())
throw new NullPointerException("The node is not setup as a map");
if (!this.map.containsKey(key))
return null;
return this.get(key).getString();
}
/**
* @return the boolean value in this map
* @throws NullPointerException if the node is not of type map or if key does not exist
*/
public boolean getBoolean(String key) {
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if( !this.map.containsKey(key) ) throw new NullPointerException("No such key in map");
if (!this.isMap())
throw new NullPointerException("The node is not setup as a map");
if (!this.map.containsKey(key))
throw new NullPointerException("No such key '" + key + "' in map");
return this.get(key).getBoolean();
}
/**
* @return the integer value in this map
* @throws NullPointerException if the node is not of type map or if key does not exist
*/
public int getInt(String key) {
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if( !this.map.containsKey(key) ) throw new NullPointerException("No such key in map");
if (!this.isMap())
throw new NullPointerException("The node is not setup as a map");
if (!this.map.containsKey(key))
throw new NullPointerException("No such key '" + key + "' in map");
return this.get(key).getInt();
}
/**
* @return the double value in this map
* @throws NullPointerException if the node is not of type map or if key does not exist
*/
public double getDouble(String key) {
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if( !this.map.containsKey(key) ) throw new NullPointerException("No such key in map");
if (!this.isMap())
throw new NullPointerException("The node is not setup as a map");
if (!this.map.containsKey(key))
throw new NullPointerException("No such key '" + key + "' in map");
return this.get(key).getDouble();
}
/**
* @return the long value in this map
* @throws NullPointerException if the node is not of type map or if key does not exist
*/
public long getLong(String key) {
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if( !this.map.containsKey(key) ) throw new NullPointerException("No such key in map");
if (!this.isMap())
throw new NullPointerException("The node is not setup as a map");
if (!this.map.containsKey(key))
throw new NullPointerException("No such key '" + key + "' in map");
return this.get(key).getLong();
}
/**
* @return the long value in this map
* @throws NullPointerException if the node is not of type map
*/
public List getList(String key) {
if (!this.isMap())
throw new NullPointerException("The node is not setup as a map");
return this.get(key).getList();
}
/**
* @return the long value in this map
* @throws NullPointerException if the node is not of type map
*/
public Map getMap(String key) {
if (!this.isMap())
throw new NullPointerException("The node is not setup as a map");
return this.get(key).getMap();
}
/**
* @return the String value in this node, null if its a Map or List
@ -306,24 +392,28 @@ public class DataNode implements Iterable<DataNode>{
public String getString() {
return value;
}
/**
* @return the boolean value in this node
*/
public boolean getBoolean() {
return Boolean.parseBoolean(value);
}
/**
* @return the integer value in this node
*/
public int getInt() {
return Integer.parseInt(value);
}
/**
* @return the double value in this node
*/
public double getDouble() {
return Double.parseDouble(value);
}
/**
* @return the long value in this node
*/
@ -331,6 +421,47 @@ public class DataNode implements Iterable<DataNode>{
return Long.parseLong(value);
}
/**
* @return a list with the items in the node
* @throws NullPointerException if the node is not of type list
*/
public List getList() {
if (!this.isList()) throw new NullPointerException("The node is not setup as a list");
List output = new ArrayList();
for (DataNode node : this) {
switch (node.getType()) {
case Map: output.add(node.getMap()); break;
case List: output.add(node.getList()); break;
case Number: output.add(node.getDouble()); break;
case Boolean: output.add(node.getBoolean()); break;
case String: output.add(node.getString()); break;
}
}
return output;
}
/**
* @return a map with the items in the node
* @throws NullPointerException if the node is not of type map
*/
public Map getMap() {
if (!this.isMap()) throw new NullPointerException("The node is not setup as a map");
Map output = new HashMap();
for (String key : map.keySet()) {
DataNode node = get(key);
switch (node.getType()) {
case Map: output.put(key, node.getMap()); break;
case List: output.put(key, node.getList()); break;
case Number: output.put(key, node.getDouble()); break;
case Boolean: output.put(key, node.getBoolean()); break;
case String: output.put(key, node.getString()); break;
}
}
return output;
}
public String toString() {
if (this.isMap())