Skip to content

Commit 757bb88

Browse files
committed
Provide a method to specify bundle documents when build app bundle (Haavar Valeur)
See: arduino/Arduino#1665 https://java.net/jira/browse/APPBUNDLER-1
1 parent c70c742 commit 757bb88

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.oracle.appbundler;
2+
3+
/**
4+
* Created by haavar on 1/8/14.
5+
*/
6+
public class AbstractKeyValue {
7+
private String value = null;
8+
9+
public String getValue() {
10+
return value;
11+
}
12+
13+
public void addText(String value) {
14+
this.value = value;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return value;
20+
}
21+
}

appbundler/src/com/oracle/appbundler/AppBundlerTask.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.nio.file.Path;
4242
import java.nio.file.StandardCopyOption;
4343
import java.util.ArrayList;
44+
import java.util.List;
4445
import java.util.zip.ZipEntry;
4546
import java.util.zip.ZipInputStream;
4647

@@ -79,6 +80,7 @@ public class AppBundlerTask extends Task {
7980
private ArrayList<FileSet> libraryPath = new ArrayList<>();
8081
private ArrayList<String> options = new ArrayList<>();
8182
private ArrayList<String> arguments = new ArrayList<>();
83+
private List<DocumentType> documentTypes = new ArrayList<>();
8284

8385
private static final String EXECUTABLE_NAME = "JavaAppLauncher";
8486
private static final String DEFAULT_ICON_NAME = "GenericApp.icns";
@@ -186,6 +188,10 @@ public void addConfiguredArgument(Argument argument) throws BuildException {
186188
arguments.add(value);
187189
}
188190

191+
public void addDocumentType(DocumentType documentType) {
192+
documentTypes.add(documentType);
193+
}
194+
189195
@Override
190196
public void execute() throws BuildException {
191197
// Validate required properties
@@ -243,6 +249,15 @@ public void execute() throws BuildException {
243249
throw new IllegalStateException("Main class name is required.");
244250
}
245251

252+
for (DocumentType documentType : documentTypes) {
253+
if(documentType.getName() == null) {
254+
throw new IllegalStateException("Name is required for document type.");
255+
}
256+
if (!documentType.getIcon().exists()) {
257+
throw new IllegalStateException("Document icon does not exist.");
258+
}
259+
}
260+
246261
// Create the app bundle
247262
try {
248263
System.out.println("Creating app bundle: " + name);
@@ -297,6 +312,12 @@ public void execute() throws BuildException {
297312

298313
// Copy icon to Resources folder
299314
copyIcon(resourcesDirectory);
315+
316+
for(DocumentType documentType : documentTypes) {
317+
if (documentType.getIcon() != null) {
318+
copy(documentType.getIcon(), new File(resourcesDirectory, documentType.getIcon().getName()));
319+
}
320+
}
300321
} catch (IOException exception) {
301322
throw new BuildException(exception);
302323
}
@@ -451,6 +472,25 @@ private void writeInfoPlist(File file) throws IOException {
451472
writeProperty(xout, "LSApplicationCategoryType", applicationCategory);
452473
}
453474

475+
if ( ! documentTypes.isEmpty()) {
476+
writeKey(xout, "CFBundleDocumentTypes");
477+
xout.writeStartElement(ARRAY_TAG);
478+
xout.writeCharacters("\n");
479+
for (DocumentType documentType : documentTypes) {
480+
writeProperty(xout, "CFBundleTypeName", documentType.getName());
481+
writeProperty(xout, "CFBundleTypeRole", documentType.getRole().name());
482+
if (documentType.getIcon() != null) {
483+
writeProperty(xout, "CFBundleTypeIconFile", documentType.getIcon().getName());
484+
}
485+
writeStringList(xout, "CFBundleTypeExtensions", documentType.getExtensions());
486+
writeStringList(xout, "CFBundleTypeMIMETypes", documentType.getMimeTypes());
487+
writeStringList(xout, "CFBundleTypeOSTypes", documentType.getOsTypes());
488+
489+
}
490+
xout.writeEndElement();
491+
xout.writeCharacters("\n");
492+
}
493+
454494
// Write runtime
455495
if (runtime != null) {
456496
writeProperty(xout, "JVMRuntime", runtime.getDir().getParentFile().getParentFile().getName());
@@ -524,6 +564,19 @@ private void writeProperty(XMLStreamWriter xout, String key, String value) throw
524564
writeString(xout, value);
525565
}
526566

567+
private void writeStringList(XMLStreamWriter xout, String key, List<? extends AbstractKeyValue> list) throws XMLStreamException {
568+
if ( ! list.isEmpty()) {
569+
writeKey(xout, key);
570+
xout.writeStartElement(ARRAY_TAG);
571+
xout.writeCharacters("\n");
572+
for (AbstractKeyValue extension : list) {
573+
writeString(xout, extension.getValue());
574+
}
575+
xout.writeEndElement();
576+
xout.writeCharacters("\n");
577+
}
578+
}
579+
527580
private void writePkgInfo(File file) throws IOException {
528581
Writer out = new BufferedWriter(new FileWriter(file));
529582

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.oracle.appbundler;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class DocumentType {
8+
public enum Role{Editor, Viewer, Shell, None}
9+
10+
private String name;
11+
private File icon;
12+
private Role role = Role.None;
13+
private List<DocumentExtension> extensions = new ArrayList<>();
14+
private List<MimeType> mimeTypes = new ArrayList<>();
15+
private List<OsType> osTypes = new ArrayList<>();
16+
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public void setIcon(File icon) {
23+
this.icon = icon;
24+
}
25+
26+
public void setRole(Role role) {
27+
this.role = role;
28+
}
29+
30+
public void addExtension(DocumentExtension extension) {
31+
extensions.add(extension);
32+
}
33+
34+
public void addMimeType(MimeType mimeType) {
35+
mimeTypes.add(mimeType);
36+
}
37+
38+
public void addOsType(OsType osType) {
39+
osTypes.add(osType);
40+
}
41+
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public File getIcon() {
48+
return icon;
49+
}
50+
51+
public Role getRole() {
52+
return role;
53+
}
54+
55+
public List<? extends AbstractKeyValue> getExtensions() {
56+
return extensions;
57+
}
58+
59+
public List<? extends AbstractKeyValue> getMimeTypes() {
60+
return mimeTypes;
61+
}
62+
63+
public List<? extends AbstractKeyValue> getOsTypes() {
64+
return osTypes;
65+
}
66+
67+
68+
public static class DocumentExtension extends AbstractKeyValue {
69+
}
70+
71+
public static class MimeType extends AbstractKeyValue {
72+
}
73+
74+
public static class OsType extends AbstractKeyValue {
75+
}
76+
77+
}

0 commit comments

Comments
 (0)