001: package net.firstpartners.nounit.snippet;
002:
003: /**
004: * Title: NoUnit - Identify Classes that are not being unit Tested
005: *
006: * Copyright (C) 2001 Paul Browne , FirstPartners.net
007: *
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * @author Paul Browne
024: * @version 0.7
025: */
026:
027: import java.util.Collection;
028: import java.util.HashSet;
029: import java.util.Iterator;
030:
031: import net.firstpartners.nounit.snippet.xml.IXmlJdomSource;
032:
033: import org.jdom.Element;
034: import org.jdom.output.XMLOutputter;
035:
036: /**
037: * Collection of Snippets
038: */
039: public class Snippets extends AbstractSnippet {
040:
041: /**
042: * Internal Snippet Store
043: */
044: private HashSet internalSnippets;
045:
046: /**
047: * Constructor - No Args
048: */
049: public Snippets() {
050: internalSnippets = new HashSet();
051: }
052:
053: /**
054: * Add a snippet to the collection
055: * @param thisSnippet to add to internal collection
056: */
057: public void add(ISnippet this Snippet) {
058:
059: if (this Snippet != null) {
060: internalSnippets.add(this Snippet);
061: }
062:
063: }
064:
065: /**
066: * Add a collection of snippet to the internal collection
067: * @param theseSnippets to add to internal collection
068: */
069: public void add(Collection theseSnippets) {
070:
071: if (theseSnippets != null) {
072: internalSnippets.addAll(theseSnippets);
073: }
074:
075: }
076:
077: /**
078: * Gets an interator over the collection
079: * @return Iterator over the internal collection
080: */
081: public Iterator getIterator() {
082: return internalSnippets.iterator();
083: }
084:
085: /**
086: * Gets a handle to the internal store (as a collection)
087: * @return Collection
088: */
089: public Collection getCollection() {
090: return internalSnippets;
091: }
092:
093: /**
094: * Gets first item in collection
095: * @return IXmlJdomSource , from Collection
096: */
097: public IXmlJdomSource getFirstItem() {
098:
099: Iterator tmpList = getIterator();
100:
101: if (tmpList.hasNext()) {
102: return (IXmlJdomSource) tmpList.next();
103: }
104:
105: //Nothing to return
106: return null;
107: }
108:
109: /**
110: * Get String Description of this class
111: * @return description as String
112: */
113: public String toString() {
114:
115: StringBuffer description = new StringBuffer();
116: Iterator internalSnippetList = getIterator();
117:
118: //loop through Interator
119: while (internalSnippetList.hasNext()) {
120:
121: description.append(internalSnippetList.next());
122: description.append("\n");
123: }
124:
125: return description.toString();
126: }
127:
128: /**
129: * Get an XML Representation of this Class (as String of XML)
130: * @return String with the XML description
131: */
132: public String toXml() {
133:
134: Element jDomDescription = addNodesTo(new Element("Snippets"));
135:
136: //Use XML Outputter (from Jdom) to format (true = nice format)
137: XMLOutputter fmt = new XMLOutputter(" ", true);
138:
139: //Do the conversion to XML as string
140: String xmlAsString = fmt.outputString(jDomDescription);
141:
142: return xmlAsString;
143:
144: }
145:
146: /**
147: * Get an XML Representation of this Class (as Jdom nodes)
148: * @param nodeToAddChildrenTo - will be parent of any children in this collection
149: * @return methodRoot with the XML description
150: */
151: public Element addNodesTo(Element nodeToAddChildrenTo) {
152:
153: //Local Variables
154: IXmlJdomSource this Snippet;
155: Element tmpElement;
156:
157: //Add Internal XML Info
158: Iterator snippetValues = getIterator();
159:
160: while (snippetValues.hasNext()) {
161:
162: this Snippet = (IXmlJdomSource) snippetValues.next();
163: tmpElement = this Snippet.getNodes();
164: nodeToAddChildrenTo.addContent(tmpElement);
165:
166: }
167:
168: //Return Class Element
169: return nodeToAddChildrenTo;
170:
171: }
172: }
|