/** * '$RCSfile$' * Purpose: A class to asyncronously do delta-T replication checking * Copyright: 2000 Regents of the University of California and the * National Center for Ecological Analysis and Synthesis * Authors: Chad Berkley * * '$Author$' * '$Date$' * '$Revision$' * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package edu.ucsb.nceas.metacat; import java.io.StringReader; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Hashtable; import java.util.Vector; import org.apache.log4j.Logger; 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; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.DocumentType; 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; import edu.ucsb.nceas.metacat.database.DBConnection; import edu.ucsb.nceas.metacat.database.DBConnectionPool; import edu.ucsb.nceas.metacat.properties.PropertyService; import edu.ucsb.nceas.metacat.util.DocumentUtil; import edu.ucsb.nceas.metacat.util.MetacatUtil; import edu.ucsb.nceas.utilities.PropertyNotFoundException; /** * This class will figure out which content type it is for a given data file. * First, from xml_relation to get all relative files to this data file. * Then from xml_documents to get physical files. From physical file pull out * the content type */ public class ContentTypeProvider { private String dataFileId = null; private String contentType = null; private String packageType = null; private Hashtable contentTypeHash = new Hashtable(); //Constant private String BETA = "beta"; private String EML2 = "eml2"; private static String DEFAULTCONTENTTYPE; static { try { DEFAULTCONTENTTYPE = PropertyService.getProperty("replication.defaultcontenttype"); } catch (PropertyNotFoundException pnfe) { System.err.println("Could not get property DEFAULTCONTENTTYPE:" + pnfe.getMessage()); } } private String FORMATPATH = "//format"; private String TEXT = "text"; private String TEXTYPE ="text/plain"; private String XML = "xml"; private String XMLTYPE = "text/xml"; 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"; private String TAR = "tar"; private String TARTYPE ="application/x-tar"; private String ZIP = "zip"; private String ZIPTYPE = "application/x-zip-compressed"; private String BINARY = "binary"; private String BINARYTYPE = "application/octet-stream"; private String ENTITYDOCTYPE = "xml.entitydoctype"; private String PHYSICALDOCTYPE = "xml.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"; private static Logger logMetacat = Logger.getLogger(ContentTypeProvider.class); /** * Constructor of ContentTypeProvider */ public ContentTypeProvider(String docIdWithRevision) { dataFileId = DocumentUtil.getDocIdFromString(docIdWithRevision); //get relative doclist for data file and package type Vector docLists = null; docLists = getRelativeDocIdList(dataFileId); if ( packageType == null) { // other situation, contenetype is default value contentType = DEFAULTCONTENTTYPE; } else if (packageType.equals(BETA)) { // for beta package and get entity docid for the data file String entityDocid = getTargetDocIdForBeta(docLists, ENTITYDOCTYPE); // get physical docid for data file docLists = getRelativeDocIdList(entityDocid); String physicalDocId = getTargetDocIdForBeta(docLists, PHYSICALDOCTYPE); // if no physical docid assign to this data file, content type is default if (physicalDocId == null) { contentType = DEFAULTCONTENTTYPE; } else { parsePhysicalDocumentForBeta(physicalDocId); } } else if (packageType.equals(EML2)) { // for eml2 package // get eml document for data file //String eml2Docid = getTargetDocIdForBeta(docLists, EML2DOCTYPE); String eml2Docid = (String)docLists.elementAt(0); findContentTypeInEML2(eml2Docid); } } /** Method to get content type */ public String getContentType() { return contentType; }//getContentType /* Method to find content type base on data format*/ private void findContentTypeInEML2(String eml2DocId) { if (eml2DocId == null) { contentType = DEFAULTCONTENTTYPE; return; } DocumentImpl xmlDoc = null; String xmlString = null; StringReader read = null; InputSource in = null; DocumentBuilderFactory dfactory = null; Document doc = null; // create xml document try { String accNumber = eml2DocId + PropertyService.getProperty("document.accNumSeparator") + DBUtil.getLatestRevisionInDocumentTable(eml2DocId); //System.out.println("the acc number is !!!!!!!!!!!!!!!!!"+accNumber); xmlDoc = new DocumentImpl(accNumber); xmlString = xmlDoc.toString(); //System.out.println("the xml doc is "+xmlDoc); // 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; logMetacat.error("Error in ContentTypeProvider." + "findContentTypeInEML2()" + e.getMessage()); return; } Node dataFormatNode = findDataFormatNodeInEML2(doc, DATAFILEPATH, dataFileId); if (dataFormatNode == null) { contentType = DEFAULTCONTENTTYPE; logMetacat.info("Couldn't find data format node"); return; } NodeList childList = dataFormatNode.getChildNodes(); // go through childList for (int i = 0; i