libextractor-java

GNU libextractor
Log | Files | Refs | README | LICENSE

MetaData.java (3606B)


      1 /*
      2      This file is part of libextractor.
      3      Copyright (C) 2002, 2003, 2004, 2007, 2010, 2012 Vidyut Samanta and Christian Grothoff
      4 
      5      libextractor is free software; you can redistribute it and/or modify
      6      it under the terms of the GNU General Public License as published
      7      by the Free Software Foundation; either version 3, or (at your
      8      option) any later version.
      9 
     10      libextractor is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      General Public License for more details.
     14 
     15      You should have received a copy of the GNU General Public License
     16      along with libextractor; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18      Boston, MA 02110-1301, USA.
     19  */
     20 package org.gnu.libextractor;
     21 
     22 import java.util.ArrayList;
     23 import java.io.File;
     24 import java.io.FileInputStream;
     25 
     26 /**
     27  * An item of meta data extracted by GNU libextractor.
     28  *
     29  * @see Extractor
     30  * @author Christian Grothoff
     31  */ 
     32 public final class MetaData {
     33    
     34     /**
     35      * Format is unknown.
     36      */
     37     public static final int METAFORMAT_UNKNOWN = 0;
     38 
     39     /**
     40      * 0-terminated, UTF-8 encoded string.  "data_len"
     41      * is strlen(data)+1.
     42      */
     43     public static final int METAFORMAT_UTF8 = 1;
     44 
     45     /**
     46      * Some kind of binary format, see given Mime type.
     47      */
     48     public static final int METAFORMAT_BINARY = 2;
     49 
     50     /**
     51      * 0-terminated string.  The specific encoding is unknown.
     52      * "data_len" is strlen(data)+1.
     53      */
     54     public static final int METAFORMAT_C_STRING = 3;
     55 
     56 
     57     /**
     58      * Cached list of Strings describing keyword types.
     59      */
     60     private final static String[] typeCache_;
     61 
     62     static {
     63 	typeCache_ = new String[Extractor.getMaxTypeInternal()];
     64     }
     65 
     66     /**
     67      * LE type number for this meta data item.
     68      */
     69     public final int type;
     70 
     71     /**
     72      * LE format given for the meta data.
     73      */
     74     public final int format;
     75 
     76     /**
     77      * The meta data itself.
     78      */
     79     public final byte[] meta;
     80 
     81     /**
     82      * Mime-type of the meta data.
     83      */
     84     public final String meta_mime;
     85 
     86 
     87     /**
     88      * Constructor is only called from "native" code.
     89      *
     90      * @param t type of the meta data
     91      * @param f format of the meta data
     92      * @param m the actual meta data
     93      * @param mm mime type of the meta data
     94      */
     95     private MetaData (int t,
     96 		      int f,
     97 		      byte[] m,
     98 		      String mm) {
     99 	this.type = t;
    100 	this.format = f;
    101 	this.meta = m;
    102 	this.meta_mime = mm;
    103     }
    104 
    105 
    106     public String toString() {
    107 	return getTypeAsString() + " - " + getMetaDataAsString ();
    108     }
    109 
    110 
    111     /**
    112      * Return the meta data as a "String" (if the format
    113      * type permits this).
    114      *
    115      * @return null if the format of the meta data is unknown
    116      */
    117     public String getMetaDataAsString () {
    118 	switch (format) {
    119 	case METAFORMAT_C_STRING:
    120 	    return new String (meta);
    121 	case METAFORMAT_UTF8:
    122 	    try {
    123 		return new String (meta, "UTF-8");
    124 	    } catch (java.io.UnsupportedEncodingException uee) {
    125 		// Java should ALWAYS support UTF-8
    126 		throw new Error (uee);
    127 	    }	
    128 	case METAFORMAT_BINARY:
    129 	    return "(binary, " + meta.length + " bytes)";
    130 	default:
    131 	    return null;
    132 	}
    133 	     
    134     }
    135 
    136 
    137     /**
    138      * @return description of this meta data item as a string
    139      */
    140     public String getTypeAsString() {
    141 	if (typeCache_[type] == null)
    142 	    typeCache_[type]
    143 		= Extractor.getTypeAsStringInternal(type);
    144 	return typeCache_[type];
    145     }
    146 
    147 
    148 } // end of MetaData