View Javadoc
1   /*
2    ORDI - Ontology Repository and Data Integration
3   
4    Copyright (c) 2004-2007, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  package examples;
19  
20  import java.io.File;
21  import java.io.FileReader;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.wsmo.common.TopEntity;
28  import org.wsmo.wsml.Serializer;
29  
30  import com.ontotext.ordi.wsmo4rdf.WSMLTripleSerializer;
31  
32  /**
33   * This class parses a user specified WSML file and save it in WSMLRDF format!
34   */
35  class ExportToWSMLRDF {
36      public static void main(String[] args) {
37  
38          // Check the input arguments supplied by the users
39          if (args.length == 0) {
40              System.out
41                      .println("Specify WSML file to be transformed to WSMLRDF!");
42              System.exit(1);
43          }
44          File file = new File(args[0]);
45          if (file.exists() == false) {
46              System.out.println(String.format(
47                      "The specified file name %s does not exist!", args[0]));
48              System.exit(1);
49          }
50  
51          // Create WSML parser and parse the input file
52          Map<String, Object> map = new HashMap<String, Object>();
53          map.put(org.wsmo.factory.Factory.PROVIDER_CLASS,
54                  "org.deri.wsmo4j.io.parser.wsml.ParserImpl");
55          org.wsmo.wsml.Parser wsmlParser = org.wsmo.factory.Factory
56                  .createParser(map);
57          TopEntity[] topEntities = null;
58          try {
59              topEntities = wsmlParser.parse(new FileReader(file));
60          } catch (Exception e) {
61              throw new RuntimeException("Could not parse the input file!", e);
62          }
63  
64          // Create output file
65          File outputFile = null;
66          FileWriter writer = null;
67          try {
68              outputFile = new File(file.getAbsolutePath() + ".rdfxml");
69              writer = new FileWriter(outputFile);
70          } catch (IOException e) {
71              throw new RuntimeException("Could not create the output file!", e);
72          }
73  
74          // Construct a WSMLRDF serializer
75          Map<String, Object> createParams = new HashMap<String, Object>();
76          createParams.put(org.wsmo.factory.Factory.PROVIDER_CLASS,
77                  WSMLTripleSerializer.class.getName());
78          Serializer rdfparser = org.wsmo.factory.Factory
79                  .createSerializer(createParams);
80  
81          // Generate the output WSMLRDF file
82          try {
83              rdfparser.serialize(topEntities, writer);
84          } catch (IOException e) {
85              throw new RuntimeException("Error while generating the WSMLRDF!", e);
86          }
87          System.out.println(String.format("Output has beed saved in %s!",
88                  outputFile.getAbsolutePath()));
89      }
90  }