Browse Source

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
master
Jan Materne 18 years ago
parent
commit
4462d80608
5 changed files with 75 additions and 6 deletions
  1. +2
    -0
      WHATSNEW
  2. +14
    -3
      src/etc/testcases/taskdefs/manifest.xml
  3. +2
    -0
      src/main/org/apache/tools/ant/taskdefs/ManifestException.java
  4. +47
    -3
      src/main/org/apache/tools/ant/taskdefs/ManifestTask.java
  5. +10
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java

+ 2
- 0
WHATSNEW View File

@@ -95,6 +95,8 @@ Other changes:

* 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
===================================


+ 14
- 3
src/etc/testcases/taskdefs/manifest.xml View File

@@ -178,8 +178,6 @@
</patternset>
</unjar>
</target>

<target name="testReplace">
<copy file="manifests/test2.mf" toFile="mftest.mf" />
@@ -205,7 +203,6 @@
</manifest>
</target>


<target name="testFrom">
<manifest file="mftestfrom.mf" >
<section name="Test">
@@ -215,6 +212,20 @@
</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>

<target name="clean">
<delete>


+ 2
- 0
src/main/org/apache/tools/ant/taskdefs/ManifestException.java View File

@@ -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.


+ 47
- 3
src/main/org/apache/tools/ant/taskdefs/ManifestTask.java View File

@@ -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.


+ 10
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java View File

@@ -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.
*/


Loading…
Cancel
Save