1
14 package demo.io.graphml;
15
16
17 import y.io.XmlXslIOHandler;
18 import y.util.D;
19 import y.view.Graph2D;
20
21 import javax.swing.AbstractAction;
22 import javax.swing.Action;
23 import javax.swing.JFileChooser;
24 import javax.swing.JMenu;
25 import javax.swing.JMenuBar;
26 import javax.swing.filechooser.FileFilter;
27 import javax.xml.transform.stream.StreamSource;
28 import java.awt.EventQueue;
29 import java.awt.event.ActionEvent;
30 import java.io.File;
31 import java.io.IOException;
32 import java.net.MalformedURLException;
33 import java.net.URL;
34 import java.util.Locale;
35
36
37
49 public class XmlXslDemo extends GraphMLDemo {
50 private String[][] sampleFiles;
51
52 public XmlXslDemo() {
53 graphMLPane.setEditable(false);
54 }
55
56 protected void loadInitialGraph() {
57 if (sampleFiles != null) {
58 loadXml(getClass().getResource(sampleFiles[0][0]), getClass().getResource(sampleFiles[0][1]));
59 }
60 }
61
62 protected void initialize() {
63 super.initialize();
64
65 sampleFiles = new String[][]{
66 {"resources/xml/ant-build.xml", "resources/xsl/ant2graphml.xsl"},
67 {"resources/xml/food.owl", "resources/xsl/owl2graphml.xsl"},
68 {"resources/xml/food.owl", "resources/xsl/xmltree2graphml.xsl"},
69 };
70 }
71
72 protected JMenuBar createMenuBar() {
73 final JMenuBar menuBar = super.createMenuBar();
74 createExamplesMenu(menuBar);
75 return menuBar;
76 }
77
78 protected void createExamplesMenu(JMenuBar menuBar) {
79 final JMenu menu = new JMenu("Example Graphs");
80 menuBar.add(menu);
81
82 for (int i = 0; i < sampleFiles.length; i++) {
83 final String xml = sampleFiles[i][0];
84 final String xsl = sampleFiles[i][1];
85 final String name = xml.substring(xml.lastIndexOf('/') + 1)
86 + " + " + xsl.substring(xsl.lastIndexOf('/') + 1);
87
88 menu.add(new AbstractAction(name) {
89 public void actionPerformed(ActionEvent e) {
90 loadXml(getClass().getResource(xml), getClass().getResource(xsl));
91 }
92 });
93 }
94 }
95
96 protected String[] getExampleResources() {
97 return null;
98 }
99
100 public void loadXml(URL xmlResource, URL xslResource) {
101 Graph2D graph = view.getGraph2D();
102 try {
103 XmlXslIOHandler ioh = new XmlXslIOHandler(createGraphMLIOHandler());
104 ioh.setXslSource(new StreamSource(xslResource.openStream()));
105 ioh.read(graph, xmlResource);
106 view.fitContent();
107 view.updateView();
108 }
109 catch (IOException ioe) {
110 D.show(ioe);
111 }
112 finally {
113 graphMLPane.updateGraphMLText(graph);
114 }
115 }
116
117 protected Action createLoadAction() {
118 return new AbstractAction("Load...") {
119
120 public void actionPerformed(ActionEvent e) {
121 JFileChooser chooser = new JFileChooser();
122 chooser.setAcceptAllFileFilterUsed(true);
123 chooser.setDialogTitle("XML input");
124
125 URL xmlResource = null;
126 if (chooser.showOpenDialog(contentPane) == JFileChooser.APPROVE_OPTION) {
127 try {
128 xmlResource = chooser.getSelectedFile().toURI().toURL();
129 } catch (MalformedURLException urlex) {
130 urlex.printStackTrace();
131 }
132 }
133 if (xmlResource != null) {
134 chooser.setAcceptAllFileFilterUsed(false);
135 chooser.setDialogTitle("XSL stylesheet");
136 chooser.addChoosableFileFilter(new FileFilter() {
137 public boolean accept(File f) {
138 return f.isDirectory() || f.getName().endsWith(".xsl");
139 }
140
141 public String getDescription() {
142 return "XML stylesheets (.xsl)";
143 }
144 });
145
146 if (chooser.showOpenDialog(contentPane) == JFileChooser.APPROVE_OPTION) {
147 try {
148 URL xslResource = chooser.getSelectedFile().toURI().toURL();
149 if (xslResource != null) {
150 loadXml(xmlResource, xslResource);
151 }
152 } catch (MalformedURLException urlex) {
153 urlex.printStackTrace();
154 }
155
156 }
157 }
158 }
159
160 };
161 }
162
163
166 public static void main(String[] args) {
167 EventQueue.invokeLater(new Runnable() {
168 public void run() {
169 Locale.setDefault(Locale.ENGLISH);
170 initLnF();
171 new XmlXslDemo().start();
172 }
173 });
174 }
175 }
176