KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > registry > OrderingSupport


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.registry;
21
22 import org.netbeans.api.registry.Context;
23 import org.netbeans.api.registry.ContextException;
24 import org.netbeans.spi.registry.BasicContext;
25 import org.openide.ErrorManager;
26 import org.openide.util.TopologicalSortException;
27 import org.openide.util.Utilities;
28
29 import java.util.*;
30
31 /**
32  * This class orders context items according to positional attributes
33  * and for backward compatibility it also accepts relative ordering
34  * attributes. After the relative ordering attrs are removed the
35  * ordering functionality in this class can be simplified.
36  *
37  * There are also several checks for "ordered" attribute
38  * which are commented out till this mechanism gets common.
39  *
40  * @author copy & pasted from old Datasystems
41  */

42 public class OrderingSupport {
43
44     private static final char SEP = '/';
45
46     public static final OrderingSupport DEFAULT = new OrderingSupport();
47     
48     private OrderingSupport() {
49     }
50
51     /**
52      * @return ordered list of binding names and subcontext names.
53      *
54      */

55     public List getOrderedNames(Context ctx) {
56         
57         // check that context is ordered
58
String JavaDoc ordered = ctx.getAttribute(null, "ordered", null);
59         if (ordered == null || (!ordered.equals("true"))) {
60             // TODO: add this diagnostic later
61
// ErrorManager.getDefault().log(ErrorManager.WARNING, "Context "+ctx+" is not marked as ordered.");
62
}
63         
64         List l = getItems(ctx);
65         Map constraints = getOrderingConstraints(ctx);
66         if (constraints.keySet().size() != 0) {
67             try {
68                 l = Utilities.topologicalSort(l, constraints);
69             } catch (TopologicalSortException ex) {
70                 List corrected = ex.partialSort();
71                 ErrorManager.getDefault().log(ErrorManager.WARNING, "Note: context [" + ctx + "] cannot be consistently sorted due to ordering conflicts."); // NOI18N
72
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
73                 ErrorManager.getDefault().log(ErrorManager.WARNING, "Using partial sort: " + corrected); // NOI18N
74
l = corrected;
75             }
76         }
77         
78         // check for fullorder attribute
79
String JavaDoc fullorder = ctx.getAttribute(null, "fullorder", null);
80         if (fullorder != null) {
81             List list = new ArrayList();
82             // ensure that items are returned in specified full order
83
// and additional items are left as they are at the end
84
StringTokenizer tok = new StringTokenizer(fullorder, ",");
85             while (tok.hasMoreTokens()) {
86                 String JavaDoc token = tok.nextToken();
87                 if (l.remove(token)) {
88                     list.add(token);
89                 }
90             }
91             list.addAll(l);
92             l = list;
93         }
94         return l;
95     }
96
97     // list all items of the context
98
private List getItems(Context ctx) {
99         List l = new ArrayList();
100         Iterator i = ctx.getBindingNames().iterator();
101         while (i.hasNext()) {
102             l.add((String JavaDoc)i.next());
103         }
104         i = ctx.getSubcontextNames().iterator();
105         while (i.hasNext()) {
106             l.add(((String JavaDoc)i.next())+"/");
107         }
108         return l;
109     }
110     
111     private Map getOrderingConstraints(Context ctx) {
112         Map map = new HashMap();
113         try {
114             addPositions(map, ctx);
115         } catch (ContextException ex) {
116             ErrorManager.getDefault().log(ErrorManager.EXCEPTION, ex.toString());
117         }
118         addPartials(map, ctx);
119         return map;
120     }
121
122     
123     /**
124      * Read old partial ordering attributes. Method updates the passed map.
125      *
126      * @param m map<String, List<String>> where key is
127      * binding name or subcontext name and value is list of
128      * binding names or subcontext names which must go after the key.
129      */

130     private void addPartials(Map m, Context ctx) {
131         Iterator i = ctx.getAttributeNames(null).iterator();
132         while (i.hasNext()) {
133             String JavaDoc attrName = (String JavaDoc)i.next();
134             if (attrName.indexOf (SEP) != -1) {
135                 // TODO: convert bool attr to String in ContextImpl
136
String JavaDoc value = ctx.getAttribute(null, attrName, "");
137                 if (value.equals("true")) {
138                     int idx = attrName.indexOf(SEP);
139                     String JavaDoc a = attrName.substring(0, idx);
140                     String JavaDoc b = attrName.substring(idx + 1);
141                     
142                     // this might not work in some cases, but this is just backward solution
143
// so I guess it is OK: I expect that file with extension is a binding and
144
// file without extension is subcontext.
145
idx = a.lastIndexOf('.');
146                     if (idx != -1) {
147                         // probably binding. cut off extension
148
a = a.substring(0, idx);
149                     } else {
150                         // probably subcontext. append "/"
151
a = a + "/";
152                     }
153                     idx = b.lastIndexOf('.');
154                     if (idx != -1) {
155                         // probably binding. cut off extension
156
b = b.substring(0, idx);
157                     } else {
158                         // probably subcontext. append "/"
159
b = b + "/";
160                     }
161                     
162                     List l = (List)m.get(a);
163                     if (l == null) {
164                         m.put(a, l = new LinkedList());
165                     }
166                     l.add(b);
167                     
168                 }
169             }
170         }
171     }
172
173     /**
174      * Read positional ordering attributes. Method updates the passed map.
175      *
176      * @param m map<String, List<String>> where key is
177      * binding name or subcontext name and value is list of
178      * binding names or subcontext names which must go after the key.
179      */

180     private void addPositions(Map m, Context ctx) throws ContextException {
181         Map map = new HashMap();
182         BasicContext basicCtx = ApiContextFactory.DEFAULT.getBasicContext(ctx);
183         Iterator i = ctx.getBindingNames().iterator();
184         while (i.hasNext()) {
185             String JavaDoc bindingName = (String JavaDoc)i.next();
186             String JavaDoc position = ctx.getAttribute(bindingName, "position", null);
187             if (position != null) {
188                 try {
189                     Float JavaDoc f = new Float JavaDoc(position);
190                     String JavaDoc bnd = (String JavaDoc)map.put(f, bindingName);
191                     if (bnd != null) {
192                         ErrorManager.getDefault().log(ErrorManager.WARNING, "Ordering Error: Two items in context ["+ctx+
193                             "] has the same position ["+position+"]: "+bnd+" "+bindingName);
194                     }
195                 } catch (NumberFormatException JavaDoc ex) {
196                     // ok to ignore.
197
}
198             } else {
199                 // TODO: add this diagnostic later
200
// ErrorManager.getDefault().log(ErrorManager.WARNING, "Binding "+bindingName+" in context "+ctx+" does not have position attribute.");
201
}
202         }
203         i = ctx.getSubcontextNames().iterator();
204         while (i.hasNext()) {
205             String JavaDoc subcontextName = (String JavaDoc)i.next();
206             Context subCtx = ctx.getSubcontext(subcontextName);
207             if (subCtx == null) {
208                 continue;
209             }
210             String JavaDoc position = subCtx.getAttribute(null, "position", null);
211             if (position != null) {
212                 try {
213                     Float JavaDoc f = new Float JavaDoc(position);
214                     String JavaDoc sub = (String JavaDoc)map.put(f, subcontextName+"/");
215                     if (sub != null) {
216                         ErrorManager.getDefault().log(ErrorManager.WARNING, "Ordering Error: Two items in context ["+ctx+
217                             "] has the same position ["+position+"]: "+sub+" "+subcontextName);
218                     }
219                 } catch (NumberFormatException JavaDoc ex) {
220                     // ok to ignore.
221
}
222             } else {
223                 // TODO: add this diagnostic later
224
// ErrorManager.getDefault().log(ErrorManager.WARNING, "Subcontext "+subcontextName+" in context "+ctx+" does not have position attribute.");
225
}
226         }
227         List l = new ArrayList(map.keySet());
228         Collections.sort(l);
229         for (int k=0; k<l.size(); k++) {
230             String JavaDoc itemA = (String JavaDoc)map.get(l.get(k));
231             List list = (List)m.get(itemA);
232             if (list == null) {
233                 m.put(itemA, list = new LinkedList());
234             }
235             for (int j=k+1; j<l.size(); j++) {
236                 String JavaDoc itemB = (String JavaDoc)map.get(l.get(j));
237                 list.add(itemB);
238             }
239         }
240     }
241     
242     
243 }
244
Popular Tags