org.mmbase.util.magicfile
Class Detector

java.lang.Object
  |
  +--org.mmbase.util.magicfile.Detector

public class Detector
extends java.lang.Object

A Detector stores one entry from the magic.xml file, and contains the functionality to determins 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.1.2.2 2003/01/29 13:30:56 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()
           
 java.lang.String getDesignation()
          todo: I noticed there is also a %5.5s variation in magic...
 java.lang.String getExtension()
           
 java.util.List getExtensions()
           
 java.lang.String getMimeType()
           
 int getOffset()
           
 java.lang.String getTest()
           
 java.lang.String getType()
           
 void setComparator(char comparator)
           
 void setDesignation(java.lang.String designation)
           
 void setExtension(java.lang.String extension)
          Adds a possible extension.
 void setInvalid()
           
 void setMimeType(java.lang.String mimetype)
           
 void setOffset(java.lang.String offset)
           
 void setTest(java.lang.String test)
           
 void setType(java.lang.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
 java.lang.String toString()
           
 void toXML(java.io.FileWriter f)
          XML notatie: foo/bar bar blablabla test string etc
 void toXML(java.io.FileWriter f, int level)
           
 boolean valid()
           
protected  java.lang.String xmlEntities(java.lang.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(java.lang.String extension)
Adds a possible extension. The last added one is the default (returned by 'getExtension').

getExtension

public java.lang.String getExtension()

getExtensions

public java.util.List getExtensions()

setMimeType

public void setMimeType(java.lang.String mimetype)

getMimeType

public java.lang.String getMimeType()

setDesignation

public void setDesignation(java.lang.String designation)

setOffset

public void setOffset(java.lang.String offset)

getOffset

public int getOffset()

setType

public void setType(java.lang.String type)

getType

public java.lang.String getType()

setTest

public void setTest(java.lang.String test)

getTest

public java.lang.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 java.lang.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

xmlEntities

protected java.lang.String xmlEntities(java.lang.String s)

toXML

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

toXML

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

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
String representation of Detector object.


MMBase build 1.6.5.20030923