1 /*******************************************************************************
2  *  Copyright (c) 2005, 2015 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.correction;
15 
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.pde.core.plugin.IPluginExtension;
20 import org.eclipse.pde.core.plugin.IPluginModelBase;
21 import org.eclipse.pde.internal.core.PDECore;
22 import org.eclipse.pde.internal.core.ischema.*;
23 import org.eclipse.pde.internal.core.schema.SchemaRegistry;
24 import org.eclipse.pde.internal.core.text.IDocumentElementNode;
25 import org.eclipse.pde.internal.core.text.plugin.PluginAttribute;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.pde.internal.ui.editor.plugin.JavaAttributeValue;
28 import org.eclipse.pde.internal.ui.util.PDEJavaHelperUI;
29 import org.eclipse.pde.internal.ui.util.TextUtil;
30 
31 public class CreateClassXMLResolution extends AbstractXMLMarkerResolution {
32 
CreateClassXMLResolution(int resolutionType, IMarker marker)33 	public CreateClassXMLResolution(int resolutionType, IMarker marker) {
34 		super(resolutionType, marker);
35 	}
36 
37 	// create class code copied from org.eclipse.pde.internal.ui.editor.plugin.rows.ClassAttributeRow
38 	@Override
createChange(IPluginModelBase model)39 	protected void createChange(IPluginModelBase model) {
40 		Object object = findNode(model);
41 		if (!(object instanceof PluginAttribute))
42 			return;
43 
44 		PluginAttribute attr = (PluginAttribute) object;
45 		String name = TextUtil.trimNonAlphaChars(attr.getValue()).replace('$', '.');
46 		IProject project = model.getUnderlyingResource().getProject();
47 
48 		JavaAttributeValue value = new JavaAttributeValue(project, model, getAttribute(attr), name);
49 		name = PDEJavaHelperUI.createClass(name, project, value, true);
50 		if (name != null && !name.equals(attr.getValue()))
51 			attr.getEnclosingElement().setXMLAttribute(attr.getName(), name);
52 	}
53 
getAttribute(PluginAttribute attr)54 	private ISchemaAttribute getAttribute(PluginAttribute attr) {
55 		SchemaRegistry registry = PDECore.getDefault().getSchemaRegistry();
56 		IDocumentElementNode element = attr.getEnclosingElement();
57 		IPluginExtension extension = null;
58 		while (element.getParentNode() != null) {
59 			if (element instanceof IPluginExtension) {
60 				extension = (IPluginExtension) element;
61 				break;
62 			}
63 			element = element.getParentNode();
64 		}
65 		if (extension == null)
66 			return null;
67 
68 		ISchema schema = registry.getSchema(extension.getPoint());
69 		ISchemaElement schemaElement = schema.findElement(attr.getEnclosingElement().getXMLTagName());
70 		if (schemaElement == null)
71 			return null;
72 		return schemaElement.getAttribute(attr.getName());
73 	}
74 
75 	@Override
getLabel()76 	public String getLabel() {
77 		return NLS.bind(PDEUIMessages.CreateClassXMLResolution_label, getNameOfNode());
78 	}
79 }
80