1
28 package demo.io.graphml;
29
30
31 import y.io.XmlXslIOHandler;
32 import y.util.D;
33 import y.view.Graph2D;
34
35 import javax.swing.AbstractAction;
36 import javax.swing.Action;
37 import javax.swing.JFileChooser;
38 import javax.swing.JMenu;
39 import javax.swing.JMenuBar;
40 import javax.swing.filechooser.FileFilter;
41 import javax.xml.transform.stream.StreamSource;
42 import java.awt.EventQueue;
43 import java.awt.event.ActionEvent;
44 import java.io.File;
45 import java.io.IOException;
46 import java.net.MalformedURLException;
47 import java.net.URL;
48 import java.util.Locale;
49
50
51
71 public class XmlXslDemo extends GraphMLDemo {
72 private String[][] sampleFiles;
73
74 public XmlXslDemo() {
75 graphMLPane.setEditable(false);
76 }
77
78 protected void loadInitialGraph() {
79 if (sampleFiles != null) {
80 loadXml(getResource(sampleFiles[0][0]), getResource(sampleFiles[0][1]));
81 }
82 }
83
84 protected void initialize() {
85 super.initialize();
86
87 sampleFiles = new String[][]{
88 {"resources/xml/ant-build.xml", "resources/xsl/ant2graphml.xsl"},
89 {"resources/xml/food.owl", "resources/xsl/owl2graphml.xsl"},
91 {"resources/xml/food.owl", "resources/xsl/xmltree2graphml.xsl"},
93 };
94 }
95
96 protected JMenuBar createMenuBar() {
97 final JMenuBar menuBar = super.createMenuBar();
98 createExamplesMenu(menuBar);
99 return menuBar;
100 }
101
102 protected void createExamplesMenu(JMenuBar menuBar) {
103 final JMenu menu = new JMenu("Example Graphs");
104 menuBar.add(menu);
105
106 for (int i = 0; i < sampleFiles.length; i++) {
107 final String xml = sampleFiles[i][0];
108 final String xsl = sampleFiles[i][1];
109 final String name = xml.substring(xml.lastIndexOf('/') + 1)
110 + " + " + xsl.substring(xsl.lastIndexOf('/') + 1);
111
112 menu.add(new AbstractAction(name) {
113 public void actionPerformed(ActionEvent e) {
114 loadXml(getResource(xml), getResource(xsl));
115 }
116 });
117 }
118 }
119
120 protected String[] getExampleResources() {
121 return null;
122 }
123
124 public void loadXml(URL xmlResource, URL xslResource) {
125 Graph2D graph = view.getGraph2D();
126 try {
127 XmlXslIOHandler ioh = new XmlXslIOHandler(createGraphMLIOHandler());
128 ioh.setXslSource(new StreamSource(xslResource.openStream()));
129 ioh.read(graph, xmlResource);
130 view.fitContent();
131 view.updateView();
132 }
133 catch (IOException ioe) {
134 D.show(ioe);
135 }
136 finally {
137 graphMLPane.updateGraphMLText(graph);
138 }
139 }
140
141 protected Action createLoadAction() {
142 return new AbstractAction("Load...") {
143
144 public void actionPerformed(ActionEvent e) {
145 JFileChooser chooser = new JFileChooser();
146 chooser.setAcceptAllFileFilterUsed(true);
147 chooser.setDialogTitle("XML input");
148
149 URL xmlResource = null;
150 if (chooser.showOpenDialog(contentPane) == JFileChooser.APPROVE_OPTION) {
151 try {
152 xmlResource = chooser.getSelectedFile().toURI().toURL();
153 } catch (MalformedURLException urlex) {
154 urlex.printStackTrace();
155 }
156 }
157 if (xmlResource != null) {
158 chooser.setAcceptAllFileFilterUsed(false);
159 chooser.setDialogTitle("XSL stylesheet");
160 chooser.addChoosableFileFilter(new FileFilter() {
161 public boolean accept(File f) {
162 return f.isDirectory() || f.getName().endsWith(".xsl");
163 }
164
165 public String getDescription() {
166 return "XML stylesheets (.xsl)";
167 }
168 });
169
170 if (chooser.showOpenDialog(contentPane) == JFileChooser.APPROVE_OPTION) {
171 try {
172 URL xslResource = chooser.getSelectedFile().toURI().toURL();
173 if (xslResource != null) {
174 loadXml(xmlResource, xslResource);
175 }
176 } catch (MalformedURLException urlex) {
177 urlex.printStackTrace();
178 }
179
180 }
181 }
182 }
183
184 };
185 }
186
187
190 public static void main(String[] args) {
191 EventQueue.invokeLater(new Runnable() {
192 public void run() {
193 Locale.setDefault(Locale.ENGLISH);
194 initLnF();
195 new XmlXslDemo().start();
196 }
197 });
198 }
199 }
200