1 /*******************************************************************************
\r
2 * Copyright (c) 2005 Oracle Corporation.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * Ian Trimble - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.eclipse.jst.pagedesigner.dtmanager.converter.operations;
\r
13 import java.util.HashMap;
\r
14 import java.util.Map;
\r
16 import org.eclipse.core.runtime.IConfigurationElement;
\r
17 import org.eclipse.core.runtime.IExtension;
\r
18 import org.eclipse.core.runtime.IExtensionPoint;
\r
19 import org.eclipse.core.runtime.InvalidRegistryObjectException;
\r
20 import org.eclipse.core.runtime.Platform;
\r
21 import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;
\r
22 import org.eclipse.jst.pagedesigner.IJMTConstants;
\r
23 import org.eclipse.jst.pagedesigner.PDPlugin;
\r
24 import org.eclipse.jst.pagedesigner.dtmanager.converter.ITransformOperation;
\r
25 import org.osgi.framework.Bundle;
\r
28 * Factory responsible for producing {@link org.eclipse.jst.pagedesigner.dtmanager.converter.ITransformOperation} instances from the <br>
\r
29 * <code>org.eclipse.jst.pagedesigner.pageDesignerExtension.tagTransformOperation</code> extensions
\r
31 * <p><b>Provisional API - subject to change</b></p>
\r
33 * @author Ian Trimble - Oracle
\r
35 public final class TransformOperationFactory {
\r
37 * AppendChildElementOperation
\r
39 public static final String OP_AppendChildElementOperation = PDPlugin.getPluginId()+".AppendChildElementOperation"; //$NON-NLS-1$
\r
41 * AppendChildTextFromXPathOperation
\r
43 public static final String OP_AppendChildTextFromXPathOperation = PDPlugin.getPluginId()+".AppendChildTextFromXPathOperation"; //$NON-NLS-1$
\r
45 * AppendChildTextOperation
\r
47 public static final String OP_AppendChildTextOperation = PDPlugin.getPluginId()+".AppendChildTextOperation"; //$NON-NLS-1$
\r
49 * ConvertAttributeToTextOperation
\r
51 public static final String OP_ConvertAttributeToTextOperation = PDPlugin.getPluginId()+".ConvertAttributeToTextOperation"; //$NON-NLS-1$
\r
53 * CopyAllAttributesOperation
\r
55 public static final String OP_CopyAllAttributesOperation = PDPlugin.getPluginId()+".CopyAllAttributesOperation"; //$NON-NLS-1$
\r
57 * CopyAttributeOperation
\r
59 public static final String OP_CopyAttributeOperation = PDPlugin.getPluginId()+".CopyAttributeOperation"; //$NON-NLS-1$
\r
61 * CopyAttributeWithRenameOperation
\r
63 public static final String OP_CopyAttributeWithRenameOperation = PDPlugin.getPluginId()+".CopyAttributeWithRenameOperation"; //$NON-NLS-1$
\r
65 * CopyChildrenOperation
\r
67 public static final String OP_CopyChildrenOperation = PDPlugin.getPluginId()+".CopyChildrenOperation"; //$NON-NLS-1$
\r
69 * CreateAttributeFromXPathOperation
\r
71 public static final String OP_CreateAttributeFromXPathOperation = PDPlugin.getPluginId()+".CreateAttributeFromXPathOperation"; //$NON-NLS-1$
\r
73 * CreateAttributeOperation
\r
75 public static final String OP_CreateAttributeOperation = PDPlugin.getPluginId()+".CreateAttributeOperation"; //$NON-NLS-1$
\r
77 * CreateElementOperation
\r
79 public static final String OP_CreateElementOperation = PDPlugin.getPluginId()+".CreateElementOperation"; //$NON-NLS-1$
\r
81 * CustomTransformOperation
\r
83 public static final String OP_CustomTransformOperation = PDPlugin.getPluginId()+".CustomTransformOperation"; //$NON-NLS-1$
\r
87 public static final String OP_IfNotOperation = PDPlugin.getPluginId()+".IfNotOperation"; //$NON-NLS-1$
\r
91 public static final String OP_IfOperation = PDPlugin.getPluginId()+".IfOperation"; //$NON-NLS-1$
\r
93 * IterateOverElementsOperation
\r
95 public static final String OP_IterateOverElementsOperation = PDPlugin.getPluginId()+".IterateOverElementsOperation"; //$NON-NLS-1$
\r
97 * MakeParentElementCurrentOperation
\r
99 public static final String OP_MakeParentElementCurrentOperation = PDPlugin.getPluginId()+".MakeParentElementCurrentOperation"; //$NON-NLS-1$
\r
101 * RemoveAttributeOperation
\r
103 public static final String OP_RemoveAttributeOperation = PDPlugin.getPluginId()+".RemoveAttributeOperation"; //$NON-NLS-1$
\r
105 * RenameAttributeOperation
\r
107 public static final String OP_RenameAttributeOperation = PDPlugin.getPluginId()+".RenameAttributeOperation"; //$NON-NLS-1$
\r
109 private static TransformOperationFactory instance;
\r
111 private Logger log = PDPlugin.getLogger(TransformOperationFactory.class);
\r
113 private Map<String, Class> _tagTransformOpsRegistry;
\r
116 * Instantiates an instance.
\r
118 private TransformOperationFactory() {
\r
119 //no external instantiation
\r
122 readAllTagTransformOperations();
\r
127 * Gets the singleton instance.
\r
129 * @return The singleton instance.
\r
131 public static synchronized TransformOperationFactory getInstance() {
\r
132 if (instance == null) {
\r
133 instance = new TransformOperationFactory();
\r
139 * Gets an ITransformOperation instance for the specified operation ID and
\r
142 * @param opID - the plugin-qualified Operation extension ID.
\r
143 * @param params - array of String parameters
\r
144 * @return ITransformOperation
\r
146 public ITransformOperation getTransformOperation(String opID, String[] params) {
\r
147 Class opClass = null;
\r
149 opClass = _tagTransformOpsRegistry.get(opID);
\r
150 if (opClass != null) {
\r
151 Object opObject = opClass.newInstance();
\r
152 if (opObject instanceof AbstractTransformOperation) {
\r
153 ((AbstractTransformOperation)opObject).setParameters(params);
\r
154 ((AbstractTransformOperation)opObject).setTransformOperationID(opID);
\r
155 return (AbstractTransformOperation)opObject;
\r
157 log.error("Warning.TransformOperationFactory.NotAbstractTransformOperation", opClass.getName()); //$NON-NLS-1$
\r
159 log.error("Warning.TransformOperationFactory.ExtensionNotFound", opID); //$NON-NLS-1$
\r
161 } catch(IllegalAccessException iae) {
\r
162 log.error("Warning.TransformOperationFactory.IllegalAccess", opClass.getName(), iae); //$NON-NLS-1$
\r
163 } catch(InstantiationException ie) {
\r
164 log.error("Warning.TransformOperationFactory.Instantiation", opClass.getName(), ie); //$NON-NLS-1$
\r
170 * Reads the custom tag transform operations from WPE registry
\r
172 private void readAllTagTransformOperations() {
\r
174 _tagTransformOpsRegistry = new HashMap<String, Class>();
\r
175 IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(PDPlugin.getPluginId(), IJMTConstants.EXTENSION_POINT_PAGEDESIGNER);
\r
176 IExtension[] extensions = point.getExtensions();
\r
177 for (int i=0;i < extensions.length;i++){
\r
178 IExtension ext = extensions[i];
\r
179 for (int j=0;j < ext.getConfigurationElements().length;j++){
\r
180 if (ext.getConfigurationElements()[j].getName()
\r
181 .equals(IJMTConstants.TAG_TRANSFORM_OPERATION)) {
\r
182 registerTransformOperation(ext.getConfigurationElements()[j]);
\r
186 } catch (InvalidRegistryObjectException e) {
\r
187 log.error("Warning.TransformOperationFactory.RegistryError", PDPlugin.getPluginId()+IJMTConstants.EXTENSION_POINT_PAGEDESIGNER+"."+IJMTConstants.TAG_TRANSFORM_OPERATION, e); //$NON-NLS-1$ //$NON-NLS-2$
\r
191 private void registerTransformOperation(final IConfigurationElement element) {
\r
193 final Bundle bundle = Platform.getBundle(element.getContributor().getName());
\r
194 final String id = element.getContributor().getName()+"."+element.getAttribute("id"); //$NON-NLS-1$ //$NON-NLS-2$
\r
195 if (bundle != null) {
\r
197 final Class transformClass = bundle.loadClass(element.getAttribute("class")); //$NON-NLS-1$
\r
198 // Not checking instance type here. Class gets checked as ITransformOperation at
\r
199 //instantiation time...
\r
200 //best to log error then rather than now.
\r
201 _tagTransformOpsRegistry.put(id, transformClass);
\r
203 catch (Exception e) {
\r
204 log.error("Warning.TransformOperationFactory.CannotLoadOpClass",id, e); //$NON-NLS-1$
\r