Skip to main content
summaryrefslogtreecommitdiffstats
blob: 509eb12d0c12d76e1a08dff7a85a780e77120d9c (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
/***************************************************************************************************
 * Copyright (c) 2005, 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.jsf.facesconfig.util;

import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jst.jsf.facesconfig.FacesConfigPlugin;
import org.eclipse.jst.jsf.facesconfig.emf.FacesConfigType;
import org.eclipse.wst.common.componentcore.ArtifactEdit;
import org.eclipse.wst.common.componentcore.internal.ArtifactEditModel;
import org.eclipse.wst.common.componentcore.resources.IVirtualFile;
import org.eclipse.wst.common.componentcore.resources.IVirtualResource;
import org.eclipse.wst.xml.core.internal.emf2xml.EMF2DOMSSERenderer;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;


/**
 * The artifact edit for the JSF Application Configuration (faces-config) model - may be used but not extended by clients.
 * 
 * This class maybe refactored to a different package in the future.
 */
public final class FacesConfigArtifactEdit extends ArtifactEdit {
	private String sFileName = null;
	private FacesConfigType facesConfig = null;
	private URI facesConfigURI = URI.createURI(IFacesConfigConstants.FACES_CONFIG_URI);
	private boolean bRegistered = false;

	/**
	 * @param aProject
	 * @param toAccessAsReadOnly
	 * @throws IllegalArgumentException
	 */
	public FacesConfigArtifactEdit(IProject aProject, boolean toAccessAsReadOnly) throws IllegalArgumentException {
		super(aProject, toAccessAsReadOnly);
	}

	/**
	 * @param aProject
	 * @param sConfigFile
	 * @return the faces config artifact for the file config file sConfig in project aProject.
	 * Opened only for read access (no write)
	 */
	public static FacesConfigArtifactEdit getFacesConfigArtifactEditForRead(IProject aProject, String sConfigFile) {
		FacesConfigArtifactEdit artifactEdit = null;
		try {
			artifactEdit = new FacesConfigArtifactEdit(aProject, true);
			if (artifactEdit != null) {
				artifactEdit.setFilename(sConfigFile);
			}
		} catch (IllegalArgumentException iae) {
            // suppress illegal argument exception
            FacesConfigPlugin.write(iae);
		}
		return artifactEdit;
	}
	/**
	 * @param aProject
	 * @return the default artifact edit
     * @deprecated Use getFacesConfigArtifactEditForRead(IProject, String) instead. This
     * method is dangerous because the caller is assuming that the "default"
     * model exists and knows nothing about where it is coming from.
	 */
	public static FacesConfigArtifactEdit getFacesConfigArtifactEditForRead(IProject aProject) {
		return getFacesConfigArtifactEditForRead(aProject, null);
	}

    /**
     * @param aProject
     * @param sConfigFile
     * @return the faces config artifact for the file config file sConfig in project aProject.
     * Opened for both write and read access
     */	
	public static FacesConfigArtifactEdit getFacesConfigArtifactEditForWrite(IProject aProject, String sConfigFile) {
		FacesConfigArtifactEdit artifactEdit = null;
		try {
			artifactEdit = new FacesConfigArtifactEdit(aProject, false);
			if (artifactEdit != null) {
				artifactEdit.setFilename(sConfigFile);
			}
		} catch (IllegalArgumentException iae) {
            // suppress illegal argument exception
            FacesConfigPlugin.write(iae);
		}
		return artifactEdit;
	}

	/**
	 * @param aProject
	 * @return the default artifact edit for read
     * @deprecated Use getFacesConfigArtifactEditForWrite(IProject, String) 
     * instead. This method is dangerous because the caller is assuming that
     * the "default" model exists and knows nothing about where it is coming
     * from.
	 */
	public static FacesConfigArtifactEdit getFacesConfigArtifactEditForWrite(IProject aProject) {
		return getFacesConfigArtifactEditForWrite(aProject, null);
	}

	/**
	 * @return the root model object for this artifact edit model
	 */
	public FacesConfigType getFacesConfig() {
		if (facesConfig == null) {
			// TODO: XN - does not seem a good way to check whether the resource has been registered each time.
			// The following comment is from StrutsArchfactEdit:
			// This is a hack to ensure that our Resource.Factory is used to
			// create
			// the Resource. It's the same hack used by JaxRPCMapArtifactEdit.
			// The problem is that there is no way to register a Resource.Factory
			// by, say, content type.
			// The best we can do is register by last file segment, which isn't
			// strictly correct.
			// Plus, there's no way to deregister.
			if (sFileName != null) {
				if (!bRegistered) {
				//if (FacesConfigResourceFactory.getRegisteredFactory(URI.createURI(sFileName)) == null) {
					FacesConfigResourceFactory.register(sFileName);
					bRegistered = true;
				}
			}
			facesConfig = (FacesConfigType)getDeploymentDescriptorRoot(); 
		}
		return facesConfig;
	}
	
	/**
	 * @return the uri pointing to the source of this faces config artifact
	 */
	public URI getFacesConfigURI() {
		return facesConfigURI;
	}

    /**
     * @return the model root
     */
    public EObject getDeploymentDescriptorRoot()
    {
        Resource deploymentDescriptorResource = getDeploymentDescriptorResource();
        if (deploymentDescriptorResource != null)
        {
            List contents = deploymentDescriptorResource.getContents();
            if (contents.size() > 0)
            {
                return (EObject) contents.get(0);
            }
        }
        return null;
        // TODO: XN
        // addFacesConfigIfNecessary((IFacesConfigResource)getDeploymentDescriptorResource());
        // return (EObject) contents.get(0);
    }

	/**
	 * @return the EMF resource retrieved for this artifact's uri or null if
	 * this descriptor has not resource.
	 */
	public Resource getDeploymentDescriptorResource() {
		if (sFileName != null) {
			facesConfigURI = URI.createURI(sFileName);
		} else {// default is "WEB-INF/faces-config.xml"
			facesConfigURI = URI.createURI(IFacesConfigConstants.FACES_CONFIG_URI);
		}
		final ArtifactEditModel artifactEditModel = getArtifactEditModel();
		if (artifactEditModel != null)
		{
		    return artifactEditModel.getResource(facesConfigURI);
		}
		return null;
	}

	/**
	 * Sets an instance's filename, used to create a URI and load a model.
	 * 
	 * @param filename This instance's filename.
	 */
	public void setFilename(String filename) {
		sFileName = filename;
	}

	// TODO: appears to be dead
//	protected void addFacesConfigIfNecessary(TranslatorResource aResource) {
//		//TO: XN - See how to create one if the config file does not exist
//		/*if (aResource != null) {
//		    if(aResource.getContents() == null || aResource.getContents().isEmpty()) {
//		        FacesConfigType facesConfigNew = WebapplicationFactory.eINSTANCE.createWebApp();
//				aResource.getContents().add(facesConfigNew);
//				aResource.setModified(true);
//		    } 
//		    FacesConfigType facesConfig = (FacesConfigType)aResource.getContents().get(0);
//			URI moduleURI = getArtifactEditModel().getModuleURI();
//			try {
//				facesConfig.setDisplayName(StructureEdit.getDeployedName(moduleURI));
//			} catch (UnresolveableURIException e) {
//				//Ignore
//			}
//			aResource.setID(facesConfig, J2EEConstants.WEBAPP_ID);
//	
//			try{
//				aResource.saveIfNecessary();
//			}catch(Exception e){
//				e.printStackTrace();
//			}
//		}*/
//	}

	/**
	 * @return the DOM model for the Struts config. file.
	 */
	public IDOMModel getIDOMModel() {
		// Modeled after SourceNestedEditorPage.getXMLModelFromResource().
		// TODO - XN This is a little hackish because it depends on the resource's
		// renderer being an EMF2DOMSedRenderer (which it is, but that could
		// change).
		Resource resource = getDeploymentDescriptorResource();
		if (resource instanceof IFacesConfigResource) {
			EMF2DOMSSERenderer renderer = (EMF2DOMSSERenderer) 
			    ((IFacesConfigResource)resource).getRenderer();
			return renderer.getXMLModel();
		}
		return null;
	}

	/**
	 * @return the IFile used to load the model or null if none.
	 */
	public IFile getFile() {
		IVirtualResource resource = getComponent().getRootFolder().findMember(facesConfigURI.toString());
		if ((resource != null) && (resource.getType() == IVirtualResource.FILE))
			return ((IVirtualFile) resource).getUnderlyingFile();
		return null;
	}
    
    /**
     * @return true if the underlying model has been disposed
     */
    public boolean isDisposed()
    {
        return getArtifactEditModel().isDisposed();
    }
}

Back to the top