Coverage Report - org.seasar.teeda.core.render.html.support.UrlString
 
Classes in this File Line Coverage Branch Coverage Complexity
UrlString
100%
42/42
95%
19/20
3.5
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.render.html.support;
 17  
 
 18  
 import java.util.Collections;
 19  
 import java.util.Iterator;
 20  
 import java.util.LinkedHashMap;
 21  
 import java.util.Map;
 22  
 import java.util.Set;
 23  
 
 24  
 import org.seasar.framework.util.StringUtil;
 25  
 
 26  
 /**
 27  
  * @author manhole
 28  
  */
 29  19
 public class UrlString {
 30  
 
 31  19
     private Map parameters = new LinkedHashMap();
 32  
 
 33  
     private String path;
 34  
 
 35  
     private String queryString;
 36  
 
 37  
     public void parse(String url) {
 38  19
         if (StringUtil.contains(url, '?')) {
 39  13
             int questionPos = url.indexOf('?');
 40  13
             path = url.substring(0, questionPos);
 41  13
             queryString = url.substring(questionPos + 1);
 42  
         } else {
 43  6
             path = url;
 44  
         }
 45  19
         String[] params = StringUtil.split(queryString, "&");
 46  47
         for (int i = 0; i < params.length; i++) {
 47  28
             final String param = params[i];
 48  28
             if (StringUtil.contains(param, '=')) {
 49  28
                 final String key = param.substring(0, param.indexOf('='));
 50  28
                 UrlParameter urlParameter = (UrlParameter) parameters.get(key);
 51  28
                 if (urlParameter == null) {
 52  20
                     urlParameter = new UrlParameter();
 53  20
                     urlParameter.setKey(key);
 54  20
                     parameters.put(urlParameter.getKey(), urlParameter);
 55  
                 }
 56  28
                 urlParameter.addValue(param.substring(param.indexOf('=') + 1));
 57  
             }
 58  
         }
 59  19
     }
 60  
 
 61  
     public String getPath() {
 62  18
         return path;
 63  
     }
 64  
 
 65  
     public String getParameter(String key) {
 66  4
         UrlParameter param = (UrlParameter) parameters.get(key);
 67  4
         if (param == null) {
 68  1
             return null;
 69  
         }
 70  3
         return param.getValue();
 71  
     }
 72  
 
 73  
     public String[] getParameters(String key) {
 74  14
         UrlParameter param = (UrlParameter) parameters.get(key);
 75  14
         if (param == null) {
 76  1
             return null;
 77  
         }
 78  13
         return param.getValues();
 79  
     }
 80  
 
 81  
     public boolean isIdentical(UrlString otherUrl) {
 82  8
         if (!getPath().equals(otherUrl.getPath())) {
 83  2
             return false;
 84  
         }
 85  6
         Set myParamNames = getParameterNames();
 86  6
         Set otherParamNames = otherUrl.getParameterNames();
 87  6
         if (myParamNames.size() != otherParamNames.size()) {
 88  1
             return false;
 89  
         }
 90  5
         for (Iterator it = myParamNames.iterator(); it.hasNext();) {
 91  6
             String key = (String) it.next();
 92  6
             String[] myValues = getParameters(key);
 93  6
             String[] otherValues = otherUrl.getParameters(key);
 94  6
             if (!org.seasar.framework.util.ArrayUtil.equalsIgnoreSequence(
 95  
                     myValues, otherValues)) {
 96  2
                 return false;
 97  
             }
 98  
         }
 99  3
         return true;
 100  
     }
 101  
 
 102  
     public Set getParameterNames() {
 103  13
         return Collections.unmodifiableSet(parameters.keySet());
 104  
     }
 105  
 
 106  
 }