From 4462d806089e69c25b2a09fa31060915d3fef1da Mon Sep 17 00:00:00 2001 From: Jan Materne Date: Thu, 19 Apr 2007 10:49:51 +0000 Subject: [PATCH] Validate the name of manifest attributes. ATM only whitespaces are catched, need some help with the regexp version. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@530374 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 2 + src/etc/testcases/taskdefs/manifest.xml | 17 +++++-- .../tools/ant/taskdefs/ManifestException.java | 2 + .../tools/ant/taskdefs/ManifestTask.java | 50 +++++++++++++++++-- .../tools/ant/taskdefs/ManifestTest.java | 10 ++++ 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 9d3e8f101..804a28df7 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -95,6 +95,8 @@ Other changes: * Patternset allows nested inverted patternsets using . +* checks for validity of attribute names. + Changes from Ant 1.6.5 to Ant 1.7.0 =================================== diff --git a/src/etc/testcases/taskdefs/manifest.xml b/src/etc/testcases/taskdefs/manifest.xml index f7bf7faf7..ac06e2346 100644 --- a/src/etc/testcases/taskdefs/manifest.xml +++ b/src/etc/testcases/taskdefs/manifest.xml @@ -178,8 +178,6 @@ - - @@ -205,7 +203,6 @@ -
@@ -215,6 +212,20 @@
+ + + + + + + + + +
+ +
+
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/ManifestException.java b/src/main/org/apache/tools/ant/taskdefs/ManifestException.java index 36624b02c..c4e721325 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ManifestException.java +++ b/src/main/org/apache/tools/ant/taskdefs/ManifestException.java @@ -24,6 +24,8 @@ package org.apache.tools.ant.taskdefs; */ 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. diff --git a/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java b/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java index e3c2d49f9..d495dac1e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java @@ -20,18 +20,19 @@ package org.apache.tools.ant.taskdefs; 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 class ManifestTask extends Task { */ 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,9 +113,47 @@ public class ManifestTask extends Task { */ 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."); + } + /* */ + } + /** * The name of the manifest file to create/update. * Required if used as a task. diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java index f16e6c388..5d2a48bd3 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java @@ -332,6 +332,16 @@ public class ManifestTest extends BuildFileTest { 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. */