Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved InfoPList to the util package. Moved the method which parses an…
…d does property replacement in plist files from IOSTarget to InfoPList.
- Loading branch information
Showing
7 changed files
with
151 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 0 additions & 77 deletions
77
compiler/src/main/java/org/robovm/compiler/target/ios/InfoPList.java
This file was deleted.
Oops, something went wrong.
136 changes: 136 additions & 0 deletions
136
compiler/src/main/java/org/robovm/compiler/util/InfoPList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.