KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > io > ComponentAddressAdaptor


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.util.io;
16
17 import java.io.IOException JavaDoc;
18
19 import org.apache.tapestry.Tapestry;
20 import org.apache.tapestry.services.DataSqueezer;
21 import org.apache.tapestry.util.ComponentAddress;
22
23 /**
24  * Squeezes a org.apache.tapestry.ComponentAddress.
25  *
26  * @author mindbridge
27  * @since 2.2
28  */

29
30 public class ComponentAddressAdaptor implements ISqueezeAdaptor
31 {
32     private static final String JavaDoc PREFIX = "A";
33
34     private static final char SEPARATOR = ',';
35
36     public String JavaDoc squeeze(DataSqueezer squeezer, Object JavaDoc data) throws IOException JavaDoc
37     {
38         ComponentAddress address = (ComponentAddress) data;
39
40         // a 'null' id path is encoded as an empty string
41
String JavaDoc idPath = address.getIdPath();
42         if (idPath == null)
43             idPath = "";
44
45         return PREFIX + address.getPageName() + SEPARATOR + idPath;
46     }
47
48     public Object JavaDoc unsqueeze(DataSqueezer squeezer, String JavaDoc string) throws IOException JavaDoc
49     {
50         int separator = string.indexOf(SEPARATOR);
51         if (separator < 0)
52             throw new IOException JavaDoc(Tapestry.getMessage("ComponentAddressAdaptor.no-separator"));
53
54         String JavaDoc pageName = string.substring(1, separator);
55         String JavaDoc idPath = string.substring(separator + 1);
56         if (idPath.equals(""))
57             idPath = null;
58
59         return new ComponentAddress(pageName, idPath);
60     }
61
62     public void register(DataSqueezer squeezer)
63     {
64         squeezer.register(PREFIX, ComponentAddress.class, this);
65     }
66
67 }
Popular Tags