Index: src/edu/ucsb/nceas/metacat/ContentTypeProvider.java
===================================================================
--- src/edu/ucsb/nceas/metacat/ContentTypeProvider.java	(revision 1552)
+++ src/edu/ucsb/nceas/metacat/ContentTypeProvider.java	(revision 1553)
@@ -34,8 +34,10 @@
 import java.util.Hashtable;
 import java.util.Vector;
 
+import org.apache.xpath.objects.XObject;
 import org.apache.xpath.XPathAPI;
 import org.apache.xerces.parsers.DOMParser;
+import org.apache.xerces.dom.DocumentTypeImpl;
 import org.w3c.dom.Attr;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.NodeList;
@@ -43,9 +45,15 @@
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.DocumentType;
-import org.apache.xerces.dom.DocumentTypeImpl;
-import org.apache.xpath.objects.XObject;
+import org.xml.sax.InputSource;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.*;
+import javax.xml.transform.stream.*;
+import javax.xml.transform.dom.*;
+
 import org.ecoinformatics.eml.EMLParser;
 /**
  * This class will figure out which content type it is for a given data file.
@@ -73,6 +81,8 @@
   private String HTML       = "HTML";
   private String HTMLTYPE   = "text/html";
   private String GIF        = "gif";
+  private String JPEG       = "jpeg";
+  private String JPEGTYPE   = "image/jpeg";
   private String GIFTYPE    = "image/gif";
   private String BMP        = "bmp";
   private String BMPTYPE    = "image/bmp";
@@ -85,7 +95,14 @@
   
   private String ENTITYDOCTYPE = "entitydoctype";
   private String PHYSICALDOCTYPE = "physicaldoctype";
+  private String EML2DOCTYPE = "eml2namespace";
+  private String DATAFORMAT = "dataFormat";
+  private String TEXTFORMAT = "textFormat";
+  private String EXTENALFORMAT = "externallyDefinedFormat";
+  private String FORMATNAME = "formatName";
+  private String BINARYRASTERFORMAT = "binaryRasterFormat";
   
+  private String DATAFILEPATH ="//physical/distribution/online/url";
  
   /**
    * Constructor of ContentTypeProvider
@@ -124,6 +141,9 @@
     else if (packageType.equals(EML2))
     {
       // for eml2 package
+      // get eml document for data file
+      String eml2Docid = getTargetDocIdForBeta(docLists, EML2DOCTYPE);
+      findContentTypeInEML2(eml2Docid);
       
     }
  
@@ -135,6 +155,179 @@
     return contentType;
   }//getContentType
   
+  /* Method to find content type base on data format*/
+  private void findContentTypeInEML2(String eml2DocId)
+  {
+    DocumentImpl xmlDoc = null;
+    String xmlString = null;
+    StringReader read = null;
+    InputSource in = null;
+    DocumentBuilderFactory dfactory = null;
+    Document doc = null;
+    // create xml document
+    try
+    {
+      xmlDoc = new DocumentImpl(eml2DocId);
+      xmlString = xmlDoc.toString();
+      // create dom tree
+      read = new StringReader(xmlString);
+      in = new InputSource(read);
+      dfactory = DocumentBuilderFactory.newInstance();
+      dfactory.setNamespaceAware(false);
+      doc = dfactory.newDocumentBuilder().parse(in);
+    }
+    catch (Exception e)
+    {
+      // if faild, set default value
+      contentType = DEFAULTCONTENTTYPE;
+      MetaCatUtil.debugMessage("Error in ContentTypeProvider." +
+                         "findContentTypeInEML2()" + e.getMessage(), 30);
+      return;
+    }
+    Node dataFormatNode = findDataFormatNodeInEML2(doc, DATAFILEPATH, 
+                                                   dataFileId);
+    NodeList childList  = dataFormatNode.getChildNodes();
+    // go through childList
+    for (int i = 0; i<childList.getLength(); i++)
+    {
+      Node child = childList.item(i);
+      
+      // if has text format child set to text/plain
+      if (child.getNodeName() != null && child.getNodeName().equals(TEXTFORMAT))
+      {
+        MetaCatUtil.debugMessage("in text format", 35);
+        contentType = TEXTYPE;
+      }
+      
+      //external format
+      if (child.getNodeName() != null && child.getNodeName().equals(EXTENALFORMAT))
+      {
+        MetaCatUtil.debugMessage("in external format ", 35);
+        String format = getTextValueForGivenChildTag(child, FORMATNAME);
+        MetaCatUtil.debugMessage("The format is: "+format, 35);
+        // if we can find the format in the contentTypeHash table
+        contentType = (String)lookUpContentType(format);
+        if (contentType == null)
+        {
+          contentType = BINARYTYPE;
+        }
+      }
+      
+      // binaryRasterFormat
+      if (child.getNodeName() != null && child.getNodeName().
+          equals(BINARYRASTERFORMAT))
+      {
+        contentType = BINARYTYPE;
+      }//if
+    }//for
+    //if contentype still be null, set default value
+    if (contentType == null)
+    {
+      contentType = DEFAULTCONTENTTYPE;
+    }
+  }
+  
+  /* Method get text value of given child tagname*/
+  private String getTextValueForGivenChildTag(Node parentNode, 
+                                              String childTagName)
+  {
+    String textValue = null;
+    NodeList childList = parentNode.getChildNodes();
+    for (int i= 0; i<childList.getLength();i++)
+    {
+      Node child = childList.item(i);
+      if (child.getNodeName() != null && child.getNodeName().equals(childTagName))
+      {
+        MetaCatUtil.debugMessage("Find child node: " + childTagName, 35);
+        Node textNode = child.getFirstChild();
+        if (textNode.getNodeType() == Node.TEXT_NODE)
+        {
+          textValue = textNode.getNodeValue();
+        }//if
+      }//if
+    }//for
+    MetaCatUtil.debugMessage("The text value for element- " + childTagName +
+                             " is " + textValue, 30);
+    return textValue;
+  }//getTExtValueForGivenChildTag
+  
+  /* Find the data format node in eml2 document */
+  private Node findDataFormatNodeInEML2(Document xml, String xPath, 
+                                       String targetDocId)
+  {
+    Node targetNode = null;
+    Node node = findDataFileNodeInEML2(xml, xPath, targetDocId);
+    // get the phycial the prent is online, grandparent is distribution
+    // the grand'parent is physical
+    Node phyicalNode = node.getParentNode().getParentNode().getParentNode();
+    NodeList list = phyicalNode.getChildNodes();
+    for (int i = 0; i<list.getLength(); i++)
+    {
+      Node kid = list.item(i);
+      // find dataFormat node
+      if (kid.getNodeType() == node.ELEMENT_NODE && 
+          kid.getNodeName().equals(DATAFORMAT))
+      {
+        targetNode = kid;
+        break;
+      }//if
+    }//for
+    MetaCatUtil.debugMessage("dataFormat node'name: "+ 
+                             targetNode.getNodeName(), 35);
+    return targetNode;
+  }
+  /* Find the datafile node */
+  private Node findDataFileNodeInEML2(Document xml, String xPath, 
+                                String targetDocId)
+  {
+    Node dataFileNode = null;
+    NodeList list = null;
+    try
+    {
+      list = XPathAPI.selectNodeList(xml, xPath);
+    }
+    catch (Exception e)
+    {
+      // catch an error and return null
+      MetaCatUtil.debugMessage("Error in findDataFileNode: "+e.getMessage(), 30);
+      return dataFileNode;
+    }
+    // go through the list and find target docid in online/url
+    for (int i = 0; i<list.getLength(); i++)
+    {
+      Node node = list.item(i);
+      Node textNode = node.getFirstChild();
+      if (textNode.getNodeType() == node.TEXT_NODE)
+      {
+        String URLData = textNode.getNodeValue();
+        MetaCatUtil.debugMessage("online/url text data: " + URLData, 30);
+        //Only handle data file in local metacat server
+        if (URLData.indexOf(MetaCatUtil.getOption("httpserver")) != -1 || 
+            URLData.indexOf(MetaCatUtil.getOption("server")) != -1)
+        {
+          // Get docid from url
+          String docId =MetaCatUtil.getDocIdWithRevFromOnlineURL(URLData);
+          // Get rid of revision
+          docId = MetaCatUtil.getDocIdFromString(docId);
+          MetaCatUtil.debugMessage("docid from url element in xml is: "+
+                                   docId, 30);
+          //if this docid equals target one, we find it
+          if (docId != null && docId.equals(targetDocId))
+          {
+            MetaCatUtil.debugMessage("Find target docid in online/url: "+
+                                      docId, 30);
+            dataFileNode = node;
+            break;
+          }
+        }//if
+        
+      }//if
+    }//for
+    MetaCatUtil.debugMessage("online/url node's name: " +
+                             dataFileNode.getNodeName(), 35);
+    return dataFileNode;
+  }//findDataFileNode
+  
   /* Get relative docid list and packagetype */
   private Vector getRelativeDocIdList(String id) 
   {
@@ -292,7 +485,7 @@
         DBConnectionPool.returnDBConnection(conn, serialNumber);
       }
     }//finally
