KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > client > listeners > ModuleExpansion


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

11 package org.eclipse.team.internal.ccvs.core.client.listeners;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.team.internal.ccvs.core.CVSException;
20 import org.eclipse.team.internal.ccvs.core.Policy;
21 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
22
23 /**
24  *
25  */

26 public class ModuleExpansion {
27     
28     /** Name of the CVS module as found in the CVSROOT/modules file */
29     private String JavaDoc moduleName;
30     
31     /** Expansion of the module name returned by the CVS server */
32     private String JavaDoc[] expansions;
33     
34     private LocalOption[] options;
35     
36     ModuleExpansion(String JavaDoc moduleName, String JavaDoc[] expansions, LocalOption[] options) {
37         this.moduleName = moduleName;
38         this.expansions = expansions;
39         this.options = options;
40     }
41     
42     /**
43      * @see IModuleExpansion#getModuleName()
44      */

45     public String JavaDoc getModuleName() {
46         return moduleName;
47     }
48
49     /**
50      * @see IModuleExpansion#getModuleExpansionString()
51      */

52     public String JavaDoc getModuleExpansionString() {
53         String JavaDoc result = expansions[0];
54         for (int i=1;i<expansions.length; i++)
55             result = result + ", " + expansions[i]; //$NON-NLS-1$
56
return result;
57     }
58     
59     /**
60      * @see IModuleExpansion#getExpansions()
61      */

62     public String JavaDoc[] getExpansions() {
63         return expansions;
64     }
65     
66     public LocalOption[] getOptions() {
67         return options;
68     }
69     
70     /**
71      * Translate an array of module names to their expansions.
72      * The resulting List of Strings may be bigger than the original
73      */

74     private List JavaDoc getExpansionsFor(String JavaDoc[] possibleModules, Map JavaDoc moduleMappings, int depth) throws CVSException {
75         List JavaDoc result = new ArrayList JavaDoc();
76         for (int i=0;i<possibleModules.length;i++) {
77             // Is it a module?
78
if (possibleModules[i].charAt(0) == '&')
79                 result.addAll(getExpansionsFor(possibleModules[i].substring(1), moduleMappings, depth));
80             else
81                 result.add(possibleModules[i]);
82         }
83         return result;
84     }
85
86     /**
87      * Translate a module name to its expansion.
88      * The resulting List may contain one or more Strings
89      */

90     private List JavaDoc getExpansionsFor(String JavaDoc module, Map JavaDoc moduleMappings, int depth) throws CVSException {
91         if (depth > moduleMappings.size()) {
92             // Indicate that a circular reference exists
93
throw new CVSException(Policy.bind("ModuleExpansion.circular", module));//$NON-NLS-1$
94
}
95         Object JavaDoc mappings = moduleMappings.get(module);
96         if (mappings == null) {
97             // If there's no mapping assume it is a project name
98
List JavaDoc result = new ArrayList JavaDoc();
99             result.add(module);
100             return result;
101         } else {
102             // Follow any expansion chains
103
return getExpansionsFor(((ModuleExpansion)mappings).expansions, moduleMappings, depth + 1);
104         }
105     }
106     
107     /**
108      * Resolve the module mappings using moduleMappings which maps
109      * module names to their ModuleExpansion
110      */

111     public void resolveModuleReferencesUsing(Map JavaDoc moduleMappings) {
112         try {
113             List JavaDoc result = getExpansionsFor(expansions, moduleMappings, 0);
114             expansions = (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
115         } catch (CVSException e) {
116             // Is this the best way to show the circular reference problem?
117
// Or should we just leave the expansions untouched?
118
List JavaDoc result = new ArrayList JavaDoc();
119             result.add(e.getStatus().getMessage());
120             result.addAll(Arrays.asList(expansions));
121             expansions = (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
122         }
123     }
124 }
125
126
127
Popular Tags