1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class Main {
static org.jdom2.Document document;
static Element racine ;
static void lireFichier(String fichier ) throws JDOMException, IOException{
SAXBuilder sxb=new SAXBuilder();
document=sxb.build(new File(fichier));
racine=document.getRootElement();
System.out.println(racine);
}
static void afficherElement(String element) throws FileNotFoundException{
OntModel onto ;
File file=new File("DataTourist.owl");
onto =ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null);
String uriBase="http://www.ontologie.fr/DataTourist#";
onto.createOntology(uriBase);
FileOutputStream fichierXML=null;
List listFilsDirect =racine.getChildren();
Iterator i= listFilsDirect.iterator();
String str = "";
while(i.hasNext()){
Element courant = (Element) i.next();
while(!str.equals(courant.toString())){
str=courant.toString();
System.out.println("..........ici on affichera les tourist Data..........");
System.out.println("le fils direct est:"+courant);
List listChildren=courant.getChildren();
if( listChildren!=null){
Iterator j = listChildren.iterator();
while(j.hasNext()){
Element filsCourant=(Element)j.next();
System.out.println("le fils du fils DataTourist : "+filsCourant);
OntClass classe=onto.createClass(uriBase+filsCourant.getName());
//liste des datatYpe
List listChildrenOfChildren =filsCourant.getChildren();
if(listChildrenOfChildren !=null){
Iterator k=listChildrenOfChildren.iterator();
while(k.hasNext()){
Element FilsFilsCourant=(Element)k.next();
DatatypeProperty prop=onto.createDatatypeProperty(uriBase+FilsFilsCourant.getName());
System.out.println(" le petit fils est : "+ FilsFilsCourant);
if( FilsFilsCourant.hasAttributes()==true){
System.out.println("le sous fils du fils direct posséde un attribut "+FilsFilsCourant.getAttributes());
//if(FilsFilsCourant.getAttributeValue("nature").equals("ForeignKey") ){
if("[[Attribute: nature=ForeignKey]]".equalsIgnoreCase(FilsFilsCourant.getAttributes().toString())){
ObjectProperty prop2=onto.createObjectProperty(uriBase+FilsFilsCourant.getName());
OntClass c =onto.createClass(uriBase+FilsFilsCourant.getAttributeValue("range")) ;
// System.out.println(FilsFilsCourant.getAttributeValue("Domaine").toString());
prop2.setRange(c);
OntClass domaine=onto.createClass(uriBase+FilsFilsCourant.getParent());
prop2.setDomain(domaine) ;
}
}
}
}
}
}
}
}
fichierXML= new FileOutputStream (file);
onto.write (fichierXML);
}
public static void main(String[] args) throws JDOMException, IOException {
// TODO Auto-generated method stub
lireFichier("touristData.xml");
afficherElement("racine");
}
} |
Partager