You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2010/01/09 11:22:25 UTC

svn commit: r897411 - /commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java

Author: simonetripodi
Date: Sat Jan  9 10:22:25 2010
New Revision: 897411

URL: http://svn.apache.org/viewvc?rev=897411&view=rev
Log:
first checkin of FromAnnotationsRuleSet class

Added:
    commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java   (with props)

Added: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java
URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java?rev=897411&view=auto
==============================================================================
--- commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java (added)
+++ commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java Sat Jan  9 10:22:25 2010
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.digester.annotations;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.commons.digester.Digester;
+import org.apache.commons.digester.Rule;
+import org.apache.commons.digester.RuleSet;
+
+/**
+ * A {@link RuleSet} implementation that's able to inject {@link Rule}s created
+ * with the annotations analysis.
+ *
+ * @author Simone Tripodi (simonetripodi)
+ * @version $Id$
+ */
+public class FromAnnotationsRuleSet implements RuleSet {
+
+    /**
+     * The data structure that stores the patterns/{@link AnnotationRuleProvider}
+     * pairs.
+     */
+    private final Map<String, List<AnnotationRuleProvider<Annotation, AnnotatedElement, Rule>>> rules =
+        new LinkedHashMap<String, List<AnnotationRuleProvider<Annotation, AnnotatedElement, Rule>>>();
+
+    /**
+     * The namespace URI.
+     */
+    private String namespaceURI;
+
+    /**
+     * {@inheritDoc}
+     */
+    public void addRuleInstances(Digester digester) {
+        String pattern;
+        Rule rule;
+        for (Entry<String, List<AnnotationRuleProvider<Annotation, AnnotatedElement, Rule>>> entry : this.rules.entrySet()) {
+            pattern = entry.getKey();
+            for (AnnotationRuleProvider<Annotation, AnnotatedElement, Rule> provider : entry.getValue()) {
+                rule = provider.get();
+                rule.setNamespaceURI(this.namespaceURI);
+                digester.addRule(pattern, rule);
+            }
+        }
+    }
+
+    /**
+     * Register an {@link AnnotationRuleProvider} for a specific pattern.
+     *
+     * @param pattern
+     * @param ruleProvider
+     */
+    public void addRuleProvider(String pattern, AnnotationRuleProvider<Annotation, AnnotatedElement, Rule> ruleProvider) {
+        if (this.rules.containsKey(pattern)) {
+            this.rules.get(pattern).add(ruleProvider);
+        } else {
+            List<AnnotationRuleProvider<Annotation, AnnotatedElement, Rule>> rules =
+                new ArrayList<AnnotationRuleProvider<Annotation, AnnotatedElement, Rule>>();
+            rules.add(ruleProvider);
+            this.rules.put(pattern, rules);
+        }
+    }
+
+    /**
+     * Retrieves a specific instance of the {@link AnnotationRuleProvider} for
+     * the input pattern.
+     *
+     * @param <T> the {@link AnnotationRuleProvider} type
+     * @param pattern the input pattern
+     * @param providerClass the {@link AnnotationRuleProvider} class
+     * @return an {@link AnnotationRuleProvider} for the input pattern if found,
+     *         null otherwise.
+     */
+    public <T extends AnnotationRuleProvider<Annotation, AnnotatedElement, Rule>> T getProvider(String pattern, Class<T> providerClass) {
+        if (!this.rules.containsKey(pattern)) {
+            return null;
+        }
+
+        for (AnnotationRuleProvider<Annotation, AnnotatedElement, Rule> rule : this.rules.get(pattern)) {
+            if (providerClass.isInstance(rule)) {
+                return providerClass.cast(rule);
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Add created {@link AnnotationRuleProvider}s created in another analysis
+     * session.
+     *
+     * @param ruleSet the {@code RuleSet} created in another analysis session.
+     */
+    public void addRulesProviderFrom(final FromAnnotationsRuleSet ruleSet) {
+        this.rules.putAll(ruleSet.getRules());
+    }
+
+    /**
+     * Returns the data structure  the patterns/{@link AnnotationRuleProvider}
+     * pairs.
+     *
+     * @return the data structure  the patterns/{@link AnnotationRuleProvider}
+     *         pairs.
+     */
+    private Map<String, List<AnnotationRuleProvider<Annotation, AnnotatedElement, Rule>>> getRules() {
+        return this.rules;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getNamespaceURI() {
+        return this.namespaceURI;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return this.rules.toString();
+    }
+
+}

Propchange: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain