You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Jon Tirsén <jo...@yahoo.com> on 2002/11/15 15:11:59 UTC
[attributes][PATCH]
- Added Attribute-interface to start converge towards
XRAI.
- Added usecases to documentation.
- Added Ara Abrahamson (from XDoclet-team) to
team-members.
- Added link to XDoclet.
_____________________________________________________
Gratis e-mail resten av livet på www.yahoo.se/mail
Busenkelt!
Re: [attributes][PATCH]
Posted by James Strachan <ja...@yahoo.co.uk>.
Patch applied, many thanks Jon.
James
-------
http://radio.weblogs.com/0112098/
----- Original Message -----
From: "Jon Tirsén" <jo...@yahoo.com>
To: "Jakarta Commons Developers List" <co...@jakarta.apache.org>
Sent: Friday, November 15, 2002 2:11 PM
Subject: [attributes][PATCH]
> - Added Attribute-interface to start converge towards
> XRAI.
> - Added usecases to documentation.
> - Added Ara Abrahamson (from XDoclet-team) to
> team-members.
> - Added link to XDoclet.
>
> _____________________________________________________
> Gratis e-mail resten av livet på www.yahoo.se/mail
> Busenkelt!
----------------------------------------------------------------------------
----
> ? commons-attributes.ipr
> ? commons-attributes.iws
> ? patchfile.txt
> ? src/java/org/apache/commons/attributes/Attribute.java
> ? src/java/org/apache/commons/attributes/impl/DefaultAttribute.java
> ? xdocs/usecases.xml
> Index: project.xml
> ===================================================================
> RCS file: /home/cvspublic/jakarta-commons-sandbox/attributes/project.xml,v
> retrieving revision 1.2
> diff -u -r1.2 project.xml
> --- project.xml 14 Nov 2002 12:50:09 -0000 1.2
> +++ project.xml 15 Nov 2002 14:07:22 -0000
> @@ -63,7 +63,7 @@
> <name>Jon Tirsen</name>
> <id>tirsen</id>
> <email>tirsen@users.sourceforge.net</email>
> - <organization></organization>
> + <organization>Lecando AB</organization>
> </developer>
>
> <developer>
> @@ -71,6 +71,13 @@
> <id>jstrachan</id>
> <email>jstrachan@apache.org</email>
> <organization>SpiritSoft, Inc.</organization>
> + </developer>
> +
> + <developer>
> + <name>Ara Abrahamian</name>
> + <id>ara</id>
> + <email>ara_e_w@yahoo.com</email>
> + <organization></organization>
> </developer>
> </developers>
>
> Index: src/java/org/apache/commons/attributes/AttributeFinder.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/attributes/src/java/org/apache/commo
ns/attributes/AttributeFinder.java,v
> retrieving revision 1.1
> diff -u -r1.1 AttributeFinder.java
> --- src/java/org/apache/commons/attributes/AttributeFinder.java 14 Nov
2002 08:09:56 -0000 1.1
> +++ src/java/org/apache/commons/attributes/AttributeFinder.java 15 Nov
2002 14:07:22 -0000
> @@ -74,9 +74,9 @@
> */
> public interface AttributeFinder {
>
> - String getAttribute(Class aClass, String attribute);
> + Attribute getAttribute(Class aClass, String attribute);
>
> - String getAttribute(Method method, String attribute);
> + Attribute getAttribute(Method method, String attribute);
>
> - String getAttribute(Field field, String attribute);
> + Attribute getAttribute(Field field, String attribute);
> }
> Index: src/java/org/apache/commons/attributes/Attributes.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/attributes/src/java/org/apache/commo
ns/attributes/Attributes.java,v
> retrieving revision 1.1
> diff -u -r1.1 Attributes.java
> --- src/java/org/apache/commons/attributes/Attributes.java 14 Nov 2002
08:09:56 -0000 1.1
> +++ src/java/org/apache/commons/attributes/Attributes.java 15 Nov 2002
14:07:22 -0000
> @@ -114,16 +114,31 @@
> Attributes.finder = finder;
> }
>
> - public static String getAttribute(Class aClass, String attribute) {
> + public static Attribute getAttribute(Class aClass, String attribute) {
> return getAttributeFinder().getAttribute(aClass, attribute);
> }
>
> - public static String getAttribute(Method method, String attribute) {
> + public static Attribute getAttribute(Method method, String attribute) {
> return getAttributeFinder().getAttribute(method, attribute);
> }
>
> - public static String getAttribute(Field field, String attribute) {
> + public static Attribute getAttribute(Field field, String attribute) {
> return getAttributeFinder().getAttribute(field, attribute);
> + }
> +
> + public static String getString(Class aClass, String attributeName) {
> + Attribute attribute = getAttribute(aClass, attributeName);
> + return attribute == null ? null : attribute.getValue();
> + }
> +
> + public static String getString(Method method, String attributeName) {
> + Attribute attribute = getAttribute(method, attributeName);
> + return attribute == null ? null : attribute.getValue();
> + }
> +
> + public static String getString(Field field, String attributeName) {
> + Attribute attribute = getAttribute(field, attributeName);
> + return attribute == null ? null : attribute.getValue();
> }
>
> public static boolean hasAttribute(Class aClass, String attribute) {
> Index:
src/java/org/apache/commons/attributes/impl/DefaultAttributeFinder.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/attributes/src/java/org/apache/commo
ns/attributes/impl/DefaultAttributeFinder.java,v
> retrieving revision 1.1
> diff -u -r1.1 DefaultAttributeFinder.java
> ---
src/java/org/apache/commons/attributes/impl/DefaultAttributeFinder.java 14
Nov 2002 08:09:55 -0000 1.1
> +++
src/java/org/apache/commons/attributes/impl/DefaultAttributeFinder.java 15
Nov 2002 14:07:22 -0000
> @@ -63,6 +63,7 @@
>
> import org.apache.commons.attributes.AttributesException;
> import org.apache.commons.attributes.AttributeFinder;
> +import org.apache.commons.attributes.Attribute;
>
> import java.io.IOException;
> import java.io.InputStream;
> @@ -82,24 +83,25 @@
>
> private static List searchPaths = new ArrayList();
>
> - private Map propertiesCache = new HashMap();
> + private Map attributesCache = new HashMap();
> + public static final char SEPARATOR = '_';
>
> - public String getAttribute(Class aClass, String attribute) {
> - return getProperty(aClass, attributeName(aClass, attribute));
> + public Attribute getAttribute(Class aClass, String attribute) {
> + return getAttributeByKey(aClass, attributeName(aClass, attribute));
> }
>
> private String attributeName(Class aClass, String attribute) {
> - return "class." + attribute;
> + return "class" + SEPARATOR + attribute;
> }
>
> - private String getProperty(Class aClass, String key) {
> - Properties properties = getProperties(aClass);
> - return properties.getProperty(key);
> + private Attribute getAttributeByKey(Class aClass, String key) {
> + Map attributes = getAttributes(aClass);
> + return (Attribute) attributes.get(key);
> }
>
> - private Properties getProperties(Class aClass) {
> - Properties properties = (Properties) propertiesCache.get(aClass);
> - if (properties == null) {
> + private Map getAttributes(Class aClass) {
> + Map attributes = (Map) attributesCache.get(aClass);
> + if (attributes == null) {
> InputStream inputStream = null;
> try {
> String className = aClass.getName();
> @@ -127,10 +129,19 @@
> }
> }
>
> + Properties properties;
> if (inputStream != null) {
> - properties = new Properties();
> - properties.load(inputStream);
> - propertiesCache.put(aClass, properties);
> + properties = new Properties();
> + properties.load(inputStream);
> + attributes = new HashMap();
> + for (Iterator iterator =
properties.entrySet().iterator(); iterator.hasNext();) {
> + Map.Entry entry = (Map.Entry) iterator.next();
> + String property = (String) entry.getKey();
> + String value = (String) entry.getValue();
> + String name =
property.substring(property.indexOf(SEPARATOR) + 1);
> + attributes.put(property, new
DefaultAttribute(name, value));
> + }
> + attributesCache.put(aClass, attributes);
> } else {
> throw new AttributesException(
> "Could not find attributes for " + aClass);
> @@ -147,12 +158,12 @@
> }
> }
> }
> - return properties;
> + return attributes;
> }
>
> - public String getAttribute(Method method, String attribute) {
> + public Attribute getAttribute(Method method, String attribute) {
> String name = attributeName(method, attribute);
> - return getProperty(method.getDeclaringClass(), name);
> + return getAttributeByKey(method.getDeclaringClass(), name);
> }
>
> private String attributeName(Method method, String attribute) {
> @@ -168,20 +179,20 @@
> }
> }
> stringBuffer.append(')');
> - stringBuffer.append('.');
> + stringBuffer.append(SEPARATOR);
> stringBuffer.append(attribute);
> String name = stringBuffer.toString();
> return name;
> }
>
> - public String getAttribute(Field field, String attribute) {
> - return getProperty(
> + public Attribute getAttribute(Field field, String attribute) {
> + return getAttributeByKey(
> field.getDeclaringClass(),
> attributeName(field, attribute));
> }
>
> private String attributeName(Field field, String attribute) {
> - return field.getName() + '.' + attribute;
> + return field.getName() + SEPARATOR + attribute;
> }
>
> public static void addSearchPath(URL searchPath) {
> Index: src/java/org/apache/commons/attributes/task/AttributesCompiler.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/attributes/src/java/org/apache/commo
ns/attributes/task/AttributesCompiler.java,v
> retrieving revision 1.1
> diff -u -r1.1 AttributesCompiler.java
> --- src/java/org/apache/commons/attributes/task/AttributesCompiler.java 14
Nov 2002 08:09:56 -0000 1.1
> +++ src/java/org/apache/commons/attributes/task/AttributesCompiler.java 15
Nov 2002 14:07:22 -0000
> @@ -72,6 +72,7 @@
>
> import org.apache.tools.ant.Task;
> import org.apache.tools.ant.BuildException;
> +import org.apache.commons.attributes.impl.DefaultAttributeFinder;
>
> /**
> * <p><code>AttributesCompiler</code> is an Ant Task which
> @@ -178,7 +179,7 @@
> Properties properties) {
> for (int i = 0; i < tags.length; i++) {
> DocletTag tag = tags[i];
> - properties.put(prefix + '.' + tag.getName(), tag.getValue());
> + properties.put(prefix + DefaultAttributeFinder.SEPARATOR +
tag.getName(), tag.getValue());
> }
> }
>
> Index: src/test/org/apache/commons/attributes/AttributesTest.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/attributes/src/test/org/apache/commo
ns/attributes/AttributesTest.java,v
> retrieving revision 1.1
> diff -u -r1.1 AttributesTest.java
> --- src/test/org/apache/commons/attributes/AttributesTest.java 14 Nov 2002
08:09:56 -0000 1.1
> +++ src/test/org/apache/commons/attributes/AttributesTest.java 15 Nov 2002
14:07:22 -0000
> @@ -130,17 +130,17 @@
> assertSame(expectField, actualField);
> }
>
> - public String getAttribute(Class aClass, String attribute) {
> + public Attribute getAttribute(Class aClass, String attribute) {
> actualClass = aClass;
> return null;
> }
>
> - public String getAttribute(Method method, String attribute) {
> + public Attribute getAttribute(Method method, String attribute) {
> actualMethod = method;
> return null;
> }
>
> - public String getAttribute(Field field, String attribute) {
> + public Attribute getAttribute(Field field, String attribute) {
> actualField = field;
> return null;
> }
> @@ -163,9 +163,9 @@
> testAttributesLoader.expectClass(testClass);
> testAttributesLoader.expectMethod(testMethod);
> testAttributesLoader.expectField(testField);
> - Attributes.getAttribute(testClass, "testAttribute");
> - Attributes.getAttribute(testMethod, "testAttribute");
> - Attributes.getAttribute(testField, "testAttribute");
> + Attributes.getString(testClass, "testAttribute");
> + Attributes.getString(testMethod, "testAttribute");
> + Attributes.getString(testField, "testAttribute");
> testAttributesLoader.verify();
> } finally {
> // clear the pluggability for coming tests
> @@ -179,7 +179,7 @@
> // maybe this shouldn't actually fail like this in the future
> // but that's what happens now so I write a test for it
> try {
> - Attributes.getAttribute(Object.class, "someAttribute");
> + Attributes.getString(Object.class, "someAttribute");
> //fail("didn't fail when asking for the attributes of
unsupported class");
> } catch (AttributesException shouldHappen) {
> }
> @@ -194,7 +194,7 @@
>
> assertEquals(
> "classValue",
> - Attributes.getAttribute(
> + Attributes.getString(
> AttributesTestClass.class,
> "classAttribute"));
> assertTrue(
> @@ -207,12 +207,18 @@
> "stupidAttribute"));
> assertEquals(
> "fieldValue",
> - Attributes.getAttribute(testField, "fieldAttribute"));
> + Attributes.getString(testField, "fieldAttribute"));
> + assertEquals(
> + "fieldAttribute",
> + Attributes.getAttribute(testField, "fieldAttribute").getName());
> assertTrue(Attributes.hasAttribute(testField, "fieldAttribute"));
> assertFalse(Attributes.hasAttribute(testField, "stupidAttribute"));
> assertEquals(
> "methodValue",
> - Attributes.getAttribute(testMethod, "methodAttribute"));
> + Attributes.getString(testMethod, "methodAttribute"));
> + assertEquals(
> + "methodAttribute",
> + Attributes.getAttribute(testMethod, "methodAttribute").getName());
> assertTrue(Attributes.hasAttribute(testMethod, "methodAttribute"));
> assertFalse(Attributes.hasAttribute(testMethod, "stupidAttribute"));
> Method argMethod =
> @@ -221,8 +227,11 @@
> new Class[] { String.class, String.class });
> assertEquals(
> "argMethodValue",
> - Attributes.getAttribute(argMethod, "methodAttribute"));
> - assertTrue(Attributes.hasAttribute(argMethod, "methodAttribute"));
> + Attributes.getString(argMethod, "argMethodAttribute"));
> + assertEquals(
> + "argMethodAttribute",
> + Attributes.getAttribute(argMethod, "argMethodAttribute").getName());
> + assertTrue(Attributes.hasAttribute(argMethod, "argMethodAttribute"));
> assertFalse(Attributes.hasAttribute(argMethod, "stupidAttribute"));
> }
> }
> Index: src/test/org/apache/commons/attributes/AttributesTestClass.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/attributes/src/test/org/apache/commo
ns/attributes/AttributesTestClass.java,v
> retrieving revision 1.1
> diff -u -r1.1 AttributesTestClass.java
> --- src/test/org/apache/commons/attributes/AttributesTestClass.java 14 Nov
2002 08:09:56 -0000 1.1
> +++ src/test/org/apache/commons/attributes/AttributesTestClass.java 15 Nov
2002 14:07:22 -0000
> @@ -86,7 +86,7 @@
> }
>
> /**
> - * @methodAttribute argMethodValue
> + * @argMethodAttribute argMethodValue
> */
> public void method(String arg, String arg2) {
> }
> Index: xdocs/navigation.xml
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/attributes/xdocs/navigation.xml,v
> retrieving revision 1.1
> diff -u -r1.1 navigation.xml
> --- xdocs/navigation.xml 14 Nov 2002 08:10:01 -0000 1.1
> +++ xdocs/navigation.xml 15 Nov 2002 14:07:22 -0000
> @@ -5,12 +5,14 @@
>
> <body>
> <links>
> + <item name="XDoclet" href="http://xdoclet.sourceforge.net/"/>
> <item name="Nanning" href="http://nanning.sourceforge.net/"/>
> <item name="Maven"
href="http://jakarta.apache.org/turbine/maven/"/>
> <item name="QDox" href="http://qdox.sourceforge.net/"/>
> </links>
> <menu name="Commons Attributes">
> <item name="Usage" href="/usage.html"/>
> + <item name="Sample Usecases" href="/usecases.html"/>
> </menu>
> </body>
> </project>
> Index: xdocs/usage.xml
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/attributes/xdocs/usage.xml,v
> retrieving revision 1.1
> diff -u -r1.1 usage.xml
> --- xdocs/usage.xml 14 Nov 2002 08:10:00 -0000 1.1
> +++ xdocs/usage.xml 15 Nov 2002 14:07:22 -0000
> @@ -10,10 +10,26 @@
>
> <section name="Using runtime attributes">
> <p>
> - This is an example from the unit-tests:
> + Accessing runtime attributes is very simple.
> + </p>
> + <source><![CDATA[
> +Field field = AttributesTestClass.class.getDeclaredField("field");
> +String fieldAttribute = Attributes.getString(field, "fieldAttribute");
> +
> +Method method = AttributesTestClass.class.getMethod("method", null);
> +String methodAttribute = Attributes.getString(method, "methodAttribute");
> +
> +Method argMethod = AttributesTestClass.class.getMethod("method", new
Class[]{String.class});
> +String argMethodAttribute = Attributes.getString(argMethod,
"methodAttribute");
> + ]]></source>
> + </section>
> + <section name="Using the default implementation">
> + <p>
> + Commons-attributes ships with a default implementation
but you can also plug in your own. This is
> + how to specify attributes and compile them for the
default implementation.
> </p>
> <p>
> - The class (with the attributes):
> + You specify your attributes like JavaDoc-tags in the
source-code. Like this:
> </p>
> <source><![CDATA[
> /**
> @@ -42,29 +58,15 @@
> }
> ]]></source>
> <p>
> - The code that compiles and accesses these attributes:
> + You can use the compiler in Java or as an Ant-task.
(TODO: sample of ant-task.) An upcoming Maven-plugin
> + will also support this. This shows how to use the
compiler in Java.
> </p>
> <source><![CDATA[
> AttributesCompiler attributesCompiler = new AttributesCompiler();
> attributesCompiler.setSrc(new File("src" + File.separator + "test"));
> attributesCompiler.setDest(targetDir);
> attributesCompiler.execute();
> -
> -assertEquals("classValue",
Attributes.getAttribute(AttributesTestClass.class, "classAttribute"));
> -Field field = AttributesTestClass.class.getDeclaredField("field");
> -assertEquals("fieldValue", Attributes.getAttribute(field,
"fieldAttribute"));
> -Method method = AttributesTestClass.class.getMethod("method", null);
> -assertEquals("methodValue", Attributes.getAttribute(method,
"methodAttribute"));
> -Method argMethod = AttributesTestClass.class.getMethod("method", new
Class[]{String.class});
> -assertEquals("argMethodValue", Attributes.getAttribute(argMethod,
"methodAttribute"));
> ]]></source>
> - <p>
> - The attributes-compiler can also be used as an ant-task
(or better yet! inside
> - <a
> - href="http://jakarta.apache.org/maven">maven</a>).
This is left as an exercise
> - for the interested reader (yeah, yeah, I'm lazy...).
> - </p>
> -
> </section>
>
> </body>
>
>
----------------------------------------------------------------------------
----
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>