View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.ppr;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  /**
26   * @author Martin Marinschek
27   */
28  public class PartialTriggerParser
29  {
30  
31      public List parse(String partialTriggerString)
32      {
33  
34          int lastTokenEnd = 0;
35          boolean subLevelMode = false;
36  
37          List partialTriggers = new ArrayList();
38          PartialTrigger currentTrigger = null;
39  
40          for (int i = 0, length = partialTriggerString.length(); i <= length; i++) {
41              boolean newTokenMode = false;
42              boolean endTopLevelMode = false;
43              boolean endSubLevelMode = false;
44  
45              if (!(i == partialTriggerString.length())) {
46                  char c = partialTriggerString.charAt(i);
47  
48                  if (c == ',' || c == ';' || c == ' ') {
49                      newTokenMode = true;
50                  }
51                  else if (c == '(') {
52                      newTokenMode = true;
53                      subLevelMode = true;
54                      endTopLevelMode = true;
55                  }
56                  else if (c == ')') {
57                      newTokenMode = true;
58                      subLevelMode = false;
59                      endSubLevelMode = true;
60                  }
61              }
62              else {
63                  newTokenMode = true;
64              }
65  
66              if (newTokenMode) {
67                  if (((!subLevelMode && !endSubLevelMode) || endTopLevelMode) && i > lastTokenEnd) {
68                      currentTrigger = new PartialTrigger();
69                      partialTriggers.add(currentTrigger);
70                      currentTrigger.setPartialTriggerId(partialTriggerString.substring(lastTokenEnd, i));
71                  }
72                  else if ((subLevelMode || endSubLevelMode) && i > lastTokenEnd) {
73                      currentTrigger.addEventHook(partialTriggerString.substring(lastTokenEnd, i));
74                  }
75  
76                  lastTokenEnd = i + 1;
77              }
78          }
79  
80          return partialTriggers;
81      }
82  
83  
84      public static class PartialTrigger
85      {
86          private String partialTriggerId;
87          private List eventHooks;
88  
89          public void addEventHook(String eventHook)
90          {
91              if (eventHooks == null) {
92                  eventHooks = new ArrayList();
93              }
94              eventHooks.add(eventHook);
95          }
96  
97          public String getPartialTriggerId()
98          {
99              return partialTriggerId;
100         }
101 
102         public void setPartialTriggerId(String partialTriggerId)
103         {
104             this.partialTriggerId = partialTriggerId;
105         }
106 
107         public List getEventHooks()
108         {
109             if (eventHooks == null) {
110                 return Collections.EMPTY_LIST;
111             }
112 
113             return eventHooks;
114         }
115     }
116 
117     public static void main(String[] args)
118     {
119         PartialTriggerParser parser = new PartialTriggerParser();
120         List li = parser.parse(/*", ; ,, test1id   (onkeyup,onkeydown,,,test2id test3id;test4id"*/"testid(onchange)");
121 
122         for (int i = 0; i < li.size(); i++) {
123             PartialTrigger partialTrigger = (PartialTrigger) li.get(i);
124             System.out.println("partialTrigger.partialTriggerId = " + partialTrigger.getPartialTriggerId());
125 
126             for (int j = 0; j < partialTrigger.getEventHooks().size(); j++) {
127                 String eventHook = (String) partialTrigger.getEventHooks().get(j);
128                 System.out.println("eventHook = " + eventHook);
129             }
130         }
131     }
132 
133 }