Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9f2a204f3f64853b588661fc587814f8f1a0e469 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************************************
 * Copyright (c) 2001, 2006 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.common.internal.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

import org.eclipse.core.internal.resources.Workspace;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jst.j2ee.application.Application;
import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;
import org.eclipse.jst.j2ee.internal.common.J2EEVersionResource;
import org.eclipse.jst.j2ee.internal.common.XMLResource;
import org.eclipse.jst.j2ee.internal.xml.J2EEXmlDtDEntityResolver;
import org.eclipse.wst.common.internal.emf.resource.Renderer;
import org.eclipse.wst.common.internal.emf.resource.TranslatorResource;
import org.eclipse.wst.common.internal.emf.resource.TranslatorResourceImpl;
import org.eclipse.wst.common.internal.emfworkbench.WorkbenchResourceHelper;
import org.xml.sax.EntityResolver;


public abstract class XMLResourceImpl extends TranslatorResourceImpl implements XMLResource,J2EEVersionResource {
	/** Indicator to determine if this resource was loaded as an alt-dd (from an ear),
	  * default is false */
	protected boolean isAlt = false;
	/** The application which declared the alt-dd for this resource; exists only if this resource is and
	  * alt dd */
	protected Application application;
	protected boolean isNew = true;
	private Boolean needsSync = new Boolean(true);
	
	
	private static class RootVersionAdapter extends AdapterImpl {
		/* (non-Javadoc)
		 * @see org.eclipse.emf.common.notify.impl.AdapterImpl#isAdapterForType(java.lang.Object)
		 */
		@Override
		public boolean isAdapterForType(Object type) {
			return super.isAdapterForType(type);
		}
		
		/* (non-Javadoc)
		 * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
		 */
		@Override
		public void notifyChanged(Notification msg) {
			if (msg.getFeatureID(null) == RESOURCE__CONTENTS &&
				msg.getEventType() == Notification.ADD) {
					((XMLResourceImpl)msg.getNotifier()).syncVersionOfRootObject();
					((Notifier)msg.getNotifier()).eAdapters().remove(this);
				}
		}
		
		

	
	}
	
	/**
	 * @deprecated since 4/29/2003 - used for compatibility
	 * Subclasses should be using the Renderers and translator framework 
	 */
	public XMLResourceImpl() {
		super();
	}

	/**
	 * @deprecated since 4/29/2003 - used for compatibility
	 * Subclasses should be using the Renderers and translator framework 
	 */
	public XMLResourceImpl(URI uri) {
		super(uri);
	}
	
	public XMLResourceImpl(URI uri, Renderer aRenderer) {
		super(uri, aRenderer);
	}

	public XMLResourceImpl(Renderer aRenderer) {
		super(aRenderer);
	}
	
	/* (non-Javadoc)
	 * @see com.ibm.etools.emf2xml.impl.TranslatorResourceImpl#initializeContents()
	 */
	@Override
	protected void initializeContents() {
		super.initializeContents();
		eAdapters().add(new RootVersionAdapter());
	}

	
	/**
	 * Is this a resource loaded as an alternate deployment descriptor?
	 */
	public boolean isAlt() {
		return isAlt;
	}
	
	
	public void setApplication(Application newApplication) {
		application = newApplication;
	}
	/**
	 * Is this a resource loaded as an alternate deployment descriptor?
	 */
	public void setIsAlt(boolean isAlternateDD) {
		isAlt = isAlternateDD;
	}
	
	/* (non-Javadoc)
	 * @see com.ibm.etools.emf2xml.impl.TranslatorResourceImpl#getDefaultVersionID()
	 */
	@Override
	protected int getDefaultVersionID() {
		return J2EE_1_4_ID;
	}
	
	/* (non-Javadoc)
	 * @see com.ibm.etools.emf2xml.TranslatorResource#setDoctypeValues(java.lang.String, java.lang.String)
	 * This is setting the module version on the resource, where values are different that the J2EE version, this will be overridden
	 */
	@Override
	public void setDoctypeValues(String publicId, String systemId) {
		int version = J2EE_1_4_ID;
		if (systemId == null) 
			version = J2EE_1_4_ID;
		else if (systemId.equals(getJ2EE_1_3_SystemID()) || systemId.equals(getJ2EE_Alt_1_3_SystemID()))
			version = J2EE_1_3_ID;
		else if (systemId.equals(getJ2EE_1_2_SystemID()) || systemId.equals(getJ2EE_Alt_1_2_SystemID()))
			version = J2EE_1_2_ID;
		super.setDoctypeValues(publicId, systemId);
		//Only set if versionID not set if version is 14
		if ((version != J2EE_1_4_ID) || (version == J2EE_1_4_ID && getModuleVersionID() == 0))
			setJ2EEVersionID(version);
	}


	/* (non-Javadoc)
	 * @see com.ibm.etools.emf2xml.TranslatorResource#usesDTD()
	 */
	@Override
	public boolean usesDTD() {
		return (getVersionID() == J2EE_1_2_ID) || (getVersionID() == J2EE_1_3_ID);
	}
	
