View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   *
25   *  @version $Id: MidletURLReference.java 1605 2008-02-25 21:07:14Z barteo $
26   */
27  package org.microemu.app.util;
28  
29  import java.net.MalformedURLException;
30  import java.net.URL;
31  import java.util.StringTokenizer;
32  import java.util.Vector;
33  
34  import nanoxml.XMLElement;
35  
36  import org.microemu.log.Logger;
37  
38  /**
39   * @author vlads
40   *
41   */
42  public class MidletURLReference implements XMLItem {
43  
44  
45  	private String name;
46  	
47  	private String url;
48  
49  	public MidletURLReference() {
50  		super();
51  	}
52  	
53  	/**
54  	 * @param name MIDlet name
55  	 * @param url  URL to locate this URL
56  	 */
57  	public MidletURLReference(String name, String url) {
58  		super();
59  		this.name = name;
60  		this.url = url;
61  	}
62  
63  	public boolean equals(Object obj) {
64  		 if (!(obj instanceof MidletURLReference)) {
65  			 return false;
66  		 }
67  		 return ((MidletURLReference)obj).url.equals(url); 
68  	}
69  	 
70  	public String toString() {
71  		// make the text presentation shorter.
72  		URL u;
73  		try {
74  			u = new URL(url);
75  		} catch (MalformedURLException e) {
76  			Logger.error(e);
77  			return url;
78  		}
79  		StringBuffer b = new StringBuffer();
80  		
81  		String scheme = u.getProtocol();
82  		if (scheme.equals("file") || scheme.startsWith("http")) {
83  			b.append(scheme).append("://");
84  			if (u.getHost() != null) {
85  				b.append(u.getHost());
86  			}
87  			Vector pathComponents = new Vector();
88  			final String pathSeparator = "/";
89  			StringTokenizer st = new StringTokenizer(u.getPath(), pathSeparator);
90  			while (st.hasMoreTokens()) {
91  				pathComponents.add(st.nextToken());
92  			}
93  			if (pathComponents.size() > 3) {
94  				b.append(pathSeparator);
95  				b.append(pathComponents.get(0));
96  				b.append(pathSeparator).append("...").append(pathSeparator);
97  				b.append(pathComponents.get(pathComponents.size()-2));
98  				b.append(pathSeparator);
99  				b.append(pathComponents.get(pathComponents.size()-1));
100 			} else {
101 				b.append(u.getPath());
102 			}
103 			
104 		} else {
105 			b.append(url);
106 		}
107 		if (name != null) {
108 			b.append(" - ");
109 			b.append(name);
110 		}
111 		return b.toString();
112 	}
113 	
114 	public void read(XMLElement xml) {
115 		name = xml.getChildString("name", "");
116 		url = xml.getChildString("url", "");
117 	}
118 
119 	public void save(XMLElement xml) {
120 		xml.removeChildren();
121 		xml.addChild("name", name);
122 		xml.addChild("url", url);
123 	}
124 
125 	public String getName() {
126 		return this.name;
127 	}
128 
129 	public String getUrl() {
130 		return this.url;
131 	}
132 	
133 }