/** * * Copyright (c) 2003 University of Mancester. * @author Kevin Garwood (garwood@cs.man.ac.uk) * */ package pedro.ontology; import java.util.ArrayList; import java.lang.reflect.Constructor; import java.lang.Class; import java.io.File; import java.net.URL; import pedro.system.GlobalConstants; /** * @author Kevin Garwood * @date * @version 1.0 */ /* Code RoadMap: Section Constants Section Properties Section Construction Section Accessors Section Mutators Section Validation Section Errors Section Interfaces Section Overload */ public class OntologyFactory { // ========================================== // Section Constants // ========================================== // ========================================== // Section Properties // ========================================== private ArrayList ontologySourceRecords; private static OntologyFactory ontologyFactory = null; // ========================================== // Section Construction // ========================================== public OntologyFactory() { ontologySourceRecords = new ArrayList(); } public static OntologyFactory getOntologyFactory() { if ( ontologyFactory == null) { ontologyFactory = new OntologyFactory(); } //end if () return ontologyFactory; } public OntologySource createSource(Class ontologyClass, URL[] fileNames) { int numberOfSources = ontologySourceRecords.size(); for ( int i = 0; i < numberOfSources; i++) { OntologySourceRecord currentOntology = (OntologySourceRecord) ontologySourceRecords.get(i); if ((ontologyClass == currentOntology.getOntologyClass()) && (currentOntology.compareURLs(fileNames) == true)) { System.out.println("OF == already found ontology =="+ontologyClass.getName()+"=="); return currentOntology.getOntologySource(); } //end if () } // end for () try { OntologySourceRecord ontologySourceRecord = new OntologySourceRecord(ontologyClass, fileNames); //use reflection to instantiate Constructor constructor = ontologyClass.getConstructor(new Class[0]); OntologySource ontologySource = (OntologySource) constructor.newInstance(new Object[0]); ontologySource.setFileNames(fileNames); ontologySourceRecord.setOntologySource(ontologySource); ontologySourceRecords.add(ontologySourceRecord); return ontologySource; } catch (Exception err) { System.out.println("OntologyFactory" + err.toString()); return null; } // end try-catch } // ========================================== // Section Accessors // ========================================== // ========================================== // Section Mutators // ========================================== // ========================================== // Section Validation // ========================================== // ========================================== // Section Errors // ========================================== // ========================================== // Section Interfaces // ========================================== // ========================================== // Section Overload // ========================================== } class OntologySourceRecord { private Class ontologyClass; private URL[] fileNames; private OntologySource ontologySource; public OntologySourceRecord(Class ontologyClass, URL[] fileNames) { this.ontologyClass = ontologyClass; this.fileNames = fileNames; } public Class getOntologyClass() { return ontologyClass; } public URL[] getFileNames() { return fileNames; } public boolean compareURLs(URL[] filesToCompare) { if ( fileNames == null) { if ( filesToCompare == null) { return true; } //end if () else { return false; } //end else } //end if () else { if ( filesToCompare == null) { return false; } //end if () for ( int i = 0; i < filesToCompare.length; i++) { boolean fileExists = false; for ( int j = 0; j < fileNames.length; j++) { if ( filesToCompare[i].sameFile(fileNames[j]) == true) { fileExists = true; } //end if () } // end for () if ( fileExists == false) { return false; } //end if () } // end for () return true; } //end else } public OntologySource getOntologySource() { return ontologySource; } public void setOntologySource(OntologySource _ontologySource) { this.ontologySource = _ontologySource; } }