001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.hivemind.schema.rules;
016    
017    import org.apache.commons.logging.Log;
018    import org.apache.commons.logging.LogFactory;
019    import org.apache.hivemind.Element;
020    import org.apache.hivemind.ErrorHandler;
021    import org.apache.hivemind.schema.SchemaProcessor;
022    import org.apache.hivemind.schema.Translator;
023    import org.apache.hivemind.util.PropertyUtils;
024    
025    /**
026     * Used to set a property of the top object on the stack to the value
027     * of the element's content.  Created from the <code>&lt;read-content&gt;</code>
028     * element.
029     * 
030     * <p>
031     * Note: an {@link org.apache.hivemind.Element}'s content is trimmed
032     * of leading and trailing whitespace as it is parsed and, additionally,
033     * will never be null (though it may be the empty string).
034     *
035     * @author Howard Lewis Ship
036     */
037    public class ReadContentRule extends BaseRule
038    {
039        private static final Log LOG = LogFactory.getLog(ReadContentRule.class);
040    
041        private String _propertyName;
042    
043        public void begin(SchemaProcessor processor, Element element)
044        {
045            String value = RuleUtils.processText(processor, element, element.getContent());
046    
047            try
048            {
049                Translator t = processor.getContentTranslator();
050    
051                Object target = processor.peek();
052    
053                Class propertyType = PropertyUtils.getPropertyType(target, _propertyName);
054    
055                Object finalValue =
056                    t.translate(
057                        processor.getContributingModule(),
058                        propertyType,
059                        value,
060                        element.getLocation());
061    
062                PropertyUtils.write(target, _propertyName, finalValue);
063            }
064            catch (Exception ex)
065            {
066                ErrorHandler eh = processor.getContributingModule().getErrorHandler();
067    
068                eh.error(
069                    LOG,
070                    RulesMessages.readContentFailure(processor, element, ex),
071                    element.getLocation(),
072                    ex);
073            }
074    
075        }
076    
077        public String getPropertyName()
078        {
079            return _propertyName;
080        }
081    
082        public void setPropertyName(String string)
083        {
084            _propertyName = string;
085        }
086    
087    }