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
{
35 // Office Installation path
36 private static String instPath
= "";
39 public static boolean createFiles(XScriptContext xsc
) {
41 XComponentContext xcc
= xsc
.getComponentContext();
42 XMultiComponentFactory xmf
= xcc
.getServiceManager();
45 xmf
.createInstanceWithContext("com.sun.star.comp.framework.PathSubstitution",
47 XStringSubstitution stringSub
= (XStringSubstitution
) UnoRuntime
.queryInterface(
48 XStringSubstitution
.class, pathSub
);
49 instPath
= stringSub
.getSubstituteVariableValue("$(inst)");
51 } catch (com
.sun
.star
.beans
.UnknownPropertyException upe
) {
52 System
.out
.println("com.sun.star.beans.UnknownPropertyException");
53 upe
.printStackTrace();
54 } catch (com
.sun
.star
.uno
.Exception e
) {
55 System
.out
.println("com.sun.star.uno.Exception");
62 // ToDo: include status feedback to StatusWindow
69 private static void writeMailCap() {
70 String mailcapPath
= getConfigDir() + System
.getProperty("file.separator") +
74 if (! new File(java
.net
.URLDecoder
.decode(mailcapPath
)).exists()) {
75 File mailcapFile
= new File(mailcapPath
);
76 FileWriter out
= new FileWriter(mailcapFile
);
77 String
[] lines
= getMailcapText();
79 for (int i
= 0; i
< lines
.length
; i
++) {
80 out
.write(lines
[i
], 0, lines
[i
].length());
89 // use prog dir, if not there then java.io to create/write new file
90 MailcapCommandMap map
= new MailcapCommandMap(mailcapPath
);
91 CommandMap
.setDefaultCommandMap(map
);
92 } catch (IOException ioe
) {
93 ioe
.printStackTrace();
94 } catch (Exception e
) {
100 private static String
[] getMailcapText() {
101 String
[] mailcapText
= {
103 "# Default mailcap file for the JavaMail System.\n",
105 "# JavaMail content-handlers:\n",
107 "text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain\n",
108 "text/html;; x-java-content-handler=com.sun.mail.handlers.text_html\n",
109 "text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml\n",
110 "image/gif;; x-java-content-handler=com.sun.mail.handlers.image_gif\n",
111 "image/jpeg;; x-java-content-handler=com.sun.mail.handlers.image_jpeg\n",
112 "multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed\n",
113 "message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822\n"
121 private static void writeMimeTypes() {
122 String mimetypesPath
= getConfigDir() + System
.getProperty("file.separator") +
126 if (! new File(java
.net
.URLDecoder
.decode(mimetypesPath
)).exists()) {
127 File mimetypesFile
= new File(mimetypesPath
);
128 FileWriter out
= new FileWriter(mimetypesFile
);
129 String
[] lines
= getMimeTypesText();
131 for (int i
= 0; i
< lines
.length
; i
++) {
132 out
.write(lines
[i
], 0, lines
[i
].length());
139 MimetypesFileTypeMap mimeTypes
= new MimetypesFileTypeMap(mimetypesPath
);
140 FileTypeMap
.setDefaultFileTypeMap(mimeTypes
);
141 } catch (IOException ioe
) {
142 ioe
.printStackTrace();
143 } catch (Exception e
) {
149 private static String
[] getMimeTypesText() {
150 String
[] mimesText
= {
152 "# A simple, old format, mime.types file\n",
154 "text/html html htm HTML HTM\n",
155 "text/plain txt text TXT TEXT\n",
156 "image/gif gif GIF\n",
158 "image/jpeg jpeg jpg jpe JPG\n",
159 "image/tiff tiff tif\n",
160 "image/x-xwindowdump xwd\n",
161 "application/postscript ai eps ps\n",
162 "application/rtf rtf\n",
163 "application/x-tex tex\n",
164 "application/x-texinfo texinfo texi\n",
165 "application/x-troff t tr roff\n",
167 "audio/midi midi mid\n",
168 "audio/x-aifc aifc\n",
169 "audio/x-aiff aif aiff\n",
170 "audio/x-mpeg mpeg mpg\n",
172 "video/mpeg mpeg mpg mpe\n",
173 "video/quicktime qt mov\n",
174 "video/x-msvideo avi\n"
181 private static String
getConfigDir() {
182 // mailcap file must be written to the Office user/config directory
184 // instPath is a URL, needs to be converted to a system pathname
185 String config
= instPath
+ "/user/config";
186 String configNonURL
= "";
188 if (System
.getProperty("os.name").indexOf("Windows") != -1) {
190 // removes "file:///"
192 configNonURL
= config
.substring(start
, config
.length());
194 // Convert forward to back-slashes
195 while (configNonURL
.indexOf("/") != -1) {
196 int fSlash
= configNonURL
.indexOf("/");
197 String firstPart
= configNonURL
.substring(0, fSlash
);
198 String secondPart
= configNonURL
.substring(fSlash
+ 1, configNonURL
.length());
199 configNonURL
= firstPart
+ "\\" + secondPart
;
205 configNonURL
= config
.substring(start
, config
.length());