KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > impl > invocation > SimpleMapper


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portal.core.impl.invocation;
12
13 /**
14  * A simple mapper implementation. The limitations is that
15  * only the root and its children can have children
16  *
17  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
18  * @version $Revision: 1.1 $
19  */

20 public class SimpleMapper implements Mapper
21 {
22    public MappingResult map(MappingContext ctx, String JavaDoc path)
23    {
24       Object JavaDoc root = ctx.getRoot();
25       Object JavaDoc target = null;
26       String JavaDoc targetPath = null;
27       String JavaDoc targetPathInfo = null;
28
29       if (path == null || path.length() == 0)
30       {
31          targetPath = null;
32          targetPathInfo = null;
33       }
34       else if ("/".equals(path))
35       {
36          targetPath = null;
37          targetPathInfo = "/";
38       }
39       else
40       {
41          int firstSlashPos = path.indexOf('/', 1);
42          if (firstSlashPos == -1)
43          {
44             String JavaDoc firstChunk = path.substring(1);
45             target = ctx.getChild(root, firstChunk);
46             if (target != null)
47             {
48                targetPath = path;
49                targetPathInfo = null;
50             }
51             else
52             {
53                targetPath = null;
54                targetPathInfo = path;
55             }
56          }
57          else if (firstSlashPos == 1)
58          {
59             targetPath = null;
60             targetPathInfo = "/";
61          }
62          else
63          {
64             String JavaDoc firstChunk = path.substring(1, firstSlashPos);
65             target = ctx.getChild(root, firstChunk);
66             if (target != null)
67             {
68                int secondSlashPos = path.indexOf('/', firstSlashPos + 1);
69                if (secondSlashPos == -1)
70                {
71                   String JavaDoc secondChunck = path.substring(firstSlashPos + 1);
72                   if (secondChunck.length() == 0)
73                   {
74                      targetPath = "/" + firstChunk;
75                      targetPathInfo = "/";
76                   }
77                   else
78                   {
79                      Object JavaDoc child = ctx.getChild(target, secondChunck);
80                      if (child != null)
81                      {
82                         target = child;
83                         targetPath = path;
84                         targetPathInfo = null;
85                      }
86                      else
87                      {
88                         targetPath = "/" + firstChunk;
89                         targetPathInfo = "/" + secondChunck;
90                      }
91                   }
92                }
93                else
94                {
95                   targetPath = "/" + firstChunk;
96                   targetPathInfo = path.substring(firstSlashPos);
97                }
98             }
99             else
100             {
101                targetPath = null;
102                targetPathInfo = path;
103             }
104          }
105       }
106       return new MappingResult(target, targetPath, targetPathInfo);
107    }
108 }
109
Popular Tags