Ignore:
Timestamp:
2003-11-24T14:26:53+13:00 (20 years ago)
Author:
cs025
Message:

Extensive additions to metadata

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFile.java

    r5800 r5945  
    88import java.net.URL;
    99import java.net.URLConnection;
     10
     11import java.sql.ResultSet;
     12import java.sql.SQLException;
    1013
    1114import org.w3c.dom.Document;
     
    2932public class METSFile
    3033{
    31     METSFileID    id;
    32     METSFilePos   location;
    33     String        MIMEType;
    34     int           sequenceNo;
    35     long          size;
    36     METSDate      created;
    37     long          checkSum;
    38     List          adminRefs;
    39     List          describeRefs;
    40     METSFileGroup group;
    41 
    42     /**
    43      *  Create a new METSFile object, with the given properties.  The remaining
    44      *  fields are filled in with default values.
    45      *
    46      *  @param <code>String</code> the id of the file
    47      *  @param <code>METSFilePos</code> the location of the file
    48      *  @param <code>String</code> the MIME type of the file
    49      */
    50     public METSFile(METSFileID id, METSFilePos location, String mimeType)
    51     {   this.id           = id;        // required
    52         this.location     = location;  // the location
    53         this.MIMEType     = mimeType;  // the MIME type;
    54         this.sequenceNo   = -1;       // -1 indicates unset
    55         this.size         = -1;        // -1 indicates unset
    56         this.checkSum     = -1;        // -1 indicates unset
    57         this.created      = null;
    58         this.adminRefs    = new ArrayList();
    59         this.describeRefs = new ArrayList();
    60         this.group        = null;
     34  METSFileID    id;
     35  METSFilePos   location;
     36  String        MIMEType;
     37  int           sequenceNo;
     38  long          size;
     39  METSDate      created;
     40  long          checkSum;
     41  List          adminRefs;
     42  List          describeRefs;
     43  METSFileGroup group;
     44
     45  /**
     46   *  Create a new METSFile object, with the given properties.  The remaining
     47   *  fields are filled in with default values.
     48   *
     49   *  @param <code>String</code> the id of the file
     50   *  @param <code>METSFilePos</code> the location of the file
     51   *  @param <code>String</code> the MIME type of the file
     52   */
     53  public METSFile(METSFileID id, METSFilePos location, String mimeType)
     54  { this.id           = id;        // required
     55    this.location     = location;  // the location
     56    this.MIMEType     = mimeType;  // the MIME type;
     57    this.sequenceNo   = -1;       // -1 indicates unset
     58    this.size         = -1;        // -1 indicates unset
     59    this.checkSum     = -1;        // -1 indicates unset
     60    this.created      = null;
     61    this.adminRefs    = new ArrayList();
     62    this.describeRefs = new ArrayList();
     63    this.group        = null;
     64  }
     65
     66  public boolean equals(URL url)
     67  { return this.location.equals(url);
     68  }
     69
     70  /**
     71   *  Set the parent group
     72   *
     73   *  @param <code>METSFileGroup</code> the new holding file group
     74   */
     75  public void setGroup(METSFileGroup group)
     76  { this.group = group;
     77  }
     78
     79  /**
     80   *  Get the parent group of this file
     81   *
     82   *  @return <code>METSFileGroup</code> the holding file group
     83   */
     84  public METSFileGroup getGroup()
     85  { return this.group;
     86  }
     87
     88  /**
     89   *  Get the identifier of this file
     90   *
     91   *  @return <code>METSFileID</code> the file identifier
     92   */
     93  public METSFileID getID()
     94  { return this.id;
     95  }
     96 
     97  /**
     98   *  Set the identifier of this file
     99   *
     100   *  @param <code>METSFileID</code> the file identifier
     101   */
     102  public void setID(METSFileID id)
     103  { this.id = id;
     104  }
     105
     106  /**
     107   *  @return <code>String</code> the MIME (content) type of the file
     108   */
     109  public String getMIMEType()
     110  { return this.MIMEType;
     111  }
     112
     113  /**
     114   *  @return <code>URL</code> the location of the file.  This will often be in
     115   *          "file://" form.
     116   */
     117  public URL getLocation()
     118  { return this.location.getLocation();
     119  }
     120
     121  /**
     122   *  Take a url and make a METSFile object out of it - works out
     123   *  details such as the Mime type, identifiers and other necessary
     124   *  information to call the constructor for METSFile
     125   *
     126   *  @param <code>URL</code> the url of the object - this may be a
     127   *                          reference to a file on the local filestore
     128   *
     129   *  @return <code>METSFile</code> the METS file object for the corresponding
     130   *                                document component.
     131   */
     132  public static METSFile makeMETSFile(URL url, String mimeType)
     133  {
     134    METSFile reply = new METSFile(new METSFileID(), new METSFilePos(url), mimeType);
     135    return reply;
     136  }
     137       
     138  public static METSFile makeMETSFile(URL url)
     139  { String mimeType;
     140 
     141    if (url.toString().startsWith("file://"))
     142    { // TODO: Work out the MIME type
     143      mimeType = URLConnection.getFileNameMap().getContentTypeFor(url.toString().substring(7));
     144      if (mimeType == null)
     145      { mimeType = "text/plain";
     146      }
     147    }
     148    else
     149    { // TODO: look up the MIME type through the pertinent connection
     150      mimeType = HTTPTools.getMIMEType(url);
     151    }
     152    // TODO: make the identifier...
     153   
     154    return makeMETSFile(url, mimeType);
     155  }
     156
     157  /**
     158   *  Write the METS file in an XML to a text-output sink
     159   *
     160   *  @param <code>PrintWriter</code> the destination of the output
     161   */
     162  public void write(PrintWriter writer)
     163  { String tag = XMLTools.getOpenTag("mets", "file");
     164    tag = XMLTools.addAttribute(tag, "MIMETYPE", this.MIMEType);
     165    tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
     166    writer.println(tag);
     167   
     168    tag = XMLTools.getOpenTag("mets", "FLocat");
     169    tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
     170    tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
     171    tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
     172    tag = XMLTools.makeSingleton(tag);
     173    writer.println(tag);
     174   
     175    writer.println(XMLTools.getCloseTag("mets", "file"));
     176  }
     177
     178   
     179  public boolean writeSQL(int groupReference, GS3SQLConnection connection)
     180  { // check if this file is in the table already...
     181    GS3SQLAction action;
     182
     183    GS3SQLSelect select = new GS3SQLSelect("files");
     184    select.addField("*");
     185    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupReference),
     186                            GS3SQLField.INTEGER_TYPE);
     187    GS3SQLWhere  where  = new GS3SQLWhere(whereItem);
     188    whereItem = new GS3SQLWhereItem("ID","=", this.id.toString());
     189    where.add(whereItem);
     190    select.setWhere(where);
     191    connection.execute(select.toString());
     192
     193    // if not, then make an insert action
     194    try {
     195      ResultSet results = connection.getResultSet();
     196      if (results == null ||
     197      !results.first())
     198      { GS3SQLInsert insert = new GS3SQLInsert("files");
     199
     200        insert.addValue("FileGroupRef", Integer.toString(groupReference), GS3SQLField.INTEGER_TYPE);
     201    insert.addValue("ID", this.id.toString());
     202   
     203    action = insert;
     204   
     205      }
     206      else {
     207    GS3SQLUpdate update = new GS3SQLUpdate("files");
     208
     209    update.setWhere(where);
     210    action = update;
     211      }
     212    }
     213    catch (SQLException ex)
     214    { System.err.println(ex);
     215      return false;
     216    }
     217    action.addValue("FileLocType", this.location.getType());
     218    action.addValue("FileLocation", this.location.getLocation().toString());
     219    action.addValue("MIMEType", this.MIMEType);
     220
     221    return connection.execute(action.toString());
     222  }
     223
     224  public static METSFile readSQL(GS3SQLConnection connection, ResultSet parentSet)
     225  {
     226    try {
     227      String locType = parentSet.getString("FileLocType");
     228      String location = parentSet.getString("FileLocation");
     229      String mimeType = parentSet.getString("MIMEType");
     230      String id = parentSet.getString("ID");
     231
     232      METSFileID metsID = new METSFileID(id);
     233      METSFilePos metsFilePos = new METSFilePos(locType, location);
     234     
     235      METSFile reply = new METSFile(metsID, metsFilePos, mimeType);
     236      return reply;
     237    }
     238    catch (SQLException ex)
     239    { System.out.println(ex);
     240    }
     241    catch (java.net.MalformedURLException urlEx)
     242    { System.out.println(urlEx);
     243    }
     244    return null;
     245  }
     246   
     247  /**
     248   *  Parse an XML Element as a METS File
     249   */
     250  public static METSFile parseXML(Element element, METSFileGroup parentGroup)
     251  { METSFile file = null;
     252    NodeList children = element.getChildNodes();
     253       
     254    for (int c = 0; c < children.getLength(); c ++)
     255    { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
     256        continue;
     257      }
     258   
     259      Element childElement = (Element) children.item(c);
     260      if (childElement.getNodeName().equals("mets:FLocat"))
     261      { METSFilePos filePos;
     262   
     263        // get most of the information from the child FLocat node
     264        String locationType = childElement.getAttribute("LOCTYPE");
     265    String href = childElement.getAttribute("xlink:href");
     266    String id   = childElement.getAttribute("ID");
     267     
     268    // some more data from the parent node
     269    String mimeType = element.getAttribute("MIMETYPE");
     270
     271    try
     272    { filePos = new METSFilePos(href, locationType);
    61273    }
    62 
    63     /**
    64      *  Set the parent group
    65      *
    66      *  @param <code>METSFileGroup</code> the new holding file group
    67      */
    68     public void setGroup(METSFileGroup group)
    69     {   this.group = group;
     274    catch (java.net.MalformedURLException ex)
     275    { // TODO: raise error
     276      continue;
    70277    }
    71 
    72     /**
    73      *  Get the parent group of this file
    74      *
    75      *  @return <code>METSFileGroup</code> the holding file group
    76      */
    77     public METSFileGroup getGroup()
    78     {   return this.group;
    79     }
    80 
    81     /**
    82      *  Get the identifier of this file
    83      *
    84      *  @return <code>METSFileID</code> the file identifier
    85      */
    86     public METSFileID getID()
    87     {   return this.id;
    88     }
    89 
    90     /**
    91      *  Set the identifier of this file
    92      *
    93      *  @param <code>METSFileID</code> the file identifier
    94      */
    95     public void setID(METSFileID id)
    96     { this.id = id;
    97     }
    98 
    99     /**
    100      *  @return <code>String</code> the MIME (content) type of the file
    101      */
    102     public String getMIMEType()
    103     {   return this.MIMEType;
    104     }
    105 
    106     /**
    107      *  @return <code>URL</code> the location of the file.  This will often be in
    108      *          "file://" form.
    109      */
    110     public URL getLocation()
    111     {   return this.location.getLocation();
    112     }
    113 
    114     /**
    115      *  Take a url and make a METSFile object out of it - works out
    116      *  details such as the Mime type, identifiers and other necessary
    117      *  information to call the constructor for METSFile
    118      *
    119      *  @param <code>URL</code> the url of the object - this may be a
    120      *                          reference to a file on the local filestore
    121      *
    122      *  @return <code>METSFile</code> the METS file object for the corresponding
    123      *                                document component.
    124      */
    125     public static METSFile makeMETSFile(URL url, String mimeType)
    126     {   
    127         METSFile reply = new METSFile(new METSFileID(), new METSFilePos(url), mimeType);
    128         return reply;
    129     }
    130        
    131     public static METSFile makeMETSFile(URL url)
    132     { String mimeType;
    133 
    134       if (url.toString().startsWith("file://"))
    135         { // TODO: Work out the MIME type
    136             mimeType = URLConnection.getFileNameMap().getContentTypeFor(url.toString().substring(7));
    137             if (mimeType == null)
    138             { mimeType = "text/plain";
    139             }
    140         }
    141         else
    142         { // TODO: look up the MIME type through the pertinent connection
    143             mimeType = HTTPTools.getMIMEType(url);
    144         }
    145         // TODO: make the identifier...
    146 
    147         return makeMETSFile(url, mimeType);
    148     }
    149 
    150     /**
    151      *  Write the METS file in an XML to a text-output sink
    152      *
    153      *  @param <code>PrintWriter</code> the destination of the output
    154      */
    155     public void write(PrintWriter writer)
    156     { String tag = XMLTools.getOpenTag("mets", "file");
    157         tag = XMLTools.addAttribute(tag, "MIMETYPE", this.MIMEType);
    158         tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
    159         writer.println(tag);
    160 
    161         tag = XMLTools.getOpenTag("mets", "FLocat");
    162         tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
    163         tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
    164         tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
    165         tag = XMLTools.makeSingleton(tag);
    166         writer.println(tag);
    167 
    168         writer.println(XMLTools.getCloseTag("mets", "file"));
    169     }
    170 
    171    
    172     public void writeSQL(GS3SQLConnection connection)
    173     { // check if this node is in the
    174     GS3SQLInsert insert = new GS3SQLInsert("files");
    175 
    176     insert.addValue("FileLocType", this.location.getType());
    177     insert.addValue("FileLocation", this.location.getLocation().toString());
    178     insert.addValue("MIMEType", this.MIMEType);
    179     insert.addValue("ID", this.id.toString());
    180 
    181     connection.execute(insert.toString()); 
    182     }
    183    
    184     /**
    185      *  Parse an XML Element as a METS File
    186      */
    187     public static METSFile parseXML(Element element, METSFileGroup parentGroup)
    188     {   METSFile file = null;
    189         NodeList children = element.getChildNodes();
    190        
    191         for (int c = 0; c < children.getLength(); c ++)
    192         { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
    193                 continue;
    194             }
    195 
    196             Element childElement = (Element) children.item(c);
    197             if (childElement.getNodeName().equals("mets:FLocat"))
    198             {   METSFilePos filePos;
    199 
    200                 // get most of the information from the child FLocat node
    201                 String locationType = childElement.getAttribute("LOCTYPE");
    202                 String href = childElement.getAttribute("xlink:href");
    203                 String id   = childElement.getAttribute("ID");
    204 
    205                 // some more data from the parent node
    206                 String mimeType = element.getAttribute("MIMETYPE");
    207 
    208                 try
    209                 { filePos = new METSFilePos(href, locationType);
    210                 }
    211                 catch (java.net.MalformedURLException ex)
    212                 { // TODO: raise error
    213                     continue;
    214                 }
    215                 file = new METSFile(new METSFileID(id), filePos, mimeType);
    216             }
    217             else if (childElement.getNodeName().equals("mets:FContent"))
    218             {
    219             }
    220             else
    221             {   // TODO: raise an error!
    222             }
    223         }
    224         return file;
    225     }
    226 
    227 
    228     /**
    229      *  Overridden <code>toString</code> for convenience - returns the location of the file.
    230      *
    231      *  @return <code>String</code> the location of the file as a string
    232      */
    233     public String toString()
    234     {   return this.location.toString();
    235     }
     278    file = new METSFile(new METSFileID(id), filePos, mimeType);
     279      }
     280      else if (childElement.getNodeName().equals("mets:FContent"))
     281      {
     282      }
     283      else
     284      { // TODO: raise an error!
     285      }
     286    }
     287    return file;
     288  }
     289
     290
     291  /**
     292   *  Overridden <code>toString</code> for convenience - returns the location of the file.
     293   *
     294   *  @return <code>String</code> the location of the file as a string
     295   */
     296  public String toString()
     297  { return this.location.toString();
     298  }
    236299}
Note: See TracChangeset for help on using the changeset viewer.