Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 429785a0f39cea96727a42eb757e586c25f3ff42 (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
/*******************************************************************************
 * Copyright (c) 2006 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.core.internal.content.orm;

import java.io.IOException;
import java.util.Collections;
import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jem.util.emf.workbench.WorkbenchResourceHelperBase;
import org.eclipse.jpt.core.internal.IJpaFileContentProvider;
import org.eclipse.jpt.core.internal.IJpaRootContentNode;
import org.eclipse.jpt.core.internal.JptCorePlugin;
import org.eclipse.jpt.core.internal.content.orm.resource.OrmXmlResourceFactory;

public class OrmXmlJpaFileContentProvider implements IJpaFileContentProvider
{
	public static OrmXmlJpaFileContentProvider INSTANCE = new OrmXmlJpaFileContentProvider();
	
	
	/**
	 * Restrict access
	 */
	private OrmXmlJpaFileContentProvider() {
		
	}

	public IJpaRootContentNode buildRootContent(IFile resourceFile) {
		OrmXmlResourceFactory.register();
		
		URI fileURI = URI.createPlatformResourceURI(resourceFile.getFullPath().toString());
		OrmXmlResource resource = (OrmXmlResource) getResourceSet(resourceFile).getResource(fileURI, true);
		XmlRootContentNode root = OrmFactory.eINSTANCE.createXmlRootContentNode();
		root.setEntityMappings(resource.getXmlFileContent());
		resource.eAdapters().add(buildRootNodeListener(root));
		return root;
	}
	
	private Adapter buildRootNodeListener(XmlRootContentNode root) {
		return new RootAdapter(root);
	}

	protected ResourceSet getResourceSet(IFile file) {
		return WorkbenchResourceHelperBase.getResourceSet(file.getProject());
	}

	public String contentType() {
		return JptCorePlugin.ORM_XML_CONTENT_TYPE;
	}
	
	
	private class RootAdapter extends AdapterImpl 
	{
		XmlRootContentNode root;
		
		private RootAdapter(XmlRootContentNode rootContentNode) {
			super();
			root = rootContentNode;
		}
		public void notifyChanged(Notification notification) {
			int featureId = notification.getFeatureID(Resource.class);
			if (featureId == Resource.RESOURCE__CONTENTS) {
				if (notification.getEventType() == Notification.ADD
						|| notification.getEventType() == Notification.REMOVE) {
					OrmXmlResource resource = (OrmXmlResource) notification.getNotifier();
					root.setEntityMappings(resource.getXmlFileContent());
				}
			}
			else if (featureId == Resource.RESOURCE__IS_LOADED) {
				// dumb translator is unloading my resource, reload it
				if (notification.getNewBooleanValue() == false) {
					OrmXmlResource resource = (OrmXmlResource) notification.getNotifier();
					try {
						resource.load(Collections.EMPTY_MAP);
					}
					catch (IOException ioe) {
						// hmmm, log for now
						JptCorePlugin.log(ioe);
					}
				}
			}
		}
	}
}

Back to the top