KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > IdUtil


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.core.util;
12
13 import java.util.StringTokenizer JavaDoc;
14
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.pde.core.plugin.IFragment;
17 import org.eclipse.pde.core.plugin.IFragmentModel;
18 import org.eclipse.pde.core.plugin.IPluginBase;
19 import org.eclipse.pde.core.plugin.IPluginExtension;
20 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
21 import org.eclipse.pde.core.plugin.IPluginModelBase;
22 import org.eclipse.pde.internal.core.text.plugin.PluginExtensionPointNode;
23
24 public class IdUtil {
25     public static boolean isValidCompositeID(String JavaDoc name) {
26         if (name.length() <= 0) {
27             return false;
28         }
29         for (int i = 0; i < name.length(); i++) {
30             char c = name.charAt(i);
31             if ((c < 'A' || 'Z' < c) && (c < 'a' || 'z' < c)
32                     && (c < '0' || '9' < c) && c != '_') {
33                 if (i == 0 || i == name.length() - 1 || c != '.') {
34                     return false;
35                 }
36             }
37         }
38         return true;
39     }
40
41     public static boolean isValidSimpleID(String JavaDoc name) {
42         if (name.length() <= 0) {
43             return false;
44         }
45         for (int i = 0; i < name.length(); i++) {
46             char c = name.charAt(i);
47             if ((c < 'A' || 'Z' < c) && (c < 'a' || 'z' < c)
48                     && (c < '0' || '9' < c) && c != '_') {
49                 return false;
50             }
51         }
52         return true;
53     }
54     
55     public static String JavaDoc getValidId(String JavaDoc projectName) {
56         return projectName.replaceAll("[^a-zA-Z0-9\\._]", "_"); //$NON-NLS-1$ //$NON-NLS-2$
57
}
58     
59     /*
60      * nameFieldQualifier must contain a placeholder variable
61      * ie. {0} Plug-in
62      */

63     public static String JavaDoc getValidName(String JavaDoc id, String JavaDoc nameFieldQualifier) {
64         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(id, "."); //$NON-NLS-1$
65
while (tok.hasMoreTokens()) {
66             String JavaDoc token = tok.nextToken();
67             if (!tok.hasMoreTokens()) {
68                 String JavaDoc name = Character.toUpperCase(token.charAt(0))
69                             + ((token.length() > 1) ? token.substring(1) : ""); //$NON-NLS-1$
70
return NLS.bind(nameFieldQualifier, name);
71             }
72         }
73         return ""; //$NON-NLS-1$
74
}
75     
76     public static String JavaDoc getValidProvider(String JavaDoc id) {
77         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(id, "."); //$NON-NLS-1$
78
int count = tok.countTokens();
79         if (count > 2 && tok.nextToken().equals("com")) //$NON-NLS-1$
80
return tok.nextToken().toUpperCase();
81         return ""; //$NON-NLS-1$
82
}
83
84     public static String JavaDoc getFullId(IPluginExtension extension) {
85         String JavaDoc id = extension.getId();
86         IPluginBase plugin = extension.getPluginBase();
87         if ("3.2".equals(plugin.getSchemaVersion())) { //$NON-NLS-1$
88
if (id.indexOf('.') > 0)
89                 return id;
90         }
91         
92         if (plugin instanceof IFragment)
93             return ((IFragment) plugin).getPluginId() + '.' + id;
94         return plugin.getId() + '.' + id;
95     }
96
97     /**
98      * @param point
99      * @param fModel
100      * @return
101      */

102     public static String JavaDoc getFullId(IPluginExtensionPoint point,
103             IPluginModelBase model) {
104         
105         if ((point instanceof PluginExtensionPointNode) &&
106                 (model != null)) {
107             String JavaDoc pointId = point.getId();
108             if ("3.2".equals(model.getPluginBase().getSchemaVersion()) && pointId.indexOf('.') > 0) //$NON-NLS-1$
109
return pointId;
110             String JavaDoc id = null;
111             if (model instanceof IFragmentModel) {
112                 IFragment fragment = ((IFragmentModel)model).getFragment();
113                 if (fragment != null)
114                     id = fragment.getPluginId();
115             }
116             if (id == null)
117                 id = model.getPluginBase().getId();
118             return id + '.' + pointId;
119         }
120         return point.getFullId();
121     }
122
123     public static boolean isInterestingExtensionPoint(String JavaDoc point) {
124         return "org.eclipse.pde.core.source".equals(point) //$NON-NLS-1$
125
|| "org.eclipse.core.runtime.products".equals(point) //$NON-NLS-1$
126
|| "org.eclipse.pde.core.javadoc".equals(point) //$NON-NLS-1$
127
|| "org.eclipse.ui.intro".equals(point); //$NON-NLS-1$
128
}
129 }
130
Popular Tags