KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > util > Context


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.subversion.util;
21
22 import org.netbeans.modules.versioning.util.FlatFolder;
23
24 import java.io.File JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.util.*;
27
28 /**
29  * Encapsulates context of an action. There are two ways in which context may be defined:
30  * - list of files (f/b.txt, f/c.txt, f/e.txt)
31  * - list of roots (top folders) plus list of exclusions (f),(a.txt, d.txt)
32  *
33  * @author Maros Sandor
34  */

35 public class Context implements Serializable JavaDoc {
36
37     public static final Context Empty = new Context( getEmptyList(), getEmptyList(), getEmptyList() );
38
39     private static final long serialVersionUID = 1L;
40     
41     private final List<File JavaDoc> filteredFiles;
42     private final List<File JavaDoc> rootFiles;
43     private final List<File JavaDoc> exclusions;
44
45     public Context(List<File JavaDoc> filteredFiles, List<File JavaDoc> rootFiles, List<File JavaDoc> exclusions) {
46         this.filteredFiles = filteredFiles;
47         this.rootFiles = rootFiles;
48         this.exclusions = exclusions;
49         while (normalize());
50     }
51
52     public Context(File JavaDoc file) {
53         this(new File JavaDoc [] { file });
54     }
55
56     public Context(File JavaDoc [] files) {
57         List<File JavaDoc> list = new ArrayList<File JavaDoc>(files.length);
58         list.addAll(Arrays.asList(files));
59         removeDuplicates(list);
60         this.filteredFiles = list;
61         this.rootFiles = list;
62         this.exclusions = Collections.emptyList();
63     }
64
65     private boolean normalize() {
66         for (Iterator<File JavaDoc> i = rootFiles.iterator(); i.hasNext();) {
67             File JavaDoc root = i.next();
68             for (Iterator<File JavaDoc> j = exclusions.iterator(); j.hasNext();) {
69                 File JavaDoc exclusion = j.next();
70                 if (SvnUtils.isParentOrEqual(exclusion, root)) {
71                     j.remove();
72                     exclusionRemoved(exclusion, root);
73                     return true;
74                 }
75             }
76         }
77         removeDuplicates(rootFiles);
78         removeDuplicates(exclusions);
79         return false;
80     }
81
82     private void removeDuplicates(List<File JavaDoc> files) {
83         List<File JavaDoc> newFiles = new ArrayList<File JavaDoc>();
84         outter: for (Iterator<File JavaDoc> i = files.iterator(); i.hasNext();) {
85             File JavaDoc file = i.next();
86             for (Iterator<File JavaDoc> j = newFiles.iterator(); j.hasNext();) {
87                 File JavaDoc includedFile = j.next();
88                 if (SvnUtils.isParentOrEqual(includedFile, file) && (file.isFile() || !(includedFile instanceof FlatFolder))) continue outter;
89                 if (SvnUtils.isParentOrEqual(file, includedFile) && (includedFile.isFile() || !(file instanceof FlatFolder))) {
90                     j.remove();
91                 }
92             }
93             newFiles.add(file);
94         }
95         files.clear();
96         files.addAll(newFiles);
97     }
98     
99     private void exclusionRemoved(File JavaDoc exclusion, File JavaDoc root) {
100         File JavaDoc [] exclusionChildren = exclusion.listFiles();
101         if (exclusionChildren == null) return;
102         for (int i = 0; i < exclusionChildren.length; i++) {
103             File JavaDoc child = exclusionChildren[i];
104             if (!SvnUtils.isParentOrEqual(root, child)) {
105                 exclusions.add(child);
106             }
107         }
108     }
109
110     public List<File JavaDoc> getRoots() {
111         return rootFiles;
112     }
113
114     public List<File JavaDoc> getExclusions() {
115         return exclusions;
116     }
117
118     /**
119      * Gets exact set of files to operate on, it is effectively defined as (rootFiles - exclusions). This set
120      * is NOT suitable for Update command because Update should operate on all rootFiles and just exclude some subfolders.
121      * Otherwise update misses new files and folders directly in rootFiles folders.
122      *
123      * @return files to operate on
124      */

125     public File JavaDoc [] getFiles() {
126         return filteredFiles.toArray(new File JavaDoc[filteredFiles.size()]);
127     }
128
129     public File JavaDoc[] getRootFiles() {
130         return rootFiles.toArray(new File JavaDoc[rootFiles.size()]);
131     }
132     
133     public boolean contains(File JavaDoc file) {
134         outter : for (Iterator i = rootFiles.iterator(); i.hasNext();) {
135             File JavaDoc root = (File JavaDoc) i.next();
136             if (SvnUtils.isParentOrEqual(root, file)) {
137                 for (Iterator j = exclusions.iterator(); j.hasNext();) {
138                     File JavaDoc excluded = (File JavaDoc) j.next();
139                     if (SvnUtils.isParentOrEqual(excluded, file)) {
140                         continue outter;
141                     }
142                 }
143                 return true;
144             }
145         }
146         return false;
147     }
148
149     public static final List<File JavaDoc> getEmptyList() {
150         // XXX
151
return Collections.emptyList();
152     }
153 }
154
Popular Tags