| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
package net.sf.jmimemagic.detectors; |
| 6 |
|
|
| 7 |
|
import java.io.BufferedInputStream; |
| 8 |
|
import java.io.File; |
| 9 |
|
import java.io.FileInputStream; |
| 10 |
|
import java.io.IOException; |
| 11 |
|
import java.util.Map; |
| 12 |
|
|
| 13 |
|
import org.apache.commons.logging.Log; |
| 14 |
|
import org.apache.commons.logging.LogFactory; |
| 15 |
|
|
| 16 |
|
import com.fasterxml.jackson.core.JsonFactory; |
| 17 |
|
import com.fasterxml.jackson.core.JsonParser; |
| 18 |
|
|
| 19 |
|
import net.sf.jmimemagic.MagicDetector; |
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
@author |
| 25 |
|
@version |
| 26 |
|
|
| |
|
| 42.9% |
Uncovered Elements: 20 (35) |
Complexity: 16 |
Complexity Density: 0.7 |
|
| 27 |
|
public class JsonFileDetector implements MagicDetector { |
| 28 |
|
private static Log log = LogFactory.getLog(JsonFileDetector.class); |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 33 |
3959 |
public JsonFileDetector() {... |
| 34 |
3929 |
super(); |
| 35 |
|
} |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
@return |
| 41 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 42 |
0 |
@Override... |
| 43 |
|
public String getDisplayName() { |
| 44 |
0 |
return "Json File Detector"; |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@return |
| 51 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 52 |
0 |
@Override... |
| 53 |
|
public String[] getHandledExtensions() { |
| 54 |
0 |
return new String[] { "js", "json" }; |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
@return |
| 61 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 62 |
1 |
@Override... |
| 63 |
|
public String[] getHandledTypes() { |
| 64 |
1 |
return new String[] { "application/json" }; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
@return |
| 71 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 72 |
0 |
@Override... |
| 73 |
|
public String getName() { |
| 74 |
0 |
return "jsonfiledetector"; |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
@return |
| 81 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 82 |
0 |
@Override... |
| 83 |
|
public String getVersion() { |
| 84 |
0 |
return "0.1"; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
@param |
| 91 |
|
@param |
| 92 |
|
@param |
| 93 |
|
@param |
| 94 |
|
@param |
| 95 |
|
@param |
| 96 |
|
@param |
| 97 |
|
|
| 98 |
|
@return |
| 99 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 5 |
Complexity Density: 0.62 |
|
| 100 |
3906 |
@Override... |
| 101 |
|
public String[] process(final byte[] data, final int offset, final int length, |
| 102 |
|
final long bitmask, final char comparator, final String mimeType, |
| 103 |
|
final Map<String, String> params) { |
| 104 |
3939 |
log.debug("processing stream data"); |
| 105 |
3857 |
try (JsonParser parser = new JsonFactory() |
| 106 |
|
.createParser(new String(data, "UTF-8"))) { |
| 107 |
10816 |
while (!parser.isClosed()) { |
| 108 |
8886 |
parser.nextToken(); |
| 109 |
|
} |
| 110 |
1989 |
return new String[] { "application/json" }; |
| 111 |
|
} catch (final IOException e) { |
| 112 |
1967 |
log.debug("JsonFileDetector: failed to process data"); |
| 113 |
|
} |
| 114 |
1961 |
return null; |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
@param |
| 121 |
|
@param |
| 122 |
|
@param |
| 123 |
|
@param |
| 124 |
|
@param |
| 125 |
|
@param |
| 126 |
|
@param |
| 127 |
|
|
| 128 |
|
@return |
| 129 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 5 |
Complexity Density: 0.56 |
|
| 130 |
0 |
@Override... |
| 131 |
|
public String[] process(final File file, final int offset, final int length, |
| 132 |
|
final long bitmask, final char comparator, final String mimeType, |
| 133 |
|
final Map<String, String> params) { |
| 134 |
0 |
log.debug("processing file data"); |
| 135 |
0 |
try (BufferedInputStream is = new BufferedInputStream( |
| 136 |
|
new FileInputStream(file))) { |
| 137 |
0 |
final byte[] b = new byte[length]; |
| 138 |
0 |
final int n = is.read(b, offset, length); |
| 139 |
0 |
if (n > 0) { |
| 140 |
0 |
return process(b, offset, length, bitmask, comparator, mimeType, |
| 141 |
|
params); |
| 142 |
|
} |
| 143 |
|
} catch (final IOException e) { |
| 144 |
0 |
log.info("JsonFileDetector: file " + file.getName()); |
| 145 |
|
} |
| 146 |
0 |
return null; |
| 147 |
|
} |
| 148 |
|
} |