Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 4931089

Browse files
author
Emmanuel Hugonnet
committed
Bug #4346 : Gessing Ms Office version with editon tool, using the registry.
1 parent 128fd59 commit 4931089

File tree

5 files changed

+153
-25
lines changed

5 files changed

+153
-25
lines changed

src/main/java/org/silverpeas/openoffice/OfficeLauncher.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ public class OfficeLauncher {
5959
public static int launch(MsOfficeType type, String url, AuthenticationInfo authInfo,
6060
boolean useDeconnectedMode) throws IOException, InterruptedException, OfficeNotFoundException {
6161
OfficeFinder finder = FinderFactory.getFinder(type);
62-
logger.log(Level.INFO, "Are we using Office 2007 : {0}", finder.isMicrosoftOffice());
62+
logger.log(Level.INFO, "Are we using Office : {0}", finder.isMicrosoftOffice());
6363
logger.log(Level.INFO, "We are on {0} OS", OsEnum.getOS());
6464
boolean modeDisconnected = ((OsEnum.isWindows() && useDeconnectedMode) || OsEnum.getOS()
6565
== OsEnum.MAC_OSX) && finder.isMicrosoftOffice();
6666
switch (type) {
6767
case EXCEL:
68-
return launch(finder.findSpreadsheet(), url, modeDisconnected, authInfo);
68+
return launch(type, finder.findSpreadsheet(), url, modeDisconnected, authInfo);
6969
case POWERPOINT:
70-
return launch(finder.findPresentation(), url, modeDisconnected, authInfo);
70+
return launch(type, finder.findPresentation(), url, modeDisconnected, authInfo);
7171
case WORD:
72-
return launch(finder.findWordEditor(), url, modeDisconnected, authInfo);
72+
return launch(type, finder.findWordEditor(), url, modeDisconnected, authInfo);
7373
case NONE:
7474
default:
75-
return launch(finder.findOther(), url, modeDisconnected, authInfo);
75+
return launch(type, finder.findOther(), url, modeDisconnected, authInfo);
7676
}
7777
}
7878

@@ -87,7 +87,7 @@ public static int launch(MsOfficeType type, String url, AuthenticationInfo authI
8787
* @throws IOException
8888
* @throws InterruptedException
8989
*/
90-
public static int launch(String path, String url, boolean modeDisconnected,
90+
protected static int launch(MsOfficeType type, String path, String url, boolean modeDisconnected,
9191
AuthenticationInfo auth) throws IOException, InterruptedException {
9292
logger.log(Level.INFO, "The path: {0}", path);
9393
logger.log(Level.INFO, "The url: {0}", url);
@@ -116,7 +116,7 @@ public static int launch(String path, String url, boolean modeDisconnected,
116116
} else {
117117
// Standard mode : just open it
118118
String webdavUrl = url;
119-
if(OsEnum.isWindows() && MsOfficeVersion.isOldOffice()) {
119+
if(OsEnum.isWindows() && MsOfficeVersion.isOldOffice(type)) {
120120
webdavUrl = webdavUrl.replace("/repository/", "/repository2000/");
121121
}
122122
logger.log(Level.INFO, "The exact exec line: {0} {2}", new Object[]{path, webdavUrl});

src/main/java/org/silverpeas/openoffice/util/MsOfficeType.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
*/
3434
public enum MsOfficeType {
3535

36-
NONE(""), EXCEL("application/vnd.ms-excel"), WORD("application/vnd.ms-word"), POWERPOINT(
37-
"application/vnd.ms-powerpoint");
36+
NONE("", RegistryApplicationKey.NONE),
37+
EXCEL("application/vnd.ms-excel", RegistryApplicationKey.EXCEL),
38+
WORD("application/vnd.ms-word", RegistryApplicationKey.WORD),
39+
POWERPOINT("application/vnd.ms-powerpoint", RegistryApplicationKey.POWERPOINT);
3840
private String contentType;
39-
40-
private MsOfficeType(String contentType) {
41+
private RegistryApplicationKey registryKey;
42+
private MsOfficeType(String contentType, RegistryApplicationKey registryKey) {
4143
this.contentType = contentType;
44+
this.registryKey = registryKey;
4245
}
4346

4447
public static MsOfficeType valueOfMimeType(String mimeType) {
@@ -53,6 +56,15 @@ public static MsOfficeType valueOfMimeType(String mimeType) {
5356
}
5457
return NONE;
5558
}
59+
60+
public RegistryApplicationKey getRegistryApplicationKey() {
61+
return this.registryKey;
62+
}
63+
64+
65+
public String getApplicationKey() {
66+
return this.registryKey.getApplicationKey();
67+
}
5668

5769
public boolean isMsOfficeCompatible() {
5870
return this != NONE;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2000 - 2013 Silverpeas
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* As a special exception to the terms and conditions of version 3.0 of
10+
* the GPL, you may redistribute this Program in connection withWriter Free/Libre
11+
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12+
* FLOSS exception. You should have recieved a copy of the text describing
13+
* the FLOSS exception, and it is also available here:
14+
* "http://www.silverpeas.org/legal/licensing"
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
package org.silverpeas.openoffice.util;
25+
26+
/**
27+
*
28+
* @author ehugonnet
29+
*/
30+
public enum RegistryApplicationKey {
31+
32+
EXCEL("Excel.Application"), OUTLOOK("Outlook.Application"), WORD("Word.Application"),
33+
POWERPOINT("Powerpoint.Application"), FRONTPAGE("FrontPage.Application"), ACCESS(
34+
"Access.Application"), NONE("");
35+
36+
private final String applicationKey;
37+
38+
private RegistryApplicationKey(String applicationKey) {
39+
this.applicationKey = applicationKey;
40+
}
41+
42+
public String getApplicationKey() {
43+
return this.applicationKey;
44+
}
45+
}

src/main/java/org/silverpeas/openoffice/windows/MsOfficeRegistryHelper.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,30 @@
2727
import java.util.regex.Matcher;
2828
import java.util.regex.Pattern;
2929

30+
import org.silverpeas.openoffice.util.RegistryApplicationKey;
31+
3032
/**
3133
* @author Emmanuel Hugonnet
3234
*/
3335
public class MsOfficeRegistryHelper implements OfficeFinder {
3436

3537
static final Logger logger = Logger.getLogger(MsOfficeRegistryHelper.class.getName());
3638
static final OfficeFinder msOfficeFinder = new MsOfficePathFinder();
37-
static final String ACCESS = "Access.Application";
38-
static final String EXCEL = "Excel.Application";
39-
static final String OUTLOOK = "Outlook.Application";
40-
static final String POWERPOINT = "Powerpoint.Application";
41-
static final String WORD = "Word.Application";
42-
static final String FRONTPAGE = "FrontPage.Application";
4339
static final String BASE_KEY_OFFICE = "\"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Office\\";
44-
static final String BASE_APPLICATION_KEY = "\"HKEY_LOCAL_MACHINE\\Software\\Classes\\";
40+
public static final String BASE_APPLICATION_KEY = "\"HKEY_LOCAL_MACHINE\\Software\\Classes\\";
4541
static final String BASE_KEY_64_CLSID =
4642
"\"HKEY_LOCAL_MACHINE\\Software\\Wow6432Node\\Classes\\CLSID\\";
4743
static final String BASE_KEY_CLSID = "\"HKEY_LOCAL_MACHINE\\Software\\Classes\\CLSID\\";
4844
static final Pattern AUTOMATION = Pattern.compile(
4945
"\\s*/[aA][uU][tT][oO][mM][aA][tT][iI][oO][nN]\\s*");
5046

51-
protected String getClsid(String type) {
52-
return RegistryKeyReader.readKey(BASE_APPLICATION_KEY + type + "\\CLSID\"");
47+
protected String getClsid(RegistryApplicationKey type) {
48+
return RegistryKeyReader.readKey(BASE_APPLICATION_KEY + type.getApplicationKey() + "\\CLSID\"");
5349
}
5450

55-
protected String getPath(String type) {
51+
52+
53+
protected String getPath(RegistryApplicationKey type) {
5654
String clsid = getClsid(type);
5755
if (clsid != null) {
5856
String path = RegistryKeyReader.readKey(BASE_KEY_CLSID + clsid + "\\LocalServer32\"");
@@ -81,7 +79,7 @@ protected String extractPath(String path) {
8179

8280
@Override
8381
public String findSpreadsheet() throws OfficeNotFoundException {
84-
String msPath = getPath(EXCEL);
82+
String msPath = getPath(RegistryApplicationKey.EXCEL);
8583
if (msPath == null) {
8684
msPath = msOfficeFinder.findSpreadsheet();
8785
}
@@ -90,7 +88,7 @@ public String findSpreadsheet() throws OfficeNotFoundException {
9088

9189
@Override
9290
public String findPresentation() throws OfficeNotFoundException {
93-
String msPath = getPath(POWERPOINT);
91+
String msPath = getPath(RegistryApplicationKey.POWERPOINT);
9492
if (msPath == null) {
9593
msPath = msOfficeFinder.findPresentation();
9694
}
@@ -99,7 +97,7 @@ public String findPresentation() throws OfficeNotFoundException {
9997

10098
@Override
10199
public String findWordEditor() throws OfficeNotFoundException {
102-
String msPath = getPath(WORD);
100+
String msPath = getPath(RegistryApplicationKey.WORD);
103101
if (msPath == null) {
104102
msPath = msOfficeFinder.findWordEditor();
105103
} else {
@@ -115,6 +113,7 @@ public String findOther() throws OfficeNotFoundException {
115113

116114
@Override
117115
public boolean isMicrosoftOffice() {
118-
return getPath(EXCEL) != null || getPath(POWERPOINT) != null || getPath(WORD) != null;
116+
return getPath(RegistryApplicationKey.EXCEL) != null || getPath(
117+
RegistryApplicationKey.POWERPOINT) != null || getPath(RegistryApplicationKey.WORD) != null;
119118
}
120119
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2000 - 2013 Silverpeas
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* As a special exception to the terms and conditions of version 3.0 of
10+
* the GPL, you may redistribute this Program in connection withWriter Free/Libre
11+
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12+
* FLOSS exception. You should have recieved a copy of the text describing
13+
* the FLOSS exception, and it is also available here:
14+
* "http://www.silverpeas.org/legal/licensing"
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
package org.silverpeas.openoffice.windows;
25+
26+
import org.silverpeas.openoffice.util.MsOfficeType;
27+
import org.silverpeas.openoffice.util.RegistryApplicationKey;
28+
import org.silverpeas.openoffice.util.RegistryKeyReader;
29+
30+
/**
31+
*
32+
* @author ehugonnet
33+
*/
34+
public enum MsOfficeVersion {
35+
36+
Office95(7), Office97(8), Office2000(9), OfficeXP(10), Office2003(11),
37+
Office2007(12), Office2010(14);
38+
private final int numeralVersion;
39+
40+
private MsOfficeVersion(int numeralVersion) {
41+
this.numeralVersion = numeralVersion;
42+
}
43+
44+
public static MsOfficeVersion fromNumeralVersion(int version) {
45+
for (MsOfficeVersion officeVersion : MsOfficeVersion.values()) {
46+
if (version == officeVersion.numeralVersion) {
47+
return officeVersion;
48+
}
49+
}
50+
return Office2000;
51+
}
52+
53+
public static MsOfficeVersion detectMsOffice(MsOfficeType type) {
54+
return getOfficeVersion(type.getRegistryApplicationKey());
55+
}
56+
57+
protected static MsOfficeVersion getOfficeVersion(RegistryApplicationKey type) {
58+
String key = RegistryKeyReader.readKey(MsOfficeRegistryHelper.BASE_APPLICATION_KEY
59+
+ type.getApplicationKey() + "\\CurVer\"");
60+
if (key != null) {
61+
int version = Integer.parseInt(key.substring(type.getApplicationKey().length() + 1));
62+
return MsOfficeVersion.fromNumeralVersion(version);
63+
}
64+
return MsOfficeVersion.Office2000;
65+
}
66+
67+
public static boolean isOldOffice(MsOfficeType type) {
68+
MsOfficeVersion currentVersion = detectMsOffice(type);
69+
return OfficeXP == currentVersion || Office2000 == currentVersion || Office95 == currentVersion
70+
|| Office97 == currentVersion;
71+
}
72+
}

0 commit comments

Comments
 (0)