1 2 3 package net.firstpartners.nounit.snippet; 4 5 28 29 import java.util.Collection ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 33 import net.firstpartners.nounit.snippet.xml.IXmlJdomSource; 34 35 import org.jdom.Element; 36 import org.jdom.output.XMLOutputter; 37 38 41 public class Snippets extends AbstractSnippet { 42 43 46 private HashSet internalSnippets; 47 48 51 public Snippets() { 52 internalSnippets = new HashSet (); 53 } 54 55 59 public void add(ISnippet thisSnippet) { 60 61 if (thisSnippet!=null) { 62 internalSnippets.add(thisSnippet); 63 } 64 65 } 66 67 71 public void add(Collection theseSnippets) { 72 73 74 if (theseSnippets!=null) { 75 internalSnippets.addAll(theseSnippets); 76 } 77 78 } 79 80 84 public Iterator getIterator() { 85 return internalSnippets.iterator(); 86 } 87 88 92 public Collection getCollection() { 93 return internalSnippets; 94 } 95 96 100 public IXmlJdomSource getFirstItem() { 101 102 Iterator tmpList = getIterator(); 103 104 if (tmpList.hasNext()) { 105 return(IXmlJdomSource)tmpList.next(); 106 } 107 108 return null; 110 } 111 112 116 public String toString() { 117 118 StringBuffer description = new StringBuffer (); 119 Iterator internalSnippetList = getIterator(); 120 121 while (internalSnippetList.hasNext()) { 123 124 description.append(internalSnippetList.next()); 125 description.append("\n"); 126 } 127 128 return description.toString(); 129 } 130 131 135 public String toXml() { 136 137 Element jDomDescription = addNodesTo(new Element("Snippets")); 138 139 XMLOutputter fmt = new XMLOutputter(" ", true); 141 142 String xmlAsString = fmt.outputString(jDomDescription); 144 145 return xmlAsString; 146 147 } 148 149 150 155 public Element addNodesTo(Element nodeToAddChildrenTo){ 156 157 IXmlJdomSource thisSnippet; 159 Element tmpElement; 160 161 Iterator 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 nodeToAddChildrenTo; 174 175 176 } 177 } 178 | Popular Tags |