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.core.util;
011:
012: import java.util.StringTokenizer;
013:
014: import org.eclipse.osgi.util.NLS;
015: import org.eclipse.pde.core.plugin.IFragment;
016: import org.eclipse.pde.core.plugin.IFragmentModel;
017: import org.eclipse.pde.core.plugin.IPluginBase;
018: import org.eclipse.pde.core.plugin.IPluginExtension;
019: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
020: import org.eclipse.pde.core.plugin.IPluginModelBase;
021: import org.eclipse.pde.internal.core.text.plugin.PluginExtensionPointNode;
022:
023: public class IdUtil {
024: public static boolean isValidCompositeID(String name) {
025: if (name.length() <= 0) {
026: return false;
027: }
028: for (int i = 0; i < name.length(); i++) {
029: char c = name.charAt(i);
030: if ((c < 'A' || 'Z' < c) && (c < 'a' || 'z' < c)
031: && (c < '0' || '9' < c) && c != '_') {
032: if (i == 0 || i == name.length() - 1 || c != '.') {
033: return false;
034: }
035: }
036: }
037: return true;
038: }
039:
040: public static boolean isValidSimpleID(String name) {
041: if (name.length() <= 0) {
042: return false;
043: }
044: for (int i = 0; i < name.length(); i++) {
045: char c = name.charAt(i);
046: if ((c < 'A' || 'Z' < c) && (c < 'a' || 'z' < c)
047: && (c < '0' || '9' < c) && c != '_') {
048: return false;
049: }
050: }
051: return true;
052: }
053:
054: public static String getValidId(String projectName) {
055: return projectName.replaceAll("[^a-zA-Z0-9\\._]", "_"); //$NON-NLS-1$ //$NON-NLS-2$
056: }
057:
058: /*
059: * nameFieldQualifier must contain a placeholder variable
060: * ie. {0} Plug-in
061: */
062: public static String getValidName(String id,
063: String nameFieldQualifier) {
064: StringTokenizer tok = new StringTokenizer(id, "."); //$NON-NLS-1$
065: while (tok.hasMoreTokens()) {
066: String token = tok.nextToken();
067: if (!tok.hasMoreTokens()) {
068: String name = Character.toUpperCase(token.charAt(0))
069: + ((token.length() > 1) ? token.substring(1)
070: : ""); //$NON-NLS-1$
071: return NLS.bind(nameFieldQualifier, name);
072: }
073: }
074: return ""; //$NON-NLS-1$
075: }
076:
077: public static String getValidProvider(String id) {
078: StringTokenizer tok = new StringTokenizer(id, "."); //$NON-NLS-1$
079: int count = tok.countTokens();
080: if (count > 2 && tok.nextToken().equals("com")) //$NON-NLS-1$
081: return tok.nextToken().toUpperCase();
082: return ""; //$NON-NLS-1$
083: }
084:
085: public static String getFullId(IPluginExtension extension) {
086: String id = extension.getId();
087: IPluginBase plugin = extension.getPluginBase();
088: if ("3.2".equals(plugin.getSchemaVersion())) { //$NON-NLS-1$
089: if (id.indexOf('.') > 0)
090: return id;
091: }
092:
093: if (plugin instanceof IFragment)
094: return ((IFragment) plugin).getPluginId() + '.' + id;
095: return plugin.getId() + '.' + id;
096: }
097:
098: /**
099: * @param point
100: * @param fModel
101: * @return
102: */
103: public static String getFullId(IPluginExtensionPoint point,
104: IPluginModelBase model) {
105:
106: if ((point instanceof PluginExtensionPointNode)
107: && (model != null)) {
108: String pointId = point.getId();
109: if ("3.2".equals(model.getPluginBase().getSchemaVersion()) && pointId.indexOf('.') > 0) //$NON-NLS-1$
110: return pointId;
111: String id = null;
112: if (model instanceof IFragmentModel) {
113: IFragment fragment = ((IFragmentModel) model)
114: .getFragment();
115: if (fragment != null)
116: id = fragment.getPluginId();
117: }
118: if (id == null)
119: id = model.getPluginBase().getId();
120: return id + '.' + pointId;
121: }
122: return point.getFullId();
123: }
124:
125: public static boolean isInterestingExtensionPoint(String point) {
126: return "org.eclipse.pde.core.source".equals(point) //$NON-NLS-1$
127: || "org.eclipse.core.runtime.products".equals(point) //$NON-NLS-1$
128: || "org.eclipse.pde.core.javadoc".equals(point) //$NON-NLS-1$
129: || "org.eclipse.ui.intro".equals(point); //$NON-NLS-1$
130: }
131: }
|