View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2007, Ontotext Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  /***
19   * <p>Title: WSMO Studio</p>
20   * <p>Description: Semantic Web Service Editor</p>
21   * <p>Copyright:  Copyright (c) 2004-2007</p>
22   * <p>Company: OntoText Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.ui.wizards;
26  
27  import java.io.*;
28  import java.util.*;
29  
30  import org.eclipse.core.resources.IFile;
31  import org.eclipse.jface.dialogs.MessageDialog;
32  import org.eclipse.jface.viewers.IStructuredSelection;
33  import org.eclipse.jface.wizard.Wizard;
34  import org.eclipse.ui.*;
35  import org.wsmo.common.TopEntity;
36  import org.wsmo.wsml.*;
37  import org.wsmostudio.runtime.*;
38  
39  import com.ontotext.ordi.wsmo4rdf.WSMLTripleSerializer;
40  
41  public class RDFExportWizard extends Wizard implements IExportWizard {
42  
43      private IStructuredSelection selection;
44      
45      public void init(IWorkbench workbench, IStructuredSelection selection) {
46          this.selection = selection;
47      }
48      ExportWorkspaceWSMLPage mainPage;
49      
50      public RDFExportWizard() {
51          super();
52          setWindowTitle("WSML in RDF Export");
53      }
54      
55      public void addPages() {
56          mainPage = new ExportWorkspaceWSMLPage(selection) {
57              public String getName() {
58                  return "WSML in RDF Export";
59              }
60          };
61          addPage(mainPage);
62      }
63  
64      public boolean performFinish() {
65          IStructuredSelection sel = mainPage.getFileSelection();
66          File outFolder = mainPage.getTargetDirectory();
67          if (outFolder == null
68                  || sel == null 
69                  || sel.size() == 0) {
70              return false; // invalid seleciton
71          }
72          
73          Parser wsmlParser = WSMORuntime.getRuntime().getWsmlParser();
74  
75          Serializer rdfSerializer = 
76              new WSMLTripleSerializer(new HashMap<Object, Object>());
77  
78          for (Iterator<?> it = sel.iterator(); it.hasNext();) {
79              TopEntity[] topEntities = null;
80              IFile sourceFile = (IFile)it.next();
81              try {
82                  topEntities = wsmlParser.parse(new FileReader(sourceFile.getRawLocation().toFile()));
83              }
84              catch (Exception e) {
85                  MessageDialog.openError(getShell(), 
86                          "Parse Error", 
87                          "Unable to parse file '"
88                          + sourceFile.getName()
89                          + "':\n"
90                          + e.getMessage());
91                  
92                  LogManager.logError("Unable to parse file '"
93                          + sourceFile.getName()
94                          + "':\n",
95                          e);
96                  continue;
97              }
98              if (topEntities == null) {
99                  MessageDialog.openError(getShell(), 
100                         "No Data Found", 
101                         "Invalid or missing data in file '"
102                         + sourceFile.getName()
103                         + "'!");
104                 continue;
105             }
106             File file = createOutputFile(outFolder, sourceFile.getName());
107             try {
108                 FileOutputStream fileOut = new FileOutputStream(file, false);
109                 
110                 StringBuffer str = new StringBuffer();
111                 rdfSerializer.serialize( topEntities, str);
112                 
113                 fileOut.write(str.toString().getBytes());
114                 fileOut.close();
115             }
116             catch(Throwable ioEx) {
117                 MessageDialog.openError(getShell(), 
118                         "Serializer Error", 
119                         ioEx.getMessage());
120                 LogManager.logError(ioEx);
121             }
122         
123             
124         }
125         return true;
126     }
127     
128     private File createOutputFile(File outFolder, String srcFile) {
129         String newName;
130         if (srcFile.toLowerCase().endsWith(".wsml")) {
131             newName = srcFile.substring(0, srcFile.length()-4) + "rdf";
132         }
133         else {
134             newName = srcFile + ".rdf";
135         }
136         return new File(outFolder, newName);
137     }
138 }
139