diff --git a/pom.xml b/pom.xml index e829741..6b67873 100644 --- a/pom.xml +++ b/pom.xml @@ -1,321 +1,227 @@ - 4.0.0 - - - - - net.sf.jmimemagic - jmimemagic - 0.2.3 - jar - - jMimeMagic - http://github.com/arimus/jmimemagic/ - jMimeMagic is a Java library for determining the content type of files or streams. - - - Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - A business-friendly OSS license - - - - - scm:git:git://github.com/arimus/jmimemagic.git - scm:git:git://github.com/arimus/jmimemagic.git - https://github.com/arimus/jmimemagic - - - - - arimus - David Castro - arimus@users.sourceforge.net - - Developer - - jmimemagic - -7 - - - - ndjones - Nate Jones - ndjones@users.sourceforge.net - - Developer - - jmimemagic - -7 - - - - - - - ossrh - jMimeMagic Repo - https://oss.sonatype.org/content/repositories/snapshots - - - - ossrh - jMimeMagic Repo - https://oss.sonatype.org/content/repositories/snapshots - - - - - - - - - - - - - - - - - src/main/filters/filter.properties - - - - src/main/resources - true - - *.properties - *.xml - *.dtd - *.json - - - log4j.properties - - - - - - src/test/resources - true - - - src/main/resources - true - - - - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.3 - - 1.6 - 1.6 - - - - - org.apache.maven.plugins - maven-resources-plugin - 2.4 - - ${project.build.sourceEncoding} - - - - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - - attach-javadocs - - jar - - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.2.1 - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.2 - true - - ossrh - https://oss.sonatype.org/ - false - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - - - - - - - - org.apache.maven.wagon - wagon-ssh-external - 1.0-alpha-5 - - - - - - - - - - - - - - commons-logging - commons-logging - 1.0.4 - compile - - - - commons-io - commons-io - 2.1 - compile - - - - - - log4j - log4j - 1.2.8 - runtime - - - - - - - junit - junit - 3.8.1 - test - - - - - - - - UTF-8 - UTF-8 - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 + + + + + net.sf.jmimemagic + jmimemagic + 0.2.3 + jar + + jMimeMagic + http://github.com/arimus/jmimemagic/ + jMimeMagic is a Java library for determining the content type of files or streams. + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + A business-friendly OSS license + + + + + scm:git:git://github.com/arimus/jmimemagic.git + scm:git:git://github.com/arimus/jmimemagic.git + https://github.com/arimus/jmimemagic + + + + + arimus + David Castro + arimus@users.sourceforge.net + + Developer + + jmimemagic + -7 + + + + ndjones + Nate Jones + ndjones@users.sourceforge.net + + Developer + + jmimemagic + -7 + + + + + UTF-8 + UTF-8 + + + + + + ossrh + jMimeMagic Repo + https://oss.sonatype.org/content/repositories/snapshots + + + + ossrh + jMimeMagic Repo + https://oss.sonatype.org/content/repositories/snapshots + + + + + + + src/main/filters/filter.properties + + + + src/main/resources + true + + *.properties + *.xml + *.dtd + *.json + + + log4j.properties + + + + + + src/test/resources + true + + + src/main/resources + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + + + + + org.apache.maven.plugins + maven-resources-plugin + 3.1.0 + + ${project.build.sourceEncoding} + + + + + org.apache.maven.plugins + maven-source-plugin + 3.1.0 + + + attach-sources + + jar-no-fork + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.1.1 + + + attach-javadocs + + jar + + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.8 + true + + ossrh + https://oss.sonatype.org/ + false + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + + + + + + org.apache.maven.wagon + wagon-ssh-external + 1.0-alpha-5 + + + + + + + + commons-logging + commons-logging + 1.2 + compile + + + + commons-io + commons-io + 2.6 + compile + + + + org.apache.logging.log4j + log4j-core + 2.12.1 + + + + junit + junit + 4.12 + test + + diff --git a/src/main/java/net/sf/jmimemagic/MagicMatcher.java b/src/main/java/net/sf/jmimemagic/MagicMatcher.java index 5bee2f8..201c91c 100644 --- a/src/main/java/net/sf/jmimemagic/MagicMatcher.java +++ b/src/main/java/net/sf/jmimemagic/MagicMatcher.java @@ -10,10 +10,9 @@ import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; - +import java.lang.reflect.InvocationTargetException; import java.nio.ByteBuffer; import java.nio.ByteOrder; - import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -27,17 +26,15 @@ * @author $Author: arimus $ * @version $Revision: 1.1 $ */ -public class MagicMatcher implements Cloneable -{ +public class MagicMatcher implements Cloneable { private static Log log = LogFactory.getLog(MagicMatcher.class); private List subMatchers = new ArrayList(0); private MagicMatch match = null; - /** - * constructor + /** + * constructor */ - public MagicMatcher() - { + public MagicMatcher() { log.debug("instantiated"); } @@ -46,8 +43,7 @@ public MagicMatcher() * * @param match DOCUMENT ME! */ - public void setMatch(MagicMatch match) - { + public void setMatch(MagicMatch match) { log.debug("setMatch()"); this.match = match; } @@ -57,8 +53,7 @@ public void setMatch(MagicMatch match) * * @return DOCUMENT ME! */ - public MagicMatch getMatch() - { + public MagicMatch getMatch() { log.debug("getMatch()"); return this.match; @@ -69,8 +64,7 @@ public MagicMatch getMatch() * * @return whether or not this match has enough data to be valid */ - public boolean isValid() - { + public boolean isValid() { log.debug("isValid()"); if ((match == null) || (match.getTest() == null)) { @@ -84,7 +78,7 @@ public boolean isValid() if ((type != null) && !type.equals("") && (comparator != '\0') && ((comparator == '=') || (comparator == '!') || (comparator == '>') || - (comparator == '<')) && (description != null) && !description.equals("") && + (comparator == '<')) && (description != null) && !description.equals("") && (test != null) && !test.equals("")) { return true; } @@ -97,8 +91,7 @@ public boolean isValid() * * @param m a magic match */ - public void addSubMatcher(MagicMatcher m) - { + public void addSubMatcher(MagicMatcher m) { log.debug("addSubMatcher()"); subMatchers.add(m); } @@ -108,8 +101,7 @@ public void addSubMatcher(MagicMatcher m) * * @param a a collection of submatches */ - public void setSubMatchers(Collection a) - { + public void setSubMatchers(Collection a) { log.debug("setSubMatchers(): for match '" + match.getDescription() + "'"); subMatchers.clear(); subMatchers.addAll(a); @@ -120,8 +112,7 @@ public void setSubMatchers(Collection a) * * @return a collection of submatches */ - public Collection getSubMatchers() - { + public Collection getSubMatchers() { log.debug("getSubMatchers()"); return subMatchers; @@ -130,17 +121,14 @@ public Collection getSubMatchers() /** * test to see if this match or any submatches match * - * @param f the file that should be used to test the match + * @param f the file that should be used to test the match * @param onlyMimeMatch DOCUMENT ME! - * * @return the deepest magic match object that matched - * - * @throws IOException DOCUMENT ME! + * @throws IOException DOCUMENT ME! * @throws UnsupportedTypeException DOCUMENT ME! */ public MagicMatch test(File f, boolean onlyMimeMatch) - throws IOException, UnsupportedTypeException - { + throws IOException, UnsupportedTypeException { log.debug("test(File)"); int offset = match.getOffset(); @@ -168,8 +156,8 @@ public MagicMatch test(File f, boolean onlyMimeMatch) } else if (type.equals("string")) { length = match.getTest().capacity(); } else if (type.equals("regex")) { - - + + final int matchLength = match.getLength(); length = (matchLength == 0) ? (int) file.length() - offset : matchLength; @@ -233,7 +221,7 @@ public MagicMatch test(File f, boolean onlyMimeMatch) // set the data on this match if ((onlyMimeMatch == false) && (subMatchers != null) && (subMatchers.size() > 0)) { log.debug("test(File): testing " + subMatchers.size() + " submatches for '" + - description + "'"); + description + "'"); for (int i = 0; i < subMatchers.size(); i++) { log.debug("test(File): testing submatch " + i); @@ -242,7 +230,7 @@ public MagicMatch test(File f, boolean onlyMimeMatch) if ((submatch = m.test(f, false)) != null) { log.debug("test(File): submatch " + i + " matched with '" + - submatch.getDescription() + "'"); + submatch.getDescription() + "'"); match.addSubMatch(submatch); } else { log.debug("test(File): submatch " + i + " doesn't match"); @@ -263,17 +251,14 @@ public MagicMatch test(File f, boolean onlyMimeMatch) /** * test to see if this match or any submatches match * - * @param data the data that should be used to test the match + * @param data the data that should be used to test the match * @param onlyMimeMatch DOCUMENT ME! - * * @return the deepest magic match object that matched - * - * @throws IOException DOCUMENT ME! + * @throws IOException DOCUMENT ME! * @throws UnsupportedTypeException DOCUMENT ME! */ public MagicMatch test(byte[] data, boolean onlyMimeMatch) - throws IOException, UnsupportedTypeException - { + throws IOException, UnsupportedTypeException { log.debug("test(byte[])"); int offset = match.getOffset(); @@ -318,7 +303,7 @@ public MagicMatch test(byte[] data, boolean onlyMimeMatch) byte[] buf = new byte[length]; log.debug("test(byte[]): offset=" + offset + ",length=" + length + ",data length=" + - data.length); + data.length); if ((offset + length) < data.length) { System.arraycopy(data, offset, buf, 0, length); @@ -342,7 +327,7 @@ public MagicMatch test(byte[] data, boolean onlyMimeMatch) // set the data on this match if ((onlyMimeMatch == false) && (subMatchers != null) && (subMatchers.size() > 0)) { log.debug("test(byte[]): testing " + subMatchers.size() + " submatches for '" + - description + "'"); + description + "'"); for (int i = 0; i < subMatchers.size(); i++) { log.debug("test(byte[]): testing submatch " + i); @@ -351,7 +336,7 @@ public MagicMatch test(byte[] data, boolean onlyMimeMatch) if ((submatch = m.test(data, false)) != null) { log.debug("test(byte[]): submatch " + i + " matched with '" + - submatch.getDescription() + "'"); + submatch.getDescription() + "'"); match.addSubMatch(submatch); } else { log.debug("test(byte[]): submatch " + i + " doesn't match"); @@ -368,12 +353,11 @@ public MagicMatch test(byte[] data, boolean onlyMimeMatch) /** * internal test switch - * + * * @param data DOCUMENT ME! * @return DOCUMENT ME! */ - private boolean testInternal(byte[] data) - { + private boolean testInternal(byte[] data) { log.debug("testInternal(byte[])"); if (data.length == 0) { @@ -442,7 +426,7 @@ private boolean testInternal(byte[] data) } } else { log.error("testInternal(byte[]): type or test is empty for '" + mimeType + " - " + - description + "'"); + description + "'"); } return false; @@ -452,11 +436,9 @@ private boolean testInternal(byte[] data) * test the data against the test byte * * @param data the data we are testing - * * @return if we have a match */ - private boolean testByte(ByteBuffer data) - { + private boolean testByte(ByteBuffer data) { log.debug("testByte()"); String test = new String(match.getTest().array()); @@ -471,21 +453,21 @@ private boolean testByte(ByteBuffer data) int tst = Integer.decode(test).byteValue(); byte t = (byte) (tst & 0xff); log.debug("testByte(): applying bitmask '" + bitmask + "' to '" + tst + "', result is '" + - t + "'"); + t + "'"); log.debug("testByte(): comparing byte '" + b + "' to '" + t + "'"); switch (comparator) { - case '=': - return t == b; + case '=': + return t == b; - case '!': - return t != b; + case '!': + return t != b; - case '>': - return t > b; + case '>': + return t > b; - case '<': - return t < b; + case '<': + return t < b; } return false; @@ -495,11 +477,9 @@ private boolean testByte(ByteBuffer data) * test the data against the byte array * * @param data the data we are testing - * * @return if we have a match */ - private boolean testString(ByteBuffer data) - { + private boolean testString(ByteBuffer data) { log.debug("testString()"); ByteBuffer test = match.getTest(); @@ -513,7 +493,7 @@ private boolean testString(ByteBuffer data) for (i = 0; i < t.length; i++) { log.debug("testing byte '" + b[i] + "' from '" + new String(data.array()) + - "' against byte '" + t[i] + "' from '" + new String(test.array()) + "'"); + "' against byte '" + t[i] + "' from '" + new String(test.array()) + "'"); if (t[i] != b[i]) { diff = true; @@ -523,17 +503,17 @@ private boolean testString(ByteBuffer data) } switch (comparator) { - case '=': - return !diff; + case '=': + return !diff; - case '!': - return diff; + case '!': + return diff; - case '>': - return t[i] > b[i]; + case '>': + return t[i] > b[i]; - case '<': - return t[i] < b[i]; + case '<': + return t[i] < b[i]; } return false; @@ -543,11 +523,9 @@ private boolean testString(ByteBuffer data) * test the data against a short * * @param data the data we are testing - * * @return if we have a match */ - private boolean testShort(ByteBuffer data) - { + private boolean testShort(ByteBuffer data) { log.debug("testShort()"); short val = 0; @@ -575,20 +553,20 @@ private boolean testShort(ByteBuffer data) } log.debug("testShort(): testing '" + Long.toHexString(val) + "' against '" + - Long.toHexString(tst) + "'"); + Long.toHexString(tst) + "'"); switch (comparator) { - case '=': - return val == tst; + case '=': + return val == tst; - case '!': - return val != tst; + case '!': + return val != tst; - case '>': - return val > tst; + case '>': + return val > tst; - case '<': - return val < tst; + case '<': + return val < tst; } return false; @@ -598,11 +576,9 @@ private boolean testShort(ByteBuffer data) * test the data against a long * * @param data the data we are testing - * * @return if we have a match */ - private boolean testLong(ByteBuffer data) - { + private boolean testLong(ByteBuffer data) { log.debug("testLong()"); long val = 0; @@ -618,20 +594,20 @@ private boolean testLong(ByteBuffer data) long tst = Long.decode(test).longValue(); log.debug("testLong(): testing '" + Long.toHexString(val) + "' against '" + test + - "' => '" + Long.toHexString(tst) + "'"); + "' => '" + Long.toHexString(tst) + "'"); switch (comparator) { - case '=': - return val == tst; + case '=': + return val == tst; - case '!': - return val != tst; + case '!': + return val != tst; - case '>': - return val > tst; + case '>': + return val > tst; - case '<': - return val < tst; + case '<': + return val < tst; } return false; @@ -641,11 +617,9 @@ private boolean testLong(ByteBuffer data) * test the data against a regex * * @param text the data we are testing - * * @return if we have a match */ - private boolean testRegex(String text) - { + private boolean testRegex(String text) { log.debug("testRegex()"); String test = new String(match.getTest().array()); @@ -674,20 +648,18 @@ private boolean testRegex(String text) * test the data using a detector * * @param data the data we are testing - * * @return if we have a match */ - private boolean testDetector(ByteBuffer data) - { + private boolean testDetector(ByteBuffer data) { log.debug("testDetector()"); - String detectorClass = new String(match.getTest().array()); + String detectorClass = new String(match.getTest().array()).trim(); try { log.debug("loading class: " + detectorClass); Class c = Class.forName(detectorClass); - MagicDetector detector = (MagicDetector) c.newInstance(); + MagicDetector detector = (MagicDetector) c.getDeclaredConstructor().newInstance(); String[] types = detector.process(data.array(), match.getOffset(), match.getLength(), match.getBitmask(), match.getComparator(), match.getMimeType(), match.getProperties()); @@ -704,6 +676,10 @@ private boolean testDetector(ByteBuffer data) log.error("specified class is not a valid detector class: " + detectorClass, e); } catch (IllegalAccessException e) { log.error("specified class cannot be accessed: " + detectorClass, e); + } catch (NoSuchMethodException e) { + log.error("failed to load empty default constructor of class: " + detectorClass, e); + } catch (InvocationTargetException e) { + e.printStackTrace(); } return false; @@ -714,17 +690,16 @@ private boolean testDetector(ByteBuffer data) * * @return DOCUMENT ME! */ - public String[] getDetectorExtensions() - { + public String[] getDetectorExtensions() { log.debug("testDetector()"); - String detectorClass = new String(match.getTest().array()); + String detectorClass = new String(match.getTest().array()).trim(); try { log.debug("loading class: " + detectorClass); Class c = Class.forName(detectorClass); - MagicDetector detector = (MagicDetector) c.newInstance(); + MagicDetector detector = (MagicDetector) c.getDeclaredConstructor().newInstance(); return detector.getHandledTypes(); } catch (ClassNotFoundException e) { @@ -733,6 +708,10 @@ public String[] getDetectorExtensions() log.error("specified class is not a valid detector class: " + detectorClass, e); } catch (IllegalAccessException e) { log.error("specified class cannot be accessed: " + detectorClass, e); + } catch (NoSuchMethodException e) { + log.error("failed to load empty default constructor of class: " + detectorClass, e); + } catch (InvocationTargetException e) { + e.printStackTrace(); } return new String[0]; @@ -742,11 +721,9 @@ public String[] getDetectorExtensions() * encode a byte as an octal string * * @param b a byte of data - * * @return an octal representation of the byte data */ - private String byteToOctalString(byte b) - { + private String byteToOctalString(byte b) { int n1; int n2; int n3; @@ -761,11 +738,9 @@ private String byteToOctalString(byte b) * convert a byte array to a short * * @param data buffer of byte data - * * @return byte array converted to a short */ - private short byteArrayToShort(ByteBuffer data) - { + private short byteArrayToShort(ByteBuffer data) { return data.getShort(0); } @@ -773,11 +748,9 @@ private short byteArrayToShort(ByteBuffer data) * convert a byte array to a long * * @param data buffer of byte data - * * @return byte arrays (high and low bytes) converted to a long value */ - private long byteArrayToLong(ByteBuffer data) - { + private long byteArrayToLong(ByteBuffer data) { return (long) data.getInt(0); } @@ -785,12 +758,10 @@ private long byteArrayToLong(ByteBuffer data) * DOCUMENT ME! * * @return DOCUMENT ME! - * * @throws CloneNotSupportedException DOCUMENT ME! */ protected MagicMatcher clone() - throws CloneNotSupportedException - { + throws CloneNotSupportedException { MagicMatcher clone = new MagicMatcher(); clone.setMatch((MagicMatch) match.clone());