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.factory.Factory;
29  import org.wsmo.wsml.Parser;
30  import org.wsmo.wsml.Serializer;
31  
32  import com.ontotext.ordi.wsmo4rdf.WSMLTripleParser;
33  
34  /**
35   * This class provides a sample how to parse WSMLRDF file and generate a WSML
36   * output.
37   */
38  class ParseWSMLRDF {
39  
40      public static void main(String[] args) {
41          // Check the input arguments supplied by the users
42          if (args.length == 0) {
43              System.out
44                      .println("Specify WSMLRDF file to be transformed to WSML!");
45              System.exit(1);
46          }
47          File file = new File(args[0]);
48          if (file.exists() == false) {
49              System.out.println(String.format(
50                      "The specified file name %s does not exist!", args[0]));
51              System.exit(1);
52          }
53  
54          // Create WSMLRDF parser
55          Map<String, Object> createParams = new HashMap<String, Object>();
56          createParams.put(org.wsmo.factory.Factory.PROVIDER_CLASS,
57                  WSMLTripleParser.class.getName());
58          Parser wsmlrdfParser = Factory.createParser(createParams);
59  
60          // Parse the input WSMLRDF file
61          TopEntity[] topEntities = null;
62          try {
63              topEntities = wsmlrdfParser.parse(new FileReader(file));
64          } catch (Exception e) {
65              throw new RuntimeException(String.format(
66                      "Could not processes the input file %s!", file
67                              .getAbsolutePath()), e);
68          }
69  
70          // Create output file
71          File outputFile = null;
72          FileWriter writer = null;
73          try {
74              outputFile = new File(file.getAbsolutePath() + ".wsml");
75              writer = new FileWriter(outputFile);
76          } catch (IOException e) {
77              throw new RuntimeException("Could not create the output file!", e);
78          }
79  
80          // Create WSML serializer and serialize the processed WSMLRDF
81          createParams = new HashMap<String, Object>();
82          createParams.put(org.wsmo.factory.Factory.PROVIDER_CLASS,
83                  "org.deri.wsmo4j.io.serializer.wsml.SerializerImpl");
84          Serializer serializer = Factory.createSerializer(createParams);
85          try {
86              serializer.serialize(topEntities, writer);
87          } catch (IOException e) {
88              throw new RuntimeException("Could not serialize to WSML!", e);
89          }
90          System.out.println(String.format("Output has beed saved in %s!",
91                  outputFile.getAbsolutePath()));
92      }
93  }