001    // Copyright 2008, 2010 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.tapestry5.internal.services;
016    
017    import java.util.Map;
018    
019    import org.apache.tapestry5.dom.Element;
020    import org.apache.tapestry5.services.HiddenFieldLocationRules;
021    import org.apache.tapestry5.services.RelativeElementPosition;
022    
023    public class HiddenFieldLocationRulesImpl implements HiddenFieldLocationRules
024    {
025        private final Map<String, RelativeElementPosition> configuration;
026    
027        public HiddenFieldLocationRulesImpl(Map<String, RelativeElementPosition> configuration)
028        {
029            this.configuration = configuration;
030        }
031    
032        private boolean match(Element element, RelativeElementPosition position)
033        {
034            assert element != null;
035            String key = element.getName();
036    
037            RelativeElementPosition actual = configuration.get(key);
038    
039            if (actual == null)
040                return false;
041    
042            return actual == position;
043        }
044    
045        public boolean placeHiddenFieldInside(Element element)
046        {
047            return match(element, RelativeElementPosition.INSIDE);
048        }
049    
050        public boolean placeHiddenFieldAfter(Element element)
051        {
052            return match(element, RelativeElementPosition.AFTER);
053        }
054    }