| XmlXslDemo.java |
1 /****************************************************************************
2 * This demo file is part of yFiles for Java 2.14.
3 * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4 * 72070 Tuebingen, Germany. All rights reserved.
5 *
6 * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7 * of demo files in source code or binary form, with or without
8 * modification, is not permitted.
9 *
10 * Owners of a valid software license for a yFiles for Java version that this
11 * demo is shipped with are allowed to use the demo source code as basis
12 * for their own yFiles for Java powered applications. Use of such programs is
13 * governed by the rights and conditions as set out in the yFiles for Java
14 * license agreement.
15 *
16 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 ***************************************************************************/
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 /**
52 * This demo shows how XML files can imported as
53 * GraphML by the means of an XSLT stylesheet.
54 * Sample stylesheets for the following XML data are provided:
55 * <ul>
56 * <li><a href="resources/xsl/ant2graphml.xsl">Ant build scripts</a></li>
57 * <li><a href="resources/xsl/owl2graphml.xsl">OWL web ontology data</a></li>
58 * <li><a href="resources/xsl/xmltree2graphml.xsl">the XML tree structure</a></li>
59 * </ul>
60 * <p>
61 * <b>Third Party Licenses:</b><br/>
62 * The OWL web ontology that is used in this demo is based on
63 * <a href="http://owl.semanticweb.org/page/TestCase:WebOnt-miscellaneous-002">WebOnt-miscellaneous-002
64 * by Michael K. Smith, Chris Welty, and Deborah L. McGuinness</a>
65 * and licensed under the
66 * <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported</a>
67 * license.
68 * </p>
69 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/graphml#graphml_xslt" target="_blank">Section yFiles XSLT Support for GraphML</a> in the yFiles for Java Developer's Guide
70 */
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 licensed under the Creative Commons Attribution 3.0 Unported license
90 {"resources/xml/food.owl", "resources/xsl/owl2graphml.xsl"},
91 // resources/xml/food.owl licensed under the Creative Commons Attribution 3.0 Unported license
92 {"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 /**
188 * Launches this demo.
189 */
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