View Javadoc

1   package org.dbe.studio.tools.sdlCompiler2Java.plugin.action;
2   
3   import java.io.*;
4   
5   import org.eclipse.jface.action.IAction;
6   import org.eclipse.jface.dialogs.MessageDialog;
7   import org.eclipse.jface.viewers.ISelection;
8   import org.eclipse.jface.viewers.StructuredSelection;
9   import org.eclipse.swt.widgets.DirectoryDialog;
10  import org.eclipse.swt.widgets.Shell;
11  import org.eclipse.ui.IObjectActionDelegate;
12  import org.eclipse.ui.IWorkbenchPart;
13  
14  import org.eclipse.core.resources.IFile;
15  import org.eclipse.core.resources.IProject;
16  import org.eclipse.core.resources.IWorkspaceRoot;
17  import org.eclipse.core.resources.ResourcesPlugin;
18  import org.eclipse.core.runtime.IPath;
19  
20  import org.dbe.studio.tools.sdlCompiler2Java.*;
21  import org.dbe.studio.tools.sdlCompiler2Java.plugin.SdlCompiler2JavaPlugin;
22  
23  public class SdlCompiler2JavaAction implements IObjectActionDelegate
24  {
25  
26      org.eclipse.core.internal.resources.File selectedFile;
27  
28      private String fileLocation;
29      private IProject pr;
30      private String prName;
31  
32      /***
33       * Constructor for Action1.
34       */
35      public SdlCompiler2JavaAction()
36      {
37          super();
38      }
39  
40      /***
41       * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
42       */
43      public void setActivePart(IAction action, IWorkbenchPart targetPart)
44      {
45      }
46  
47      /***
48       * 
49       * @param String
50       *            java code
51       * @return String java class name
52       */
53      private String getClassName(String java)
54      {
55          // get the class name
56          String className = "noName";
57          int plus = 0;
58          int i = java.toLowerCase().indexOf("interface ");
59          if (i == -1)
60          {
61              i = java.toLowerCase().indexOf("class ");
62              if (i != -1)
63                  plus = "class ".length();
64          } else
65          {
66              plus = "interface ".length();
67          }
68  
69          if (i != -1)
70          {
71              // found class/interface
72  
73              int q = i + plus;
74              for (q = i + plus; q < java.length(); q++)
75              {
76                  char c = java.charAt(q);
77  
78                  if (!Character.isWhitespace(c))
79                      break;
80              }
81  
82              int w;
83              for (w = q + 1; w < java.length(); w++)
84              {
85                  char c = java.charAt(w);
86  
87                  if (Character.isWhitespace(c))
88                      break;
89              }
90  
91              className = java.substring(q, w).trim();
92          }
93  
94          return className;
95      }
96  
97      /***
98       * 
99       * @param java
100      * @return package name
101      */
102     
103     private String getPackageName(String java)
104     {
105         // get the package name
106         String packageName = null;
107         int plus = 0;
108         int i = java.toLowerCase().indexOf("package ");
109             plus = "package ".length();
110 
111         if (i != -1)
112         {
113             // found package
114 
115             int q = i + plus;
116             for (q = i + plus; q < java.length(); q++)
117             {
118                 char c = java.charAt(q);
119 
120                 if (!Character.isWhitespace(c))
121                     break;
122             }
123 
124             int w;
125             for (w = q + 1; w < java.length(); w++)
126             {
127                 char c = java.charAt(w);
128 
129                 if (Character.isWhitespace(c))
130                     break;
131             }
132 
133             packageName = java.substring(q, w).replaceAll(";","").trim();
134         }
135 
136         return</strong> packageName;
137     }
138     
139     
140     /***
141      * @see IActionDelegate#run(IAction)
142      */
143     public void run(IAction action)
144     {
145     	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
146     	prName = pr.getName();
147         IProject project = root.getProject(prName);
148         String basePath = project.getLocation().toFile().getAbsolutePath();
149         //System.out.println(basePath);
150         
151         DirectoryDialog dd = new DirectoryDialog(new Shell());
152         dd.setFilterPath(basePath);
153         String saveDir = dd.open();
154         if (saveDir == null)
155         {
156         	return;
157         }
158 
159         try
160         {
161 
162             // the sdl2java compiler
163             Sdl2Java sdl2java = new Sdl2Java();
164 
165             // transform templates
166             sdl2java.setSdl2wsdlTransform(SdlCompiler2JavaPlugin
167                     .getXSLContent("sdl2wsdl.xsl"));
168             sdl2java.setWsdl2mjavaTransform(SdlCompiler2JavaPlugin
169                     .getXSLContent("wsdl2mJava.xsl"));
170             sdl2java.setMjava2javaTransform(SdlCompiler2JavaPlugin
171                     .getXSLContent("mJava2Java.xsl"));
172 
173             // transformations
174             String wsdl = sdl2java.sdl2wsdl(Sdl2Java.loadFile(new java.io.File(
175                     fileLocation)));
176             String mJava = sdl2java.wsdl2mjava(wsdl);
177 
178             //System.out.println(mJava);
179             
180             String javaFile = sdl2java.mjava2java(mJava);
181             String properties = null;
182             
183             // split the generated mJava file into classes
184             int classStart = 0;
185             int lastStart = 0;
186             String TAG_CLASS = "<class>";
187             String TAG_END_CLASS = "</class>";
188             do
189             {
190                 classStart = mJava.indexOf(TAG_CLASS, lastStart);
191                 if (classStart == -1)
192                     break;
193 
194                 int classEnd = mJava.indexOf(TAG_END_CLASS, classStart
195                         + TAG_CLASS.length());
196                 if (classEnd == -1)
197                     classEnd = mJava.length();
198 
199                 // the main transformation
200                 String javaString = sdl2java.mjava2java(mJava.substring(classStart,
201                         classEnd + TAG_END_CLASS.length()));
202 
203                 if(javaString.indexOf("interface ") > -1)
204                 	properties = Sdl2JavaProps.getPropFile(javaString);
205                 
206                 //edit or create the deployment.props file             
207                 IFile deploymentFile = project.getFile("deployment.props");
208                 
209                 // if the file allready exist in the project just add the props for generated service
210                 if(deploymentFile.exists()){
211                 	
212                    IPath depPath = deploymentFile.getLocation();
213                    String deploymentFileContext = Sdl2Java.loadFile(new java.io.File(depPath.toString()));
214                    
215                    //if the file is empty just add the props to it
216                    if(deploymentFileContext.equals("")){
217                 	   
218                 	   deploymentFileContext = properties;
219                 	   Sdl2Java.saveFile(new java.io.File(pr.getLocation().toString(),"deployment.props"),deploymentFileContext);
220                 	   
221                    //the file is not empty   
222                    }else{
223                 	   
224                 	   
225                 	   //if the file is not empty first we verify if the props are allready in the file
226                 	   if(deploymentFileContext.indexOf(properties) > -1){
227                 		   
228                 		  //do nothing, the props are allready in the deployment.props file
229                 		  
230                 	   }else{
231 
232                 		   deploymentFileContext = deploymentFileContext + "\n" + properties;
233                 		   Sdl2Java.saveFile(new java.io.File(pr.getLocation().toString(),"deployment.props"),deploymentFileContext);
234 
235                 	   }
236                    }
237                 
238                 // if the file does not exist in the project creat it with the necessary props
239                 }else{
240                 	
241                 	Sdl2Java.saveFile(new java.io.File(pr.getLocation().toString(),"deployment.props"),properties);
242                 	
243                 }
244                 
245                 //create folders 
246                 String packageName = getPackageName(javaString);
247                 packageName = packageName.replace('.','/');
248                 packageName = saveDir + "/" + packageName;
249                 java.io.File files = new java.io.File(packageName);
250                 files.mkdirs();
251 
252                 // saving the files
253                 Sdl2Java.saveFile(ong>new java.io.File(packageName, getClassName(javaString)
254                         + ".java"), javaString);
255 
256                 lastStart = classEnd + TAG_END_CLASS.length();
257 
258             } while (classStart != -1);
259             
260             // show a message to the user when the files were successefully generated
261             MessageDialog.openInformation(null,
262                     Sdl2JavaMessages.MSG_SUCC_SAVE_TITLE,
263                     Sdl2JavaMessages.MSG_SUCC_SAVE_Java);
264 
265             pr.refreshLocal(2, null);
266             
267         } catch (Exception e)
268         {
269             System.err.println(e.getMessage());
270             MessageDialog.openError(null, this.getClass().getName(), this.getClass().getName()+ " " + e.getStackTrace().toString()+ " " + e.getMessage());
271             e.printStackTrace(System.err);
272         }
273     }
274 
275     /***
276      * @see IActionDelegate#selectionChanged(IAction, ISelection)
277      */
278     public void selectionChanged(IAction action, ISelection selection)
279     {
280 
281         selectedFile = (org.eclipse.core.internal.resources.File) ((StructuredSelection) selection)
282                 .getFirstElement();
283         fileLocation = selectedFile.getLocation().toString();
284         pr = selectedFile.getProject();
285         return;
286         
287     }   
288 
289 }