UrlPattern.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms,
  6.  * with or without modification, are permitted provided
  7.  * that the following conditions are met:
  8.  *
  9.  * 1) Redistributions of source code must retain the above
  10.  * copyright notice, this list of conditions and the
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the
  13.  * above copyright notice, this list of conditions and
  14.  * the following disclaimer in the documentation and/or
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor
  17.  * the names of its contributors may be used to endorse
  18.  * or promote products derived from this software without
  19.  * specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  *
  39.  *
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum;
  44. /**
  45.  * URL Patterns keeper.
  46.  * Represents a single URL pattern. Each pattern is composed
  47.  * by a name, the pattern itself, the pattern's size and the
  48.  * splited variables. <br><br>
  49.  *
  50.  * The pattern is expected in the form <i>var1, var2, varN</i>, in the
  51.  * correct order. This means that if <i>var1</i> comes first, it <b>must</b>
  52.  * come first in the URL. The same is valid to others.<br><br>
  53.  *
  54.  * Please note that "first" here is "first" after regular URL, which is
  55.  * composed by server and servlet name, in the most simple case.<br><br>
  56.  *
  57.  * <b>Example:</b><br>
  58.  *
  59.  * URL: <i>http://localhost:8080/webappName/someDir/myServlet/news/view/3.page<i>.
  60.  * <br>
  61.  * In this case, <i>http://localhost:8080/webappName/someDir/myServlet/</i> is the
  62.  * regular URL, the part that we don't care about. We only want the part
  63.  * <i>news/view/3.page</i> ( where .page is the servlet extension ).
  64.  * <br>For this URL, we could make the following pattern:<br><br>
  65.  *
  66.  * <i>news.view.1 = news_id</i><br><br>
  67.  *
  68.  * Here, <i>news.view.1</i> is the pattern's name, and <i>news_id</i> is
  69.  * the patterns itself. <br>
  70.  * Another example:<br><br>
  71.  *
  72.  * <i>news.view.2 = page, news_id</i><br><br>
  73.  *
  74.  * In this case we have a new var called <i>page</i>, that represents the page being seen.<br>
  75.  * Each entry is composed in the form:<br><br>
  76.  *
  77.  * <i>&lt;moduleName&gt;.&lt;actionName&gt;.&lt;numberOfParameters&gt; = &lt;var 1&gt;,&lt;var n&gt;</i>
  78.  * <br><br>
  79.  *
  80.  * Please note that module and action's name aren't pattern's composition, so
  81.  * don't put them there. The system will consider that the pattern only contains
  82.  * the variables diferent to each request ( e.g, id's ). If the pattern you're
  83.  * constructing doesn't have any variable, just leave it blank, like<br><br>
  84.  *
  85.  * <i>myModule.myAction.0 = </i><br><br>
  86.  *
  87.  * @author Rafael Steil
  88.  * @version $Id: UrlPattern.java,v 1.1 2006/08/23 02:13:49 rafaelsteil Exp $
  89.  */
  90. public class UrlPattern
  91. {
  92.     private String name;
  93.     private String value;
  94.     private int size;
  95.     private String[] vars;
  96.     public UrlPattern(String name, String value)
  97.     {
  98.         this.name = name;
  99.         this.value = value;
  100.         this.processPattern();
  101.     }
  102.     private void processPattern()
  103.     {
  104.         String[] p = this.value.split(",");
  105.         this.vars = new String[p.length];
  106.         this.size = ((((p[0]).trim()).equals("")) ? 0 : p.length);
  107.         for (int i = 0; i < this.size; i++) {
  108.             this.vars[i] = (p[i]).trim();
  109.         }
  110.     }
  111.     /**
  112.      * Gets the pattern name
  113.      *
  114.      * @return String with the pattern name
  115.      */
  116.     public String getName()
  117.     {
  118.         return this.name;
  119.     }
  120.     /**
  121.      * Get pattern's total vars
  122.      *
  123.      * @return The total
  124.      */
  125.     public int getSize()
  126.     {
  127.         return this.size;
  128.     }
  129.     /**
  130.      * Gets the vars.
  131.      * The URL variables are in the correct order, which means
  132.      * that the first position always will be "something1", the
  133.      * second "something2" and so on. The system expects this
  134.      * order never changes from requisition to requisition.
  135.      *
  136.      * @return The vars
  137.      */
  138.     public String[] getVars()
  139.     {
  140.         return this.vars;
  141.     }
  142. }