001: /*******************************************************************************
002: * Copyright (c) 2006 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.ui.internal.navigator.extensions;
011:
012: import java.util.HashSet;
013: import java.util.Iterator;
014: import java.util.Set;
015: import java.util.regex.Pattern;
016:
017: import org.eclipse.core.runtime.Assert;
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.osgi.util.NLS;
020: import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
021: import org.eclipse.ui.internal.navigator.NavigatorPlugin;
022:
023: class Binding {
024:
025: private final Set rootPatterns = new HashSet();
026:
027: private final Set includePatterns = new HashSet();
028:
029: private final Set excludePatterns = new HashSet();
030:
031: private final String TAG_EXTENSION;
032:
033: protected Binding(String tagExtension) {
034: TAG_EXTENSION = tagExtension;
035: }
036:
037: /*
038: * (non-Javadoc)
039: *
040: * @see org.eclipse.ui.internal.navigator.extensions.INavigatorViewerDescriptor#isVisibleExtension(java.lang.String)
041: */
042: boolean isVisibleExtension(String anExtensionId) {
043: Pattern pattern = null;
044: for (Iterator itr = includePatterns.iterator(); itr.hasNext();) {
045: pattern = (Pattern) itr.next();
046: if (pattern.matcher(anExtensionId).matches()) {
047: return true;
048: }
049: }
050:
051: for (Iterator itr = excludePatterns.iterator(); itr.hasNext();) {
052: pattern = (Pattern) itr.next();
053: if (pattern.matcher(anExtensionId).matches()) {
054: return false;
055: }
056: }
057: return false;
058: }
059:
060: /*
061: * (non-Javadoc)
062: *
063: * @see org.eclipse.ui.internal.navigator.extensions.INavigatorViewerDescriptor#isRootExtension(java.lang.String)
064: */
065: boolean isRootExtension(String anExtensionId) {
066: if (rootPatterns.size() == 0) {
067: return false;
068: }
069: Pattern pattern = null;
070: for (Iterator itr = rootPatterns.iterator(); itr.hasNext();) {
071: pattern = (Pattern) itr.next();
072: if (pattern.matcher(anExtensionId).matches()) {
073: return true;
074: }
075: }
076: return false;
077: }
078:
079: /*
080: * (non-Javadoc)
081: *
082: * @see org.eclipse.ui.internal.navigator.extensions.INavigatorViewerDescriptor#hasOverriddenRootExtensions()
083: */
084: boolean hasOverriddenRootExtensions() {
085: return rootPatterns.size() > 0;
086: }
087:
088: void consumeIncludes(IConfigurationElement element,
089: boolean toRespectRoots) {
090:
091: Assert.isTrue(NavigatorViewerDescriptor.TAG_INCLUDES
092: .equals(element.getName()));
093: IConfigurationElement[] contentExtensionPatterns = element
094: .getChildren(TAG_EXTENSION);
095: String isRootString = null;
096: boolean isRoot = false;
097: String patternString = null;
098: Pattern compiledPattern = null;
099: for (int i = 0; i < contentExtensionPatterns.length; i++) {
100: if (toRespectRoots) {
101: isRootString = contentExtensionPatterns[i]
102: .getAttribute(NavigatorViewerDescriptor.ATT_IS_ROOT);
103: isRoot = (isRootString != null) ? Boolean.valueOf(
104: isRootString.trim()).booleanValue() : false;
105: }
106:
107: patternString = contentExtensionPatterns[i]
108: .getAttribute(NavigatorViewerDescriptor.ATT_PATTERN);
109: if (patternString == null) {
110: NavigatorPlugin
111: .logError(
112: 0,
113: NLS
114: .bind(
115: CommonNavigatorMessages.Attribute_Missing_Warning,
116: new Object[] {
117: NavigatorViewerDescriptor.ATT_PATTERN,
118: element
119: .getDeclaringExtension()
120: .getUniqueIdentifier(),
121: element
122: .getDeclaringExtension()
123: .getNamespace() }),
124: null);
125: } else {
126: compiledPattern = Pattern.compile(patternString);
127: includePatterns.add(compiledPattern);
128: if (toRespectRoots && isRoot) {
129: rootPatterns.add(compiledPattern);
130: }
131: }
132: }
133:
134: }
135:
136: void consumeExcludes(IConfigurationElement element) {
137: Assert.isTrue(NavigatorViewerDescriptor.TAG_EXCLUDES
138: .equals(element.getName()));
139: IConfigurationElement[] contentExtensionPatterns = element
140: .getChildren(TAG_EXTENSION);
141: String patternString = null;
142: Pattern compiledPattern = null;
143: for (int i = 0; i < contentExtensionPatterns.length; i++) {
144:
145: patternString = contentExtensionPatterns[i]
146: .getAttribute(NavigatorViewerDescriptor.ATT_PATTERN);
147: if (patternString == null) {
148: NavigatorPlugin
149: .logError(
150: 0,
151: NLS
152: .bind(
153: CommonNavigatorMessages.Attribute_Missing_Warning,
154: new Object[] {
155: NavigatorViewerDescriptor.ATT_PATTERN,
156: element
157: .getDeclaringExtension()
158: .getUniqueIdentifier(),
159: element
160: .getDeclaringExtension()
161: .getNamespace() }),
162: null);
163: } else {
164: compiledPattern = Pattern.compile(patternString);
165: excludePatterns.add(compiledPattern);
166: }
167: }
168:
169: }
170: }
|