KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > snippet > Snippets


1
2
3 package net.firstpartners.nounit.snippet;
4
5 /**
6  * Title: NoUnit - Identify Classes that are not being unit Tested
7  *
8  * Copyright (C) 2001 Paul Browne , FirstPartners.net
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  *
25  * @author Paul Browne
26  * @version 0.7
27  */

28
29 import java.util.Collection JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import net.firstpartners.nounit.snippet.xml.IXmlJdomSource;
34
35 import org.jdom.Element;
36 import org.jdom.output.XMLOutputter;
37
38 /**
39  * Collection of Snippets
40  */

41 public class Snippets extends AbstractSnippet {
42
43     /**
44      * Internal Snippet Store
45      */

46     private HashSet JavaDoc internalSnippets;
47     
48     /**
49      * Constructor - No Args
50      */

51     public Snippets() {
52         internalSnippets = new HashSet JavaDoc();
53     }
54     
55     /**
56      * Add a snippet to the collection
57      * @param thisSnippet to add to internal collection
58      */

59     public void add(ISnippet thisSnippet) {
60         
61         if (thisSnippet!=null) {
62            internalSnippets.add(thisSnippet);
63         }
64         
65     }
66     
67     /**
68      * Add a collection of snippet to the internal collection
69      * @param theseSnippets to add to internal collection
70      */

71     public void add(Collection JavaDoc theseSnippets) {
72         
73         
74         if (theseSnippets!=null) {
75            internalSnippets.addAll(theseSnippets);
76         }
77         
78     }
79     
80     /**
81      * Gets an interator over the collection
82      * @return Iterator over the internal collection
83      */

84     public Iterator JavaDoc getIterator() {
85         return internalSnippets.iterator();
86     }
87     
88     /**
89      * Gets a handle to the internal store (as a collection)
90      * @return Collection
91      */

92     public Collection JavaDoc getCollection() {
93         return internalSnippets;
94     }
95     
96    /**
97      * Gets first item in collection
98      * @return IXmlJdomSource , from Collection
99      */

100     public IXmlJdomSource getFirstItem() {
101         
102         Iterator JavaDoc tmpList = getIterator();
103         
104         if (tmpList.hasNext()) {
105             return(IXmlJdomSource)tmpList.next();
106         }
107         
108         //Nothing to return
109
return null;
110     }
111     
112     /**
113      * Get String Description of this class
114      * @return description as String
115      */

116     public String JavaDoc toString() {
117        
118         StringBuffer JavaDoc description = new StringBuffer JavaDoc();
119         Iterator JavaDoc internalSnippetList = getIterator();
120         
121         //loop through Interator
122
while (internalSnippetList.hasNext()) {
123             
124             description.append(internalSnippetList.next());
125             description.append("\n");
126         }
127         
128         return description.toString();
129     }
130     
131     /**
132      * Get an XML Representation of this Class (as String of XML)
133      * @return String with the XML description
134      */

135     public String JavaDoc toXml() {
136         
137         Element jDomDescription = addNodesTo(new Element("Snippets"));
138         
139          //Use XML Outputter (from Jdom) to format (true = nice format)
140
XMLOutputter fmt = new XMLOutputter(" ", true);
141         
142         //Do the conversion to XML as string
143
String JavaDoc xmlAsString = fmt.outputString(jDomDescription);
144
145         return xmlAsString;
146         
147     }
148     
149     
150     /**
151      * Get an XML Representation of this Class (as Jdom nodes)
152      * @param nodeToAddChildrenTo - will be parent of any children in this collection
153      * @return methodRoot with the XML description
154      */

155     public Element addNodesTo(Element nodeToAddChildrenTo){
156
157         //Local Variables
158
IXmlJdomSource thisSnippet;
159         Element tmpElement;
160         
161         //Add Internal XML Info
162
Iterator JavaDoc snippetValues = getIterator();
163         
164         while (snippetValues.hasNext()){
165             
166             thisSnippet = (IXmlJdomSource)snippetValues.next();
167             tmpElement= thisSnippet.getNodes();
168             nodeToAddChildrenTo.addContent(tmpElement);
169                         
170         }
171                 
172         //Return Class Element
173
return nodeToAddChildrenTo;
174         
175         
176     }
177 }
178
Popular Tags