hlship 2004/06/25 16:26:42
Modified: framework/src/java/org/apache/hivemind/parse
ParseStrings.properties ParseMessages.java
DescriptorParser.java
Log:
Add validation of ids parsed in module deployment descriptors.
Revision Changes Path
1.5 +4 -1
jakarta-hivemind/framework/src/java/org/apache/hivemind/parse/ParseStrings.properties
Index: ParseStrings.properties
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/parse/ParseStrings.properties,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ParseStrings.properties 17 Jun 2004 15:16:11 -0000 1.4
+++ ParseStrings.properties 25 Jun 2004 23:26:42 -0000 1.5
@@ -19,7 +19,6 @@
not-module=Document element should be module not {0} (at {1}).
required-attribute=Missing required attribute ''{0}'' in element {1} (at
{2}).
unknown-attribute=Unknown attribute ''{0}'' in element {1}.
-invalid-id-format=Attribute {0} ({1}) of element {2} is not a valid id.
boolean-attribute=''{0}'' is not a valid value for attribute {1} of {2}.
Expected values are ''true'' or ''false''.
invalid-attribute-value=''{0}'' is not a valid value for attribute ''{1}''
of {2}.
invalid-numeric-value=''{0}'' (attribute {1} of {2}) can not be converted to
a numeric value.
@@ -30,3 +29,7 @@
unexpected-element=Unexpected element {0} within {1}.
+invalid-attribute-format=Attribute {0} ({1}) of element {2} is improperly
formatted. {3}
+module-id-format=Module identifiers should consist of a period-seperated
series of names, like a Java package.
+id-format=Schema and extension point ids should be simple names with no
punctuation.
+version-format=Version numbers should be a sequence of three numbers
seperated by periods.
\ No newline at end of file
1.7 +13 -5
jakarta-hivemind/framework/src/java/org/apache/hivemind/parse/ParseMessages.java
Index: ParseMessages.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/parse/ParseMessages.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ParseMessages.java 18 Jun 2004 13:50:03 -0000 1.6
+++ ParseMessages.java 25 Jun 2004 23:26:42 -0000 1.7
@@ -66,11 +66,6 @@
return _formatter.format("unknown-attribute", name, path);
}
- public static String invalidIdFormat(String attributeName, String value,
String elementName)
- {
- return _formatter.format("invalid-id-format", attributeName, value,
elementName);
- }
-
public static String booleanAttribute(String value, String name, String
path)
{
return _formatter.format("boolean-attribute", new Object[] { value,
name, path });
@@ -110,5 +105,18 @@
public static String unexpectedElement(String elementName, String
elementPath)
{
return _formatter.format("unexpected-element", elementName,
elementPath);
+ }
+
+ public static String invalidAttributeFormat(
+ String attributeName,
+ String value,
+ String elementPath,
+ String formatKey)
+ {
+ String inputValueFormat = _formatter.getMessage(formatKey);
+
+ return _formatter.format(
+ "invalid-attribute-format",
+ new Object[] { attributeName, value, elementPath,
inputValueFormat });
}
}
1.22 +73 -26
jakarta-hivemind/framework/src/java/org/apache/hivemind/parse/DescriptorParser.java
Index: DescriptorParser.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/parse/DescriptorParser.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- DescriptorParser.java 17 Jun 2004 15:16:11 -0000 1.21
+++ DescriptorParser.java 25 Jun 2004 23:26:42 -0000 1.22
@@ -35,7 +35,6 @@
import org.apache.hivemind.Attribute;
import org.apache.hivemind.ClassResolver;
import org.apache.hivemind.ErrorHandler;
-import org.apache.hivemind.HiveMind;
import org.apache.hivemind.Location;
import org.apache.hivemind.Occurances;
import org.apache.hivemind.Resource;
@@ -58,6 +57,10 @@
import org.apache.hivemind.schema.rules.SetPropertyRule;
import org.apache.hivemind.sdl.SDLResourceParser;
import org.apache.hivemind.util.PropertyUtils;
+import org.apache.oro.text.regex.MalformedPatternException;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.Perl5Compiler;
+import org.apache.oro.text.regex.Perl5Matcher;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -172,6 +175,27 @@
private static final int STATE_NO_CONTENT = 300;
+ private static final String SIMPLE_ID = "_?[a-zA-Z0-9_]+";
+
+ /**
+ * Format for configuration point ids, service point ids and schema ids.
+ * Consists of an optional leading underscore, followed by alphanumerics
+ * and underscores. Normal naming convention is to use a single
CamelCase word,
+ * like a Java class name.
+ */
+ public static final String ID_PATTERN = "^" + SIMPLE_ID + "$";
+
+ /**
+ * Module ids are a sequence of simple ids seperated by periods.
+ * In practice, they look like Java package names.
+ */
+ public static final String MODULE_ID_PATTERN = "^" + SIMPLE_ID + "(\\."
+ SIMPLE_ID + ")*$";
+
+ public static final String VERSION_PATTERN =
"^[1-9]*[0-9](\\.[1-9]*[0-9]){2}$";
+
+ /**
+ *
+ */
/**
* Temporary storage of the current [EMAIL PROTECTED] Attributes}.
*/
@@ -195,6 +219,10 @@
private ClassResolver _resolver;
+ private Perl5Compiler _compiler;
+ private Perl5Matcher _matcher;
+ private Map _compiledPatterns;
+
/**
* Map of Rule keyed on class name, used with <custom> rules.
*/
@@ -583,8 +611,8 @@
checkAttributes();
- md.setModuleId(getAttribute("id"));
- md.setVersion(getAttribute("version"));
+ md.setModuleId(getValidatedAttribute("id", MODULE_ID_PATTERN,
"module-id-format"));
+ md.setVersion(getValidatedAttribute("version", VERSION_PATTERN,
"version-format"));
// And, this is what we ultimately return from the parse.
@@ -742,7 +770,7 @@
checkAttributes();
- cpd.setId(extractId("id"));
+ cpd.setId(getValidatedAttribute("id", ID_PATTERN, "id-format"));
Occurances count = (Occurances) getEnumAttribute("occurs",
OCCURS_MAP);
@@ -1003,7 +1031,7 @@
checkAttributes();
- String id = getAttribute("id");
+ String id = getValidatedAttribute("id", ID_PATTERN, "id-format");
// TODO: check for duplicate name!
@@ -1020,7 +1048,7 @@
checkAttributes();
- spd.setId(extractId("id"));
+ spd.setId(getValidatedAttribute("id", ID_PATTERN, "id-format"));
spd.setInterfaceClassName(getAttribute("interface"));
Schema s = obtainSchema(getAttribute("parameters-schema-id"), spd,
"parametersSchema");
@@ -1102,42 +1130,61 @@
_registryAssembly.enqueueModuleParse(subModuleDescriptor, _resolver);
}
- /**
- * Extracts the specified attribute and checks that it is a valid local
id
- * (does not contain a '.' character).
- *
- * @return the id, possibly null
- */
- private String extractId(String attributeName)
+ private String getAttribute(String name)
+ {
+ return (String) _attributes.get(name);
+ }
+
+ private String getAttribute(String name, String defaultValue)
{
- String result = getAttribute(attributeName);
+ String result = (String) _attributes.get(name);
if (result == null)
- return result;
+ result = defaultValue;
- if (result.indexOf('.') >= 0)
+ return result;
+ }
+
+ private String getValidatedAttribute(String name, String pattern, String
formatKey)
+ {
+ String result = getAttribute(name);
+
+ if (!validateFormat(result, pattern))
_errorHandler.error(
LOG,
- ParseMessages.invalidIdFormat(attributeName, result,
peekElementName()),
+ ParseMessages.invalidAttributeFormat(name, result,
getElementPath(), formatKey),
getLocation(),
null);
return result;
}
- private String getAttribute(String name)
+ private boolean validateFormat(String input, String pattern)
{
- return (String) _attributes.get(name);
- }
+ if (_compiler == null)
+ {
+ _compiler = new Perl5Compiler();
+ _matcher = new Perl5Matcher();
+ _compiledPatterns = new HashMap();
+ }
- private String getAttribute(String name, String defaultValue)
- {
- String result = (String) _attributes.get(name);
+ Pattern compiled = (Pattern) _compiledPatterns.get(pattern);
+ if (compiled == null)
+ {
- if (result == null)
- result = defaultValue;
+ try
+ {
+ compiled = _compiler.compile(pattern);
+ }
+ catch (MalformedPatternException ex)
+ {
+ throw new ApplicationRuntimeException(ex);
+ }
- return result;
+ _compiledPatterns.put(pattern, compiled);
+ }
+
+ return _matcher.matches(input, compiled);
}
private boolean getBooleanAttribute(String name, boolean defaultValue)
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]