View Javadoc

1   package net.sourceforge.argval.packageinfo.impl;
2   
3   
4   import java.util.Iterator;
5   import java.util.Set;
6   import java.util.TreeSet;
7   
8   import net.sourceforge.argval.packageinfo.PackageInfo;
9   import net.sourceforge.argval.packageinfo.PackageInfoVisitor;
10  
11  import org.jdom.Document;
12  import org.jdom.Element;
13  import org.jdom.Text;
14  
15  
16  /**
17   * This visitor creates a JDom <code>Document</code> containing all the details of 
18   * the visited <code>PackageInfo</code> instances.
19   * 
20   * @author $Author: $
21   * @version $Revision: $
22   * Last modified: $Date: $
23   */
24  public class DomPackageVisitor implements PackageInfoVisitor {
25  	private Document doc;
26  	private boolean isIncludePackageNameActive = true;
27  	
28  	
29  	/**
30  	 * Create a JDom document, containing all visited PackageInfo instances. 
31  	 * With the package names (Java language dot separated names, like 
32  	 * <code>net.sourceforge.argval.packageinfo</code> ). 
33  	 */
34  	public DomPackageVisitor() {
35  		super();
36  		doc = new Document();
37  		doc.addContent(new Element("packages"));
38  	}
39  	/**
40  	 * Create a JDom document, containing all visited PackageInfo instances. 
41  	 * With or without the package names (Java language dot separated names, like 
42  	 * <code>net.sourceforge.argval.packageinfo</code> ). 
43  	 *  
44  	 * @param  isIncludePackageNameActive  When <code>true</code> all package names 
45  	 *         assosiated with this <code>Package</code> are added to the text.
46  	 */
47  	public DomPackageVisitor(boolean isIncludePackageNameActive) {
48  		this();
49  		this.isIncludePackageNameActive = isIncludePackageNameActive;
50  	}
51  	
52  	/** {@inheritDoc}
53  	 *
54  	 * 
55  	 */ 
56  	public void visit(PackageInfo packageInfo) {
57  		Element packagesElement = doc.getRootElement();
58  		Element packageElement = new Element("package");
59  		packagesElement.addContent(packageElement);
60  		packageElement.addContent(		
61  				createElementTitleVendorVersion("specification", 
62  						packageInfo.getSpecificationTitle(), 
63  						packageInfo.getSpecificationVendor(), 
64  						packageInfo.getSpecificationVersion()));
65  		packageElement.addContent(		
66  				createElementTitleVendorVersion("implementation", 
67  						packageInfo.getImplementationTitle(), 
68  						packageInfo.getImplementationVendor(), 
69  						packageInfo.getImplementationVersion()));
70  
71  		if (isIncludePackageNameActive) {
72  	        for (Iterator<String> nameIter = convert(packageInfo.getNameSet()).iterator(); nameIter.hasNext();) {
73  	        	packageElement.addContent(createElementWithText("name", nameIter.next().toString()));
74  	        }
75          }
76  	}
77  
78  	
79  	/**
80  	 * Returns the created JDom <code>Document</code> instance.
81  	 * 
82  	 * @return The JDom <code>Document</code> instance.
83  	 */
84  	public Document getDocument() {
85  		return doc;
86  	}
87  	
88  	
89  	private Element createElementTitleVendorVersion(String tag, String title, String vendor, String version) {
90  		Element element = new Element(tag);
91  		element.addContent(createElementWithText("title", title));
92  		element.addContent(createElementWithText("vendor", vendor));
93  		element.addContent(createElementWithText("version", version));
94  		return element;
95  	}
96  
97  
98  	private Element createElementWithText(String tag, String value) {
99  		Element element = new Element(tag);
100 		element.setContent(new Text(value));
101 		return element;
102 	}
103 	
104     
105 	private TreeSet<String> convert(Set<?> originalSet) {
106         TreeSet<String> newSet = new TreeSet<String>();
107         for (Object object : originalSet) {
108             newSet.add(object.toString());
109         }
110         return newSet;
111     }
112     
113 }