Skip to content

Commit

Permalink
[FIXUP] Further enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Aug 11, 2024
1 parent e95cd21 commit 739b14e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,66 +110,40 @@ public void setKey(String key, String value) {
}

private void addKey(String key, String value) throws DOMException, XPathExpressionException {
Element keyNode = document.createElement("key"); //$NON-NLS-1$
Text keyName = document.createTextNode(key);
keyNode.appendChild(keyName);

Element stringNode = document.createElement("string"); //$NON-NLS-1$
Text stringValue = document.createTextNode(value);
stringNode.appendChild(stringValue);
getNode(infoPList, "/plist/dict").appendChild(keyNode); //$NON-NLS-1$
getNode(infoPList, "/plist/dict").appendChild(stringNode); //$NON-NLS-1$
Node dict = getNode(infoPList, "/plist/dict"); //$NON-NLS-1$
appendTextContentChild(dict, "key", key); //$NON-NLS-1$
appendTextContentChild(dict, "string", value); //$NON-NLS-1$
}

public void addCfBundleUrlType(String scheme, String displayName) {
if (scheme == null)
if (scheme == null) {
throw new IllegalArgumentException("Scheme can't be null"); //$NON-NLS-1$
if (displayName == null)
}
if (displayName == null) {
throw new IllegalArgumentException("Display Name can't be null"); //$NON-NLS-1$

}
Node bundleUrlTypesNode = getOrCreateCfBundleUrlTypesArray();
Element dictNode = document.createElement("dict"); //$NON-NLS-1$
bundleUrlTypesNode.appendChild(dictNode);

Element dict = appendNewChild(bundleUrlTypesNode, "dict"); //$NON-NLS-1$
{
Element keyNode = document.createElement("key"); //$NON-NLS-1$
dictNode.appendChild(keyNode);
Text keyName = document.createTextNode("CFBundleURLName"); //$NON-NLS-1$
keyNode.appendChild(keyName);
Element stringNode = document.createElement("string"); //$NON-NLS-1$
dictNode.appendChild(stringNode);
Text stringValue = document.createTextNode(displayName);
stringNode.appendChild(stringValue);
appendTextContentChild(dict, "key", "CFBundleURLName"); //$NON-NLS-1$ //$NON-NLS-2$
appendTextContentChild(dict, "string", displayName); //$NON-NLS-1$
}

{
Element keyNode = document.createElement("key"); //$NON-NLS-1$
dictNode.appendChild(keyNode);
Text keyName = document.createTextNode("CFBundleURLSchemes"); //$NON-NLS-1$
keyNode.appendChild(keyName);
Element arrayNode = document.createElement("array"); //$NON-NLS-1$
dictNode.appendChild(arrayNode);
Element stringNode = document.createElement("string"); //$NON-NLS-1$
arrayNode.appendChild(stringNode);
Text stringValue = document.createTextNode(scheme);
stringNode.appendChild(stringValue);
appendTextContentChild(dict, "key", "CFBundleURLSchemes"); //$NON-NLS-1$ //$NON-NLS-2$
Element array = appendNewChild(dict, "array"); //$NON-NLS-1$
appendTextContentChild(array, "string", scheme); //$NON-NLS-1$
}
}

