⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iointerface.java

📁 toocom源代码,主要应用在本体匹配方面!
💻 JAVA
字号:
package toocom.iotools;

import java.awt.*;
import javax.swing.*;
import toocom.ocgl.*;
import java.io.*;
import javax.swing.filechooser.*;
import toocom.exceptions.*;

/** 
 * This class contains methods dedicated to IO operations.
 *
 * @author Fr閐閞ic F黵st
 */
public class IOInterface{
	
	public static final String NOT_VALID_GRAPH_FILE = "Not valid graph file";
	public static final String OVERWRITE_FILE_MESSAGE = "Do you want to overwrite the file ?";
	public static final String SAVING_GRAPH_SUCCESSFULL_MESSAGE = "Graph successfully saved !";
	public static final String SAVING_GRAPH_PROBLEM_MESSAGE = "A problem occurs, retry to save the graph";
	
	/** Open a file chooser to allow the user to load a graph and returns the loaded graph,
	 *  null if the choosen file is not valid or if the user abort the loading. */
	public static Graph loadGraph(Component c,String directory,Ontology onto,Language l){
		JFileChooser fileChooser = new JFileChooser(directory);
		fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		fileChooser.setAcceptAllFileFilterUsed(false);
		fileChooser.setFileFilter(new CGXMLFileFilter());
		if(fileChooser.showOpenDialog(c) == JFileChooser.APPROVE_OPTION){
			try{
				Graph g = CGXMLParser.loadGraph(onto,l,fileChooser.getSelectedFile().getPath(),true);
				if(g == null){
					JOptionPane.showMessageDialog(c,NOT_VALID_GRAPH_FILE);
					return null;
				}				
				else return g;
			}
			catch(Exception e){
				JOptionPane.showMessageDialog(c,NOT_VALID_GRAPH_FILE);
				return null;
			}
		}
		else return null;
	}
	
	/** Open a file chooser to allow the user to save a graph and returns true if the graph
	 *  is successfully saved, false otherwise. */
	public static boolean saveGraph(Component c,String directory,Ontology onto,Language l,Graph g){
		JFileChooser fileChooser = new JFileChooser(directory);
		fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
		fileChooser.setAcceptAllFileFilterUsed(false);
		fileChooser.setFileFilter(new CGXMLFileFilter());
		if(fileChooser.showSaveDialog(c) == JFileChooser.APPROVE_OPTION){
			File f = fileChooser.getSelectedFile();
			if(!f.getName().endsWith(CGXMLFileFilter.CGXML_FILE_EXTENSION)) f = new File(f.getPath() + CGXMLFileFilter.CGXML_FILE_EXTENSION);
			if(f.exists()){
				if(JOptionPane.showConfirmDialog(c,f.getName() + "\n" + OVERWRITE_FILE_MESSAGE) == JOptionPane.OK_OPTION){
					try{
						if(CGXMLParser.saveGraph(onto,g,l,f.getPath(),true)){
							JOptionPane.showMessageDialog(c,SAVING_GRAPH_SUCCESSFULL_MESSAGE);
							return true;
						}
						else{
							JOptionPane.showMessageDialog(c,SAVING_GRAPH_PROBLEM_MESSAGE);
							return false;
						}
					}
					catch(Exception e){
						JOptionPane.showMessageDialog(c,SAVING_GRAPH_PROBLEM_MESSAGE + "\n\n" + e.getMessage());
						return false;
					}
				}
				else return false;
			}
			else{
				try{
					if(CGXMLParser.saveGraph(onto,g,l,f.getPath(),true)){
						JOptionPane.showMessageDialog(c,SAVING_GRAPH_SUCCESSFULL_MESSAGE);
						return true;
					}
					else{
						JOptionPane.showMessageDialog(c,SAVING_GRAPH_PROBLEM_MESSAGE);
						return false;
					}
				}
				catch(Exception e){
					JOptionPane.showMessageDialog(c,SAVING_GRAPH_PROBLEM_MESSAGE + "\n\n" + e.getMessage());
					return false;
				}
			}
		}
		else return false;
	}
	
	/** Load ontology file by using CGXML or OWL parser depending on the type of file. */
	public static Ontology loadOntology(File f,Language l, boolean trace) throws OntologyFileLoadingException{
		try{
			if(f.getName().substring(f.getName().lastIndexOf(".")).equals(CGXMLFileFilter.CGXML_FILE_EXTENSION))
				return CGXMLParser.loadOntology(f.getPath(),l,trace);
			else
				return OWLParser.loadOntology(f.toURI(),l,trace);
		}
		catch(Exception e){
			throw new OntologyFileLoadingException(f.getName());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -