Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: d0a7630843f17d5e9599125eb49c192d96ef4622 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*******************************************************************************
 * Copyright (c) 2001, 2004 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.jst.j2ee.commonarchivecore.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;



/**
 * @author mdelder
 */
public class ArchiveTypeDiscriminatorRegistry {

	private Collection discriminators = null;
	private Collection customTypes = null;

	/*
	 * Most known types are of length 3. Whenver a new type is added that is not of length 3,
	 * modifications may be necessary to the 'isKnownArchiveType() method
	 */
	private static final String[] defaultKnownTypes = new String[]{"ear", "war", "jar", "zip", "far"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$


	public static final ArchiveTypeDiscriminatorRegistry INSTANCE = new ArchiveTypeDiscriminatorRegistry();

	public static void registorDiscriminator(GenericArchiveTypeDiscriminator discriminator) {
		INSTANCE.getDiscriminators().add(discriminator);
		INSTANCE.addKnownArchiveTypes(discriminator.getCustomFileExtensions());
	}

	public static ArchiveTypeDiscriminatorRegistry getInstance() {
		return INSTANCE;
	}

	/**
	 * @return
	 */
	public Collection getDiscriminators() {
		if (discriminators == null)
			discriminators = new ArrayList();
		return discriminators;
	}

	public void contributeTypes(Archive archive) {
		if (discriminators == null)
			return;
		GenericArchiveTypeDiscriminator discriminator = null;
		for (Iterator itr = discriminators.iterator(); itr.hasNext();) {
			discriminator = (GenericArchiveTypeDiscriminator) itr.next();
			if (discriminator.discriminate(archive))
				archive.getTypes().add(discriminator.getTypeKey());
		}
	}

	public void addKnownArchiveTypes(String[] newTypes) {
		if (customTypes == null) {
			customTypes = new ArrayList();
		}
		for (int i = 0; i < newTypes.length; i++) {
			customTypes.add(newTypes[i]);
		}
	}

	public boolean isKnownArchiveType(String fileURI) {
		if (fileURI == null || fileURI.length() == 0)
			return false;

		String lowerCaseUri = fileURI.toLowerCase();
		/*
		 * Ensure that the length of the URI is long enough to contain a .3 style extension
		 */
		if (lowerCaseUri.length() > 4 && lowerCaseUri.charAt(lowerCaseUri.length() - 4) == '.') {
			String ending = lowerCaseUri.substring(lowerCaseUri.length() - 3);
			for (int i = 0; i < defaultKnownTypes.length; i++)
				if (defaultKnownTypes[i].equals(ending))
					return true;
		}

		String customType = null;
		if (customTypes != null) {
			Iterator customTypesIterator = customTypes.iterator();
			while (customTypesIterator.hasNext()) {
				customType = (String) customTypesIterator.next();
				if (fileURI.endsWith(customType))
					return true;
			}
		}

		return false;
	}

}

Back to the top