Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2f8ea74626610efab3e9a5b0ee8f70c6e5af848f (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2008, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.internal.transforms;

import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
import org.eclipse.osgi.internal.framework.EquinoxContainer;
import org.eclipse.osgi.internal.log.EquinoxLogServices;

/**
 * This class is used by the transformer hook to parse urls provided by transform developers that specifies the particular transforms that should be utilized for a particular transformer.  
 * TODO: factor this out into a new type of service the transformer uses.  Then there could be CSV transforms, programatic transforms, etc.
 */
public class CSVParser {

	/**
	 * Parse the given url as a CSV file containing transform tuples.  The tuples have the form:
	 * <pre>
	 * bundleRegex,pathRegex,transformerResource
	 * </pre> 
	 * @param transformMapURL the map url
	 * @return an array of tuples derived from the contents of the file
	 * @throws IOException thrown if there are issues parsing the file
	 */
	public static TransformTuple[] parse(URL transformMapURL, EquinoxLogServices logServices) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(transformMapURL.openStream()));
		String currentLine = null;
		List<TransformTuple> list = new ArrayList<TransformTuple>();
		while ((currentLine = reader.readLine()) != null) {
			if (currentLine.startsWith("#")) { //$NON-NLS-1$
				continue;
			}
			currentLine = currentLine.trim();
			if (currentLine.length() == 0)
				continue;
			StringTokenizer toker = new StringTokenizer(currentLine, ","); //$NON-NLS-1$
			try {
				String bundlePatternString = toker.nextToken().trim();
				String pathPatternString = toker.nextToken().trim();
				String transformPath = toker.nextToken().trim();
				try {
					Pattern bundlePattern = Pattern.compile(bundlePatternString);
					Pattern pathPattern = Pattern.compile(pathPatternString);
					URL transformerURL = new URL(transformMapURL, transformPath);
					try {
						transformerURL.openStream();
						TransformTuple tuple = new TransformTuple();
						tuple.bundlePattern = bundlePattern;
						tuple.pathPattern = pathPattern;
						tuple.transformerUrl = transformerURL;
						list.add(tuple);
					} catch (IOException e) {
						logServices.log(EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Could not add transform :" + transformerURL.toString(), e); //$NON-NLS-1$
					}
				} catch (PatternSyntaxException e) {
					logServices.log(EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Could not add compile transform matching regular expression", e); //$NON-NLS-1$
				}

			} catch (NoSuchElementException e) {
				logServices.log(EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Could not parse transform file record :" + currentLine, e); //$NON-NLS-1$
			}
		}
		return list.toArray(new TransformTuple[list.size()]);
	}
}

Back to the top