summaryrefslogtreecommitdiffstats
path: root/sandbox/mobile-android/android-jdk-classes/src/org/apache/tuscany/sca/android/DexResource.java
blob: fb9029547f98c8993ff5a9e786475eff7059aed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package org.apache.tuscany.sca.android;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;

import android.content.Context;

public class DexResource {
	
	private URL url;
	
	private String folder;
	
	private String file;
	
	public DexResource(URL url) {
		String protocol = url.getProtocol();
		
		if (!"dex".equals(protocol)) {
			throw new IllegalArgumentException("The URL protocol should be \"dex\"");
		}
		
		String host = url.getHost();
		
		if ("".equals(host)) {
			throw new IllegalArgumentException("The host should not be empty!");
		}
		
		String path = url.getPath();
		file = getFile(path);
		folder = getFolder(path);
		
		if (file != null && file.indexOf('/') != -1) {
			throw new IllegalArgumentException("The dex URL format should be: dex:/<package>/[<folder>]/[<file>] only");
		}
		
		this.url = url;
		
	}
	
	public static String getFolder(String path) {
		String file = path.trim();
		
		int firstSlashIndex = file.indexOf('/');
		
		if ("".equals(file) || "/".equals(file) || firstSlashIndex == -1 || firstSlashIndex >= file.length() - 1) {
			return null;
		}
		
		int secondSlashIndex = file.indexOf("/", firstSlashIndex + 1);
		
		if (secondSlashIndex == -1 || secondSlashIndex >= file.length() - 1 || firstSlashIndex == secondSlashIndex - 1) {
			return null;
		}
		
		return file.substring(firstSlashIndex + 1, secondSlashIndex);
		
	}
	
	public static String getFile(String path) {
		String file = path.trim();
		
		int firstSlashIndex = file.indexOf('/');
		
		if ("".equals(file) || "/".equals(file) || firstSlashIndex == -1 || firstSlashIndex >= file.length() - 1) {
			return null;
		}
		
		int secondSlashIndex = file.indexOf("/", firstSlashIndex + 1);
		
		if (secondSlashIndex == -1 || secondSlashIndex >= file.length() - 1 || firstSlashIndex == secondSlashIndex - 1) {
			return null;
		}
		
		return file.substring(secondSlashIndex + 1);
		
	}
	
	public String getFolderName() {
		return folder;
	}
	
	public Context getContext() {
		Context[] contexts = ContextRegistry.getContexts(url.getHost());
		
		if (contexts.length == 0) {
			return null;
		}
		
		return contexts[0];
		
	}
	
	public String getFileName() {
		return file;
	}
	
	public boolean isPackage() {
		return folder == null;
	}
	
	public boolean isFolder() {
		return file == null && folder != null;
	}
	
	public boolean isFile() {
		return file != null;
	}
	
	public String getPackageName() {
		return url.getHost();
	}
	
	public URI[] getContentFiles() throws IOException {
		
		if (isFile()) {
			throw new UnsupportedOperationException("Not supported when the resource is a file!");
		}
		
		String packageName = url.getHost();
		
		Context[] contexts = ContextRegistry.getContexts(packageName);
		
		if (contexts.length == 0) {
			throw new IOException("Android context not found!");
		}
		
		Context context = contexts[0];
		
		ArrayList<URI> files = new ArrayList<URI>();
		StringBuffer className = new StringBuffer(packageName).append(".R");
		
			if (isPackage()) {
				ClassLoader classLoader = context.getClass().getClassLoader();
				
				try {
				
					for (String folderName : new String[] {"raw", "xml"}) {
						Class clazz = classLoader.loadClass(className.toString() + '$' + folderName);
						folderName = '/' + folderName + '/';
						Field[] fields = clazz.getFields();
						
						for (Field field : fields) {
							try {
								files.add(new URI("dex://" + packageName + folderName + field.getName()));
							} catch (URISyntaxException e) {}
						}
						
					}
				
				} catch (ClassNotFoundException e) {}
				
			} else {
				
				try {
					className.append('$').append(folder);
					Class clazz = getClass().getClassLoader().loadClass(className.toString());
					String folderName = '/' + clazz.getSimpleName() + '/';  
					Field[] fields = clazz.getFields();
					
					for (Field field : fields) {
						try {
							files.add(new URI("dex://" + packageName + folderName + field.getName()));
						} catch (URISyntaxException e) {}
					}
				
				} catch (ClassNotFoundException e) {
					throw new IOException("Resource not found!");
				}
				
			}
			
		
		
		URI[] ret = new URI[files.size()];
		files.toArray(ret);
		
		return ret;
		
	}
	
	public URL getURL() {
		return url;
	}
	
}