View Javadoc

1   package org.dbe.studio.tools.sdlCompiler2Java.plugin;
2   
3   import java.io.BufferedInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.net.URL;
8   
9   import org.dbe.studio.tools.sdlCompiler2Java.Sdl2JavaException;
10  import org.dbe.studio.tools.sdlCompiler2Java.Sdl2JavaMessages;
11  import org.eclipse.ui.plugin.*;
12  import org.eclipse.jface.dialogs.MessageDialog;
13  import org.eclipse.jface.resource.ImageDescriptor;
14  import org.osgi.framework.BundleContext;
15  
16  /***
17   * The main plugin class to be used in the desktop.
18   */
19  public class SdlCompiler2JavaPlugin extends AbstractUIPlugin {
20  
21  	//The shared instance.
22  	private static SdlCompiler2JavaPlugin plugin;
23  	
24  	/***
25  	 * The constructor.
26  	 */
27  	public SdlCompiler2JavaPlugin() {
28  		plugin = this;
29  	}
30  
31  	/***
32  	 * This method is called upon plug-in activation
33  	 */
34  	public void start(BundleContext context) throws Exception {
35  		super.start(context);
36  	}
37  
38  	/***
39  	 * This method is called when the plug-in is stopped
40  	 */
41  	public void stop(BundleContext context) throws Exception {
42  		super.stop(context);
43  		plugin = null;
44  	}
45  
46  	/***
47  	 * Returns the shared instance.
48  	 */
49  	public static SdlCompiler2JavaPlugin getDefault() {
50  		return plugin;
51  	}
52  
53  	/***
54  	 * Returns an image descriptor for the image file at the given
55  	 * plug-in relative path.
56  	 *
57  	 * @param path the path
58  	 * @return the image descriptor
59  	 */
60  	public static ImageDescriptor getImageDescriptor(String path) {
61  		return AbstractUIPlugin.imageDescriptorFromPlugin("org.dbe.studio.tools.sdlCompiler2Java.plugin.SdlCompiler2JavaPlugin", path);
62  	}
63  	
64  	/***
65  	 * 
66  	 * @param sFileName
67  	 * @return the URL where the file is located
68  	 */
69  	/*
70  	public static URL getXSLUrl(String sFileName)
71  	{
72  		try {
73  			return getDefault().getBundle().getEntry("xslt/" + sFileName);
74  		} 
75  		catch (Exception e) {
76  			return null;
77  		}
78  	}
79  	
80  	*/
81  	
82  	/***
83  	 * 
84  	 * @param fileName
85  	 * @return the context of a file as String
86  	 * @throws Exception
87  	 */
88  	/*
89  	public static String getXSLContent (String fileName)
90  	throws Sdl2JavaException
91  	{
92  		URL xslUrl = getXSLUrl(fileName);
93          
94          if(xslUrl == null)
95          {
96              MessageDialog.openError(null, Sdl2JavaMessages.MSG_SUCC_SAVE_TITLE, Sdl2JavaMessages.ERR_MSG_ERR_TO_USER);
97              throw new Sdl2JavaException(Sdl2JavaMessages.ERR_MSG_NULL_TRANSF + ": " + fileName);
98          }
99          
100 		BufferedInputStream bis = null;
101 		try {
102 			bis = new BufferedInputStream(xslUrl.openStream());
103 		} catch (IOException e) {
104 			e.printStackTrace();
105 		}
106 		
107 		ByteArrayOutputStream baos = new ByteArrayOutputStream((int)fileName.length());
108 		
109 		byte[] b = new byte[4096];
110 		int nb;
111 		try {
112 			while ( (nb = bis.read(b)) != -1 ) {
113 				baos.write(b, 0, nb);
114 			}
115 		} catch (IOException e) {
116 			e.printStackTrace();
117 		}
118 		return baos.toString();
119 	}
120 	*/
121 	
122 	public static String getXSLContent (String fileName) throws Exception {
123 		
124 		 ClassLoader loader = getDefault().getClass().getClassLoader();
125          InputStream is = loader.getResourceAsStream(fileName);
126          
127  		ByteArrayOutputStream baos = new ByteArrayOutputStream(10000);
128  		byte[] b = new byte[4096];
129 		int nb;
130 		while ( (nb = is.read(b)) != -1 ) {
131 			baos.write(b, 0, nb);
132 		}
133 
134 		return baos.toString();
135 	}
136 	
137 	/*
138 	public static void test()
139 	{
140 		try {
141 			System.out.println(getXSLContent("sdl2wsdl.xsl"));
142 		} catch (Exception e) {
143 			// TODO Auto-generated catch block
144 			e.printStackTrace();
145 		}
146 	}
147 	*/
148 }