KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > XMLUtil


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.text;
12
13 import java.util.Hashtable JavaDoc;
14 import java.util.Locale JavaDoc;
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     /**
37      * Scans up the node's parents till it reaches
38      * a IPluginExtension or IPluginExtensionPoint (or null)
39      * and returns the result.
40      *
41      * @param node
42      * @return the IPluginExtension or IPluginExtensionPoint that contains <code>node</code>
43      */

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             // not an attribute/text node/element -> return direct node
54
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     /**
88      * Get the ISchemaElement corresponding to this IDocumentNode
89      * @param node
90      * @param extensionPoint the extension point of the schema, if <code>null</code> it will be deduced
91      * @return the ISchemaElement for <code>node</code>
92      */

93     public static ISchemaElement getSchemaElement(IDocumentNode node, String JavaDoc 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     /**
109      * Get the ISchemaAttribute corresponding to this IDocumentAttribute
110      * @param attr
111      * @param extensionPoint the extension point of the schema, if <code>null</code> it will be deduced
112      * @return the ISchemaAttribute for <code>attr</code>
113      */

114     public static ISchemaAttribute getSchemaAttribute(IDocumentAttribute attr, String JavaDoc 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     /**
124      * @param project
125      * @param attInfo
126      * @param counter
127      * @return
128      */

129     public static String JavaDoc createDefaultClassName(IProject project,
130             ISchemaAttribute attInfo, int counter) {
131         String JavaDoc tag = attInfo.getParent().getName();
132         String JavaDoc expectedType = attInfo.getBasedOn();
133         String JavaDoc className = ""; //$NON-NLS-1$
134
if (expectedType == null) {
135             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(tag);
136             buf.setCharAt(0, Character.toUpperCase(tag.charAt(0)));
137             className = buf.toString();
138         } else {
139             // package will be the same as the plugin ID
140
// class name will be generated based on the required interface
141
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 JavaDoc packageName = createDefaultPackageName(project, className);
150         className += counter;
151         return packageName + "." + className; //$NON-NLS-1$
152
}
153     
154     
155     /**
156      * @param id
157      * @param className
158      * @return
159      */

160     public static String JavaDoc createDefaultPackageName(IProject project, String JavaDoc className) {
161         String JavaDoc id = project.getName();
162         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
195      * @param project
196      * @param attInfo
197      * @param counter
198      * @return
199      */

200     public static String JavaDoc 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 JavaDoc 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 JavaDoc counters = PDEPlugin.getDefault().getDefaultNameCounters();
215         String JavaDoc counterKey = getCounterKey(elementInfo);
216         Integer JavaDoc counter = (Integer JavaDoc) counters.get(counterKey);
217         if (counter == null) {
218             counter = new Integer JavaDoc(1);
219         } else
220             counter = new Integer JavaDoc(counter.intValue() + 1);
221         counters.put(counterKey, counter);
222         return counter.intValue();
223     }
224
225     public static String JavaDoc getCounterKey(ISchemaElement elementInfo) {
226         return elementInfo.getSchema().getQualifiedPointId()
227                 + "." + elementInfo.getName(); //$NON-NLS-1$
228
}
229     
230
231     
232 }
233
Popular Tags