KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > jmerge > JPatternDictionary


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: JPatternDictionary.java,v 1.5 2005/06/12 13:19:04 emerks Exp $
16  */

17 package org.eclipse.emf.codegen.jmerge;
18
19
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.regex.Matcher JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 import org.eclipse.jdt.core.jdom.IDOMCompilationUnit;
30 import org.eclipse.jdt.core.jdom.IDOMField;
31 import org.eclipse.jdt.core.jdom.IDOMImport;
32 import org.eclipse.jdt.core.jdom.IDOMInitializer;
33 import org.eclipse.jdt.core.jdom.IDOMMethod;
34 import org.eclipse.jdt.core.jdom.IDOMNode;
35 import org.eclipse.jdt.core.jdom.IDOMPackage;
36 import org.eclipse.jdt.core.jdom.IDOMType;
37
38
39 /**
40  * A dictionary of signatures and JDOM nodes.
41  */

42 public class JPatternDictionary
43 {
44   protected IDOMCompilationUnit compilationUnit;
45   protected IDOMPackage jPackage;
46   protected JControlModel options;
47   protected Map JavaDoc importMap = new HashMap JavaDoc();
48   protected Map JavaDoc typeMap = new HashMap JavaDoc();
49   protected Map JavaDoc initializerMap = new HashMap JavaDoc();
50   protected Map JavaDoc fieldMap = new HashMap JavaDoc();
51   protected Map JavaDoc methodMap = new HashMap JavaDoc();
52   protected Map JavaDoc markupMap = new HashMap JavaDoc();
53   protected Collection JavaDoc noImportSet = new HashSet JavaDoc();
54
55   /**
56    * This creates an instance.
57    */

58   public JPatternDictionary(IDOMCompilationUnit compilationUnit, JControlModel options)
59   {
60     this.options = options;
61     analyzeCompilationUnit(compilationUnit);
62   }
63
64   protected void analyzeCompilationUnit(IDOMCompilationUnit compilationUnit)
65   {
66     this.compilationUnit = compilationUnit;
67
68     if (options.getNoImportPattern() != null)
69     {
70       Matcher JavaDoc matcher = options.getNoImportPattern().matcher(compilationUnit.getContents());
71       while (matcher.find())
72       {
73         noImportSet.add(matcher.group(1));
74       }
75     }
76
77     match(compilationUnit);
78     for (IDOMNode child = compilationUnit.getFirstChild(); child != null; child = child.getNextNode())
79     {
80       switch (child.getNodeType())
81       {
82         case IDOMNode.PACKAGE:
83         {
84           analyzePackage((IDOMPackage)child);
85           break;
86         }
87         case IDOMNode.IMPORT:
88         {
89           analyzeImport((IDOMImport)child);
90           break;
91         }
92         case IDOMNode.TYPE:
93         {
94           analyzeType((IDOMType)child);
95           break;
96         }
97       }
98     }
99   }
100
101   protected void analyzePackage(IDOMPackage jPackage)
102   {
103     this.jPackage = jPackage;
104     match(jPackage);
105   }
106
107   protected void analyzeImport(IDOMImport jImport)
108   {
109     importMap.put(getQualifiedName(jImport), jImport);
110     match(jImport);
111   }
112
113   protected void analyzeType(IDOMType type)
114   {
115     typeMap.put(getQualifiedName(type), type);
116     match(type);
117     for (IDOMNode child = type.getFirstChild(); child != null; child = child.getNextNode())
118     {
119       switch (child.getNodeType())
120       {
121         case IDOMNode.INITIALIZER:
122         {
123           analyzeInitializer((IDOMInitializer)child);
124           break;
125         }
126         case IDOMNode.FIELD:
127         {
128           analyzeField((IDOMField)child);
129           break;
130         }
131         case IDOMNode.METHOD:
132         {
133           analyzeMethod((IDOMMethod)child);
134           break;
135         }
136         case IDOMNode.TYPE:
137         {
138           analyzeType((IDOMType)child);
139           break;
140         }
141       }
142     }
143   }
144
145   protected void analyzeInitializer(IDOMInitializer initializer)
146   {
147     initializerMap.put(getQualifiedName(initializer), initializer);
148     match(initializer);
149   }
150
151   protected void analyzeField(IDOMField field)
152   {
153     fieldMap.put(getQualifiedName(field), field);
154     match(field);
155   }
156
157   protected void analyzeMethod(IDOMMethod method)
158   {
159     methodMap.put(getQualifiedName(method), method);
160     match(method);
161   }
162
163   public String JavaDoc getQualifiedName(IDOMNode jdomNode)
164   {
165     switch (jdomNode.getNodeType())
166     {
167       case IDOMNode.COMPILATION_UNIT:
168       {
169         return jdomNode.getName();
170       }
171       case IDOMNode.PACKAGE:
172       {
173         return jdomNode.getName();
174       }
175       case IDOMNode.IMPORT:
176       {
177         return jdomNode.getName();
178       }
179       case IDOMNode.TYPE:
180       {
181         return jPackage != null ? jPackage.getName() + "." + jdomNode.getName() : jdomNode.getName();
182       }
183       case IDOMNode.FIELD:
184       {
185         return getQualifiedName(jdomNode.getParent()) + "." + jdomNode.getName();
186       }
187       case IDOMNode.INITIALIZER:
188       {
189         String JavaDoc name = getQualifiedName(jdomNode.getParent());
190         int index = 0;
191         for (jdomNode = jdomNode.getNextNode(); jdomNode != null; jdomNode = jdomNode.getNextNode())
192         {
193           if (jdomNode.getNodeType() == IDOMNode.INITIALIZER)
194           {
195             ++index;
196           }
197         }
198         return name + "." + index;
199       }
200       case IDOMNode.METHOD:
201       {
202         IDOMMethod jdomMethod = (IDOMMethod)jdomNode;
203         StringBuffer JavaDoc result = new StringBuffer JavaDoc(getQualifiedName(jdomNode.getParent()));
204         result.append(".");
205         if (jdomMethod.isConstructor())
206         {
207           result.append(jdomMethod.getParent().getName());
208         }
209         else
210         {
211           result.append(jdomMethod.getName());
212         }
213         result.append("("); //)
214
String JavaDoc [] parameters = jdomMethod.getParameterTypes();
215         if (parameters != null)
216         {
217           for (int i = 0; i < parameters.length; ++i)
218           {
219             if (i != 0)
220             {
221               result.append(", ");
222             }
223             result.append(parameters[i]);
224           }
225         }
226         // (
227
result.append(")");
228         return result.toString();
229       }
230       default:
231       {
232         return "";
233       }
234     }
235   }
236
237   public void dump()
238   {
239     System.out.println("---- imports ---------------------------------------------");
240     dumpStringToIDOMNodeMap(importMap);
241     System.out.println("---- types -----------------------------------------------");
242     dumpStringToIDOMNodeMap(typeMap);
243     System.out.println("---- initializers ----------------------------------------");
244     dumpStringToIDOMNodeMap(initializerMap);
245     System.out.println("---- fields ----------------------------------------------");
246     dumpStringToIDOMNodeMap(fieldMap);
247     System.out.println("---- methods ---------------------------------------------");
248     dumpStringToIDOMNodeMap(methodMap);
249
250     dumpMarkup();
251   }
252
253   public void dumpMarkup()
254   {
255     System.out.println("==== markup ============================================");
256     for (Iterator JavaDoc entries = markupMap.entrySet().iterator(); entries.hasNext(); )
257     {
258       Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
259       System.out.println("==== " + entry.getKey() + " ============================================");
260       for (Iterator JavaDoc values = ((Collection JavaDoc)entry.getValue()).iterator(); values.hasNext(); )
261       {
262         IDOMNode node = (IDOMNode)values.next();
263         System.out.println(getQualifiedName(node));
264         //dumpNodeContents(node);
265
}
266     }
267   }
268
269   public void dumpNodeContents(IDOMNode node)
270   {
271     System.out.println("____ " + getQualifiedName(node) + " ____________________________________________");
272     System.out.print(node.getContents());
273     System.out.println("_____________________________________________________________________");
274   }
275
276   public void dumpStringToIDOMNodeMap(Map JavaDoc map)
277   {
278     for (Iterator JavaDoc entries = map.entrySet().iterator(); entries.hasNext(); )
279     {
280       Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
281       String JavaDoc key = (String JavaDoc)entry.getKey();
282       IDOMNode node = (IDOMNode)entry.getValue();
283       System.out.println(key + "->" + getQualifiedName(node));
284       // dumpNodeContents(node);
285
}
286   }
287
288   protected static Pattern JavaDoc comment = Pattern.compile("/\\*.*?\\*/", Pattern.MULTILINE | Pattern.DOTALL);
289   protected static Object JavaDoc [] noArguments = new Object JavaDoc [0];
290   protected void match(IDOMNode node)
291   {
292     for (Iterator JavaDoc dictionaryPatterns = options.getDictionaryPatterns().iterator(); dictionaryPatterns.hasNext(); )
293     {
294       JControlModel.DictionaryPattern dictionaryPattern = (JControlModel.DictionaryPattern)dictionaryPatterns.next();
295       if (dictionaryPattern.getSelectorFeature().getFeatureClass().isInstance(node))
296       {
297         try
298         {
299           String JavaDoc selection = (String JavaDoc)dictionaryPattern.getSelectorFeature().getFeatureMethod().invoke(node, noArguments);
300           if (dictionaryPattern.getSelectorFeature().getFeatureMethod().getName().equals("getComment"))
301           {
302             String JavaDoc contents = node.getContents();
303             for (int start = 0, end = contents.length(), count = 0; start < end; )
304             {
305               Matcher JavaDoc matcher = comment.matcher(contents.subSequence(start, end));
306               if (matcher.find())
307               {
308                 // Ignore it if there are multiple comments.
309
//
310
if (++count > 1)
311                 {
312                   int braceIndex = contents.indexOf("{", start); // }
313
if (braceIndex > start + matcher.start(0))
314                   {
315                     selection = null;
316                   }
317
318                   break;
319                 }
320                 start += matcher.end(0) + 1;
321               }
322               else
323               {
324                 break;
325               }
326             }
327           }
328
329           if (selection != null)
330           {
331             Matcher JavaDoc matcher = dictionaryPattern.getPattern().matcher(selection);
332             if (matcher.find())
333             {
334               for (int i = 1; i <= matcher.groupCount(); ++i)
335               {
336                 String JavaDoc markup = matcher.group(i);
337                 Collection JavaDoc collection = (Collection JavaDoc)markupMap.get(markup);
338                 if (collection == null)
339                 {
340                   collection = new HashSet JavaDoc();
341                   markupMap.put(markup, collection);
342                 }
343                 collection.add(node);
344               }
345             }
346           }
347         }
348         catch (IllegalAccessException JavaDoc exception)
349         {
350           // exception.printStackTrace();
351
}
352         catch (InvocationTargetException JavaDoc exception)
353         {
354           // exception.printStackTrace();
355
}
356       }
357     }
358   }
359
360   public IDOMCompilationUnit getCompilationUnit()
361   {
362     return compilationUnit;
363   }
364
365   public IDOMPackage getPackage()
366   {
367     return jPackage;
368   }
369
370   public JControlModel options()
371   {
372     return options;
373   }
374
375   public Map JavaDoc getImportMap()
376   {
377     return importMap;
378   }
379
380   public Map JavaDoc getTypeMap()
381   {
382     return typeMap;
383   }
384
385   public Map JavaDoc getInitializerMap()
386   {
387     return initializerMap;
388   }
389
390   public Map JavaDoc getFieldMap()
391   {
392     return fieldMap;
393   }
394
395   public Map JavaDoc getMethodMap()
396   {
397     return methodMap;
398   }
399
400   public Map JavaDoc getMarkupMap()
401   {
402     return markupMap;
403   }
404
405   public boolean isMarkedUp(Pattern JavaDoc markupPattern, IDOMNode node)
406   {
407     if (markupPattern == null)
408     {
409       return true;
410     }
411     else
412     {
413       for (Iterator JavaDoc markupEntries = markupMap.entrySet().iterator(); markupEntries.hasNext(); )
414       {
415         Map.Entry JavaDoc markupEntry = (Map.Entry JavaDoc)markupEntries.next();
416         String JavaDoc key = (String JavaDoc)markupEntry.getKey();
417         if (key != null && markupPattern.matcher(key).find())
418         {
419           if (((Collection JavaDoc)markupEntry.getValue()).contains(node))
420           {
421             return true;
422           }
423         }
424       }
425       return false;
426     }
427   }
428
429   public boolean isNoImport(IDOMImport domImport)
430   {
431     return noImportSet.contains(getQualifiedName(domImport));
432   }
433 }
434
Popular Tags