update dev300-m58
[ooovba.git] / scripting / examples / java / Newsgroup / MimeConfiguration.java
blob32bb09036944ac79d30eca097c9cc495d3346b33
1 import com.sun.star.uno.UnoRuntime;
2 import com.sun.star.lang.XMultiComponentFactory;
3 import com.sun.star.uno.XComponentContext;
4 import com.sun.star.script.framework.runtime.XScriptContext;
5 import com.sun.star.util.XStringSubstitution;
7 import javax.mail.*;
8 import javax.activation.*;
10 import java.io.*;
13 public class MimeConfiguration
16 // Office Installation path
17 private static String instPath = "";
20 public static boolean createFiles( XScriptContext xsc )
22 try
24 XComponentContext xcc = xsc.getComponentContext();
25 XMultiComponentFactory xmf = xcc.getServiceManager();
27 Object pathSub = xmf.createInstanceWithContext( "com.sun.star.comp.framework.PathSubstitution", xcc );
28 XStringSubstitution stringSub = ( XStringSubstitution ) UnoRuntime.queryInterface( XStringSubstitution.class, pathSub );
29 instPath = stringSub.getSubstituteVariableValue( "$(inst)" );
32 catch( com.sun.star.beans.UnknownPropertyException upe )
34 System.out.println( "com.sun.star.beans.UnknownPropertyException" );
35 upe.printStackTrace();
37 catch( com.sun.star.uno.Exception e )
39 System.out.println( "com.sun.star.uno.Exception" );
40 e.printStackTrace();
43 writeMailCap();
44 writeMimeTypes();
46 // ToDo: include status feedback to StatusWindow
47 return true;
53 private static void writeMailCap()
55 String mailcapPath = getConfigDir() + System.getProperty( "file.separator" ) + "mailcap";
57 try
59 if( ! new File( java.net.URLDecoder.decode( mailcapPath ) ).exists() )
61 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) );
62 File mailcapFile = new File( mailcapPath );
63 FileWriter out = new FileWriter( mailcapFile );
64 String[] lines = getMailcapText();
65 for( int i=0; i<lines.length; i++ )
67 out.write( lines[i], 0, lines[i].length() );
69 out.close();
71 else
73 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) );
78 // use prog dir, if not there then java.io to create/write new file
79 MailcapCommandMap map = new MailcapCommandMap( mailcapPath );
80 CommandMap.setDefaultCommandMap ( map );
82 catch( IOException ioe )
84 ioe.printStackTrace();
86 catch( Exception e )
88 e.printStackTrace();
93 private static String[] getMailcapText()
95 String[] mailcapText = {
96 "#\n",
97 "# Default mailcap file for the JavaMail System.\n",
98 "#\n",
99 "# JavaMail content-handlers:\n",
100 "#\n",
101 "text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain\n",
102 "text/html;; x-java-content-handler=com.sun.mail.handlers.text_html\n",
103 "text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml\n",
104 "image/gif;; x-java-content-handler=com.sun.mail.handlers.image_gif\n",
105 "image/jpeg;; x-java-content-handler=com.sun.mail.handlers.image_jpeg\n",
106 "multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed\n",
107 "message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822\n"
110 return mailcapText;
115 private static void writeMimeTypes()
117 String mimetypesPath = getConfigDir() + System.getProperty( "file.separator" ) + "mimetypes.default";
121 if( ! new File( java.net.URLDecoder.decode( mimetypesPath ) ).exists() )
123 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) );
124 File mimetypesFile = new File( mimetypesPath );
125 FileWriter out = new FileWriter( mimetypesFile );
126 String[] lines = getMimeTypesText();
127 for( int i=0; i<lines.length; i++ )
129 out.write( lines[i], 0, lines[i].length() );
131 out.close();
133 else
135 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) );
138 MimetypesFileTypeMap mimeTypes = new MimetypesFileTypeMap( mimetypesPath );
139 FileTypeMap.setDefaultFileTypeMap( mimeTypes );
141 catch( IOException ioe )
143 ioe.printStackTrace();
145 catch( Exception e )
147 e.printStackTrace();
152 private static String[] getMimeTypesText()
154 String[] mimesText = {
155 "#\n",
156 "# A simple, old format, mime.types file\n",
157 "#\n",
158 "text/html html htm HTML HTM\n",
159 "text/plain txt text TXT TEXT\n",
160 "image/gif gif GIF\n",
161 "image/ief ief\n",
162 "image/jpeg jpeg jpg jpe JPG\n",
163 "image/tiff tiff tif\n",
164 "image/x-xwindowdump xwd\n",
165 "application/postscript ai eps ps\n",
166 "application/rtf rtf\n",
167 "application/x-tex tex\n",
168 "application/x-texinfo texinfo texi\n",
169 "application/x-troff t tr roff\n",
170 "audio/basic au\n",
171 "audio/midi midi mid\n",
172 "audio/x-aifc aifc\n",
173 "audio/x-aiff aif aiff\n",
174 "audio/x-mpeg mpeg mpg\n",
175 "audio/x-wav wav\n",
176 "video/mpeg mpeg mpg mpe\n",
177 "video/quicktime qt mov\n",
178 "video/x-msvideo avi\n"
181 return mimesText;
185 private static String getConfigDir()
187 // mailcap file must be written to the Office user/config directory
189 // instPath is a URL, needs to be converted to a system pathname
190 String config = instPath + "/user/config";
191 String configNonURL = "";
193 if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
195 // Windows
196 // removes "file:///"
197 int start = 8;
198 configNonURL = config.substring( start, config.length() );
199 // Convert forward to back-slashes
200 while( configNonURL.indexOf( "/" ) != -1 )
202 int fSlash = configNonURL.indexOf( "/" );
203 String firstPart = configNonURL.substring( 0, fSlash );
204 String secondPart = configNonURL.substring( fSlash + 1, configNonURL.length() );
205 configNonURL = firstPart + "\\" + secondPart;
208 else
210 // Unix/Linux
211 // removes "file://"
212 int start = 7;
213 configNonURL = config.substring( start, config.length() );
216 return configNonURL;