001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.text;
011:
012: import java.util.Hashtable;
013: import java.util.Locale;
014:
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.jdt.core.JavaConventions;
018: import org.eclipse.pde.core.plugin.IPluginElement;
019: import org.eclipse.pde.core.plugin.IPluginExtension;
020: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
021: import org.eclipse.pde.core.plugin.IPluginObject;
022: import org.eclipse.pde.internal.core.PDECore;
023: import org.eclipse.pde.internal.core.ischema.ISchema;
024: import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
025: import org.eclipse.pde.internal.core.ischema.ISchemaElement;
026: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
027: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
028: import org.eclipse.pde.internal.core.text.IDocumentRange;
029: import org.eclipse.pde.internal.core.text.IDocumentTextNode;
030: import org.eclipse.pde.internal.core.util.PDEJavaHelper;
031: import org.eclipse.pde.internal.ui.PDEPlugin;
032:
033: public abstract class XMLUtil {
034:
035: /**
036: * Scans up the node's parents till it reaches
037: * a IPluginExtension or IPluginExtensionPoint (or null)
038: * and returns the result.
039: *
040: * @param node
041: * @return the IPluginExtension or IPluginExtensionPoint that contains <code>node</code>
042: */
043: public static IPluginObject getTopLevelParent(IDocumentRange range) {
044: IDocumentElementNode node = null;
045: if (range instanceof IDocumentAttributeNode)
046: node = ((IDocumentAttributeNode) range)
047: .getEnclosingElement();
048: else if (range instanceof IDocumentTextNode)
049: node = ((IDocumentTextNode) range).getEnclosingElement();
050: else if (range instanceof IPluginElement)
051: node = (IDocumentElementNode) range;
052: else if (range instanceof IPluginObject)
053: // not an attribute/text node/element -> return direct node
054: return (IPluginObject) range;
055:
056: while (node != null && !(node instanceof IPluginExtension)
057: && !(node instanceof IPluginExtensionPoint))
058: node = node.getParentNode();
059:
060: return node != null ? (IPluginObject) node : null;
061: }
062:
063: private static boolean withinRange(int start, int len, int offset) {
064: return start <= offset && offset <= start + len;
065: }
066:
067: public static boolean withinRange(IDocumentRange range, int offset) {
068: if (range instanceof IDocumentAttributeNode)
069: return withinRange(((IDocumentAttributeNode) range)
070: .getValueOffset(), ((IDocumentAttributeNode) range)
071: .getValueLength(), offset);
072: if (range instanceof IDocumentElementNode)
073: return withinRange(((IDocumentElementNode) range)
074: .getOffset(), ((IDocumentElementNode) range)
075: .getLength(), offset);
076: if (range instanceof IDocumentTextNode)
077: return withinRange(((IDocumentTextNode) range).getOffset(),
078: ((IDocumentTextNode) range).getLength(), offset);
079: return false;
080: }
081:
082: /**
083: * Get the ISchemaElement corresponding to this IDocumentElementNode
084: * @param node
085: * @param extensionPoint the extension point of the schema, if <code>null</code> it will be deduced
086: * @return the ISchemaElement for <code>node</code>
087: */
088: public static ISchemaElement getSchemaElement(
089: IDocumentElementNode node, String extensionPoint) {
090: if (extensionPoint == null) {
091: IPluginObject obj = getTopLevelParent(node);
092: if (!(obj instanceof IPluginExtension))
093: return null;
094: extensionPoint = ((IPluginExtension) obj).getPoint();
095: }
096: ISchema schema = PDECore.getDefault().getSchemaRegistry()
097: .getSchema(extensionPoint);
098: if (schema == null)
099: return null;
100:
101: ISchemaElement sElement = schema.findElement(node
102: .getXMLTagName());
103: return sElement;
104: }
105:
106: /**
107: * Get the ISchemaAttribute corresponding to this IDocumentAttributeNode
108: * @param attr
109: * @param extensionPoint the extension point of the schema, if <code>null</code> it will be deduced
110: * @return the ISchemaAttribute for <code>attr</code>
111: */
112: public static ISchemaAttribute getSchemaAttribute(
113: IDocumentAttributeNode attr, String extensionPoint) {
114: ISchemaElement ele = getSchemaElement(attr
115: .getEnclosingElement(), extensionPoint);
116: if (ele == null)
117: return null;
118:
119: return ele.getAttribute(attr.getAttributeName());
120: }
121:
122: /**
123: * @param project
124: * @param attInfo
125: * @param counter
126: * @return
127: */
128: public static String createDefaultClassName(IProject project,
129: ISchemaAttribute attInfo, int counter) {
130: String tag = attInfo.getParent().getName();
131: String expectedType = attInfo.getBasedOn();
132: String className = ""; //$NON-NLS-1$
133: if (expectedType == null) {
134: StringBuffer buf = new StringBuffer(tag);
135: buf.setCharAt(0, Character.toUpperCase(tag.charAt(0)));
136: className = buf.toString();
137: } else {
138: // package will be the same as the plugin ID
139: // class name will be generated based on the required interface
140: className = expectedType;
141: int dotLoc = className.lastIndexOf('.');
142: if (dotLoc != -1)
143: className = className.substring(dotLoc + 1);
144: if (className.length() > 2 && className.charAt(0) == 'I'
145: && Character.isUpperCase(className.charAt(1)))
146: className = className.substring(1);
147: }
148: String packageName = createDefaultPackageName(project,
149: className);
150: className += counter;
151: return packageName + "." + className; //$NON-NLS-1$
152: }
153:
154: /**
155: * @param id
156: * @param className
157: * @return
158: */
159: public static String createDefaultPackageName(IProject project,
160: 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(buffer
174: .toString(), PDEJavaHelper
175: .getJavaSourceLevel(project), PDEJavaHelper
176: .getJavaComplianceLevel(project));
177: if (status.getSeverity() == IStatus.ERROR)
178: buffer.append(className
179: .toLowerCase(Locale.ENGLISH));
180: buffer.append(ch);
181: }
182: }
183: }
184:
185: status = JavaConventions.validatePackageName(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: /**
195: * @param project
196: * @param attInfo
197: * @param counter
198: * @return
199: */
200: public static String createDefaultName(IProject project,
201: ISchemaAttribute attInfo, int counter) {
202: if (attInfo.getType().getName().equals("boolean")) //$NON-NLS-1$
203: return "true"; //$NON-NLS-1$
204:
205: String tag = attInfo.getParent().getName();
206: return project.getName() + "." + tag + counter; //$NON-NLS-1$
207: }
208:
209: /**
210: * @param elementInfo
211: * @return
212: */
213: public static int getCounterValue(ISchemaElement elementInfo) {
214: Hashtable counters = PDEPlugin.getDefault()
215: .getDefaultNameCounters();
216: String counterKey = getCounterKey(elementInfo);
217: Integer counter = (Integer) counters.get(counterKey);
218: if (counter == null) {
219: counter = new Integer(1);
220: } else
221: counter = new Integer(counter.intValue() + 1);
222: counters.put(counterKey, counter);
223: return counter.intValue();
224: }
225:
226: public static String getCounterKey(ISchemaElement elementInfo) {
227: return elementInfo.getSchema().getQualifiedPointId()
228: + "." + elementInfo.getName(); //$NON-NLS-1$
229: }
230:
231: }
|