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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Affichage {
public static void main(String[] args) throws ClassNotFoundException {
/* Création du parseur */
SAXBuilder builder = new SAXBuilder();
File fichierXML = new File("Exercice1.xml");
Document document;
try {
/* Parsing du fichier */
document = builder.build(fichierXML);
/* Racine du document XML : dans notre cas <diagramme> */
Element rootNode = document.getRootElement();
List<Element> elmts=rootNode.getChildren();
List<String> temps = new ArrayList<String>();
List<String> autres = new ArrayList<String>();
parserFichier(elmts,temps,autres);
window();
} catch (JDOMException e) {
e.printStackTrace(System.out);
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
private static void addOnce(List<String> list, String text) {
if ( !list.contains(text) ) list.add(text);
}
private static void collect(List<String> list, Element elmt, boolean addText) {
addOnce(list, elmt.getName());
if ( addText ) {
addOnce(list, elmt.getText());
}
}
private static void parserFichier(List<Element> elmts, List<String> temps, List<String> autres) throws ClassNotFoundException{
for (Element elmt : elmts) {
List<Element> children = elmt.getChildren();
if(("instant".equals(elmt.getName()))||("intervalle".equals(elmt.getName()))) {
collect( temps, elmt, children.isEmpty() );
for (Element child : children) {
collect(temps,child,children.isEmpty());
}
}
else{
collect( autres, elmt, children.isEmpty() );
if(!children.isEmpty()){
parserFichier( children, temps, autres );
}
}
}
}
public static void window(){
JScrollPane texteAsc;
JScrollPane texteAsc1;
JLabel label = new JLabel("Extraction de connaissances");
JFrame window=new JFrame("extraction connaissances");
JTextArea grandeZone = new JTextArea(7,10);
JTextArea grandeZone1 = new JTextArea(7, 10);
texteAsc = new JScrollPane(grandeZone);
texteAsc1 = new JScrollPane(grandeZone1);
JPanel container = new JPanel();
JPanel surfTotPanel = new JPanel();
JPanel surfTotPanel2 = new JPanel();
surfTotPanel.setLayout(new BoxLayout(surfTotPanel,BoxLayout.Y_AXIS));
Dimension dimension = new Dimension(200,400);
surfTotPanel.setPreferredSize(dimension);
surfTotPanel2.setLayout(new BoxLayout(surfTotPanel2,BoxLayout.Y_AXIS));
Dimension dimension2 = new Dimension(200,400);
surfTotPanel2.setPreferredSize(dimension2);
surfTotPanel.add(texteAsc1);
surfTotPanel2.add(texteAsc);
container.setLayout(new BorderLayout());
label.setHorizontalAlignment(JLabel.CENTER);
container.add(label, BorderLayout.NORTH);
container.add(surfTotPanel2, BorderLayout.EAST);
container.add(surfTotPanel,BorderLayout.WEST);
window.setContentPane(container);
window.setVisible(true);
window.setSize(500,400);
Font police = new Font("Andalus", Font.CENTER_BASELINE, 25);
label.setFont(police);
label.setForeground(Color.blue);
parserFichier (elmts, temps, autres);
StringBuilder texte = new StringBuilder("temps :");
for( String temp : temps ) {
texte.append( "\n" );
texte.append( "- " );
texte.append( temp );
}
grandeZone.setText(texte.toString());
}
} |
Partager