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.info;
018
019import org.apache.wicket.util.lang.Args;
020import org.apache.wicket.util.string.Strings;
021
022/**
023 * Encapsulates both page and component info. Rendered in form of
024 * <pageInfo>-<componentInfo>
025 * 
026 * @author Matej Knopp
027 */
028public class PageComponentInfo
029{
030        private static final char SEPARATOR = '-';
031
032        private final PageInfo pageInfo;
033
034        private final ComponentInfo componentInfo;
035
036        /**
037         * Construct.
038         *
039         * @param pageInfo
040         * @param componentInfo
041         */
042        public PageComponentInfo(final PageInfo pageInfo, final ComponentInfo componentInfo)
043        {
044                Args.notNull(pageInfo, "pageInfo");
045
046                this.pageInfo = pageInfo;
047                this.componentInfo = componentInfo;
048        }
049
050        /**
051         * @return page info instance
052         */
053        public PageInfo getPageInfo()
054        {
055                return pageInfo;
056        }
057
058        /**
059         * @return component info instance or <code>null</code>
060         */
061        public ComponentInfo getComponentInfo()
062        {
063                return componentInfo;
064        }
065
066        /**
067         * @see java.lang.Object#toString()
068         */
069        @Override
070        public String toString()
071        {
072                String result = pageInfo.toString();
073                if (componentInfo != null)
074                {
075                        result = result + SEPARATOR + componentInfo;
076                }
077
078                return result;
079        }
080
081        /**
082         * Parses the given string
083         *
084         * @param s
085         * @return {@link PageComponentInfo} or <code>null</code> if the string is not in valid format.
086         */
087        public static PageComponentInfo parse(final String s)
088        {
089                if (Strings.isEmpty(s))
090                {
091                        return null;
092                }
093
094                final PageInfo pageInfo;
095                final ComponentInfo componentInfo;
096
097                int i = s.indexOf(SEPARATOR);
098                if (i == -1)
099                {
100                        pageInfo = PageInfo.parse(s);
101                        componentInfo = null;
102                }
103                else
104                {
105                        pageInfo = PageInfo.parse(s.substring(0, i));
106                        componentInfo = ComponentInfo.parse(s.substring(i + 1));
107                }
108
109                if (pageInfo == null)
110                {
111                        return null;
112                }
113
114                return new PageComponentInfo(pageInfo, componentInfo);
115        }
116}