View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.container.namespace;
18  
19  import org.apache.pluto.om.common.ObjectID;
20  
21  
22  /***
23   * Jetspeed implementation of Name space mapping for creating named attributes.
24   *
25   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
26   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
27   * @version $Id: JetspeedNamespaceMapperImpl.java 551860 2007-06-29 11:56:23Z ate $
28   */
29  public class JetspeedNamespaceMapperImpl implements JetspeedNamespaceMapper
30  {
31      private String prefix;
32      
33      public JetspeedNamespaceMapperImpl(String prefix)
34      {
35          this.prefix = prefix;
36          if ( this.prefix == null )
37          {
38              this.prefix = DEFAULT_PREFIX;
39          }        
40      }
41      
42      public JetspeedNamespaceMapperImpl()
43      {
44          this(null);
45      }
46      
47      public String getPrefix()
48      {
49          return prefix;
50      }
51      
52      public String encode(String ns, String name)
53      {
54          return join(prefix,ns,"_",name,null,null);
55      }
56  
57      public String encode(String ns1, String ns2, String name)
58      {
59          return join(prefix,ns1,"_",ns2,"_",name);
60      }
61  
62      public String decode(String ns, String name)
63      {
64          if (!name.startsWith(prefix)) return null;
65          String tmp = join(prefix,ns,"_",null,null,null);
66          if (!name.startsWith(tmp)) return null;
67          return name.substring(tmp.length());
68      }
69  
70      public String encode(long id, String name)
71      {
72          return encode(new Long(id).toString(),name);
73      }
74  
75      /* (non-Javadoc)
76       * @see org.apache.pluto.util.NamespaceMapper#encode(org.apache.pluto.om.common.ObjectID, java.lang.String)
77       */
78      public String encode(ObjectID ns, String name)
79      {
80          return encode(ns.toString(),name);
81      }
82  
83      /* (non-Javadoc)
84       * @see org.apache.pluto.util.NamespaceMapper#encode(org.apache.pluto.om.common.ObjectID, org.apache.pluto.om.common.ObjectID, java.lang.String)
85       */
86      public String encode(ObjectID ns1, ObjectID ns2, String name)
87      {
88          return encode(ns1.toString(),ns2.toString(),name);
89      }
90  
91      /* (non-Javadoc)
92       * @see org.apache.pluto.util.NamespaceMapper#decode(org.apache.pluto.om.common.ObjectID, java.lang.String)
93       */
94      public String decode(ObjectID ns, String name)
95      {
96          return decode(ns.toString(),name);
97      }
98      
99      private static String join(String s1, String s2, String s3, String s4, String s5, String s6)
100     {
101         int len = 0;
102         if (s1 != null)
103         {
104             len+=s1.length();
105             if (s2 != null)
106             {
107                 len+=s2.length();
108                 if (s3 != null)
109                 {
110                     len+=s3.length();
111                     if (s4 != null)
112                     {
113                         len+=s4.length();
114                         if (s5 != null)
115                         {
116                             len+=s5.length();
117                             if (s6 != null)
118                             {
119                                 len+=s6.length();
120                             }
121                         }
122                     }
123                 }
124             }
125         }
126         char[] buffer = new char[len];
127         int index = 0;
128         if (s1 != null) 
129         {
130             len = s1.length();
131             s1.getChars(0,len,buffer,index);
132             index+= len;
133             if (s2 != null) 
134             {
135                 len = s2.length();
136                 s2.getChars(0,len,buffer,index);
137                 index+= len;
138                 if (s3 != null) 
139                 {
140                     len = s3.length();
141                     s3.getChars(0,len,buffer,index);
142                     index+= len;
143                     if (s4 != null) 
144                     {
145                         len = s4.length();
146                         s4.getChars(0,len,buffer,index);
147                         index+= len;
148                         if (s5 != null) 
149                         {
150                             len = s5.length();
151                             s5.getChars(0,len,buffer,index);
152                             index+= len;
153                             if (s6 != null) 
154                             {
155                                 len = s6.length();
156                                 s6.getChars(0,len,buffer,index);
157                             }
158                         }
159                     }
160                 }
161             }
162         }
163         return new String(buffer);
164     }
165 }