KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > extensions > Binding


1 /*******************************************************************************
2  * Copyright (c) 2006 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
12 package org.eclipse.ui.internal.navigator.extensions;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.regex.Pattern JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
23 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
24
25 class Binding {
26
27     private final Set JavaDoc rootPatterns = new HashSet JavaDoc();
28
29     private final Set JavaDoc includePatterns = new HashSet JavaDoc();
30
31     private final Set JavaDoc excludePatterns = new HashSet JavaDoc();
32
33     private final String JavaDoc TAG_EXTENSION;
34
35     protected Binding(String JavaDoc tagExtension) {
36         TAG_EXTENSION = tagExtension;
37     }
38
39     /*
40      * (non-Javadoc)
41      *
42      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorViewerDescriptor#isVisibleExtension(java.lang.String)
43      */

44     boolean isVisibleExtension(String JavaDoc anExtensionId) {
45         Pattern JavaDoc pattern = null;
46         for (Iterator JavaDoc itr = includePatterns.iterator(); itr.hasNext();) {
47             pattern = (Pattern JavaDoc) itr.next();
48             if (pattern.matcher(anExtensionId).matches()) {
49                 return true;
50             }
51         }
52
53         for (Iterator JavaDoc itr = excludePatterns.iterator(); itr.hasNext();) {
54             pattern = (Pattern JavaDoc) itr.next();
55             if (pattern.matcher(anExtensionId).matches()) {
56                 return false;
57             }
58         }
59         return false;
60     }
61
62     /*
63      * (non-Javadoc)
64      *
65      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorViewerDescriptor#isRootExtension(java.lang.String)
66      */

67     boolean isRootExtension(String JavaDoc anExtensionId) {
68         if (rootPatterns.size() == 0) {
69             return false;
70         }
71         Pattern JavaDoc pattern = null;
72         for (Iterator JavaDoc itr = rootPatterns.iterator(); itr.hasNext();) {
73             pattern = (Pattern JavaDoc) itr.next();
74             if (pattern.matcher(anExtensionId).matches()) {
75                 return true;
76             }
77         }
78         return false;
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorViewerDescriptor#hasOverriddenRootExtensions()
85      */

86     boolean hasOverriddenRootExtensions() {
87         return rootPatterns.size() > 0;
88     }
89
90     void consumeIncludes(IConfigurationElement element, boolean toRespectRoots) {
91
92         Assert.isTrue(NavigatorViewerDescriptor.TAG_INCLUDES.equals(element
93                 .getName()));
94         IConfigurationElement[] contentExtensionPatterns = element
95                 .getChildren(TAG_EXTENSION);
96         String JavaDoc isRootString = null;
97         boolean isRoot = false;
98         String JavaDoc patternString = null;
99         Pattern JavaDoc compiledPattern = null;
100         for (int i = 0; i < contentExtensionPatterns.length; i++) {
101             if (toRespectRoots) {
102                 isRootString = contentExtensionPatterns[i]
103                         .getAttribute(NavigatorViewerDescriptor.ATT_IS_ROOT);
104                 isRoot = (isRootString != null) ? Boolean.valueOf(
105                         isRootString.trim()).booleanValue() : false;
106             }
107
108             patternString = contentExtensionPatterns[i]
109                     .getAttribute(NavigatorViewerDescriptor.ATT_PATTERN);
110             if (patternString == null) {
111                 NavigatorPlugin
112                         .logError(
113                                 0,
114                                 NLS
115                                         .bind(
116                                                 CommonNavigatorMessages.Attribute_Missing_Warning,
117                                                 new Object JavaDoc[] {
118                                                         NavigatorViewerDescriptor.ATT_PATTERN,
119                                                         element
120                                                                 .getDeclaringExtension()
121                                                                 .getUniqueIdentifier(),
122                                                         element
123                                                                 .getDeclaringExtension()
124                                                                 .getNamespace() }),
125                                 null);
126             } else {
127                 compiledPattern = Pattern.compile(patternString);
128                 includePatterns.add(compiledPattern);
129                 if (toRespectRoots && isRoot) {
130                     rootPatterns.add(compiledPattern);
131                 }
132             }
133         }
134
135     }
136
137     void consumeExcludes(IConfigurationElement element) {
138         Assert.isTrue(NavigatorViewerDescriptor.TAG_EXCLUDES.equals(element
139                 .getName()));
140         IConfigurationElement[] contentExtensionPatterns = element
141                 .getChildren(TAG_EXTENSION);
142         String JavaDoc patternString = null;
143         Pattern JavaDoc compiledPattern = null;
144         for (int i = 0; i < contentExtensionPatterns.length; i++) {
145
146             patternString = contentExtensionPatterns[i]
147                     .getAttribute(NavigatorViewerDescriptor.ATT_PATTERN);
148             if (patternString == null) {
149                 NavigatorPlugin
150                         .logError(
151                                 0,
152                                 NLS
153                                         .bind(
154                                                 CommonNavigatorMessages.Attribute_Missing_Warning,
155                                                 new Object JavaDoc[] {
156                                                         NavigatorViewerDescriptor.ATT_PATTERN,
157                                                         element
158                                                                 .getDeclaringExtension()
159                                                                 .getUniqueIdentifier(),
160                                                         element
161                                                                 .getDeclaringExtension()
162                                                                 .getNamespace() }),
163                                 null);
164             } else {
165                 compiledPattern = Pattern.compile(patternString);
166                 excludePatterns.add(compiledPattern);
167             }
168         }
169
170     }
171 }
Popular Tags