summaryrefslogtreecommitdiffstats
path: root/sandbox/mobile-android/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/DexContributionProcessor.java
blob: 83c209180d82562af960f496d17178fe14f73057 (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
package org.apache.tuscany.sca.contribution.processor.impl;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.tuscany.sca.android.DexResource;
import org.apache.tuscany.sca.contribution.processor.PackageProcessor;
import org.apache.tuscany.sca.contribution.service.ContributionException;

public class DexContributionProcessor implements PackageProcessor {

	public URL getArtifactURL(URL packageSourceURL, URI artifact)
			throws MalformedURLException {
		return new URL(artifact.toString());
	}

	public List<URI> getArtifacts(URL packageSourceURL, InputStream inputStream)
			throws ContributionException, IOException {

		ArrayList<URI> uris = new ArrayList<URI>();
		DexResource res = new DexResource(packageSourceURL);

		URI[] contentFiles = res.getContentFiles();

		for (URI uri : contentFiles) {
			String fileName = DexResource.getFile(uri.getPath());
			URL url = uri.toURL();

			if (fileName != null) {

				if (fileName.endsWith("_composite")) {

					url.openConnection();
					try {
						XMLStreamReader r = XMLInputFactory.newInstance()
								.createXMLStreamReader(url.openStream());

						while (r.hasNext()) {

							if (r.isStartElement()) {
								QName name = r.getName();

								if ("implementation.java".equals(name
										.getLocalPart())) {
									int attributeCount = r.getAttributeCount();

									for (int i = 0; i < attributeCount; i++) {

										if (r.getAttributeLocalName(i).equals(
												"class")) {
											StringBuffer sb = new StringBuffer(
													"dex://");
											sb.append(
													r.getAttributeValue(i)
															.replace('.', '/'))
													.append(".class");

											try {
												uris
														.add(new URI(sb
																.toString()));
											} catch (URISyntaxException e) {
											}

											break;

										}

									}

								}

							}

							r.next();

						}

					} catch (XMLStreamException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (FactoryConfigurationError e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					StringBuffer sb = new StringBuffer("dex://");
					sb.append(url.getHost()).append(url.getPath());
					sb.delete(sb.length() - 10, sb.length()).append(
							".composite");

					try {
						uris.add(new URI(sb.toString()));
					} catch (URISyntaxException e) {
						continue;
					}

				}

			}

		}

		return uris;

	}

	public String getPackageType() {
		return "application/x-dex";
	}

}