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  /**
19   This demo shows how XML files can imported as
20   GraphML by the means of an XSLT stylesheet. 
21   Sample stylesheets for the following XML data are provided:
22   <ul>
23    <li><a href="resources/xsl/ant2graphml.xsl">Ant build scripts</a></li>
24    <li><a href="resources/xsl/owl2graphml.xsl">OWL web ontology data</a></li>
25    <li><a href="resources/xsl/xmltree2graphml.xsl">the XML tree structure</a></li>
26   </ul>
27   */
28  public class XMLXSLDemo extends GraphMLDemo
29  {
30    
31    /** 
32     * Creates a new instance of XMLXSLDemo 
33     */
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    /**
72     * Action that loads a graph from a specific file in GraphML format.
73     */
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   /**
113    * Launches this demo.
114    */
115   public static void main(String[] args)
116   {
117     initLnF();
118     final XMLXSLDemo demo = new XMLXSLDemo();
119     demo.start();
120   }
121 }
122