Skip to content

Commit

Permalink
Moved InfoPList to the util package. Moved the method which parses an…
Browse files Browse the repository at this point in the history
…d does

property replacement in plist files from IOSTarget to InfoPList.
  • Loading branch information
ntherning committed Aug 4, 2015
1 parent e6d8855 commit 1ffd8a6
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 147 deletions.
Expand Up @@ -74,10 +74,10 @@
import org.robovm.compiler.target.ConsoleTarget;
import org.robovm.compiler.target.Target;
import org.robovm.compiler.target.ios.IOSTarget;
import org.robovm.compiler.target.ios.InfoPList;
import org.robovm.compiler.target.ios.ProvisioningProfile;
import org.robovm.compiler.target.ios.SigningIdentity;
import org.robovm.compiler.util.DigestUtil;
import org.robovm.compiler.util.InfoPList;
import org.robovm.compiler.util.io.RamDiskTools;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
Expand Down
Expand Up @@ -413,7 +413,7 @@ protected void copyFile(Resource resource, File file, File destDir) throws IOExc
public void install() throws IOException {
config.getLogger().info("Installing %s binary to %s", config.getTarget().getType(), config.getInstallDir());
config.getInstallDir().mkdirs();
doInstall(config.getInstallDir(), config.getExecutableName());
doInstall(config.getInstallDir(), config.getExecutableName(), config.getInstallDir());
}

@Override
Expand All @@ -426,7 +426,7 @@ public void archive() throws IOException {
throw new UnsupportedOperationException("Archiving is not supported for this target");
}

