KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > dependency > DefaultResultRenderer


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 package classycle.dependency;
26
27 /**
28  * @author Franz-Josef Elmer
29  */

30 public class DefaultResultRenderer extends ResultRenderer
31 {
32   private static final String JavaDoc SHOW
33       = DependencyDefinitionParser.SHOW_KEY_WORD + ' ';
34   private static final PreferenceFactory FACTORY
35                           = new DefaultPreferenceFactory();
36   private static final TextResult ONLY_SHORTEST_PATHS
37       = new TextResult(SHOW
38                        + DefaultPreferenceFactory.ONLY_SHORTEST_PATHS.getKey()
39                        + '\n');
40   private static final TextResult ALL_PATHS
41       = new TextResult(SHOW + DefaultPreferenceFactory.ALL_PATHS.getKey()
42                        + '\n');
43
44   private boolean _allResults;
45   
46   public PreferenceFactory getPreferenceFactory()
47   {
48     return FACTORY;
49   }
50
51   public void considerPreference(Preference preference)
52   {
53     if (preference == DefaultPreferenceFactory.ONLY_SHORTEST_PATHS)
54     {
55       _shortestPaths = true;
56     } else if (preference == DefaultPreferenceFactory.ALL_PATHS)
57     {
58       _shortestPaths = false;
59     } else if (preference == DefaultPreferenceFactory.ALL_RESULTS)
60     {
61       _allResults = true;
62     } else if (preference == DefaultPreferenceFactory.ONLY_FAILURES)
63     {
64       _allResults = false;
65     }
66   }
67
68   public Result getDescriptionOfCurrentPreferences()
69   {
70     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(SHOW);
71     buffer.append(_shortestPaths
72                       ? DefaultPreferenceFactory.ONLY_SHORTEST_PATHS.getKey()
73                       : DefaultPreferenceFactory.ALL_PATHS.getKey())
74           .append(' ')
75           .append(_allResults ? DefaultPreferenceFactory.ALL_RESULTS.getKey()
76                               : DefaultPreferenceFactory.ONLY_FAILURES.getKey())
77           .append('\n');
78     
79     return new TextResult(new String JavaDoc(buffer));
80   }
81
82   public String JavaDoc render(Result result)
83   {
84     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
85     render(buffer, result);
86     return new String JavaDoc(buffer);
87   }
88   
89   private void render(StringBuffer JavaDoc buffer, Result result)
90   {
91     if (result instanceof ResultContainer)
92     {
93       ResultContainer results = (ResultContainer) result;
94       for (int i = 0, n = results.getNumberOfResults(); i < n; i++)
95       {
96         render(buffer, results.getResult(i));
97       }
98     } else if (_allResults || result.isOk() == false)
99     {
100       buffer.append(result.toString());
101     }
102   }
103
104 }
105
Popular Tags