001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.request.mapper.parameter;
018
019import org.apache.wicket.request.Url;
020import org.apache.wicket.request.Url.QueryParameter;
021import org.apache.wicket.util.string.Strings;
022
023/**
024 * Simple encoder with direct indexed/named parameters mapping.
025 * 
026 * @author Matej Knopp
027 */
028public class PageParametersEncoder implements IPageParametersEncoder
029{
030        /**
031         * Construct.
032         */
033        public PageParametersEncoder()
034        {
035        }
036
037        @Override
038        public PageParameters decodePageParameters(final Url url)
039        {
040                PageParameters parameters = new PageParameters();
041
042                int i = 0;
043                for (String s : url.getSegments())
044                {
045                        parameters.set(i, s);
046                        ++i;
047                }
048
049                for (QueryParameter p : url.getQueryParameters())
050                {
051                        String parameterName = p.getName();
052                        if (Strings.isEmpty(parameterName) == false)
053                        {
054                                parameters.add(parameterName, p.getValue(), INamedParameters.Type.QUERY_STRING);
055                        }
056                }
057                
058                parameters.setFragment(url.getFragment());
059
060                return parameters.isEmpty() ? null : parameters;
061        }
062
063        @Override
064        public Url encodePageParameters(final PageParameters pageParameters)
065        {
066                Url url = new Url();
067
068                if (pageParameters != null)
069                {
070                        for (int i = 0; i < pageParameters.getIndexedCount(); ++i)
071                        {
072                                url.getSegments().add(pageParameters.get(i).toString());
073                        }
074
075                        for (PageParameters.NamedPair pair : pageParameters.getAllNamed())
076                        {
077                                QueryParameter param = new QueryParameter(pair.getKey(), pair.getValue());
078                                url.getQueryParameters().add(param);
079                        }
080                        
081                        url.setFragment(pageParameters.getFragment());
082                }
083
084                return url;
085        }
086}