diff --git a/.gitignore b/.gitignore index 571df8b..960e49b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /.classpath /.project /.settings/ -/target/ \ No newline at end of file +/target/ +*~ +#*# diff --git a/NOTICE.txt b/NOTICE.txt new file mode 100644 index 0000000..801d783 --- /dev/null +++ b/NOTICE.txt @@ -0,0 +1,9 @@ + ========================================================================= + == NOTICE file for use with the Apache License, Version 2.0 == + ========================================================================= + + XCodeProjectJavaAPI + Copyright 2012-2013 SAP AG + + This product includes software developed at + The Apache Software Foundation (http://www.apache.org/). diff --git a/README.md b/README.md index ee823f0..e223151 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,10 @@ See [UsageExamples.java](https://github.com/sap-production/XcodeProjectJavaAPI/b ##Known limitations -In order to use this library, you have to convert the Xcode project file to XML (see ```convert``` method in [JAXBPlistParser.java](https://github.com/sap-production/XcodeProjectJavaAPI/blob/master/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParser.java). This step can only be done on MAC OS X. The project file cannot be converted back! +In order to use this library, you have to convert the Xcode project file to XML (see ```convert``` method in [JAXBPlistParser.java](https://github.com/sap-production/XcodeProjectJavaAPI/blob/master/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParser.java). This step can only be done on Mac OS X. The project file cannot be converted back! -The remainder of the API uses standard Java features. \ No newline at end of file +The remainder of the API uses standard Java features. + +## License + +This project is copyrighted by [SAP AG](http://www.sap.com/) and made available under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0.html). Please also confer to the text files "LICENSE" and "NOTICE" included with the project sources. diff --git a/pom.xml b/pom.xml index 736ee4a..8307ff7 100644 --- a/pom.xml +++ b/pom.xml @@ -2,11 +2,20 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.sap.prd.mobile.ios.mios - xcode-project-reader + xcode-project-java-api jar - 1.1.0-SNAPSHOT + 1.2.0-SNAPSHOT + + + com.sap.prd.mobile.ios.mios + quality-check-parent + 1.0.0 + + + + Xcode Project Java API - The xcode-project-reader library provides an Java API for read and write + The xcode-project-java-api library provides an Java API for read and write access to an Xcode project file. 2012 @@ -21,6 +30,8 @@ + https://github.com/sap-production/XcodeProjectJavaAPI + scm:git:https://github.com/sap-production/XcodeProjectJavaAPI.git scm:git:https://github.com/sap-production/XcodeProjectJavaAPI.git @@ -82,11 +93,20 @@ + + org.jacoco + jacoco-maven-plugin + - apache-codec + org.apache.commons + commons-lang3 + 3.1 + + + commons-codec commons-codec 1.2 @@ -112,4 +132,27 @@ + + + + release + + + + maven-javadoc-plugin + 2.8.1 + + + attach-javadocs + + jar + + + + + + + + + diff --git a/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlist.java b/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlist.java index 89720b7..a7b0786 100644 --- a/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlist.java +++ b/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlist.java @@ -55,7 +55,7 @@ public Dict getDict() return dict; } - void setDict(Dict dict) + public void setDict(Dict dict) { this.dict = dict; } diff --git a/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParser.java b/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParser.java index f48a888..006d28b 100644 --- a/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParser.java +++ b/src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParser.java @@ -22,11 +22,14 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; +import javax.xml.bind.PropertyException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; @@ -34,6 +37,7 @@ import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.sax.SAXSource; +import org.apache.commons.lang3.SystemUtils; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -43,6 +47,8 @@ public class JAXBPlistParser { + private static final String xmlHeaders = "\n"; + public Plist load(String projectFile) throws SAXException, ParserConfigurationException, FileNotFoundException, JAXBException { @@ -106,19 +112,35 @@ public void save(Plist plist, String projectFile) throws JAXBException } public void save(Plist plist, File projectFile) throws JAXBException + { + try + { + save(plist, new FileWriter(projectFile)); + } + catch (IOException ex) + { + throw new JAXBException(ex); + } + } + + public void save(Plist plist, Writer projectFile) throws JAXBException { marshallPlist(plist, projectFile); } - private void marshallPlist(Plist plist, File projectFile) throws JAXBException + private void marshallPlist(Plist plist, Writer projectFile) throws JAXBException { JAXBContext ctx = JAXBContext.newInstance(com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlist.class); Marshaller marshaller = ctx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - marshaller - .setProperty( - "com.sun.xml.internal.bind.xmlHeaders", - "\n"); + try + { + marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", xmlHeaders); + } + catch(PropertyException ex) + { + marshaller.setProperty("com.sun.xml.bind.xmlHeaders", xmlHeaders); + } marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.marshal(plist, projectFile); } @@ -130,6 +152,10 @@ public void convert(String projectFile, String destinationProjectFile) throws IO public void convert(File projectFile, File destinationProjectFile) throws IOException { + if (!SystemUtils.IS_OS_MAC_OSX) { + throw new UnsupportedOperationException("The pbxproj file conversion can only be performed on a Mac OS X " + + "operating system as the Mac OS X specific tool 'plutil' gets called."); + } Process exec = Runtime.getRuntime().exec( new String[] { "plutil", "-convert", "xml1", "-o", destinationProjectFile.getAbsolutePath(), projectFile.getAbsolutePath() }); diff --git a/src/test/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParserTest.java b/src/test/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParserTest.java index 0d153a8..13b3f8b 100644 --- a/src/test/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParserTest.java +++ b/src/test/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParserTest.java @@ -20,17 +20,23 @@ package com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import junit.framework.Assert; +import org.apache.commons.lang3.SystemUtils; import org.custommonkey.xmlunit.XMLAssert; import org.custommonkey.xmlunit.XMLUnit; import org.junit.Test; @@ -90,9 +96,19 @@ public void convertOpenStepToXML() throws Exception File xmlProj = File.createTempFile("project", ".pbxproj"); xmlProj.deleteOnExit(); - parser.convert(fileNameOpenStep, xmlProj.getAbsolutePath()); - Plist plist = parser.load(xmlProj.getAbsolutePath()); - assertEquals("1.0", plist.getVersion()); + try + { + parser.convert(fileNameOpenStep, xmlProj.getAbsolutePath()); + Plist plist = parser.load(xmlProj.getAbsolutePath()); + assertEquals("1.0", plist.getVersion()); + } + catch (UnsupportedOperationException ex) + { + // If we are running on a non Mac OS X system an UnsupportedOperationException is expected + assertFalse("The convert function should only fail on non Mac OS X systems", SystemUtils.IS_OS_MAC_OSX); + assertTrue("Wrong UnsupportedOperationException message", ex.getMessage().contains("Mac OS X")); + } + } @Test(expected = javax.xml.bind.UnmarshalException.class) @@ -127,10 +143,28 @@ public void save() throws Exception output.deleteOnExit(); parser.save(plist, output.getAbsolutePath()); - assertXMLEqual(new File(fileName), output); + DocumentBuilder db = initDocumentBuilder(); + Document expected = db.parse(new File(fileName)); + Document actual = db.parse(output); + assertXMLEqual(expected, actual); } - private void assertXMLEqual(File expected, File actual) throws Exception + @Test + public void saveToWriter() throws Exception + { + JAXBPlistParser parser = new JAXBPlistParser(); + Plist plist = parser.load(fileName); + + Writer output = new StringWriter(); + parser.save(plist, output); + + DocumentBuilder db = initDocumentBuilder(); + Document expected = db.parse(new File(fileName)); + Document actual = db.parse(new ByteArrayInputStream(output.toString().getBytes())); + assertXMLEqual(expected, actual); + } + + private DocumentBuilder initDocumentBuilder() throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); db.setEntityResolver(new EntityResolver() { @@ -141,11 +175,13 @@ public InputSource resolveEntity(String publicId, String systemId) return new InputSource(new StringReader("")); } }); + return db; + } - Document doc1 = db.parse(expected); - Document doc2 = db.parse(actual); + private void assertXMLEqual(Document expected, Document actual) throws Exception + { XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); - XMLAssert.assertXMLEqual(doc1, doc2); + XMLAssert.assertXMLEqual(expected, actual); } }