KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > binding > JXPathBindingBuilderBase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.woody.binding;
17
18 import org.apache.avalon.framework.logger.LogEnabled;
19 import org.apache.avalon.framework.logger.Logger;
20 import org.apache.cocoon.woody.util.DomHelper;
21 import org.w3c.dom.Element JavaDoc;
22
23 /**
24  * Abstract base class enabling logging and supporting the intrepretation of
25  * common configuration settings on all specific implementations of
26  * {@link org.apache.cocoon.woody.binding.JXPathBindingBase}.
27  *
28  * Common supported configurations:
29  * <ul>
30  * <li>Attribute direction="load|save|both": {@link #getCommonAttributes(Element)}</li>
31  * </ul>
32  *
33  * @version CVS $Id: JXPathBindingBuilderBase.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public abstract class JXPathBindingBuilderBase implements LogEnabled {
36
37     private Logger logger;
38
39     /**
40      * Receives the Avalon logger to use.
41      */

42     public void enableLogging(Logger logger) {
43         this.logger = logger;
44         if (logger.isDebugEnabled()) {
45             logger.debug("JXPathBindingBuilderBase got logger...");
46         }
47     }
48
49     /**
50      * Makes the logger available to the subclasses.
51      * @return Logger
52      */

53     protected Logger getLogger() {
54         return this.logger;
55     }
56
57     /**
58      * Builds a configured binding object based on the configuration as
59      * described in the bindingElement. The BuilderMap can be used to
60      * find appropriate builders for possible subBinders.
61      *
62      * @param bindingElm
63      * @param assistant
64      * @return JXPathBindingBase
65      */

66     public abstract JXPathBindingBase buildBinding(
67         Element JavaDoc bindingElm,
68         JXPathBindingManager.Assistant assistant) throws BindingException;
69
70     /**
71      * Helper method for interpreting the common attributes which are supported
72      * on each of the Bindings. These are
73      * <br>
74      * <code>@direction</code> can hold one of the following values:
75      * <ol><li><code>'load'</code>: This binding will only load.</li>
76      * <li><code>'save'</code>: This binding will only save.</li>
77      * <li><code>'both'</code>: This binding will perform both operations.</li>
78      * </ol>
79      * <br>
80      * <code>@lenient</code> can either be:
81      * <ol><li><code>'true'</code>: This binding will set the jxpath context to
82      * be lenient towards the usage of inexisting paths on the back-end model.</li>
83      * <li><code>'false'</code>: This binding will set the jxpath context to be
84      * strict and throwing exceptions for the usage of inexisting paths on the
85      * back-end model.</li>
86      * <li><code>(unset)</code>: This binding will not change the leniency behaviour
87      * on the jxpath this binding receives from his parent binding.</li>
88      * </ol>
89      * @param bindingElm
90      * @return an instance of CommonAttributes
91      * @throws BindingException
92      */

93      static CommonAttributes getCommonAttributes(Element JavaDoc bindingElm) throws BindingException {
94         try {
95             //TODO: should we eventually remove this?
96
//throw an error if people are still using the old-style @read-only or @readonly
97
if (DomHelper.getAttributeAsBoolean(bindingElm, "readonly", false)) {
98                 throw new BindingException("Error in binding file " + DomHelper.getLocation(bindingElm)
99                         + "\nThe usage of the attribute @readonly has been deprecated in favour of @direction.");
100             }
101             if (DomHelper.getAttributeAsBoolean(bindingElm, "read-only", false)) {
102                 throw new BindingException("Error in binding file " + DomHelper.getLocation(bindingElm)
103                         + "\nThe usage of the attribute @read-only has been deprecated in favour of @direction.");
104             }
105
106             String JavaDoc direction = DomHelper.getAttribute(bindingElm, "direction", "both");
107
108             String JavaDoc leniency = DomHelper.getAttribute(bindingElm, "lenient", null);
109
110             return new CommonAttributes(direction, leniency);
111         } catch (BindingException e) {
112             throw e;
113         } catch (Exception JavaDoc e) {
114             throw new BindingException("Error building binding defined at " + DomHelper.getLocation(bindingElm), e);
115         }
116      }
117
118      /**
119       * CommonAttributes is a simple helper class for holding the distinct data
120       * member fields indicating the activity of the sepearate load and save
121       * actions of a given binding.
122       */

123      static class CommonAttributes{
124         /**
125          * Flag which controls whether a binding is active during loading.
126          */

127         final boolean loadEnabled;
128         /**
129          * Flag which controls whether a binding is active during saving.
130          */

131         final boolean saveEnabled;
132         final Boolean JavaDoc leniency;
133
134         final static CommonAttributes DEFAULT = new CommonAttributes(true, true, null);
135
136         CommonAttributes(String JavaDoc direction, String JavaDoc leniency){
137             this(isLoadEnabled(direction), isSaveEnabled(direction), decideLeniency(leniency));
138         }
139
140         CommonAttributes(boolean loadEnabled, boolean saveEnabled, Boolean JavaDoc leniency){
141             this.loadEnabled = loadEnabled;
142             this.saveEnabled = saveEnabled;
143             this.leniency = leniency;
144         }
145
146         /**
147          * Interpretes the value of the direction attribute into activity of the load action.
148          * @param direction
149          * @return true if direction is either set to "both" or "load"
150          */

151         private static boolean isLoadEnabled(String JavaDoc direction) {
152             return "both".equals(direction) || "load".equals(direction);
153         }
154
155         /**
156          * Interpretes the value of the direction attribute into activity of the save action.
157          * @param direction value of the @direction attribute
158          * @return true if direction is either set to "both" or "save"
159          */

160         private static boolean isSaveEnabled(String JavaDoc direction) {
161             return "both".equals(direction) || "save".equals(direction);
162         }
163
164
165         /**
166          * Interpretes the value of the lenient attribute into a Boolean object
167          * allowing three-state logic (true/false/unset)
168          * @param leniency value of the @lenient attribute
169          * @return null if the leniency parameter is String, otherwise the
170          */

171         private static Boolean JavaDoc decideLeniency(String JavaDoc leniency) {
172             if (leniency == null) {
173                 return null;
174             }
175             return new Boolean JavaDoc(leniency);
176         }
177     }
178 }
179
Popular Tags