diff options
author | Andrew Niefer | 2011-03-24 20:44:15 +0000 |
---|---|---|
committer | Andrew Niefer | 2011-03-24 20:44:15 +0000 |
commit | 4c5c46c36dd3351bbb31cc2ed11c0c7a47f6aaa7 (patch) | |
tree | 55383f756c6e2ab512c597ca4f2a48955e79cf66 | |
parent | 60d84a70a9ce805128a7c952d81df7f0047c3fe2 (diff) | |
download | eclipse.pde.build-4c5c46c36dd3351bbb31cc2ed11c0c7a47f6aaa7.tar.gz eclipse.pde.build-4c5c46c36dd3351bbb31cc2ed11c0c7a47f6aaa7.tar.xz eclipse.pde.build-4c5c46c36dd3351bbb31cc2ed11c0c7a47f6aaa7.zip |
bug 338835 - license feature corruption
2 files changed, 90 insertions, 21 deletions
diff --git a/org.eclipse.pde.build.tests/src/org/eclipse/pde/build/internal/tests/p2/LicenseTests.java b/org.eclipse.pde.build.tests/src/org/eclipse/pde/build/internal/tests/p2/LicenseTests.java index 265d1b9a..2ad23498 100644 --- a/org.eclipse.pde.build.tests/src/org/eclipse/pde/build/internal/tests/p2/LicenseTests.java +++ b/org.eclipse.pde.build.tests/src/org/eclipse/pde/build/internal/tests/p2/LicenseTests.java @@ -440,4 +440,43 @@ public class LicenseTests extends P2TestCase { } source.copy(dest.getFullPath(), true, null); } + + public void testBug338835_MissingLicenseSection() throws Exception { + IFolder buildFolder = newTest("338835"); + IFolder featureFolder = Utils.createFolder(buildFolder, "feature"); + IFolder licenseFolder = Utils.createFolder(buildFolder, "license"); + + StringBuffer buffer = new StringBuffer(); + buffer.append("<feature id=\"org.eclipse.ptp\" version=\"5.0.0.qualifier\" \n"); + buffer.append(" license-feature=\"org.eclipse.ptp.license\" license-feature-version=\"1.0.0.qualifier\">\n"); + buffer.append(" <description url=\"http://eclipse.org/ptp\">%description</description> \n"); + buffer.append(" <copyright>%copyright</copyright> \n"); + buffer.append(" <url> \n"); + buffer.append(" <update label=\"%updateSiteName\" url=\"http://download.eclipse.org/updates\"/> \n"); + buffer.append(" </url> \n"); + buffer.append("</feature> \n"); + Utils.writeBuffer(featureFolder.getFile("feature.xml"), buffer); + + buffer.append("<feature id=\"license\" version=\"5.0.0.qualifier\" >\n"); + buffer.append(" <license url=\"http://eclipse.org/license.html\"> \n"); + buffer.append(" This is legal stuff \n"); + buffer.append(" </license> \n"); + buffer.append("</feature> \n"); + Utils.writeBuffer(licenseFolder.getFile("feature.xml"), buffer); + + buffer = new StringBuffer(); + buffer.append("<project name=\"build\" basedir=\".\" > \n"); + buffer.append(" <target name=\"test\"> \n"); + buffer.append(" <eclipse.licenseReplacer featureFilePath=\"" + featureFolder.getLocation().toOSString() + "\" \n"); + buffer.append(" licenseFilePath=\"" + licenseFolder.getLocation().toOSString() + "\" /> \n"); + buffer.append(" </target> \n"); + buffer.append("</project> \n"); + IFile buildXml = buildFolder.getFile("build.xml"); + Utils.writeBuffer(buildXml, buffer); + + runAntScript(buildXml.getLocation().toOSString(), new String[] {"test"}, buildFolder.getLocation().toOSString(), null); + BuildTimeFeature feature = new BuildTimeFeatureFactory().parseBuildFeature(featureFolder.getFile("feature.xml").getLocationURI().toURL()); + + assertEquals(feature.getLicense().trim(), "This is legal stuff"); + } } diff --git a/org.eclipse.pde.build/src_ant/org/eclipse/pde/internal/build/tasks/LicenseReplaceTask.java b/org.eclipse.pde.build/src_ant/org/eclipse/pde/internal/build/tasks/LicenseReplaceTask.java index 7610f225..cde22c8b 100644 --- a/org.eclipse.pde.build/src_ant/org/eclipse/pde/internal/build/tasks/LicenseReplaceTask.java +++ b/org.eclipse.pde.build/src_ant/org/eclipse/pde/internal/build/tasks/LicenseReplaceTask.java @@ -34,6 +34,7 @@ public class LicenseReplaceTask extends Task { private class Feature { private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$ + private static final String FEATURE_START_TAG = "<feature";//$NON-NLS-1$ private static final String LICENSE_START_TAG = "<license"; //$NON-NLS-1$; private static final String LICENSE_END_TAG = "</license>"; //$NON-NLS-1$; private static final String URL_ATTR = "url";//$NON-NLS-1$ @@ -45,12 +46,13 @@ public class LicenseReplaceTask extends Task { private String urlText; private String license; private StringBuffer buffer; - private int startLicenseText; - private int endLicenseText; - private int startURLText; - private int endURLText; - private int startURLWord; - private int endURLWord; + private int startLicenseText = -1; + private int endLicenseText = -1; + private int startURLText = -1; + private int endURLText = -1; + private int startURLWord = -1; + private int endURLWord = -1; + private int insertionPoint = -1; private boolean contentChanged; public String getUrl() { @@ -72,9 +74,27 @@ public class LicenseReplaceTask extends Task { throw new IllegalStateException(TaskMessages.error_noCallAfterReplace); } - // Replace license text - buffer.replace(startLicenseText, endLicenseText, licenseText); - contentChanged = true; + if (startLicenseText > 0 && endLicenseText > startLicenseText) { + // Replace license text + buffer.replace(startLicenseText, endLicenseText, licenseText); + contentChanged = true; + } else if (insertionPoint > -1) { + //insert new license after <feature> + StringBuffer newLicense = new StringBuffer(); + newLicense.append('\n'); + newLicense.append(LICENSE_START_TAG + " " + URL_ATTR + "="); //$NON-NLS-1$//$NON-NLS-2$ + if (licenseURL != null) + newLicense.append(licenseURL); + newLicense.append(" >"); //$NON-NLS-1$ + newLicense.append(licenseText); + newLicense.append(LICENSE_END_TAG); + + buffer.insert(insertionPoint, newLicense.toString()); + contentChanged = true; + return; + } else { + return; + } if (startURLText == endURLText) { // Replace empty payload URL @@ -148,17 +168,14 @@ public class LicenseReplaceTask extends Task { throw new BuildException(e); } - //Skip feature declaration because it contains the word "plugin" - int startComment = scan(buffer, 0, COMMENT_START_TAG); - int endComment = startComment > -1 ? scan(buffer, startComment, COMMENT_END_TAG) : -1; - int startLicense = scan(buffer, 0, LICENSE_START_TAG, false); + int startFeature = scanNoComment(buffer, 0, FEATURE_START_TAG, true); + if (startFeature == -1) + return; - while (startComment != -1 && startLicense > startComment && startLicense < endComment) { - startLicense = scan(buffer, endComment, LICENSE_START_TAG, true); - startComment = scan(buffer, endComment, COMMENT_START_TAG); - endComment = startComment > -1 ? scan(buffer, startComment, COMMENT_END_TAG) : -1; - } + int endFeature = scan(buffer, startFeature, ">"); //$NON-NLS-1$ + insertionPoint = endFeature + 1; + int startLicense = scanNoComment(buffer, 0, LICENSE_START_TAG, false); if (startLicense == -1) return; @@ -196,10 +213,10 @@ public class LicenseReplaceTask extends Task { urlText = (buffer.substring(startURLText, endURLText + 1)); } urlFound = true; - startLicenseText = scan(buffer, endURLText, ">") + 1; //$NON-NLS-1$ - endLicenseText = scan(buffer, startLicenseText, LICENSE_END_TAG, true) - 1; - license = buffer.substring(startLicenseText, endLicenseText); } + startLicenseText = scan(buffer, endURLText, ">") + 1; //$NON-NLS-1$ + endLicenseText = scan(buffer, startLicenseText, LICENSE_END_TAG, true) - 1; + license = buffer.substring(startLicenseText, endLicenseText); } private StringBuffer readFile(File targetName) throws IOException { @@ -246,6 +263,19 @@ public class LicenseReplaceTask extends Task { } return -1; } + + private int scanNoComment(StringBuffer bug, int start, String target, boolean wholeWord) { + int startComment = scan(buffer, start, COMMENT_START_TAG); + int endComment = startComment > -1 ? scan(buffer, startComment, COMMENT_END_TAG) : -1; + int startTarget = scan(buffer, start, target, wholeWord); + + while (startComment != -1 && startTarget > startComment && startTarget < endComment) { + startTarget = scan(buffer, endComment, target, wholeWord); + startComment = scan(buffer, endComment, COMMENT_START_TAG); + endComment = startComment > -1 ? scan(buffer, startComment, COMMENT_END_TAG) : -1; + } + return startTarget; + } } /** |