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                StringBuilder result = new StringBuilder();
073                if (pageInfo != null)
074                {
075                        result.append(pageInfo.toString());
076                }
077                if (componentInfo != null)
078                {
079                        result.append(SEPARATOR);
080                        result.append(componentInfo);
081                }
082
083                return result.toString();
084        }
085
086        /**
087         * Parses the given string
088         * 
089         * @param s
090         * @return {@link PageComponentInfo} or <code>null</code> if the string is not in valid format.
091         */
092        public static PageComponentInfo parse(final String s)
093        {
094                if (Strings.isEmpty(s))
095                {
096                        return null;
097                }
098
099                final PageInfo pageInfo;
100                final ComponentInfo componentInfo;
101
102                int i = s.indexOf(SEPARATOR);
103                if (i == -1)
104
105                {
106                        pageInfo = PageInfo.parse(s);
107                        componentInfo = null;
108                }
109                else
110                {
111                        pageInfo = PageInfo.parse(s.substring(0, i));
112                        componentInfo = ComponentInfo.parse(s.substring(i + 1));
113                }
114
115                if (pageInfo == null)
116                {
117                        return null;
118                }
119
120                return new PageComponentInfo(pageInfo, componentInfo);
121        }
122}