1 11 package org.eclipse.pde.internal.ui.editor.text; 12 13 import java.util.Hashtable ; 14 import java.util.Locale ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.jdt.core.JavaConventions; 19 import org.eclipse.pde.core.plugin.IPluginElement; 20 import org.eclipse.pde.core.plugin.IPluginExtension; 21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 22 import org.eclipse.pde.core.plugin.IPluginObject; 23 import org.eclipse.pde.internal.core.PDECore; 24 import org.eclipse.pde.internal.core.ischema.ISchema; 25 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute; 26 import org.eclipse.pde.internal.core.ischema.ISchemaElement; 27 import org.eclipse.pde.internal.core.text.IDocumentAttribute; 28 import org.eclipse.pde.internal.core.text.IDocumentNode; 29 import org.eclipse.pde.internal.core.text.IDocumentRange; 30 import org.eclipse.pde.internal.core.text.IDocumentTextNode; 31 import org.eclipse.pde.internal.core.util.PDEJavaHelper; 32 import org.eclipse.pde.internal.ui.PDEPlugin; 33 34 public abstract class XMLUtil { 35 36 44 public static IPluginObject getTopLevelParent(IDocumentRange range) { 45 IDocumentNode node = null; 46 if (range instanceof IDocumentAttribute) 47 node = ((IDocumentAttribute)range).getEnclosingElement(); 48 else if (range instanceof IDocumentTextNode) 49 node = ((IDocumentTextNode)range).getEnclosingElement(); 50 else if (range instanceof IPluginElement) 51 node = (IDocumentNode)range; 52 else if (range instanceof IPluginObject) 53 return (IPluginObject)range; 55 56 while (node != null && 57 !(node instanceof IPluginExtension) && 58 !(node instanceof IPluginExtensionPoint)) 59 node = node.getParentNode(); 60 61 return node != null ? (IPluginObject)node : null; 62 } 63 64 private static boolean withinRange(int start, int len, int offset) { 65 return start <= offset && offset <= start + len; 66 } 67 68 public static boolean withinRange(IDocumentRange range, int offset) { 69 if (range instanceof IDocumentAttribute) 70 return withinRange( 71 ((IDocumentAttribute)range).getValueOffset(), 72 ((IDocumentAttribute)range).getValueLength(), 73 offset); 74 if (range instanceof IDocumentNode) 75 return withinRange( 76 ((IDocumentNode)range).getOffset(), 77 ((IDocumentNode)range).getLength(), 78 offset); 79 if (range instanceof IDocumentTextNode) 80 return withinRange( 81 ((IDocumentTextNode)range).getOffset(), 82 ((IDocumentTextNode)range).getLength(), 83 offset); 84 return false; 85 } 86 87 93 public static ISchemaElement getSchemaElement(IDocumentNode node, String extensionPoint) { 94 if (extensionPoint == null) { 95 IPluginObject obj = getTopLevelParent(node); 96 if (!(obj instanceof IPluginExtension)) 97 return null; 98 extensionPoint = ((IPluginExtension)obj).getPoint(); 99 } 100 ISchema schema = PDECore.getDefault().getSchemaRegistry().getSchema(extensionPoint); 101 if (schema == null) 102 return null; 103 104 ISchemaElement sElement = schema.findElement(node.getXMLTagName()); 105 return sElement; 106 } 107 108 114 public static ISchemaAttribute getSchemaAttribute(IDocumentAttribute attr, String extensionPoint) { 115 ISchemaElement ele = getSchemaElement(attr.getEnclosingElement(), extensionPoint); 116 if (ele == null) 117 return null; 118 119 return ele.getAttribute(attr.getAttributeName()); 120 } 121 122 123 129 public static String createDefaultClassName(IProject project, 130 ISchemaAttribute attInfo, int counter) { 131 String tag = attInfo.getParent().getName(); 132 String expectedType = attInfo.getBasedOn(); 133 String className = ""; if (expectedType == null) { 135 StringBuffer buf = new StringBuffer (tag); 136 buf.setCharAt(0, Character.toUpperCase(tag.charAt(0))); 137 className = buf.toString(); 138 } else { 139 className = expectedType; 142 int dotLoc = className.lastIndexOf('.'); 143 if (dotLoc != -1) 144 className = className.substring(dotLoc + 1); 145 if (className.length() > 2 && className.charAt(0) == 'I' 146 && Character.isUpperCase(className.charAt(1))) 147 className = className.substring(1); 148 } 149 String packageName = createDefaultPackageName(project, className); 150 className += counter; 151 return packageName + "." + className; } 153 154 155 160 public static String createDefaultPackageName(IProject project, String className) { 161 String id = project.getName(); 162 StringBuffer buffer = new StringBuffer (); 163 IStatus status; 164 for (int i = 0; i < id.length(); i++) { 165 char ch = id.charAt(i); 166 if (buffer.length() == 0) { 167 if (Character.isJavaIdentifierStart(ch)) 168 buffer.append(Character.toLowerCase(ch)); 169 } else { 170 if (Character.isJavaIdentifierPart(ch)) 171 buffer.append(ch); 172 else if (ch == '.') { 173 status = JavaConventions.validatePackageName( 174 buffer.toString(), 175 PDEJavaHelper.getJavaSourceLevel(project), 176 PDEJavaHelper.getJavaComplianceLevel(project)); 177 if (status.getSeverity() == IStatus.ERROR) 178 buffer.append(className.toLowerCase(Locale.ENGLISH)); 179 buffer.append(ch); 180 } 181 } 182 } 183 184 status = JavaConventions.validatePackageName( 185 buffer.toString(), 186 PDEJavaHelper.getJavaSourceLevel(project), 187 PDEJavaHelper.getJavaComplianceLevel(project)); 188 if (status.getSeverity() == IStatus.ERROR) 189 buffer.append(className.toLowerCase(Locale.ENGLISH)); 190 191 return buffer.toString(); 192 } 193 194 200 public static String createDefaultName(IProject project, 201 ISchemaAttribute attInfo, int counter) { 202 if (attInfo.getType().getName().equals("boolean")) return "true"; 205 String tag = attInfo.getParent().getName(); 206 return project.getName() + "." + tag + counter; } 208 209 213 public static int getCounterValue(ISchemaElement elementInfo) { 214 Hashtable counters = PDEPlugin.getDefault().getDefaultNameCounters(); 215 String counterKey = getCounterKey(elementInfo); 216 Integer counter = (Integer ) counters.get(counterKey); 217 if (counter == null) { 218 counter = new Integer (1); 219 } else 220 counter = new Integer (counter.intValue() + 1); 221 counters.put(counterKey, counter); 222 return counter.intValue(); 223 } 224 225 public static String getCounterKey(ISchemaElement elementInfo) { 226 return elementInfo.getSchema().getQualifiedPointId() 227 + "." + elementInfo.getName(); } 229 230 231 232 } 233 | Popular Tags |