1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
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  /**
38   This demo shows how XML files can imported as
39   GraphML by the means of an XSLT stylesheet.
40   Sample stylesheets for the following XML data are provided:
41   <ul>
42   <li><a href="resources/xsl/ant2graphml.xsl">Ant build scripts</a></li>
43   <li><a href="resources/xsl/owl2graphml.xsl">OWL web ontology data</a></li>
44   <li><a href="resources/xsl/xmltree2graphml.xsl">the XML tree structure</a></li>
45   </ul>
46  
47   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/graphml.html#graphml_xslt">Section yFiles XSLT Support for GraphML</a> in the yFiles for Java Developer's Guide
48   */
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   /**
164    * Launches this demo.
165    */
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