View Javadoc

1   package fr.in2p3.jsaga.engine.session;
2   
3   /* ***************************************************
4    * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
5    * ***             http://cc.in2p3.fr/             ***
6    * ***************************************************
7    * File:   BaseUrlItem
8    * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
9    * ***************************************************
10   * Description:                                      */
11  
12  /**
13   *
14   */
15  public abstract class BaseUrlItem {
16      protected static final String TOKEN = "\\p{Alnum}-_";
17  
18      private String m_value;
19      private Mode m_mode;
20  
21      public BaseUrlItem(String value, Object pre, Object post) {
22          if (value != null) {
23              m_value = value;
24              if (pre==null && post==null) {
25                  m_mode = Mode.EQUALS;
26              } else if (pre==null && post!=null) {
27                  m_mode = Mode.STARTS_WITH;
28              } else if (pre!=null && post==null) {
29                  m_mode = Mode.ENDS_WITH;
30              } else {
31                  m_mode = Mode.CONTAINS;
32              }
33          } else {
34              m_value = null;
35              m_mode = Mode.ANY;
36          }
37      }
38      public BaseUrlItem() {
39          m_value = null;
40          m_mode = Mode.ANY;
41      }
42  
43      public String getValue() {
44          return m_value;
45      }
46  
47      public Mode getMode() {
48          return m_mode;
49      }
50  
51      public String toString() {
52          String separator = this.getSimpleSeparator();
53          final String any = "*";
54          if (m_value != null) {
55              String expr = separator+this.toExpression(m_value, any);
56              if (this.isRequired(true)) {
57                  return expr;
58              } else {
59                  return "["+expr+"]";
60              }
61          } else {
62              String expr = separator+any;
63              if (this.isRequired(false)) {
64                  return expr;
65              } else {
66                  return "";
67              }
68          }
69      }
70      public String toRegExp() {
71          String separator = this.getRegExpSeparator();
72          String any = "["+this.getAllowedChars()+"]*";
73          if (m_value != null) {
74              String expr = separator+this.toExpression(m_value.replaceAll("\\.", "\\\\."), any);
75              if (this.isRequired(true)) {
76                  return expr;
77              } else {
78                  return "("+expr+")?";
79              }
80          } else {
81              String expr = separator+any;
82              if (this.isRequired(false)) {
83                  return expr;
84              } else {
85                  return "("+expr+")?";
86              }
87          }
88      }
89      private String toExpression(String value, String any) {
90          switch (m_mode) {
91              case EQUALS:
92                  return value;
93              case STARTS_WITH:
94                  return value+any;
95              case ENDS_WITH:
96                  return any+value;
97              case CONTAINS:
98                  return any+value+any;
99              default:
100                 throw new RuntimeException("[INTERNAL ERROR] unexpected mode: "+m_mode);
101         }
102     }
103 
104     public String getNextRegExp() {
105         return "("+this.getRegExpSeparatorNext()+".*)?";
106     }
107 
108     public boolean conflictsWith(BaseUrlItem ref) {
109         if (Mode.ANY.equals(m_mode) || Mode.ANY.equals(ref.getMode())) {
110             return true;
111         } else {
112             String refValue = ref.getValue();
113             switch (m_mode) {
114                 case EQUALS:
115                     switch (ref.getMode()) {
116                         case EQUALS: return m_value.equals(refValue);
117                         case STARTS_WITH: return m_value.startsWith(refValue);
118                         case ENDS_WITH: return m_value.endsWith(refValue);
119                         case CONTAINS: return m_value.contains(refValue);
120                     }
121                 case STARTS_WITH:
122                     switch (ref.getMode()) {
123                         case EQUALS: return refValue.startsWith(m_value);
124                         case STARTS_WITH: return refValue.startsWith(m_value) || m_value.startsWith(refValue);
125                         case ENDS_WITH: return true;
126                         case CONTAINS: return true;
127                     }
128                 case ENDS_WITH:
129                     switch (ref.getMode()) {
130                         case EQUALS: return refValue.endsWith(m_value);
131                         case STARTS_WITH: return true;
132                         case ENDS_WITH: return refValue.endsWith(m_value) || m_value.endsWith(refValue);
133                         case CONTAINS: return true;
134                     }
135                 case CONTAINS:
136                     switch (ref.getMode()) {
137                         case EQUALS: return refValue.contains(m_value);
138                         case STARTS_WITH: return true;
139                         case ENDS_WITH: return true;
140                         case CONTAINS: return true;
141                     }
142             }
143             throw new RuntimeException("[INTERNAL ERROR] unexpected mode: "+m_mode+" or "+ref.getMode());
144         }
145     }
146 
147     protected abstract boolean isRequired(boolean hasValue);
148     protected abstract String getSimpleSeparator();
149     protected abstract String getRegExpSeparator();
150     protected abstract String getRegExpSeparatorNext();
151     protected abstract String getAllowedChars();
152 }