Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6f99c5c69b01919477c6e51d30af25bc8e6a9944 (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
/*******************************************************************************
 * Copyright (c) 2005, 2008 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Ian Trimble - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.jst.pagedesigner.dtmanager.converter.operations;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.InvalidRegistryObjectException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;
import org.eclipse.jst.pagedesigner.IJMTConstants;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.dtmanager.converter.ITransformOperation;
import org.osgi.framework.Bundle;

/**
 * Factory responsible for producing {@link org.eclipse.jst.pagedesigner.dtmanager.converter.ITransformOperation} instances from the <br>
 * <code>org.eclipse.jst.pagedesigner.pageDesignerExtension.tagTransformOperation</code> extensions 
 * <br>
 * <p><b>Provisional API - subject to change</b></p>
 * 
 * @author Ian Trimble - Oracle
 */
public final class TransformOperationFactory {
	/**
	 * AppendChildElementOperation
	 */
	public static final String OP_AppendChildElementOperation = PDPlugin.getPluginId()+".AppendChildElementOperation"; //$NON-NLS-1$
	/**
	 * AppendChildTextFromXPathOperation
	 */
	public static final String OP_AppendChildTextFromXPathOperation = PDPlugin.getPluginId()+".AppendChildTextFromXPathOperation"; //$NON-NLS-1$
	/**
	 * AppendChildTextOperation
	 */
	public static final String OP_AppendChildTextOperation = PDPlugin.getPluginId()+".AppendChildTextOperation"; //$NON-NLS-1$
	/**
	 * ConvertAttributeToTextOperation
	 */
	public static final String OP_ConvertAttributeToTextOperation = PDPlugin.getPluginId()+".ConvertAttributeToTextOperation"; //$NON-NLS-1$
	/**
	 * CopyAllAttributesOperation
	 */
	public static final String OP_CopyAllAttributesOperation = PDPlugin.getPluginId()+".CopyAllAttributesOperation"; //$NON-NLS-1$
	/**
	 * CopyAttributeOperation
	 */
	public static final String OP_CopyAttributeOperation = PDPlugin.getPluginId()+".CopyAttributeOperation"; //$NON-NLS-1$
	/**
	 * CopyAttributeWithRenameOperation
	 */
	public static final String OP_CopyAttributeWithRenameOperation = PDPlugin.getPluginId()+".CopyAttributeWithRenameOperation"; //$NON-NLS-1$
	/**
	 * CopyChildrenOperation
	 */
	public static final String OP_CopyChildrenOperation = PDPlugin.getPluginId()+".CopyChildrenOperation"; //$NON-NLS-1$
	/**
	 * CreateAttributeFromXPathOperation
	 */
	public static final String OP_CreateAttributeFromXPathOperation = PDPlugin.getPluginId()+".CreateAttributeFromXPathOperation"; //$NON-NLS-1$
	/**
	 * CreateAttributeOperation
	 */
	public static final String OP_CreateAttributeOperation = PDPlugin.getPluginId()+".CreateAttributeOperation"; //$NON-NLS-1$
	/**
	 * CreateElementOperation
	 */
	public static final String OP_CreateElementOperation = PDPlugin.getPluginId()+".CreateElementOperation"; //$NON-NLS-1$
	/**
	 * CustomTransformOperation
	 */
	public static final String OP_CustomTransformOperation = PDPlugin.getPluginId()+".CustomTransformOperation"; //$NON-NLS-1$
	/**
	 * IfNotOperation
	 */
	public static final String OP_IfNotOperation = PDPlugin.getPluginId()+".IfNotOperation"; //$NON-NLS-1$
	/**
	 * IfOperation
	 */
	public static final String OP_IfOperation = PDPlugin.getPluginId()+".IfOperation"; //$NON-NLS-1$
	/**
	 * IterateOverElementsOperation
	 */
	public static final String OP_IterateOverElementsOperation = PDPlugin.getPluginId()+".IterateOverElementsOperation"; //$NON-NLS-1$
	/**
	 * MakeParentElementCurrentOperation
	 */
	public static final String OP_MakeParentElementCurrentOperation = PDPlugin.getPluginId()+".MakeParentElementCurrentOperation"; //$NON-NLS-1$
	/**
	 * RemoveAttributeOperation
	 */
	public static final String OP_RemoveAttributeOperation = PDPlugin.getPluginId()+".RemoveAttributeOperation"; //$NON-NLS-1$
	/**
	 * RenameAttributeOperation
	 */
	public static final String OP_RenameAttributeOperation = PDPlugin.getPluginId()+".RenameAttributeOperation"; //$NON-NLS-1$

	private static TransformOperationFactory instance;

	private Logger log = PDPlugin.getLogger(TransformOperationFactory.class);
	
	private Map<String, Class> _tagTransformOpsRegistry;

	/**
	 * Instantiates an instance.
	 */
	private TransformOperationFactory() {
		//no external instantiation
		
		//read extensions
		readAllTagTransformOperations();
	}

	
	/**
	 * Gets the singleton instance.
	 * 
	 * @return The singleton instance.
	 */
	public static synchronized TransformOperationFactory getInstance() {
		if (instance == null) {
			instance = new TransformOperationFactory();
		}
		return instance;
	}

	/**
	 * Gets an ITransformOperation instance for the specified operation ID and
	 * parameters.
	 * 
	 * @param opID - the plugin-qualified Operation extension ID.
	 * @param params - array of String parameters
	 * @return ITransformOperation
	 */
	public ITransformOperation getTransformOperation(String opID, String[] params) {
		Class opClass = null;
		try {
			opClass = _tagTransformOpsRegistry.get(opID);
			if (opClass != null) {
				Object opObject = opClass.newInstance();
				if (opObject instanceof AbstractTransformOperation) {
					((AbstractTransformOperation)opObject).setParameters(params);
					((AbstractTransformOperation)opObject).setTransformOperationID(opID);			
					return (AbstractTransformOperation)opObject;
				}
				log.error("Warning.TransformOperationFactory.NotAbstractTransformOperation", opClass.getName()); //$NON-NLS-1$
			} else {
				log.error("Warning.TransformOperationFactory.ExtensionNotFound", opID); //$NON-NLS-1$
			}		
		} catch(IllegalAccessException iae) {
			log.error("Warning.TransformOperationFactory.IllegalAccess", opClass.getName(), iae); //$NON-NLS-1$
		} catch(InstantiationException ie) {
			log.error("Warning.TransformOperationFactory.Instantiation", opClass.getName(), ie); //$NON-NLS-1$
		}
		return null;
	}
	
	/**
	 * Reads the custom tag transform operations from WPE registry
	 */
	private void readAllTagTransformOperations() {
		try {
			_tagTransformOpsRegistry = new HashMap<String, Class>();
			IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(PDPlugin.getPluginId(), IJMTConstants.EXTENSION_POINT_PAGEDESIGNER);
			IExtension[] extensions = point.getExtensions();
			for (int i=0;i < extensions.length;i++){
				IExtension ext = extensions[i];
				for (int j=0;j < ext.getConfigurationElements().length;j++){
					if (ext.getConfigurationElements()[j].getName()
							.equals(IJMTConstants.TAG_TRANSFORM_OPERATION)) {							
						registerTransformOperation(ext.getConfigurationElements()[j]);					
					}				
				}
			}
		} catch (InvalidRegistryObjectException e) {
			log.error("Warning.TransformOperationFactory.RegistryError", PDPlugin.getPluginId()+IJMTConstants.EXTENSION_POINT_PAGEDESIGNER+"."+IJMTConstants.TAG_TRANSFORM_OPERATION, e); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	private void registerTransformOperation(final IConfigurationElement element) {
	
		final Bundle bundle = Platform.getBundle(element.getContributor().getName());        
		final String id = element.getContributor().getName()+"."+element.getAttribute("id"); //$NON-NLS-1$ //$NON-NLS-2$
        if (bundle != null) {
            try {
                final Class transformClass = bundle.loadClass(element.getAttribute("class"));                 //$NON-NLS-1$
                // Not checking instance type here.  Class gets checked as ITransformOperation at 
                //instantiation time...  
                //best to log error then rather than now.
                _tagTransformOpsRegistry.put(id, transformClass);
            }
            catch (Exception e) {
                log.error("Warning.TransformOperationFactory.CannotLoadOpClass",id,  e);  //$NON-NLS-1$
            }
        }		
	}
	
	
}

Back to the top