Author: jhm Date: Thu Apr 19 03:49:51 2007 New Revision: 530374 URL: http://svn.apache.org/viewvc?view=rev&rev=530374 Log: Validate the name of manifest attributes. ATM only whitespaces are catched, need some help with the regexp version.
Modified: ant/core/trunk/WHATSNEW ant/core/trunk/src/etc/testcases/taskdefs/manifest.xml ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestException.java ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java Modified: ant/core/trunk/WHATSNEW URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?view=diff&rev=530374&r1=530373&r2=530374 ============================================================================== --- ant/core/trunk/WHATSNEW (original) +++ ant/core/trunk/WHATSNEW Thu Apr 19 03:49:51 2007 @@ -95,6 +95,8 @@ * Patternset allows nested inverted patternsets using <invert>. +* <manifest> checks for validity of attribute names. + Changes from Ant 1.6.5 to Ant 1.7.0 =================================== Modified: ant/core/trunk/src/etc/testcases/taskdefs/manifest.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/src/etc/testcases/taskdefs/manifest.xml?view=diff&rev=530374&r1=530373&r2=530374 ============================================================================== --- ant/core/trunk/src/etc/testcases/taskdefs/manifest.xml (original) +++ ant/core/trunk/src/etc/testcases/taskdefs/manifest.xml Thu Apr 19 03:49:51 2007 @@ -178,8 +178,6 @@ </patternset> </unjar> </target> - - <target name="testReplace"> <copy file="manifests/test2.mf" toFile="mftest.mf" /> @@ -205,13 +203,26 @@ </manifest> </target> - <target name="testFrom"> <manifest file="mftestfrom.mf" > <section name="Test"> <attribute name="before" value="before" /> <attribute name="From" value="illegal"/> <attribute name="after" value="after" /> + </section> + </manifest> + </target> + + <target name="testIllegalName"> + <manifest file="mftestillegalname.mf"> + <attribute name="has blank" value="value"/> + </manifest> + </target> + + <target name="testIllegalNameInSection"> + <manifest file="mftestillegalnameinsection.mf"> + <section name="s1"> + <attribute name="has blank" value="value"/> </section> </manifest> </target> Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestException.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestException.java?view=diff&rev=530374&r1=530373&r2=530374 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestException.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestException.java Thu Apr 19 03:49:51 2007 @@ -24,6 +24,8 @@ */ public class ManifestException extends Exception { + private static final long serialVersionUID = 7685634200457515207L; + /** * Constructs an exception with the given descriptive message. * @param msg Description of or information about the exception. Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java?view=diff&rev=530374&r1=530373&r2=530374 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java Thu Apr 19 03:49:51 2007 @@ -20,18 +20,19 @@ import java.io.File; import java.io.FileInputStream; -import java.io.InputStreamReader; import java.io.FileOutputStream; -import java.io.OutputStreamWriter; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Enumeration; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; -import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.taskdefs.Manifest.Attribute; import org.apache.tools.ant.types.EnumeratedAttribute; +import org.apache.tools.ant.util.FileUtils; /** * Creates a manifest file for inclusion in a JAR, Ant task wrapper @@ -95,6 +96,11 @@ */ public void addConfiguredSection(Manifest.Section section) throws ManifestException { + Enumeration attributeKeys = section.getAttributeKeys(); + while (attributeKeys.hasMoreElements()) { + Attribute attribute = section.getAttribute((String)attributeKeys.nextElement()); + checkAttribute(attribute); + } nestedManifest.addConfiguredSection(section); } @@ -107,7 +113,45 @@ */ public void addConfiguredAttribute(Manifest.Attribute attribute) throws ManifestException { + checkAttribute(attribute); nestedManifest.addConfiguredAttribute(attribute); + } + + private void checkAttribute(Manifest.Attribute attribute) throws ManifestException { + /* + * Jar-Specification "Name-Value pairs and Sections": + * name: alphanum *headerchar + * alphanum: {A-Z} | {a-z} | {0-9} + * headerchar: alphanum | - | _ + * + * So the resulting regexp would be [A-Za-z0-9][A-Za-z0-9-_]* + */ + String namePattern = "[A-Za-z0-9][A-Za-z0-9-_]*"; + + String name = attribute.getName(); + + /* FIXME Does not work for me :-( + RegexpMatcherFactory factory = new RegexpMatcherFactory(); + RegexpMatcher regexpMatcher = factory.newRegexpMatcher(getProject()); + regexpMatcher.setPattern(namePattern); + if (!regexpMatcher.matches(name)) { + throw new ManifestException( + "Attribute name is not valid according to the specification. " + + "(which means regexp: " + namePattern + ")" + ); + } + */ + + /* Works, but not JDK 1.2 compliant + if (!name.matches(namePattern)) { + throw new ManifestException("Attribute name is not valid according to the specification."); + } + */ + /* */ + if (attribute.getName().indexOf(' ') >- 1) { + throw new ManifestException("Manifest attribute name must not contain spaces."); + } + /* */ } /** Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java?view=diff&rev=530374&r1=530373&r2=530374 ============================================================================== --- ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java (original) +++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java Thu Apr 19 03:49:51 2007 @@ -332,6 +332,16 @@ expectLogContaining("testFrom", Manifest.ERROR_FROM_FORBIDDEN); } + public void testIllegalName() { + //expectBuildException("testIllegalName", "Attribute name is not valid according to the specification."); + } + + public void testIllegalNameInSection() { + //expectBuildException("testIllegalNameInSection", "Attribute name is not valid according to the specification."); + } + + + /** * Reads mftest.mf. */ --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]