velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r545416 - in /velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic: AbstractLockConfig.java FormatConfig.java LocaleConfig.java
Date Fri, 08 Jun 2007 05:17:44 GMT
Author: nbubna
Date: Thu Jun  7 22:17:44 2007
New Revision: 545416

URL: http://svn.apache.org/viewvc?view=rev&rev=545416
Log:
pull common generic tool config stuff into reusable superclasses

Added:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/AbstractLockConfig.java
  (with props)
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/FormatConfig.java
  (with props)
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/LocaleConfig.java
  (with props)

Added: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/AbstractLockConfig.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/AbstractLockConfig.java?view=auto&rev=545416
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/AbstractLockConfig.java
(added)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/AbstractLockConfig.java
Thu Jun  7 22:17:44 2007
@@ -0,0 +1,65 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * 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.
+ */
+
+import java.util.Map;
+
+/**
+ * Implements common logic and constants for tools which by default
+ * lock down the public configure(Map) method, to keep the tool
+ * thread-safe in templates and most other common uses.
+ *
+ * @author Nathan Bubna
+ */
+public abstract class AbstractLockConfig
+{
+    /**
+     * The key used for specifying whether or not to prevent templates
+     * from reconfiguring this tool.  The default is true.
+     */
+    public static final String LOCK_CONFIG_KEY = "lock-config";
+
+    private boolean configLocked = false;
+
+    /**
+     * Looks for configuration values in the given params.
+     */
+    public void configure(Map params)
+    {
+        if (!configLocked)
+        {
+            ValueParser values = new ValueParser(params);
+            configure(values);
+
+            // by default, lock down this method after use
+            // to prevent templates from re-configuring this instance
+            configLocked = values.getBoolean(LOCK_CONFIG_KEY, true);
+        }
+    }
+
+    /**
+     * Does the actual configuration. This is protected, so
+     * subclasses may share the same ValueParser and call configure
+     * at any time, while preventing templates from doing so when 
+     * configure(Map) is locked.
+     */
+    protected abstract void configure(ValueParser values);
+
+}

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/AbstractLockConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/AbstractLockConfig.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Added: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/FormatConfig.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/FormatConfig.java?view=auto&rev=545416
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/FormatConfig.java
(added)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/FormatConfig.java
Thu Jun  7 22:17:44 2007
@@ -0,0 +1,77 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * 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.
+ */
+
+/**
+ * Implements common logic and constants for tools which allow their
+ * default format to be configured.
+ *
+ * @author Nathan Bubna
+ */
+public class FormatConfig extends LocaleConfig
+{
+    /**
+     * The default format to be used when none is specified.
+     */
+    public static final String DEFAULT_FORMAT = "default";
+
+    /**
+     * The key used for specifying a default format via tool configuration.
+     */
+    public static final String FORMAT_KEY = "format";
+
+    private String format = DEFAULT_FORMAT;
+
+    /**
+     * Does the actual configuration. This is protected, so
+     * subclasses may share the same ValueParser and call configure
+     * at any time, while preventing templates from doing so when 
+     * configure(Map) is locked.
+     */
+    protected void configure(ValueParser values)
+    {
+        super.configure(values);
+
+        String format = values.getString(FORMAT_KEY);
+        if (format != null)
+        {
+            setFormat(format);
+        }
+    }
+
+    /**
+     * This returns the configured default format for this tool.
+     *
+     * @return the default {@link String}
+     */
+    public String getFormat()
+    {
+        return this.format;
+    }
+
+    /**
+     * Sets the default format for this instance.
+     */
+    protected void setFormat(String format)
+    {
+        this.format = format;
+    }
+
+}

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/FormatConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/FormatConfig.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Added: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/LocaleConfig.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/LocaleConfig.java?view=auto&rev=545416
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/LocaleConfig.java
(added)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/LocaleConfig.java
Thu Jun  7 22:17:44 2007
@@ -0,0 +1,77 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * 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.
+ */
+
+import java.util.Locale;
+
+/**
+ * Implements common logic and constants for tools which allow their
+ * default {@link Locale} to be configured.
+ *
+ * @author Nathan Bubna
+ */
+public class LocaleConfig extends AbstractLockConfig
+{
+    /**
+     * The default {@link Locale} to be used when none is specified.
+     */
+    public static final Locale DEFAULT_LOCALE = Locale.getDefault();
+
+    /**
+     * The key used for specifying a default locale via tool configuration.
+     */
+    public static final String LOCALE_KEY = "locale";
+
+    private Locale locale = DEFAULT_LOCALE;
+
+    /**
+     * Does the actual configuration. This is protected, so
+     * subclasses may share the same ValueParser and call configure
+     * at any time, while preventing templates from doing so when 
+     * configure(Map) is locked.
+     */
+    protected void configure(ValueParser values)
+    {
+        Locale locale = values.getLocale(LOCALE_KEY);
+        if (locale != null)
+        {
+            setLocale(locale);
+        }
+    }
+
+    /**
+     * This returns the configured default {@link Locale} for this tool.
+     *
+     * @return the default {@link Locale}
+     */
+    public Locale getLocale()
+    {
+        return this.locale;
+    }
+
+    /**
+     * Sets the default locale for this instance.
+     */
+    protected void setLocale(Locale locale)
+    {
+        this.locale = locale;
+    }
+
+}

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/LocaleConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/LocaleConfig.java
------------------------------------------------------------------------------
    svn:keywords = Revision



Mime
View raw message