Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5e2689a92e590d994140855c43e85a8a2cc2c3b2 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.jsf.facesconfig.ui.pageflow.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowPage;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.Pageflow;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowFactory;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowNode;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowPackage;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.impl.PageflowPackageImpl;

/**
 * Drives the model. Acts as the model entry point, including create, load, save
 * operations.
 * 
 * @author Xiao-guang Zhang
 */
public class PageflowModelManager {
	/**
	 * In EMF, a resource provides the way to have access to the model content.
	 */
	private Resource resourcePageflow = null;

	/**
	 * the path of current pageflow model resource provides the way to have
	 * access to the model content.
	 */
	private IPath pathPageflow = null;

	/**
	 * Contains the factory associated with the model.
	 */
	private static PageflowFactory pageflowFactory = null;

	/**
	 * Gives access to the top level pageflow contained in the resource.
	 */
	private Pageflow pageflow = null;

	/**
	 * resource set
	 */
	private ResourceSet resourceSet = null;

	/** unicode encoding UTF-8 support */
	private static HashMap defaultSaveOptions = new HashMap();

	static {
		defaultSaveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
	}

	/**
	 * Uses lazy initialization.
	 * 
	 * @param path -
	 *            pageflow file name
	 * @return the resource containing the pageflow
	 */
	public Resource getResource(IPath path) {
		if (resourcePageflow == null) {
			pathPageflow = path;
			ResourceSet resSet = getResourceSet();
			resourcePageflow = resSet.getResource(URI.createPlatformResourceURI(path
					.toString(), false), true);
		}

		return resourcePageflow;
	}

	/**
	 * 
	 * @return the top level pageflow model
	 */
	public Pageflow getModel() {
		if (null == pageflow) {
			EList l = resourcePageflow.getContents();
			Iterator i = l.iterator();
			while (i.hasNext()) {
				Object o = i.next();
				if (o instanceof Pageflow) {
					pageflow = (Pageflow) o;
				}
			}
		}
		return pageflow;
	}

	/**
	 * Creates a resource to contain the network. The resource file does not
	 * exist yet.
	 * 
	 * @param path
	 * @return
	 */
	private Resource createResource(IPath path) {
		if (resourcePageflow == null) {
			pathPageflow = path;
			ResourceSet resSet = getResourceSet();
			resourcePageflow = resSet.createResource(URI.createPlatformResourceURI(path
					.toString(), false));
		}
		return resourcePageflow;
	}

	/**
	 * Returns the resource set.
	 * 
	 * @param
	 * @return
	 */
	private ResourceSet getResourceSet() {
		if (null == resourceSet) {
			// Initialize the pageflow package, this line can not be removed.
			PageflowPackageImpl.init();
			// Register the XML resource factory for the .pageflow extension
			Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
			Map regMap = reg.getExtensionToFactoryMap();
			// m.put("pageflow", new XMIResourceFactoryImpl());
			regMap.put("pageflow", new PageflowResourceFactoryImpl()); //$NON-NLS-1$

			resourceSet = new ResourceSetImpl();
		}

		return resourceSet;
	}

	/**
	 * Returns the factory associated with the model. Object creation are made
	 * through that factory.
	 * 
	 * @return - pageflow factory
	 */
	static public PageflowFactory getFactory() {
		if (pageflowFactory == null) {
			// Access the factory (needed to create instances)
			Map registry = EPackage.Registry.INSTANCE;
			String pageflowURI = PageflowPackage.eNS_URI;
			PageflowPackage pageflowPackage = (PageflowPackage) registry
					.get(pageflowURI);
			pageflowFactory = pageflowPackage.getPageflowFactory();
		}
		return pageflowFactory;
	}

	/**
	 * Creates a new pageflow model with begin and end nodes.
	 * 
	 * @param path -
	 *            the new pageflow file name
	 * @return - new pageflow model
	 */
	public Pageflow createPageflow(IPath path) {
		createResource(path);
		// Create a new pageflow model
		Map registry = EPackage.Registry.INSTANCE;
		String pageflowURI = PageflowPackage.eNS_URI;
		PageflowPackage nPackage = (PageflowPackage) registry.get(pageflowURI);
		PageflowFactory nFactory = nPackage.getPageflowFactory();
		pageflow = nFactory.createPageflow();

		resourcePageflow.getContents().add(pageflow);
		return pageflow;
	}

	/**
	 * Loads the content of the model from the file.
	 * 
	 * @param path
	 */
	public void load(IPath path) {
		getResource(path);
	}

	/**
	 * reloads the content of the model from the file.
	 * 
	 * @param path
	 */
	public void reload(IPath path) {
		getResource(path).unload();
		load(path);
	}

	/**
	 * Saves the content of the model to the file.
	 * 
	 * @param path
	 * @throws IOException 
	 */
	public void save(final IPath path) throws IOException {
		if (!pathPageflow.toString().equalsIgnoreCase(path.toString())) {
			pathPageflow = path;
			URI fileURI = URI.createPlatformResourceURI(path.toString(), false);
			resourcePageflow.setURI(fileURI);
		}
		resourcePageflow.save(defaultSaveOptions);
	}

	/**
	 * get the file path of current pageflow resource
	 * 
	 * @return - the file path
	 */
	public IPath getPath() {
		return pathPageflow;
	}

	/**
	 * @param webPath
	 * @return found page node according the web path.
	 */
	public PageflowPage foundPage(String webPath) {
		PageflowPage page = null;

		if (getModel() != null) {
			Iterator iterNodes = getModel().getNodes().iterator();
			while (iterNodes.hasNext()) {
				PageflowNode node = (PageflowNode) iterNodes.next();
				if (node instanceof PageflowPage) {
					if (((PageflowPage) node).getPath().equalsIgnoreCase(webPath)) {
						page = (PageflowPage) node;
						break;
					}
				}
			}
		}

		return page;
	}

	/**
	 * 
	 * Build a path for the resource in the .metadata directory given the path
	 * of the model resource. For example, given a model resource path of
	 * \test\folder\filename.ext the resulting Pageflow path name will be
	 * \test\.metadata\folder\filename.pageflow
	 * 
	 * @param pathFacesConfig -
	 *            faces-config file path.
	 * 
	 * @return the ipath
	 */
	public static IPath makePageflowPath(IPath pathFacesConfig) {
		IPath pageflowPath;
		String[] segs = pathFacesConfig.removeFileExtension().segments();
		pageflowPath = new Path(segs[0]).makeAbsolute();
		pageflowPath = pageflowPath.append(".metadata"); //$NON-NLS-1$
		for (int i = 1; i < segs.length; i++) {
			pageflowPath = pageflowPath.append(segs[i]);
		}
		// pageflowPath.removeFileExtension();
		pageflowPath = pageflowPath.addFileExtension("pageflow"); //$NON-NLS-1$
		return pageflowPath;
	}
}

Back to the top