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: 7d7b2681897b0249590cc64501062ca8e9fe8acf (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
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
/*******************************************************************************
 * 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.helpers;



import java.util.List;

import org.eclipse.jst.j2ee.commonarchivecore.internal.Archive;
import org.eclipse.jst.j2ee.commonarchivecore.internal.CommonArchiveResourceHandler;
import org.eclipse.jst.j2ee.commonarchivecore.internal.ModuleFile;
import org.eclipse.jst.j2ee.commonarchivecore.internal.exception.OpenFailureException;
import org.eclipse.jst.j2ee.commonarchivecore.internal.strategy.ImportStrategy;


/**
 * @see ArchiveTypeDiscriminator
 */
public abstract class ArchiveTypeDiscriminatorImpl implements ArchiveTypeDiscriminator {
	protected List children;

	public ArchiveTypeDiscriminatorImpl() {
		super();
	}

	/**
	 * @see com.ibm.etools.archive.ArchiveTypeDiscriminator
	 */
	public void addChild(ArchiveTypeDiscriminator child) {
		if (hasChild(child))
			return;
		getChildren().add(child);
	}

	/**
	 * @see com.ibm.etools.archive.ArchiveTypeDiscriminator
	 */
	public void addChildAfter(org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveTypeDiscriminator child, org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveTypeDiscriminator predecessor) throws java.util.NoSuchElementException {
		if (hasChild(child))
			return;
		int index = getChildren().indexOf(predecessor);
		if (index >= 0) {
			index++;
			getChildren().add(index, child);
		} else {
			throw new java.util.NoSuchElementException(predecessor.toString());
		}
	}

	/**
	 * @see com.ibm.etools.archive.ArchiveTypeDiscriminator
	 */
	public void addChildBefore(ArchiveTypeDiscriminator child, ArchiveTypeDiscriminator successor) throws java.util.NoSuchElementException {
		if (hasChild(child))
			return;
		int index = getChildren().indexOf(successor);
		if (index >= 0) {
			getChildren().add(index, child);
		} else {
			throw new java.util.NoSuchElementException(successor.toString());
		}
	}

	/**
	 * @see com.ibm.etools.archive.ArchiveTypeDiscriminator
	 */
	public abstract boolean canImport(Archive anArchive);

	/**
	 * @see com.ibm.etools.archive.ArchiveTypeDiscriminator
	 */
	public Archive convert(Archive anArchive) throws OpenFailureException {
		Archive destination = createConvertedArchive();

		//turn of notifications
		destination.eSetDeliver(false);
		destination.eSetDeliver(false);

		//Copy the relevant attributes
		destination.setURI(anArchive.getURI());
		destination.setOriginalURI(anArchive.getURI());
		destination.setSize(anArchive.getSize());
		destination.setLastModified(anArchive.getLastModified());

		destination.setLoadStrategy(anArchive.getLoadStrategy());
		destination.setOptions(anArchive.getOptions());
		destination.setExtraClasspath(anArchive.getExtraClasspath());
		if (destination.isModuleFile()) {
			ImportStrategy importStrategy = createImportStrategy(anArchive, destination);
			((ModuleFile) destination).setImportStrategy(importStrategy);
		}



		//turn notifications back on
		destination.eSetDeliver(true);
		destination.eSetDeliver(true);

		return destination;
	}

	public abstract Archive createConvertedArchive();

	/**
	 * @see com.ibm.etools.archive.ArchiveTypeDiscriminator
	 */
	public abstract ImportStrategy createImportStrategy(Archive old, Archive newArchive);

	public java.util.List getChildren() {
		if (children == null)
			children = new java.util.ArrayList();
		return children;
	}

	/**
	 * Iterate through each child and attempt to convert the archive to the child's type; return the
	 * converted archive from the first child that succeeds, or null if no child succeeds or no
	 * child exists
	 */
	protected Archive getImportableArchiveFromChild(Archive anArchive) throws OpenFailureException {
		if (!hasChildren()) {
			return null;
		}
		List theChildren = getChildren();
		Archive childConvertedArchive = null;
		for (int i = 0; i < theChildren.size(); i++) {
			ArchiveTypeDiscriminator child = (ArchiveTypeDiscriminator) theChildren.get(i);
			childConvertedArchive = child.openArchive(anArchive);
			if (childConvertedArchive != null) {
				return childConvertedArchive;
			}
		}
		return null;
	}

	protected String getXmlDDMessage(String archiveType, String ddUri) {
		return CommonArchiveResourceHandler.getString("invalid_archive_EXC_", (new Object[]{archiveType, ddUri})); //$NON-NLS-1$ = "Archive is not a valid {0} because the deployment descriptor can not be found (case sensitive): {1}"
	}

	public boolean hasChild(ArchiveTypeDiscriminator disc) {
		return hasChildren() && getChildren().contains(disc);
	}

	public boolean hasChildren() {
		return children != null && children.size() > 0;
	}

	/**
	 * @see com.ibm.etools.archive.ArchiveTypeDiscriminator
	 */
	public Archive openArchive(Archive anArchive) throws OpenFailureException {
		if (!canImport(anArchive)) {
			return null;
		}
		Archive convertedArchive = convert(anArchive);
		Archive childConvertedArchive = getImportableArchiveFromChild(convertedArchive);
		if (childConvertedArchive != null)
			return childConvertedArchive;
		return convertedArchive;
	}

	/**
	 * @see com.ibm.etools.archive.ArchiveTypeDiscriminator
	 */
	public void removeChild(org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveTypeDiscriminator child) {
		getChildren().remove(child);
	}

	public void setChildren(java.util.List newChildren) {
		children = newChildren;
	}
}

Back to the top