-    MetaCatUtil.debugMessage("!!!!!!!!!target docid is: "+ docId + " "+
+    MetaCatUtil.debugMessage("target docid is: "+ docId + " "+
                              "for target doctype: "+targetType, 25);
     return docId;
   }
@@ -382,6 +575,7 @@
     contentTypeHash.put(XML, XMLTYPE);
     contentTypeHash.put(HTML,HTMLTYPE);
     contentTypeHash.put(GIF, GIFTYPE);
+    contentTypeHash.put(JPEG, JPEGTYPE);
     contentTypeHash.put(BMP, BMPTYPE);
     contentTypeHash.put(TAR, TARTYPE);
     contentTypeHash.put(ZIP, ZIPTYPE);
@@ -389,15 +583,17 @@
     
   }//constructrContentHashTable();
   
+  
+  
   public static void main(String[] argus)
   {
      try
      {
        DBConnectionPool pool = DBConnectionPool.getInstance();
-       ContentTypeProvider provider = new ContentTypeProvider("tao.9830");
+       //ContentTypeProvider provider = new ContentTypeProvider("tao.9830");
+       ContentTypeProvider provider = new ContentTypeProvider("tao.0001");
        String str = provider.getContentType();
        MetaCatUtil.debugMessage("content type is : " + str, 20);
-       
      }
      catch(Exception e)
      {
