Présentation

123hani

Pseudo: HaniCatégorie: ScienceDescription:
Bonjour c'est Hani
Recommander ce blog
Mercredi 23 Janvier 2008

 

 
2007-2008
 
 
 
Prénom : MOHAMMED HANNI  
 
Nom : DIGOU
 
Numéro : 20707198
 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1- Le fichier "gedcom.dtd" :

<!ELEMENT gedcom (HEAD,(INDI|FAM)*,TRLR) >

<!ELEMENT HEAD EMPTY >
<!ELEMENT TRLR EMPTY >
 
<!ELEMENT INDI (NAME?,SEX?,FAMC?,FAMS?,OBJE?) >
<!ELEMENT FAM (HUSB?,WIFE?,CHIL*,OBJE?) >
<!ATTLIST INDI id ID #REQUIRED >
<!ATTLIST FAM id ID #REQUIRED >

<!ELEMENT NAME (#PCDATA) >

<!ATTLIST FAMC idref IDREF #REQUIRED >
<!ELEMENT FAMC EMPTY >
<!ATTLIST FAMS idref IDREF #REQUIRED >
<!ELEMENT FAMS EMPTY >

<!ELEMENT SEX (#PCDATA) >

<!ATTLIST HUSB idref IDREF #REQUIRED >
<!ELEMENT HUSB EMPTY >
<!ATTLIST WIFE idref IDREF #REQUIRED >
<!ELEMENT WIFE EMPTY >
<!ATTLIST CHIL idref IDREF #REQUIRED >
<!ELEMENT CHIL EMPTY >

<!ELEMENT OBJE (FORM,TITL,FILE) >
<!ELEMENT FORM (#PCDATA) >
<!ELEMENT TITL (#PCDATA) >
<!ELEMENT FILE (#PCDATA) >

 

2- Le code source du fichier "XML.java" :

 package xml;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.Dimension;
import java.awt.FileDialog;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.List;

import javax.swing.JLabel;

/**
 * Example AWT Frame with some common components on it
 */
//AWT Application
public class XML extends JFrame {

 private JPanel jPanel = null;
 private JButton jButton = null;
 private FileDialog fopen=null;
 private String chem=null;
 private JLabel jLabel = null;
 private JLabel jLabel2 = null;
 final static boolean report_times = true;
 private JButton jButton2 = null;
 String iName = null, oName = null;
 /**
  * This method initializes
  *
  */
 public XML() {
  super();
  initialize();
 }

 /**
  * This method initializes this
  *
  */
 private void initialize() {
  this.setSize(new Dimension(634, 169));
  this.setTitle("GedcomToXML");
  this.setContentPane(getJPanel());

 }

 /**
  * This method initializes jPanel 
  *  
  * @return javax.swing.JPanel 
  */
 private JPanel getJPanel() {
  if (jPanel == null) {
   jLabel2 = new JLabel();
   jLabel2.setBounds(new Rectangle(23, 66, 367, 28));
   jLabel2.setText("");
   jLabel = new JLabel();
   jLabel.setBounds(new Rectangle(23, 32, 185, 25));
   jLabel.setText("");
   jPanel = new JPanel();
   jPanel.setLayout(null);
   jPanel.add(getJButton(), null);
   jPanel.add(jLabel, null);
   jPanel.add(jLabel2, null);
   jPanel.add(getJButton2(), null);
  }
  return jPanel;
 }

 /**
  * This method initializes jButton 
  *  
  * @return javax.swing.JButton 
  */
 private JButton getJButton() {
  if (jButton == null) {
   jButton = new JButton();
   jButton.setBounds(new Rectangle(450, 45, 121, 30));
   jButton.setText("Parcourir...");
   jButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e) {
     //ouvre une boite de dialogue
     fopen= new FileDialog(new XML(),"Ouvrir",FileDialog.LOAD); //ouvrir une boite de dialogue
     fopen.setVisible(true);
     String nom = fopen.getFile();
     String dir = fopen.getDirectory();
     //le chemin du fichier à charger
     chem = dir+nom;
     String ext="";
     jLabel.setText("");
     jLabel2.setText("");

     try{
      ext = nom.substring(nom.length()-4,nom.length());
     }catch(NullPointerException ne){};
     //charger que les fichiers .ged
     if(nom != null && ext.equals(".ged")){
      System.out.println("l'emplacement du fichier .ged "+chem);
      jLabel.setText("Le chemin du fichier .ged :");
      jLabel2.setText(chem);
      jButton.setVisible(false);
      jButton2.setVisible(true);
      jButton2.setBounds(new Rectangle(450, 45, 121, 30));

     }
     else{

      JOptionPane.showMessageDialog(null,"Veuillez sélectionner un fichier .ged");
     }
    }
   });
  }

  return jButton;
 }


// fonction qui retire les espaces avant l'entree
 public static String SupprimeEspace(String s) {
  while (s.startsWith(" ")) {
   s = s.substring(1);   
  }
  return s;
 }

 //conversion
 public static void conversion_xml(BufferedReader buf, PrintWriter p, String s,
   List<String> balise_xml) {
  // recuperer les chaines d'une ligne .ged
  String[] tab = s.split(" ");
  // le niveau
  int hierarchie = Integer.parseInt(tab[0]);

  // fermer les balises
  for (int i = balise_xml.size() - 1; i >= hierarchie; i--) {
   p.println("</" + balise_xml.remove(i) + ">");
   
  }
  //ajouter les balises selon l'entrée
  if (s.endsWith("HEAD")) {
   p.println("<HEAD>");
   balise_xml.add(hierarchie, "HEAD");

  }
  else if (s.endsWith("INDI")) {
   //balise avec identifiant
   p.println("<INDI id=" + '"'
     + tab[1].substring(1, tab[1].lastIndexOf("@")) + '"' + ">");

   balise_xml.add(hierarchie, "INDI");

  }
  else if (s.endsWith("FAM")) {
   p.println("<FAM id=" + '"'
     + tab[1].substring(1, tab[1].lastIndexOf("@")) + '"' + ">");

   balise_xml.add(hierarchie, "FAM");

  }
  else if (tab[1].equals("NAME")) {
   //extraire à partir de la position7 le name
   p.println("<NAME>" + s.substring(7) + "</NAME>");

  }
  else if (tab[1].equals("SEX")) {
   //tab[2] va étre soit M soit F
   p.println("<SEX>" + tab[2] + "</SEX>");

  }
  else if (tab[1].equals("BIRT")) {
   p.println("<BIRT>");
   balise_xml.add(hierarchie, "BIRT");
  }
  else if (tab[1].equals("DEAT")) {
   p.println("<DEAT>");

   balise_xml.add(hierarchie, "DEAT");
  }
  else if (tab[1].equals("FAMC")) {
   p.println("<FAMC idref=" + '"'
     + tab[2].substring(1, tab[2].lastIndexOf("@")) + '"'
     + "></FAMC>");

  }
  else if (tab[1].equals("FAMS")) {
   p.println("<FAMS idref=" + '"'
     + tab[2].substring(1, tab[2].lastIndexOf("@")) + '"'
     + "></FAMS>");

  }
  else if (tab[1].equals("HUSB")) {
   p.println("<HUSB idref=" + '"'
     + tab[2].substring(1, tab[2].lastIndexOf("@")) + '"'
     + "></HUSB>");

  }
  else if (tab[1].equals("WIFE")) {
   p.println("<WIFE idref=" + '"'
     + tab[2].substring(1, tab[2].lastIndexOf("@")) + '"'
     + "></WIFE>");

  } else if (tab[1].equals("CHIL")) {
   p.println("<CHIL idref=" + '"'
     + tab[2].substring(1, tab[2].lastIndexOf("@")) + '"'
     + "></CHIL>");

  } else if (tab[1].equals("MARR")) {
   p.println("<MARR>");

   balise_xml.add(hierarchie, "MARR");
  }
  else if (tab[1].equals("DATE")) {
   p.println("<DATE>" + s.substring(7) + "</DATE>");

  }
  else if (tab[1].equals("PLAC")) {
   p.println("<PLAC>" + s.substring(7) + "</PLAC>");

  }
  else if (tab[1].equals("SOUR")) {
   p.println("<SOUR sour=" + '"' + s.substring(7) + '"' + ">");

   balise_xml.add(hierarchie, "SOUR");
  }
  else if (tab[1].equals("VERS")) {
   p.println("<VERS>" + s.substring(7) + "</VERS>");

  } else if (tab[1].equals("CORP")) {
   p.println("<CORP corp=" + '"' + s.substring(7) + '"' + ">");

   balise_xml.add(hierarchie, "CORP");
  }
  else if (tab[1].equals("ADDR")) {
   p.println("<ADDR addr=" + '"' + s.substring(7) + '"' + ">");

   balise_xml.add(hierarchie, "ADDR");
  }
  else if (tab[1].equals("CONT")) {
   p.println("<CONT>" + s.substring(7) + "</CONT>");

  }
  else if (tab[1].equals("PHON")) {
   p.println("<PHON>" + s.substring(7) + "</PHON>");

  }
  else if (tab[1].equals("DEST")) {
   p.println("<DEST>" + s.substring(7) + "</DEST>");

  }
  else if (tab[1].equals("CHAR")) {
   p.println("<CHAR>" + s.substring(7) + "</CHAR>");

  } else if (tab[1].equals("FILE")) {
   p.println("<FILE>" + s.substring(7) + "</FILE>");

  }
  else if (tab[1].equals("GEDC")) {
   p.println("<GEDC>");
   balise_xml.add(hierarchie, "GEDC");
  }
  else if (s.endsWith("SUBM")) {
   p.println("<SUBM id=" + '"'
     + tab[1].substring(1, tab[1].lastIndexOf("@")) + '"' + ">");
   balise_xml.add(hierarchie, "SUBM");
  }
  else if (tab[1].equals("COMM")) {
   p.println("<COMM comm=" + '"' + s.substring(7) + '"' + ">");
   balise_xml.add(hierarchie, "COMM");
  }
  else if (tab[1].equals("TITL")) {
   p.println("<TITL title=" + '"' + s.substring(7) + '"' + ">");
   balise_xml.add(hierarchie, "TITL");
  }
  else if (tab[1].equals("BURI")) {
   p.println("<BURI>");
   balise_xml.add(hierarchie, "BURI");
  }
  else if (tab[1].equals("REFN")) {
   p.println("<REFN>" + s.substring(7) + "</REFN>");
  }
  else if (s.endsWith("TRLR")) {
   p.println("<TRLR></TRLR>");

   return;
  }
  p.flush();
  try {
   s = buf.readLine();
   s = SupprimeEspace(s);
   while (s.equals("")) {
    s = buf.readLine();
    s = SupprimeEspace(s);
   }
  } catch (Exception e) {
   System.err.println(e);
   e.printStackTrace();
   System.exit(1);
  }
  conversion_xml(buf, p, s, balise_xml);
 }

 public static void Entete_xml(BufferedReader buf, PrintWriter p) {
  long start_time;
  try {
   //calculer le temps d'éxecution
   if (report_times) {
    start_time = System.currentTimeMillis();
   }

   //entete XML
   p.println("<?xml version=" + '"' + "1.0" + '"' + " encoding="
     + '"' + "iso-8859-1" + '"' + " ?>");
   //dtd externe
   p.println("<!DOCTYPE gedcom SYSTEM " + '"' + "gedcom.dtd" + '"'
     + " >");
   p.println("<gedcom>");

   // convertion
   String s;
   s = buf.readLine();
   s = SupprimeEspace(s);
   while (s.equals("")) {
    s = buf.readLine();
    s = SupprimeEspace(s);
   }
   conversion_xml(buf, p, s, new java.util.ArrayList<String>(0));

   // fermer la balise gedcom
   p.println("</gedcom>");

   if (report_times) {
    start_time = System.currentTimeMillis() - start_time;
    System.out.println("Temps de conversion pris:" + ((start_time / 10) / 100F)
      + " seconds.");
   }

  }catch(NumberFormatException ee){System.err.println("Erreur de format");}
  catch (Exception e) {
   System.err.println(e);
   e.printStackTrace();
   System.exit(1);
  }

 }


 /**
  * This method initializes jButton2 
  *  
  * @return javax.swing.JButton 
  */
 private JButton getJButton2() {
  if (jButton2 == null) {
   jButton2 = new JButton();
   jButton2.setBounds(new Rectangle(452, 82, 114, 27));
   jButton2.setText("Convertir");
   jButton2.setVisible(false);
   jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e) {
     try {
      iName = chem;
      //chemin du fichier de sortie
      oName= chem.substring(0,chem.length() - 4) + ".xml";

      BufferedReader buf = new BufferedReader(new FileReader(iName));
      PrintWriter p = new PrintWriter(oName);

      Entete_xml(buf, p);
      System.out.println("Conversion Gedcom en xml terminée");
      buf.close();
      p.close();
      jLabel.setText("");
      jLabel2.setText("");
      jLabel.setText("Chemin du fichier .xml");
      jLabel2.setText(oName);
      System.out.println("l'emplacement du fichier .xml "+oName);
      jButton2.setVisible(false);
      jButton.setVisible(true);
      jLabel.setText("");
      jLabel2.setText("");
      //System.exit(0);

     }catch(Exception ef) {
      System.err.println(ef);
      System.exit(0);
     }

    }
   });
  }
  return jButton2;
 }

 public static void main(String[] args) {
  XML x= new XML();
  x.setVisible(true);


 }

}  //  @jve:decl-index=0:visual-constraint="49,50"

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

publié par Hani dans: 123hani

Portail de l'emploi 100% gratuit

Créer un blog sur dzblog.com - Contact - C.G.U. - Reporter un abus