Coverage Report - org.seasar.cubby.admin.servlet.RoutingSection
 
Classes in this File Line Coverage Branch Coverage Complexity
RoutingSection
0%
0/78
0%
0/16
2.8
 
 1  
 /*
 2  
  * Copyright 2004-2010 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  
 
 17  
 package org.seasar.cubby.admin.servlet;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.PrintWriter;
 21  
 import java.util.Arrays;
 22  
 import java.util.HashMap;
 23  
 import java.util.Iterator;
 24  
 import java.util.Locale;
 25  
 import java.util.Map;
 26  
 import java.util.regex.Matcher;
 27  
 
 28  
 import javax.servlet.http.HttpServletRequest;
 29  
 import javax.servlet.http.HttpServletResponse;
 30  
 
 31  
 import org.seasar.cubby.routing.PathResolver;
 32  
 import org.seasar.cubby.routing.Routing;
 33  
 import org.seasar.cubby.spi.PathResolverProvider;
 34  
 import org.seasar.cubby.spi.ProviderFactory;
 35  
 
 36  
 class RoutingSection implements Section {
 37  
 
 38  
         private HttpServletRequest request;
 39  
 
 40  
         private PrintWriter out;
 41  
 
 42  
         private Locale locale;
 43  
 
 44  0
         private Iterator<String> rowClasses = new LoopingIterator<String>(Arrays
 45  
                         .asList(new String[] { "odd", "even" }));
 46  
 
 47  
         private Messages messages;
 48  
 
 49  
         public RoutingSection(HttpServletRequest request,
 50  0
                         HttpServletResponse response) throws IOException {
 51  0
                 this.request = request;
 52  0
                 this.out = response.getWriter();
 53  0
                 this.locale = request.getLocale();
 54  0
                 this.messages = new Messages(locale);
 55  0
         }
 56  
 
 57  
         public void print() {
 58  0
                 final String path = request.getParameter("path");
 59  
 
 60  0
                 out.println("<form name=\"pathSerachForm\">");
 61  
 
 62  0
                 out.print("<label for=\"path\">");
 63  0
                 out.print(messages.getString("lbl.path"));
 64  0
                 out.print("</label>");
 65  0
                 out.print("<input type=\"text\" name=\"path\" size=\"40\" value=\"");
 66  0
                 out.print(escapeHtml(path));
 67  0
                 out.println("\" />");
 68  
 
 69  0
                 out.print("<input type=\"submit\" value=\"");
 70  0
                 out.print(messages.getString("lbl.test"));
 71  0
                 out.println("\">");
 72  
 
 73  0
                 out.print("<input type=\"button\" value=\"");
 74  0
                 out.print(messages.getString("lbl.clear"));
 75  0
                 out
 76  
                                 .println("\" onclick=\"pathSerachForm.path.value='';pathSerachForm.submit();\">");
 77  
 
 78  0
                 out.println("</form>");
 79  
 
 80  0
                 out.println("<table>");
 81  0
                 out.println("<thead>");
 82  0
                 out.println(th(messages.getString("lbl.no")));
 83  0
                 out.println(th(messages.getString("lbl.regexp")));
 84  0
                 out.println(th(messages.getString("lbl.requestMethod")));
 85  0
                 out.println(th(messages.getString("lbl.actionMethod")));
 86  0
                 out.println(th(messages.getString("lbl.pathParams")));
 87  0
                 out.println(th(messages.getString("lbl.priority")));
 88  0
                 out.println("</thead>");
 89  0
                 out.println("<tbody>");
 90  
 
 91  0
                 final PathResolverProvider pathResolverProvider = ProviderFactory
 92  
                                 .get(PathResolverProvider.class);
 93  0
                 final PathResolver pathResolver = pathResolverProvider
 94  
                                 .getPathResolver();
 95  
 
 96  0
                 int no = 1;
 97  0
                 for (final Routing routing : pathResolver.getRoutings()) {
 98  
                         final boolean write;
 99  
                         final Map<String, String> pathParameters;
 100  0
                         if (path == null || path.length() == 0) {
 101  0
                                 write = true;
 102  0
                                 pathParameters = null;
 103  
                         } else {
 104  0
                                 final Matcher matcher = routing.getPattern().matcher(path);
 105  0
                                 if (matcher.matches()) {
 106  0
                                         write = true;
 107  0
                                         pathParameters = new HashMap<String, String>();
 108  0
                                         for (int i = 0; i < matcher.groupCount(); i++) {
 109  0
                                                 final String name = routing.getUriParameterNames().get(
 110  
                                                                 i);
 111  0
                                                 final String value = matcher.group(i + 1);
 112  0
                                                 pathParameters.put(name, value);
 113  
                                         }
 114  
                                 } else {
 115  0
                                         write = false;
 116  0
                                         pathParameters = null;
 117  
                                 }
 118  
                         }
 119  0
                         if (write) {
 120  0
                                 out.print("<tr class=\"");
 121  0
                                 out.print(rowClasses.next());
 122  0
                                 out.println("\">");
 123  0
                                 out.println(td(no));
 124  0
                                 out.println(td(routing.getPattern().pattern()));
 125  0
                                 out.println(td(routing.getRequestMethod()));
 126  0
                                 out.println(td(routing.getActionClass().getName() + "#"
 127  
                                                 + routing.getActionMethod().getName()));
 128  0
                                 if (pathParameters == null) {
 129  0
                                         out.println(td(routing.getUriParameterNames()));
 130  
                                 } else {
 131  0
                                         out.println(td(pathParameters));
 132  
                                 }
 133  0
                                 out.println(td(routing.getPriority()));
 134  0
                                 out.println("</tr>");
 135  
                         }
 136  0
                         no++;
 137  0
                 }
 138  0
                 out.println("</tbody>");
 139  0
                 out.println("</table>");
 140  0
         }
 141  
 
 142  
         private static String th(final Object body) {
 143  0
                 return "<th>" + String.valueOf(body) + "</th>";
 144  
         }
 145  
 
 146  
         private static String td(final Object body) {
 147  0
                 return "<td>" + String.valueOf(body) + "</td>";
 148  
         }
 149  
 
 150  
         /**
 151  
          * 指定された文字列をHTMLとしてエスケープします。
 152  
          * <p>
 153  
          * <table>
 154  
          * <thead>
 155  
          * <tr>
 156  
          * <th>変換前</th>
 157  
          * <th>変換後</th>
 158  
          * </tr>
 159  
          * </thead> <tbody>
 160  
          * <tr>
 161  
          * <td>&amp;</td>
 162  
          * <td>&amp;amp;</td>
 163  
          * </tr>
 164  
          * <tr>
 165  
          * <td>&lt;</td>
 166  
          * <td>&amp;lt;</td>
 167  
          * </tr>
 168  
          * <tr>
 169  
          * <td>&gt;</td>
 170  
          * <td>&amp;gt;</td>
 171  
          * </tr>
 172  
          * <tr>
 173  
          * <td>&quot;</td>
 174  
          * <td>&amp;quot;</td>
 175  
          * </tr>
 176  
          * <tr>
 177  
          * <td>&#39</td>
 178  
          * <td>&amp;#39</td>
 179  
          * </tr>
 180  
          * </tbody>
 181  
          * </table>
 182  
          * </p>
 183  
          * 
 184  
          * @param str
 185  
          * @return エスケープされた文字列
 186  
          */
 187  
         public static String escapeHtml(final Object str) {
 188  0
                 if (str == null) {
 189  0
                         return "";
 190  
                 }
 191  0
                 String text = str.toString();
 192  0
                 text = StringUtils.replace(text, "&", "&amp;");
 193  0
                 text = StringUtils.replace(text, "<", "&lt;");
 194  0
                 text = StringUtils.replace(text, ">", "&gt;");
 195  0
                 text = StringUtils.replace(text, "\"", "&quot;");
 196  0
                 text = StringUtils.replace(text, "'", "&#39;");
 197  0
                 return text;
 198  
         }
 199  
 
 200  
 }