private Node getOrCreateCfBundleUrlTypesArray() {
String expression = String.format("/plist/dict/key[text() = '%s']/following-sibling::array[1]", //$NON-NLS-1$
BUNDLE_URL_TYPES);
Node arrayNode;
try {
arrayNode = getNode(infoPList, expression);
String expression = String.format("/plist/dict/key[text() = '%s']/following-sibling::array[1]", //$NON-NLS-1$
BUNDLE_URL_TYPES);
Node arrayNode = getNode(infoPList, expression);
if (arrayNode == null) {
Element keyNode = document.createElement("key"); //$NON-NLS-1$
Text keyName = document.createTextNode("CFBundleURLTypes"); //$NON-NLS-1$
keyNode.appendChild(keyName);

arrayNode = document.createElement("array"); //$NON-NLS-1$
getNode(infoPList, "/plist/dict").appendChild(keyNode); //$NON-NLS-1$
getNode(infoPList, "/plist/dict").appendChild(arrayNode); //$NON-NLS-1$
Node dict = getNode(infoPList, "/plist/dict"); //$NON-NLS-1$
appendTextContentChild(dict, "key", "CFBundleURLTypes"); //$NON-NLS-1$ //$NON-NLS-2$
arrayNode = appendNewChild(dict, "array"); //$NON-NLS-1$
}
return arrayNode;
} catch (XPathExpressionException e) {
Expand All @@ -178,6 +152,17 @@ private Node getOrCreateCfBundleUrlTypesArray() {
}
}

Element appendNewChild(Node parent, String name) {
Element node = document.createElement(name);
parent.appendChild(node);
return node;
}

private void appendTextContentChild(Node parent, String elementName, String text) {
Element keyNode = appendNewChild(parent, elementName);
keyNode.appendChild(document.createTextNode(text));
}

private XPath getXPathTool() {
if (xPathTool == null) {
xPathTool = XPathFactory.newInstance().newXPath();
Expand Down Expand Up @@ -235,12 +220,9 @@ public List<String> getEclipseArguments() {
public void setEclipseArguments(List<String> arguments) {
try {
removeNodes(infoPList, "/plist/dict/key[text() = 'Eclipse']/following-sibling::array[1]/string"); //$NON-NLS-1$
Node parent = getNode(infoPList, "/plist/dict/key[text() = 'Eclipse']/following-sibling::array[1]"); //$NON-NLS-1$
for (String arg : arguments) {
Element stringNode = document.createElement("string"); //$NON-NLS-1$
Text stringName = document.createTextNode(arg);
stringNode.appendChild(stringName);
Node toAppendTo = getNode(infoPList, "/plist/dict/key[text() = 'Eclipse']/following-sibling::array[1]"); //$NON-NLS-1$
toAppendTo.appendChild(stringNode);
appendTextContentChild(parent, "string", arg); //$NON-NLS-1$
}
} catch (XPathExpressionException e) {
//can't happen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,15 @@

import org.eclipse.equinox.p2.publisher.eclipse.IMacOsBundleUrlType;

public class MacOsBundleUrlType implements IMacOsBundleUrlType {

private final String scheme;
private final String name;

public MacOsBundleUrlType(String scheme, String name) {
this.scheme = scheme;
this.name = name;
}
record MacOsBundleUrlType(String scheme, String name) implements IMacOsBundleUrlType {

@Override
public String getScheme() {
return scheme;
return scheme();
}

@Override
public String getName() {
return name;
return name();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -823,31 +823,24 @@ public void startElement(String uri, String localName, String qName, Attributes
}
break;


case STATE_LAUNCHER_MAC :
if (null != localName) switch (localName) {
case EL_BUNDLE_URL_TYPES:
state = STATE_LAUNCHER_MAC_BUNDLE_URL_TYPES;
break;
default:
break;
case STATE_LAUNCHER_MAC:
if (null != localName) {
if (EL_BUNDLE_URL_TYPES.equals(localName)) {
state = STATE_LAUNCHER_MAC_BUNDLE_URL_TYPES;
}
}
break;


case STATE_LAUNCHER_MAC_BUNDLE_URL_TYPES :
if (null != localName) switch (localName) {
case EL_BUNDLE_URL_TYPE:
processMacOsBundleUrlTypes(attributes);
state = STATE_LAUNCHER_MAC_BUNDLE_URL_TYPES;
break;
default:
break;
case STATE_LAUNCHER_MAC_BUNDLE_URL_TYPES:
if (null != localName) {
if (EL_BUNDLE_URL_TYPE.equals(localName)) {
processMacOsBundleUrlTypes(attributes);
state = STATE_LAUNCHER_MAC_BUNDLE_URL_TYPES;
}
}
break;


case STATE_LAUNCHER_ARGS :
case STATE_LAUNCHER_ARGS:
if (null != localName) switch (localName) {
case PROGRAM_ARGS:
state = STATE_PROGRAM_ARGS;
Expand Down Expand Up @@ -1118,12 +1111,14 @@ public void endElement(String uri, String localName, String qName) {
state = STATE_PRODUCT;
break;
case STATE_LAUNCHER_MAC_BUNDLE_URL_TYPES:
if (EL_BUNDLE_URL_TYPES.equals(localName))
if (EL_BUNDLE_URL_TYPES.equals(localName)) {
state = STATE_LAUNCHER_MAC;
}
break;
case STATE_LAUNCHER_MAC:
if (OS_MACOSX.equals(localName))
if (OS_MACOSX.equals(localName)) {
state = STATE_LAUNCHER;
}
break;
case STATE_VM :
state = STATE_PRODUCT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
Expand Down Expand Up @@ -238,39 +236,33 @@ private void checkExecutableContents(IArtifactKey key) throws IOException {
* properly rewritten.
* @param zip file to check for the Info.plist
*/
private void checkInfoPlist(ZipFile zip) {
ZipEntry candidate = null;
boolean found = false;
for (Enumeration<? extends ZipEntry> iter = zip.entries(); !found && iter.hasMoreElements();) {
candidate = iter.nextElement();
found = candidate.getName().endsWith("Info.plist");
}
assertTrue(found);
try (InputStream is = zip.getInputStream(candidate)) {
private void checkInfoPlist(ZipFile zip) throws IOException {
var candidate = zip.stream().filter(e -> e.getName().endsWith("Info.plist")).findFirst();
assertTrue(candidate.isPresent());
try (InputStream is = zip.getInputStream(candidate.get())) {
String contents = new String(is.readAllBytes(), StandardCharsets.UTF_8);
assertEquals(id, getPlistStringValue(contents, "CFBundleIdentifier"));
assertEquals(EXECUTABLE_NAME, getPlistStringValue(contents, "CFBundleExecutable"));
assertEquals(EXECUTABLE_NAME, getPlistStringValue(contents, "CFBundleName"));
assertEquals(EXECUTABLE_NAME, getPlistStringValue(contents, "CFBundleDisplayName"));
assertEquals(version.toString(), getPlistStringValue(contents, "CFBundleVersion"));
assertEquals("<dict>" //
+ "<key>CFBundleURLName</key>" //
+ "<string>Eclipse Command</string>" //
+ "<key>CFBundleURLSchemes</key>" //
+ "<array>" //
+ "<string>eclipse+command</string>" //
+ "</array>" //
+ "</dict>" //
+ "<dict>" //
+ "<key>CFBundleURLName</key>" //
+ "<string>Vendor Application</string>" //
+ "<key>CFBundleURLSchemes</key>" //
+ "<array>" //
+ "<string>vendor</string>" //
+ "</array>" //
+ "</dict>", getPlistBundleUrlTypesXmlWithoutWhitespace(contents));
} catch (IOException e) {
fail();
assertEquals("""
<dict>
<key>CFBundleURLName</key>
<string>Eclipse Command</string>
<key>CFBundleURLSchemes</key>
<array>
<string>eclipse+command</string>
</array>
</dict>
<dict>
<key>CFBundleURLName</key>
<string>Vendor Application</string>
<key>CFBundleURLSchemes</key>
<array>
<string>vendor</string>
</array>
</dict>""".replaceAll(">\\s+<", "><"), getPlistBundleUrlTypesXmlWithoutWhitespace(contents));
}
}

Expand All @@ -283,10 +275,11 @@ private String getPlistStringValue(String contents, String key) {
return null;
}

private static final Pattern PLIST_BUNDLE_URL_TYPES_PATTERN = Pattern
.compile("<key>CFBundleURLTypes</key>\\s*<array>\\s*(.*</dict>.*?)\\s*</array>", Pattern.DOTALL);

private String getPlistBundleUrlTypesXmlWithoutWhitespace(String contents) {
Pattern p = Pattern.compile("<key>CFBundleURLTypes</key>\\s*<array>\\s*(.*</dict>.*?)\\s*</array>",
Pattern.DOTALL);
Matcher m = p.matcher(contents);
Matcher m = PLIST_BUNDLE_URL_TYPES_PATTERN.matcher(contents);
if (m.find()) {
return m.group(1).replace("\\n", "").replaceAll(">\\s+<", "><");
}
Expand Down

0 comments on commit 739b14e

Please sign in to comment.