org.mmbase.util.magicfile
Class Detector

java.lang.Object
  extended byorg.mmbase.util.magicfile.Detector

public class Detector
extends Object

A Detector stores one entry from the magic.xml file, and contains the functionality to determines if a certain byte[] satisfies it. Implementation made on the basis of actual magic file and its manual.
TODO:
- link the info with mimetypes
- add test modifiers
- add commandline switches for warning, error and debugging messages

Ignored features of magic:
- date types
- indirect offsets (prefix of '&' in sublevel match or (address+bytes) where offset = value of address plus bytes
- AND'ing of type

BUGS:
- test string isn't read when end of line is reached in absence of a message string

Tested:
- .doc
- .rtf
- .pdf
- .sh
- .gz
- .bz2
- .html
- .rpm
- .wav

Not supported by magic file:
- StarOffice

Version:
$Id: Detector.java,v 1.11 2006/01/25 19:09:43 michiel Exp $

Method Summary
 void addChild(Detector detector, int level)
          Add an embedded detector object that searches for more details after an initial match.
 char getComparator()
           
 String getDesignation()
          todo: I noticed there is also a %5.5s variation in magic...
 String getExtension()
           
 List getExtensions()
           
 String getMimeType()
           
 int getOffset()
           
 String getRawInput()
           
 String getTest()
           
 String getType()
           
 void setComparator(char comparator)
           
 void setDesignation(String designation)
           
 void setExtension(String extension)
          Adds a possible extension.
 void setInvalid()
           
 void setMimeType(String mimetype)
           
 void setOffset(String offset)
           
 void setTest(String test)
           
 void setType(String type)
           
 boolean test(byte[] lithmus)
           
protected  boolean testByte(byte[] lithmus)
          Test whether a byte matches
protected  boolean testLong(byte[] lithmus, int endian)
          Test whether a long matches
protected  boolean testShort(byte[] lithmus, int endian)
          Test whether a short matches
protected  boolean testString(byte[] lithmus)
          Test whether a string matches
 String toString()
           
 void toXML(FileWriter f)
          XML notatie: foo/bar bar blablabla test string etc
 void toXML(FileWriter f, int level)
           
 boolean valid()
           
protected  String xmlEntities(String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

addChild

public void addChild(Detector detector,
                     int level)
Add an embedded detector object that searches for more details after an initial match.


setExtension

public void setExtension(String extension)
Adds a possible extension. The last added one is the default (returned by 'getExtension').


getExtension

public String getExtension()

getExtensions

public List getExtensions()

setMimeType

public void setMimeType(String mimetype)

getMimeType

public String getMimeType()

setDesignation

public void setDesignation(String designation)

setOffset

public void setOffset(String offset)

getOffset

public int getOffset()

setType

public void setType(String type)

getType

public String getType()

setTest

public void setTest(String test)

getTest

public String getTest()

setComparator

public void setComparator(char comparator)

getComparator

public char getComparator()

test

public boolean test(byte[] lithmus)
Returns:
Whether detector matches the prefix/lithmus of the file

getDesignation

public String getDesignation()
todo: I noticed there is also a %5.5s variation in magic...


setInvalid

public void setInvalid()

valid

public boolean valid()
Returns:
Whether parsing of magic line for this detector succeeded

testString

protected boolean testString(byte[] lithmus)
Test whether a string matches


testShort

protected boolean testShort(byte[] lithmus,
                            int endian)
Test whether a short matches


testLong

protected boolean testLong(byte[] lithmus,
                           int endian)
Test whether a long matches


testByte

protected boolean testByte(byte[] lithmus)
Test whether a byte matches


getRawInput

public String getRawInput()
Returns:
Original unprocessed input line
Since:
MMBase-1.7

xmlEntities

protected String xmlEntities(String s)

toXML

public void toXML(FileWriter f)
           throws IOException
XML notatie: foo/bar bar blablabla test string etc

Throws:
IOException

toXML

public void toXML(FileWriter f,
                  int level)
           throws IOException
Parameters:
level - Indicates depth of (child) element
Throws:
IOException

toString

public String toString()
Returns:
String representation of Detector object.


MMBase build 1.8.1.20060716