protected void doInstall(File installDir, String image) throws IOException {
protected void doInstall(File installDir, String image, File resourcesDir) throws IOException {
if (!config.getTmpDir().equals(installDir) || !image.equals(config.getExecutableName())) {
File destFile = new File(installDir, image);
FileUtils.copyFile(new File(config.getTmpDir(), config.getExecutableName()), destFile);
Expand All @@ -438,7 +438,7 @@ protected void doInstall(File installDir, String image) throws IOException {
}
}
stripArchives(installDir);
copyResources(installDir);
copyResources(resourcesDir);
copyDynamicFrameworks(installDir);
}

Expand Down
Expand Up @@ -20,7 +20,6 @@
import java.io.FileFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -32,11 +31,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
Expand All @@ -63,18 +57,13 @@
import org.robovm.libimobiledevice.InstallationProxyClient.StatusCallback;
import org.robovm.libimobiledevice.util.AppLauncher;
import org.robovm.libimobiledevice.util.AppLauncherCallback;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSNumber;
import com.dd.plist.NSObject;
import com.dd.plist.NSString;
import com.dd.plist.PropertyListParser;
import com.dd.plist.XMLPropertyListParser;

/**
* @author niklas
Expand Down Expand Up @@ -389,7 +378,7 @@ private void copyProvisioningProfile(ProvisioningProfile profile, File destDir)
}

protected void prepareLaunch(File appDir) throws IOException {
super.doInstall(appDir, getExecutable());
super.doInstall(appDir, getExecutable(), appDir);
createInfoPList(appDir);
generateDsym(appDir, getExecutable(), true);

Expand Down Expand Up @@ -551,8 +540,8 @@ private void strip(File dir, String executable) throws IOException {
}

@Override
protected void doInstall(File installDir, String executable) throws IOException {
super.doInstall(installDir, getExecutable());
protected void doInstall(File installDir, String executable, File resourcesDir) throws IOException {
super.doInstall(installDir, getExecutable(), resourcesDir);
prepareInstall(installDir);
}

Expand Down Expand Up @@ -581,7 +570,7 @@ public void archive() throws IOException {
File tmpDir = new File(config.getInstallDir(), getExecutable() + ".app");
FileUtils.deleteDirectory(tmpDir);
tmpDir.mkdirs();
super.doInstall(tmpDir, getExecutable());
super.doInstall(tmpDir, getExecutable(), tmpDir);
prepareInstall(tmpDir);
packageApplication(tmpDir);
}
Expand Down Expand Up @@ -996,48 +985,4 @@ public int compare(File o1, File o2) {
}
}
}

private final static Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\{([^}]+)\\}");

static void replacePropertyRefs(Node node, Properties props) {
if (node instanceof Text) {
Text el = (Text) node;
String value = el.getNodeValue();
if (value != null && value.trim().length() > 0) {
Matcher matcher = VARIABLE_PATTERN.matcher(value);
StringBuilder sb = new StringBuilder();
int pos = 0;
while (matcher.find()) {
if (pos < matcher.start()) {
sb.append(value.substring(pos, matcher.start()));
}
String key = matcher.group(1);
sb.append(props.getProperty(key, matcher.group()));
pos = matcher.end();
}
if (pos < value.length()) {
sb.append(value.substring(pos));
}
el.setNodeValue(sb.toString());
}
}
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
replacePropertyRefs(children.item(i), props);
}
}

static NSObject parsePropertyList(File file, Properties props) throws Exception {
Properties allProps = new Properties(System.getProperties());
allProps.putAll(props);

Method getDocBuilder = XMLPropertyListParser.class.getDeclaredMethod("getDocBuilder");
getDocBuilder.setAccessible(true);
Method parseDocument = XMLPropertyListParser.class.getDeclaredMethod("parseDocument", Document.class);
parseDocument.setAccessible(true);
DocumentBuilder docBuilder = (DocumentBuilder) getDocBuilder.invoke(null);
Document doc = docBuilder.parse(file);
replacePropertyRefs(doc, allProps);
return (NSObject) parseDocument.invoke(null, doc);
}
}

This file was deleted.

136 changes: 136 additions & 0 deletions compiler/src/main/java/org/robovm/compiler/util/InfoPList.java
@@ -0,0 +1,136 @@
/*
* Copyright (C) 2013-2015 RoboVM AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.robovm.compiler.util;

import java.io.File;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import com.dd.plist.NSDictionary;
import com.dd.plist.NSObject;
import com.dd.plist.XMLPropertyListParser;

public class InfoPList {
private File file;
private NSDictionary dictionary;

public InfoPList(File file) {
this.file = file;
}

public void parse(Properties props) {
parse(props, true);
}

public void parse(Properties props, boolean includeSystemProperties) {
try {
this.dictionary = (NSDictionary) parsePropertyList(file, props, includeSystemProperties);
} catch (Throwable t) {
throw new IllegalArgumentException("Failed to parse Info.plist XML file: " + file, t);
}
}

public String getBundleIdentifier() {
if (dictionary != null) {
NSObject object = dictionary.objectForKey("CFBundleIdentifier");
if (object != null) {
return object.toString();
}
}
return null;
}

public String getBundleExecutable() {
if (dictionary != null) {
NSObject object = dictionary.objectForKey("CFBundleExecutable");
if (object != null) {
return object.toString();
}
}
return null;
}

public String getMinimumOSVersion() {
if (dictionary != null) {
NSObject object = dictionary.objectForKey("MinimumOSVersion");
if (object != null) {
return object.toString();
}
}
return null;
}

public File getFile() {
return file;
}

public NSDictionary getDictionary() {
return dictionary;
}

private final static Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\{([^}]+)\\}");

static void replacePropertyRefs(Node node, Properties props) {
if (node instanceof Text) {
Text el = (Text) node;
String value = el.getNodeValue();
if (value != null && value.trim().length() > 0) {
Matcher matcher = VARIABLE_PATTERN.matcher(value);
StringBuilder sb = new StringBuilder();
int pos = 0;
while (matcher.find()) {
if (pos < matcher.start()) {
sb.append(value.substring(pos, matcher.start()));
}
String key = matcher.group(1);
sb.append(props.getProperty(key, matcher.group()));
pos = matcher.end();
}
if (pos < value.length()) {
sb.append(value.substring(pos));
}
el.setNodeValue(sb.toString());
}
}
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
replacePropertyRefs(children.item(i), props);
}
}

static NSObject parsePropertyList(File file, Properties props, boolean includeSystemProperties) throws Exception {
Properties allProps = new Properties(includeSystemProperties ? System.getProperties() : new Properties());
allProps.putAll(props);

Method getDocBuilder = XMLPropertyListParser.class.getDeclaredMethod("getDocBuilder");
getDocBuilder.setAccessible(true);
Method parseDocument = XMLPropertyListParser.class.getDeclaredMethod("parseDocument", Document.class);
parseDocument.setAccessible(true);
DocumentBuilder docBuilder = (DocumentBuilder) getDocBuilder.invoke(null);
Document doc = docBuilder.parse(file);
replacePropertyRefs(doc, allProps);
return (NSObject) parseDocument.invoke(null, doc);
}
}

0 comments on commit 1ffd8a6

Please sign in to comment.