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                return parameters.isEmpty() ? null : parameters;
059        }
060
061        @Override
062        public Url encodePageParameters(final PageParameters pageParameters)
063        {
064                Url url = new Url();
065
066                if (pageParameters != null)
067                {
068                        for (int i = 0; i < pageParameters.getIndexedCount(); ++i)
069                        {
070                                url.getSegments().add(pageParameters.get(i).toString());
071                        }
072
073                        for (PageParameters.NamedPair pair : pageParameters.getAllNamed())
074                        {
075                                QueryParameter param = new QueryParameter(pair.getKey(), pair.getValue());
076                                url.getQueryParameters().add(param);
077                        }
078                }
079
080                return url;
081        }
082}