2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package org
.libreoffice
.example
.java_scripts
;
21 import com
.sun
.star
.uno
.UnoRuntime
;
22 import com
.sun
.star
.lang
.XMultiComponentFactory
;
23 import com
.sun
.star
.uno
.XComponentContext
;
24 import com
.sun
.star
.script
.framework
.runtime
.XScriptContext
;
25 import com
.sun
.star
.util
.XStringSubstitution
;
28 import javax
.activation
.*;
33 public class MimeConfiguration
36 // Office Installation path
37 private static String instPath
= "";
40 public static boolean createFiles( XScriptContext xsc
)
44 XComponentContext xcc
= xsc
.getComponentContext();
45 XMultiComponentFactory xmf
= xcc
.getServiceManager();
47 Object pathSub
= xmf
.createInstanceWithContext( "com.sun.star.comp.framework.PathSubstitution", xcc
);
48 XStringSubstitution stringSub
= ( XStringSubstitution
) UnoRuntime
.queryInterface( XStringSubstitution
.class, pathSub
);
49 instPath
= stringSub
.getSubstituteVariableValue( "$(inst)" );
52 catch( com
.sun
.star
.beans
.UnknownPropertyException upe
)
54 System
.out
.println( "com.sun.star.beans.UnknownPropertyException" );
55 upe
.printStackTrace();
57 catch( com
.sun
.star
.uno
.Exception e
)
59 System
.out
.println( "com.sun.star.uno.Exception" );
66 // ToDo: include status feedback to StatusWindow
73 private static void writeMailCap()
75 String mailcapPath
= getConfigDir() + System
.getProperty( "file.separator" ) + "mailcap";
79 if( ! new File( java
.net
.URLDecoder
.decode( mailcapPath
) ).exists() )
81 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) );
82 File mailcapFile
= new File( mailcapPath
);
83 FileWriter out
= new FileWriter( mailcapFile
);
84 String
[] lines
= getMailcapText();
85 for( int i
=0; i
<lines
.length
; i
++ )
87 out
.write( lines
[i
], 0, lines
[i
].length() );
93 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) );
98 // use prog dir, if not there then java.io to create/write new file
99 MailcapCommandMap map
= new MailcapCommandMap( mailcapPath
);
100 CommandMap
.setDefaultCommandMap ( map
);
102 catch( IOException ioe
)
104 ioe
.printStackTrace();
113 private static String
[] getMailcapText()
115 String
[] mailcapText
= {
117 "# Default mailcap file for the JavaMail System.\n",
119 "# JavaMail content-handlers:\n",
121 "text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain\n",
122 "text/html;; x-java-content-handler=com.sun.mail.handlers.text_html\n",
123 "text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml\n",
124 "image/gif;; x-java-content-handler=com.sun.mail.handlers.image_gif\n",
125 "image/jpeg;; x-java-content-handler=com.sun.mail.handlers.image_jpeg\n",
126 "multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed\n",
127 "message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822\n"
135 private static void writeMimeTypes()
137 String mimetypesPath
= getConfigDir() + System
.getProperty( "file.separator" ) + "mimetypes.default";
141 if( ! new File( java
.net
.URLDecoder
.decode( mimetypesPath
) ).exists() )
143 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) );
144 File mimetypesFile
= new File( mimetypesPath
);
145 FileWriter out
= new FileWriter( mimetypesFile
);
146 String
[] lines
= getMimeTypesText();
147 for( int i
=0; i
<lines
.length
; i
++ )
149 out
.write( lines
[i
], 0, lines
[i
].length() );
155 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) );
158 MimetypesFileTypeMap mimeTypes
= new MimetypesFileTypeMap( mimetypesPath
);
159 FileTypeMap
.setDefaultFileTypeMap( mimeTypes
);
161 catch( IOException ioe
)
163 ioe
.printStackTrace();
172 private static String
[] getMimeTypesText()
174 String
[] mimesText
= {
176 "# A simple, old format, mime.types file\n",
178 "text/html html htm HTML HTM\n",
179 "text/plain txt text TXT TEXT\n",
180 "image/gif gif GIF\n",
182 "image/jpeg jpeg jpg jpe JPG\n",
183 "image/tiff tiff tif\n",
184 "image/x-xwindowdump xwd\n",
185 "application/postscript ai eps ps\n",
186 "application/rtf rtf\n",
187 "application/x-tex tex\n",
188 "application/x-texinfo texinfo texi\n",
189 "application/x-troff t tr roff\n",
191 "audio/midi midi mid\n",
192 "audio/x-aifc aifc\n",
193 "audio/x-aiff aif aiff\n",
194 "audio/x-mpeg mpeg mpg\n",
196 "video/mpeg mpeg mpg mpe\n",
197 "video/quicktime qt mov\n",
198 "video/x-msvideo avi\n"
205 private static String
getConfigDir()
207 // mailcap file must be written to the Office user/config directory
209 // instPath is a URL, needs to be converted to a system pathname
210 String config
= instPath
+ "/user/config";
211 String configNonURL
= "";
213 if( System
.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
216 // removes "file:///"
218 configNonURL
= config
.substring( start
, config
.length() );
219 // Convert forward to back-slashes
220 while( configNonURL
.indexOf( "/" ) != -1 )
222 int fSlash
= configNonURL
.indexOf( "/" );
223 String firstPart
= configNonURL
.substring( 0, fSlash
);
224 String secondPart
= configNonURL
.substring( fSlash
+ 1, configNonURL
.length() );
225 configNonURL
= firstPart
+ "\\" + secondPart
;
233 configNonURL
= config
.substring( start
, config
.length() );