Changeset 8460


Ignore:
Timestamp:
2004-11-05T10:08:31+13:00 (19 years ago)
Author:
kjdon
Message:

added in Chi's changes for METS doc handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/METSDocument.java

    r8448 r8460  
    1818
    1919import org.greenstone.gsdl3.gs3build.metadata.*;
     20import org.greenstone.gsdl3.gs3build.util.HTMLDoc;
     21import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
     22import org.greenstone.gsdl3.gs3build.xpointer.XPointer;
    2023
    2124public class METSDocument extends AbstractDocument {
    2225
    2326    public static final String METS_DOCUMENT_TYPE = "METS";
     27    Document domDocument;
     28    public METSDocument(DocumentID id) {
     29        super(id);
     30    //domDocument = null;
     31    }
    2432
    2533    public METSDocument(URL url) {
     
    2836    if (url.getProtocol().equals("file")) {
    2937        this._parseFile(new File(url.getPath()));
    30     }
     38    } else {
     39        System.err.println("Error: METSDocument must currently be a file");
     40        System.err.println("       URL = " + url.getPath());
     41    }
    3142    }
    3243   
    3344    private void _parseFile(File file) {
    3445   
     46    String parseFilePath;
    3547    try {
    3648        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    3749        DocumentBuilder builder = factory.newDocumentBuilder();
    38         Document document = builder.parse(file);
    39 
     50        domDocument = builder.parse(file);
     51
     52        int filePosition = file.getPath().indexOf("import/")+7;
     53        parseFilePath = file.getPath().substring(0, filePosition);
    4054        // TODO: get all the types in the tree
    4155     
     
    4660        System.out.println("Read");
    4761     
    48         NodeList fileSecs = document.getElementsByTagName("mets:fileSec");
    49      
    50         for (int g = 0; g < fileSecs.getLength(); g ++) {
     62        // Get METSDmdSec metadata sections
     63        NodeList dmdSecs = domDocument.getElementsByTagName("mets:dmdSec");
     64       
     65        this.metadata = METSDescriptiveSet.parseXML(dmdSecs);
     66
     67        // Get METSFileSec sections
     68        NodeList fileSecs = domDocument.getElementsByTagName("mets:fileSec");
     69        this.fileSet = METSFileSet.parseXML(fileSecs, fileSet, parseFilePath);
     70        // Get METSStructureMap Sections
     71        NodeList structMapSecs = domDocument.getElementsByTagName("mets:structMap");
     72        this.structureSet = METSStructureSet.parseXML(structMapSecs);
     73
     74        //for (int g = 0; g < fileSecs.getLength(); g ++) {
    5175        //Schema schema = new Schema(schemas.item(s));
    52         this._parseFileSec(fileSecs.item(g));
    53         }
     76        //this._parseFileSec(fileSecs.item(g));
     77        //}
    5478     
    5579        // Get document metadata sections
    56         NodeList dmdSecs = document.getElementsByTagName("mets:dmdSec");
     80        //NodeList dmdSecs = document.getElementsByTagName("mets:dmdSec");
    5781     
    5882        //Schema schema = new Schema(schemas.item(s));
    59         this.metadata = METSDescriptiveSet.parseXML(fileSecs);
    60     }
    61     catch (FactoryConfigurationError e) {
     83        //this.metadata = METSDescriptiveSet.parseXML(fileSecs);
     84    } catch (FactoryConfigurationError e) {
    6285        System.out.println(e);
    63     }
    64     catch (ParserConfigurationException ex) {
     86    } catch (ParserConfigurationException ex) {
    6587        System.out.println(ex);
    66     }
    67     catch (SAXException ex) {
     88    } catch (SAXException ex) {
    6889        System.out.println(ex);
    69     }
    70     catch (IOException ex) {
     90    } catch (IOException ex) {
    7191        System.out.println(ex);
    7292    }
    7393    }
    74 
     94    /*
    7595    private void _parseFileSec(Node fileSec) {
    7696    // this is in effect a group without a sense of 'self'...
     
    108128        }
    109129    }
    110     }
     130    }*/
    111131   
    112132    public String getDocumentType() {
     
    116136    public String getDocumentText() {
    117137    // TODO: make this more than a dummy function!
    118     return null;
    119     }
    120 
    121     public String getSectionText(String sectionId) {
    122     return null;
    123     }
    124    
     138    HTMLDoc metsDoc;
     139    URL url = (URL) this.fileSet.getFile(0).getLocation();
     140
     141    this.getSectionText("1");
     142   
     143    if (url.getProtocol().equals("file")) {
     144        metsDoc = new HTMLDoc(url,url.getPath());
     145    } else {
     146        metsDoc = new HTMLDoc(url);
     147    }
     148    return metsDoc.getContent();
     149    }
     150    public Document getDOMDocument()
     151    {
     152    if (domDocument == null) {
     153        URL url = (URL) this.fileSet.getFile(0).getLocation();
     154        try {
     155        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     156        DocumentBuilder builder = factory.newDocumentBuilder();
     157        domDocument = builder.parse(new File(url.getPath()));
     158        } catch (FactoryConfigurationError e) {
     159        System.out.println(e);
     160        } catch (ParserConfigurationException ex) {
     161        System.out.println(ex);
     162        } catch (SAXException ex) {
     163        System.out.println(ex);
     164        } catch (IOException ex) {
     165        System.out.println(ex);
     166        }
     167    }
     168    return domDocument;
     169    }
     170
     171    private XPointer getXPointer(METSDivision division)
     172    {
     173    String fileId = division.getDefaultFileReference();
     174
     175    if (fileId == null) {
     176        System.err.println("Unable to obtain file reference for section " + division.getLabel());
     177        return null;
     178    }
     179   
     180    METSFileGroup fileGroup = this.getDocumentFiles().getGroup(fileId);
     181    if (fileGroup == null) {
     182        System.err.println("Unable to obtain file reference for filegroup " + fileId);
     183        return null;
     184    }
     185    METSFile file = fileGroup.getFile(0);
     186    if (file == null) {
     187        System.err.println("Unable to obtain any files within filegroup " + fileId);
     188        return null;
     189    }
     190    URL url = file.getLocation();
     191    XPointer xpointer = XPointer.processXPointer(domDocument, url);
     192   
     193    return xpointer;
     194    }
     195   
     196    private XPointer getXPointer(String sectionId)
     197    {
     198    if (domDocument == null) {
     199        URL url = (URL) this.fileSet.getFile(0).getLocation();
     200        try {
     201        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     202        DocumentBuilder builder = factory.newDocumentBuilder();
     203        domDocument = builder.parse(new File(url.getPath()));
     204        } catch (FactoryConfigurationError e) {
     205        System.out.println(e);
     206        } catch (ParserConfigurationException ex) {
     207        System.out.println(ex);
     208        } catch (SAXException ex) {
     209        System.out.println(ex);
     210        } catch (IOException ex) {
     211        System.out.println(ex);       
     212        }
     213    }
     214   
     215    METSDivision division =
     216        this.getDocumentStructure().getDivision(METSStructureSet.GSDL3_SECTION_STRUCTURE, sectionId);
     217   
     218    if(division == null) {
     219        return null;
     220    }
     221        return this.getXPointer(division);
     222    }
     223
     224    public Node getSectionStartNode(METSDivision division)
     225    {
     226    XPointer xpointer = this.getXPointer(division);
     227    return xpointer.getStartNode();
     228    }
     229
     230    public Node getSectionStartNode(String sectionId)
     231    {
     232    XPointer xpointer = this.getXPointer(sectionId);
     233    return xpointer.getStartNode();
     234    }
     235   
     236    public String getSectionText(String sectionId)
     237    {
     238    XPointer xpointer = this.getXPointer(sectionId);
     239   
     240    if (xpointer == null) {
     241        return "";
     242    }
     243
     244    //get the XML content of the xpointer
     245    return xpointer.toString();
     246    }
    125247}
     248
     249
Note: See TracChangeset for help on using the changeset viewer.