	/* (non-Javadoc)
	 * @see com.ibm.etools.emf2xml.TranslatorResource#setVersionID(int)
	 * @deprecated, Use setJ2EEVersionID() to set module version based on j2ee version
	 **/
	@Override
	public void setVersionID(int id) {
		setJ2EEVersionID(id);
	}
	protected void primSetVersionID(int id) {
		super.setVersionID(id);
	}
	protected void primSetDoctypeValues(String aPublicId, String aSystemId) {
		super.setDoctypeValues(aPublicId,aSystemId);
	}
	/*
	 * Sets the module version based on the J2EE version
	 */
	public abstract void setJ2EEVersionID(int id);
	/*
	 * Sets the module version directly
	 * */
	 public abstract void setModuleVersionID(int id);
	/**
	 * @deprecated 
	 * (non-Javadoc)
	 * @see org.eclipse.jst.j2ee.internal.XMLResource#isJ2EE1_3()
	 */
	public boolean isJ2EE1_3() {
		return getVersionID() == J2EE_1_3_ID;
	}
	
	/**
	 * @deprecated use {@link TranslatorResource#setVersionID(int)}, 
	 * {@link TranslatorResource#setDoctypeValues(String, String)}
	 * Sets the system id of the XML document.
	 * @see J2EEVersionConstants
	 */
	public void setPublicId(String id) {
		setDoctypeValues(id, getSystemId());
	}
	
	/**
	 * @deprecated use {@link TranslatorResource#setVersionID(int)},
	 * {@link TranslatorResource#setDoctypeValues(String, String)}
	 * Sets the public id of the XML document.
	 * @see J2EEVersionConstants
	 */
	public void setSystemId(String id) {
		setDoctypeValues(getPublicId(), id);
	}
	@Override
	protected String getDefaultPublicId() {
		switch (getVersionID()) {
			case (J2EE_1_2_ID) :
				return getJ2EE_1_2_PublicID();
			case (J2EE_1_3_ID) :	
				return getJ2EE_1_3_PublicID();
			default :
				return null;
		}	
	}

	@Override
	protected String getDefaultSystemId() {
		switch (getVersionID()) {
			case (J2EE_1_2_ID) :
				return getJ2EE_1_2_SystemID();
			case (J2EE_1_3_ID) :	
				return getJ2EE_1_3_SystemID();
			default :
				return null;
		}
	}
	
	public abstract String getJ2EE_1_2_PublicID();
	
	public abstract String getJ2EE_1_2_SystemID();
	
	/**
	 * By default just return the proper 1.2 system ID, subclasses may override
	 * @return alternate string for system ID
	 */
	public String getJ2EE_Alt_1_2_SystemID() {
		return getJ2EE_1_2_SystemID();
	}
	
	public abstract String getJ2EE_1_3_PublicID();
	
	public abstract String getJ2EE_1_3_SystemID();
	
	/**
	 * By default just return the proper 1.3 system ID, subclasses may override
	 * @return alternate string for system ID
	 */
	public String getJ2EE_Alt_1_3_SystemID() {
		return getJ2EE_1_3_SystemID();
	}
	
	
	@Override
	public NotificationChain basicSetResourceSet(ResourceSet aResourceSet, NotificationChain notifications) {
		if (aResourceSet == null && this.resourceSet != null)
			preDelete();
		return super.basicSetResourceSet(aResourceSet, notifications);
	}
		
	public Application getApplication() {
		return application;
	}
	
	/**
	 * @deprecated - use getJ2EEVersionID() and getModuleVersionID()
	 */
	@Override
	public int getVersionID() {
		return getJ2EEVersionID();
	}
	
	@Override
	public EntityResolver getEntityResolver() {
		return J2EEXmlDtDEntityResolver.INSTANCE;
	}	
	/* All subclasses will derive this value based on their module version
	 */
	public abstract int getJ2EEVersionID();

	

	/* This will be computed during loads of the resource
	 */
	public int getModuleVersionID() {
		return super.getVersionID();
	}
	
	protected abstract void syncVersionOfRootObject();
	
	protected String getModuleVersionString() {
		
		int ver = getModuleVersionID();
		return new BigDecimal(String.valueOf(ver)).movePointLeft(1).toString();
	}

	@Override
	public void loadExisting(Map options) throws IOException {
		boolean localNeedsSync = false;
		synchronized (needsSync) {
			localNeedsSync = needsSync;
		}
		if (localNeedsSync) { // Only check sync once for life of this model
			IFile file = WorkbenchResourceHelper.getFile(this);
			if (!file.isSynchronized(IResource.DEPTH_ZERO))
			{
				try {
					Workspace workspace = (Workspace)file.getWorkspace();
					if (workspace.getElementTree().isImmutable())
					{
						workspace.newWorkingTree();
					}
					((org.eclipse.core.internal.resources.Resource)file).getLocalManager().refresh(file.getProject(), IResource.DEPTH_INFINITE, true, null);
				} catch (CoreException e) {
					throw new org.eclipse.emf.ecore.resource.Resource.IOWrappedException(e);
				}
			}
			synchronized (needsSync) {
				needsSync = new Boolean(false);
			}
		}
		super.loadExisting(options);
	}
}

Back to the top