1 package demo.yext.graphml;
2
3
4 import java.awt.event.ActionEvent;
5 import java.io.IOException;
6 import java.net.URL;
7
8 import javax.swing.AbstractAction;
9 import javax.swing.JMenu;
10 import javax.xml.transform.stream.StreamSource;
11
12 import y.util.D;
13 import y.view.Graph2D;
14 import yext.graphml.graph2D.GraphMLIOHandler;
15 import yext.graphml.graph2D.XMLXSLIOHandler;
16
17
18
28 public class XMLXSLDemo extends GraphMLDemo
29 {
30
31
34 public XMLXSLDemo()
35 {
36 }
37
38 protected JMenu createSampleGraphMenu() {
39 JMenu menu = new JMenu("XML/XSL Samples");
40 String[][] resources = {
41 {"resources/xml/ant-build.xml",
42 "resources/xsl/ant2graphml.xsl"},
43 {"resources/xml/food.owl",
44 "resources/xsl/owl2graphml.xsl"},
45 {"resources/xml/food.owl",
46 "resources/xsl/xmltree2graphml.xsl"},
47 };
48 int resolvedResourceCount = 0;
49 for (int i = 0; i < resources.length; ++i) {
50 final URL xml = getClass().getResource(resources[i][0]);
51 final URL xsl = getClass().getResource(resources[i][1]);
52 if (xml != null && xsl != null) {
53 ++resolvedResourceCount;
54 menu.add(new LoadXMLPlusXSLAction(xml, xsl));
55 } else {
56 if (xml == null) {
57 System.err.println("Could not resolve sample resource " + resources[i][0]);
58 }
59 if (xsl == null) {
60 System.err.println("Could not resolve sample resource " + resources[i][1]);
61 }
62 }
63 }
64 if (resolvedResourceCount > 0) {
65 return menu;
66 } else {
67 return null;
68 }
69 }
70
71
74 class LoadXMLPlusXSLAction extends AbstractAction
75 {
76 URL xmlResource, xslResource;
77 LoadXMLPlusXSLAction(URL xmlResource, URL xslResource)
78 {
79 putValue(AbstractAction.NAME,
80 "XML:" + urlToName(xmlResource) + " XSL:" + urlToName(xslResource));
81 this.xmlResource = xmlResource;
82 this.xslResource = xslResource;
83 }
84
85 String urlToName(URL url)
86 {
87 String file = url.getFile();
88 return file.substring(file.lastIndexOf('/')+1);
89 }
90
91 public void actionPerformed(ActionEvent ev)
92 {
93 try
94 {
95 Graph2D graph = view.getGraph2D();
96 graph.clear();
97 GraphMLIOHandler graphml = new GraphMLIOHandler();
98 XMLXSLIOHandler ioh = new XMLXSLIOHandler(graphml);
99
100 ioh.setXSLSource(new StreamSource(xslResource.openStream()));
101 ioh.read(graph, xmlResource);
102 view.fitContent();
103 view.updateView();
104 }
105 catch (IOException ioe)
106 {
107 D.show(ioe);
108 }
109 }
110 }
111
112
115 public static void main(String[] args)
116 {
117 initLnF();
118 final XMLXSLDemo demo = new XMLXSLDemo();
119 demo.start();
120 }
121 }
122