KickJava   Java API By Example, From Geeks To Geeks.

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


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: JControlModel.java,v 1.3 2005/06/08 06:15:57 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.jmerge;
18
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32
33 import org.eclipse.emf.codegen.CodeGenPlugin;
34
35
36 /**
37  * A control model that provides dictionaries and rules to drive a merge process.
38  */

39 public class JControlModel
40 {
41   public static class Feature
42   {
43     protected Class JavaDoc featureClass;
44     protected Method JavaDoc featureMethod;
45
46     public Feature(String JavaDoc path, Class JavaDoc [] parameterTypes)
47     {
48       int index = path.indexOf('/');
49       String JavaDoc className = "org.eclipse.jdt.core.jdom.IDOM" + path.substring(0, index);
50       String JavaDoc methodName = path.substring(index + 1);
51       try
52       {
53         featureClass = Class.forName(className);
54         featureMethod = featureClass.getMethod(methodName, parameterTypes);
55       }
56       catch (NoSuchMethodException JavaDoc exception)
57       {
58         // CodeGenPlugin.INSTANCE.log(exception);
59
}
60       catch (ClassNotFoundException JavaDoc exception)
61       {
62         // CodeGenPlugin.INSTANCE.log(exception);
63
}
64     }
65
66     public Class JavaDoc getFeatureClass()
67     {
68       return featureClass;
69     }
70
71     public Method JavaDoc getFeatureMethod()
72     {
73       return featureMethod;
74     }
75   }
76
77   public static class DictionaryPattern
78   {
79     protected static Class JavaDoc [] noParameterTypes = new Class JavaDoc [0];
80     protected static Class JavaDoc [] stringParameterType = new Class JavaDoc [] { String JavaDoc.class };
81     protected String JavaDoc name;
82     protected Feature selectorFeature;
83     protected Pattern JavaDoc pattern;
84
85     public DictionaryPattern()
86     {
87     }
88
89     public DictionaryPattern(Element JavaDoc element)
90     {
91       initialize(element);
92     }
93
94     public void initialize(Element JavaDoc element)
95     {
96       name = element.getAttribute("name");
97       selectorFeature = new Feature(element.getAttribute("select"), noParameterTypes);
98       pattern = Pattern.compile(element.getAttribute("match"), Pattern.MULTILINE | Pattern.DOTALL);
99     }
100
101     public String JavaDoc getName()
102     {
103       return name;
104     }
105
106     public void setName(String JavaDoc name)
107     {
108       this.name = name;
109     }
110
111     public Feature getSelectorFeature()
112     {
113       return selectorFeature;
114     }
115
116     public void setSelectorFeature(Feature selectorFeature)
117     {
118       this.selectorFeature = selectorFeature;
119     }
120
121     public Pattern JavaDoc getPattern()
122     {
123       return pattern;
124     }
125
126     public void setPattern(Pattern JavaDoc pattern)
127     {
128       this.pattern = pattern;
129     }
130   }
131
132   public static class PullRule
133   {
134     protected static Class JavaDoc [] noParameterTypes = new Class JavaDoc [0];
135     protected String JavaDoc name;
136
137     protected Pattern JavaDoc sourceMarkup;
138     protected Feature sourceGetFeature;
139     protected Pattern JavaDoc sourceTransfer;
140
141     protected Pattern JavaDoc targetMarkup;
142     protected Feature targetPutFeature;
143     protected Pattern JavaDoc targetTransfer;
144
145     public PullRule()
146     {
147     }
148
149     public PullRule(Element JavaDoc element)
150     {
151       initialize(element);
152     }
153
154     public void initialize(Element JavaDoc element)
155     {
156       sourceGetFeature = new Feature(element.getAttribute("sourceGet"), noParameterTypes);
157       if (sourceGetFeature != null)
158       {
159         Class JavaDoc sourceReturnType = sourceGetFeature.getFeatureMethod().getReturnType();
160         targetPutFeature = new Feature(element.getAttribute("targetPut"), new Class JavaDoc [] { sourceReturnType });
161         if (targetPutFeature.getFeatureMethod() == null && sourceReturnType.isArray())
162         {
163           targetPutFeature = new Feature(element.getAttribute("targetPut"), new Class JavaDoc [] { sourceReturnType.getComponentType() });
164         }
165       }
166       if (element.hasAttribute("sourceMarkup"))
167       {
168         sourceMarkup= Pattern.compile(element.getAttribute("sourceMarkup"), Pattern.MULTILINE | Pattern.DOTALL);
169       }
170       if (element.hasAttribute("targetMarkup"))
171       {
172         targetMarkup= Pattern.compile(element.getAttribute("targetMarkup"), Pattern.MULTILINE | Pattern.DOTALL);
173       }
174       if (element.hasAttribute("sourceTransfer"))
175       {
176         sourceTransfer= Pattern.compile(element.getAttribute("sourceTransfer"), Pattern.MULTILINE | Pattern.DOTALL);
177       }
178       if (element.hasAttribute("targetTransfer"))
179       {
180         targetTransfer= Pattern.compile(element.getAttribute("targetTransfer"), Pattern.MULTILINE | Pattern.DOTALL);
181       }
182     }
183
184     public String JavaDoc getName()
185     {
186       return name;
187     }
188
189     public void setName(String JavaDoc name)
190     {
191       this.name = name;
192     }
193
194     public Feature getSourceGetFeature()
195     {
196       return sourceGetFeature;
197     }
198
199     public void setSourceGetFeature(Feature sourceGetFeature)
200     {
201       this.sourceGetFeature = sourceGetFeature;
202     }
203
204     public Feature getTargetPutFeature()
205     {
206       return targetPutFeature;
207     }
208
209     public void setTargetPutFeature(Feature targetPutFeature)
210     {
211       this.targetPutFeature = targetPutFeature;
212     }
213
214     public Pattern JavaDoc getSourceTransfer()
215     {
216       return sourceTransfer;
217     }
218
219     public void setSourceTransfer(Pattern JavaDoc sourceTransfer)
220     {
221       this.sourceTransfer = sourceTransfer;
222     }
223
224     public Pattern JavaDoc getTargetTransfer()
225     {
226       return targetTransfer;
227     }
228
229     public void setTargetTransfer(Pattern JavaDoc targetTransfer)
230     {
231       this.targetTransfer = targetTransfer;
232     }
233
234     public Pattern JavaDoc getSourceMarkup()
235     {
236       return sourceMarkup;
237     }
238
239     public void setSourceMarkup(Pattern JavaDoc sourceMarkup)
240     {
241       this.sourceMarkup = sourceMarkup;
242     }
243
244     public Pattern JavaDoc getTargetMarkup()
245     {
246       return targetMarkup;
247     }
248
249     public void setTargetMarkup(Pattern JavaDoc targetMarkup)
250     {
251       this.targetMarkup = targetMarkup;
252     }
253   }
254
255   public static class SweepRule
256   {
257     protected String JavaDoc name;
258     protected Class JavaDoc selector;
259     protected Pattern JavaDoc markup;
260
261     public SweepRule()
262     {
263     }
264
265     public SweepRule(Element JavaDoc element)
266     {
267       initialize(element);
268     }
269
270     public void initialize(Element JavaDoc element)
271     {
272       if (element.hasAttribute("select"))
273       {
274         selector = classForClassName(element.getAttribute("select"));
275       }
276       if (element.hasAttribute("markup"))
277       {
278         markup= Pattern.compile(element.getAttribute("markup"), Pattern.MULTILINE | Pattern.DOTALL);
279       }
280     }
281
282     public String JavaDoc getName()
283     {
284       return name;
285     }
286
287     public void setName(String JavaDoc name)
288     {
289       this.name = name;
290     }
291
292     public Class JavaDoc getSelector()
293     {
294       return selector;
295     }
296
297     public void setSelector(Class JavaDoc selector)
298     {
299       this.selector = selector;
300     }
301
302     public Pattern JavaDoc getMarkup()
303     {
304       return markup;
305     }
306
307     public void setMarkup(Pattern JavaDoc markup)
308     {
309       this.markup = markup;
310     }
311   }
312
313   public static class SortRule
314   {
315     protected String JavaDoc name;
316     protected Class JavaDoc selector;
317     protected Pattern JavaDoc markup;
318
319     public SortRule()
320     {
321     }
322
323     public SortRule(Element JavaDoc element)
324     {
325       initialize(element);
326     }
327
328     public void initialize(Element JavaDoc element)
329     {
330       if (element.hasAttribute("select"))
331       {
332         selector = classForClassName(element.getAttribute("select"));
333       }
334       if (element.hasAttribute("markup"))
335       {
336         markup= Pattern.compile(element.getAttribute("markup"), Pattern.MULTILINE | Pattern.DOTALL);
337       }
338     }
339
340     public String JavaDoc getName()
341     {
342       return name;
343     }
344
345     public void setName(String JavaDoc name)
346     {
347       this.name = name;
348     }
349
350     public Class JavaDoc getSelector()
351     {
352       return selector;
353     }
354
355     public void setSelector(Class JavaDoc selector)
356     {
357       this.selector = selector;
358     }
359
360     public Pattern JavaDoc getMarkup()
361     {
362       return markup;
363     }
364
365     public void setMarkup(Pattern JavaDoc markup)
366     {
367       this.markup = markup;
368     }
369   }
370
371
372   protected List JavaDoc dictionaryPatterns;
373   protected List JavaDoc pullRules;
374   protected List JavaDoc sweepRules;
375   protected List JavaDoc sortRules;
376   protected String JavaDoc indent;
377   protected String JavaDoc redirect;
378   protected boolean standardBraceStyle;
379   protected Pattern JavaDoc blockPattern;
380   protected Pattern JavaDoc noImportPattern;
381
382   /**
383    * This creates an instance.
384    */

385   public JControlModel(String JavaDoc uri)
386   {
387     initialize(uri);
388   }
389
390   public JControlModel(Element JavaDoc element)
391   {
392     initialize(element);
393   }
394
395   public boolean convertToStandardBraceStyle()
396   {
397     return standardBraceStyle;
398   }
399
400   public void setConvertToStandardBraceStyle(boolean standardBraceStyle)
401   {
402     this.standardBraceStyle = standardBraceStyle;
403   }
404
405   public String JavaDoc getLeadingTabReplacement()
406   {
407     return indent;
408   }
409
410   public void setLeadingTabReplacement(String JavaDoc indent)
411   {
412     this.indent = indent;
413   }
414
415   public String JavaDoc getRedirect()
416   {
417     return redirect;
418   }
419
420   public Pattern JavaDoc getBlockPattern()
421   {
422     return blockPattern;
423   }
424
425   public Pattern JavaDoc getNoImportPattern()
426   {
427     return noImportPattern;
428   }
429
430   public List JavaDoc getDictionaryPatterns()
431   {
432     if (dictionaryPatterns == null)
433     {
434       dictionaryPatterns = new ArrayList JavaDoc();
435     }
436     return dictionaryPatterns;
437   }
438
439   public List JavaDoc getPullRules()
440   {
441     if (pullRules == null)
442     {
443       pullRules = new ArrayList JavaDoc();
444     }
445     return pullRules;
446   }
447
448   public List JavaDoc getSweepRules()
449   {
450     if (sweepRules == null)
451     {
452       sweepRules = new ArrayList JavaDoc();
453     }
454     return sweepRules;
455   }
456
457   public List JavaDoc getSortRules()
458   {
459     if (sortRules == null)
460     {
461       sortRules = new ArrayList JavaDoc();
462     }
463     return sortRules;
464   }
465
466   protected void initialize(String JavaDoc uri)
467   {
468     DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
469     documentBuilderFactory.setNamespaceAware(true);
470     documentBuilderFactory.setValidating(false);
471     try
472     {
473       DocumentBuilder JavaDoc documentBuilder = documentBuilderFactory.newDocumentBuilder();
474       Document JavaDoc document = documentBuilder.parse(new InputSource JavaDoc(uri));
475       initialize(document.getDocumentElement());
476     }
477     catch (Exception JavaDoc exception)
478     {
479       CodeGenPlugin.INSTANCE.log(exception);
480     }
481   }
482
483   protected void initialize(Element JavaDoc element)
484   {
485     if (element.getLocalName().equals("options"))
486     {
487       if ("standard".equals(element.getAttributeNS(null, "braceStyle")))
488       {
489         standardBraceStyle = true;
490       }
491
492       if (element.hasAttributeNS(null, "indent"))
493       {
494         indent = element.getAttributeNS(null, "indent");
495       }
496
497       if (element.hasAttributeNS(null, "redirect"))
498       {
499         redirect = element.getAttributeNS(null, "redirect");
500       }
501
502       if (element.hasAttributeNS(null, "block"))
503       {
504         blockPattern = Pattern.compile(element.getAttributeNS(null, "block"), Pattern.MULTILINE | Pattern.DOTALL);
505       }
506
507       if (element.hasAttributeNS(null, "noImport"))
508       {
509         noImportPattern = Pattern.compile(element.getAttributeNS(null, "noImport"), Pattern.MULTILINE | Pattern.DOTALL);
510       }
511
512       for (Node JavaDoc child = element.getFirstChild(); child != null; child = child.getNextSibling())
513       {
514         if (child.getNodeType() == Node.ELEMENT_NODE)
515         {
516           Element JavaDoc elementChild = (Element JavaDoc)child;
517           if (elementChild.getLocalName().equals("dictionaryPattern"))
518           {
519             getDictionaryPatterns().add(new DictionaryPattern(elementChild));
520           }
521           else if (elementChild.getLocalName().equals("pull"))
522           {
523             getPullRules().add(new PullRule(elementChild));
524           }
525           else if (elementChild.getLocalName().equals("sweep"))
526           {
527             getSweepRules().add(new SweepRule(elementChild));
528           }
529           else if (elementChild.getLocalName().equals("sort"))
530           {
531             getSortRules().add(new SortRule(elementChild));
532           }
533         }
534       }
535     }
536   }
537
538   public static Class JavaDoc classForClassName(String JavaDoc className)
539   {
540     className = "org.eclipse.jdt.core.jdom.IDOM" + className;
541     try
542     {
543       Class JavaDoc result = Class.forName(className);
544       return result;
545     }
546     catch (ClassNotFoundException JavaDoc exception)
547     {
548       CodeGenPlugin.INSTANCE.log(exception);
549     }
550     return null;
551   }
552 }
553
Popular Tags