KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > generator > SiteTreeXMLWriter


1 package de.webman.generator;
2
3 import java.io.*;
4 import java.util.*;
5
6 import org.xml.sax.*;
7 import org.xml.sax.helpers.AttributesImpl JavaDoc;
8 import com.megginson.sax.DataWriter;
9
10 import com.teamkonzept.lib.*;
11
12 /**
13 Class, that writes a SiteTree or GenTree (as used internally by
14 the generator) in XML-format to a Writer.
15
16 Mainly class is mainly for debugging purposes. The writers for
17 each node are not complete: to keep the files readable, only the
18 most important Attributes are written to the writer.
19
20 To use the classs, create an instance of it by specifying a writer
21 in the constructor. Then invoke the <code>write</code>-method
22 with a SiteNode or GenNode.
23
24 The class makes use of the freely available <code>DataWriter</code>
25 class of the XMLWriter from megginson.
26     * @author $Author: alex $
27     * @version $Revision: 1.1 $
28 */

29 public class SiteTreeXMLWriter
30 {
31     private DataWriter wr;
32      
33     /**
34     Constructor
35     @param writer the writer to which the output is written.
36     */

37     public SiteTreeXMLWriter (Writer writer) throws SAXException
38     {
39         wr = new DataWriter(writer);
40         wr.setIndentStep(2);
41         wr.startDocument();
42     }
43     
44     /**
45     Writes the SiteNode <code>root</code>.
46     @param root the root of the site node to be written
47     */

48     public void write (SiteNode root) throws SAXException
49     {
50         writeSiteNode(root);
51     }
52     
53     /**
54     Writes the GenNode <code>root</code>.
55     @param root the root of the gen node to be written
56     */

57     public void write (GenNode root) throws SAXException
58     {
59         writeGenNode(root);
60     }
61     
62     protected void finalize ()
63     {
64         try {
65             wr.endDocument();
66         }
67         catch(SAXException e) {
68             ;
69         }
70     }
71     
72     
73     
74     
75     
76     //- private methods ---------------------------------------------
77

78     private void writeSiteNode(SiteNode node) throws SAXException
79     {
80         if (node == null)
81             return;
82             
83         AttributesImpl JavaDoc attribs = new AttributesImpl JavaDoc();
84         attribs.addAttribute("", "shortName", "", "CDATA", node.getShortName());
85         attribs.addAttribute("", "type", "CDATA", "",
86         node.getType() == SiteNode.NODE_TYPE_SINGLE ? "SINGLE" : "GROUP");
87         if (node.getType() == SiteNode.NODE_TYPE_GROUP)
88         {
89             attribs.addAttribute("", "primaryContent", "", "CDATA", node.getPrimaryContentNode().getShortName());
90         }
91         
92         writeStartElement("SiteNode", attribs);
93         writeSiteNodes(node.getChilds().elements());
94         writeSiteDocuments(node.getDocuments().elements());
95         writeEndElement("SiteNode");
96     }
97
98     private void writeSiteDocument(SiteDocument node) throws SAXException
99     {
100         if (node == null)
101             return;
102             
103         AttributesImpl JavaDoc attribs = new AttributesImpl JavaDoc();
104         attribs.addAttribute("", "name", "", "CDATA", node.getName());
105         attribs.addAttribute("", "shortName", "", "CDATA", node.getShortName());
106         attribs.addAttribute("", "templateName", "", "CDATA", node.getTemplateName());
107             
108         writeStartElement("SiteDocument", attribs);
109
110         if (node.getReferences ()!= null)
111             writeSiteReferences(node.getReferences().elements());
112             
113         if (node.getContents() != null)
114             writeSiteContentIntegrations(node.getContents().elements());
115              
116         if (node.getStructureContents() != null)
117             writeSiteContents(node.getStructureContents().elements());
118     
119         writeEndElement("SiteDocument");
120     }
121
122     private void writeSiteContentIntegration(SiteContentIntegration node) throws SAXException
123     {
124         if (node == null)
125             return;
126             
127         AttributesImpl JavaDoc attribs = new AttributesImpl JavaDoc();
128         attribs.addAttribute("", "presentationComp", "", "CDATA", node.getShortName());
129         attribs.addAttribute("", "type", "", "CDATA",
130             node.getIntegrationType() == SiteReference.INTEGRATION_TYPE_SINGLE ? "SINGLE" : "GROUP");
131         
132         writeStartElement("SiteContentIntegration", attribs);
133
134         writeSiteContentNode(node.getContentNode());
135                 
136         writeEndElement("SiteContentIntegration");
137     }
138     
139     private void writeSiteContentNode(SiteContentNode node) throws SAXException
140     {
141         if (node == null)
142             return;
143             
144         AttributesImpl JavaDoc attribs = new AttributesImpl JavaDoc();
145         attribs.addAttribute("", "name", "", "CDATA", node.getName());
146         attribs.addAttribute("", "shortName", "", "CDATA", node.getShortName());
147         attribs.addAttribute("", "type", "", "CDATA",
148             node.getType()==SiteContentNode.NODE_TYPE_SINGLE ? "SINGLE" : "GROUP");
149             
150         writeStartElement("SiteContentNode", attribs);
151
152         writeSiteContents(node.getContents().elements());
153                 
154         writeEndElement("SiteContentNode");
155     }
156
157     private void writeSiteContent(SiteContent node) throws SAXException
158     {
159         if (node == null)
160             return;
161             
162         AttributesImpl JavaDoc attribs = new AttributesImpl JavaDoc();
163         if (node.getName() != null)
164             attribs.addAttribute("", "name", "", "CDATA", node.getName());
165             
166         if (node.getShortName() != null)
167             attribs.addAttribute("", "shortName", "", "CDATA", node.getShortName());
168             
169         attribs.addAttribute("", "isStructureContent", "", "CDATA", node.isStructureContent() ? "true" : "false");
170         
171         writeStartElement("SiteContent", attribs);
172
173         writeEndElement("SiteContent");
174     }
175     
176     private void writeSiteReference(SiteReference node) throws SAXException
177     {
178         if (node == null)
179             return;
180             
181         AttributesImpl JavaDoc attribs = new AttributesImpl JavaDoc();
182         attribs.addAttribute("", "presentationComp", "CDATA", "", node.getIntegrationShortName());
183         attribs.addAttribute("", "destNode", "CDATA", "", node.getDestinationNode().getShortName());
184         
185         writeStartElement("SiteReference", attribs);
186         writeEndElement("SiteReference");
187     }
188     
189     private void writeGenNode(GenNode node) throws SAXException
190     {
191         if (node == null)
192             return;
193         
194         AttributesImpl JavaDoc attribs = new AttributesImpl JavaDoc();
195         if (node.getNodeName() != null)
196             attribs.addAttribute("", "name", "", "CDATA", node.getNodeName());
197         
198         writeStartElement("GenNode", attribs);
199         writeSiteContent(node.getPrimaryContent());//?
200
writeGenNodes(node.getSubNodes().elements());
201         writeGenDocuments(node.getDocuments().elements());
202         writeEndElement("GenNode");
203     }
204     
205     private void writeGenDocument(GenDocument node) throws SAXException
206     {
207         if (node == null)
208             return;
209             
210         AttributesImpl JavaDoc attribs = new AttributesImpl JavaDoc();
211         attribs.addAttribute("", "name", "", "CDATA", node.getDocumentName());
212         attribs.addAttribute("", "template", "", "CDATA", node.getTemplateName());
213         if (node.getExternURL() != null)
214             attribs.addAttribute("", "externUrl", "", "CDATA", node.getExternURL());
215         
216         writeStartElement("GenDocument", attribs);
217         
218         Enumeration conts = node.getContents().elements();
219         while(conts.hasMoreElements())
220         {
221             Object JavaDoc cont = conts.nextElement();
222             if( cont instanceof TKVector ) {
223                 writeSiteContents(((TKVector) cont).elements());
224             }
225             else {
226                 writeSiteContent((SiteContent) cont);
227             }
228         }
229         
230         writeSiteReferences(node.getReferences().elements());
231
232         writeEndElement("GenDocument");
233     }
234     
235     // Some convenience method for writing Enumertions of specific types:
236
private void writeGenDocuments(Enumeration nodes) throws SAXException
237     {
238         while(nodes.hasMoreElements())
239         {
240             writeGenDocument((GenDocument) nodes.nextElement());
241         }
242     }
243     
244     private void writeGenNodes(Enumeration nodes) throws SAXException
245     {
246         while(nodes.hasMoreElements())
247         {
248             writeGenNode((GenNode) nodes.nextElement());
249         }
250     }
251         
252     private void writeSiteNodes(Enumeration nodes) throws SAXException
253     {
254         while(nodes.hasMoreElements())
255         {
256             writeSiteNode((SiteNode) nodes.nextElement());
257         }
258     }
259     
260     private void writeSiteDocuments(Enumeration nodes) throws SAXException
261     {
262         while(nodes.hasMoreElements())
263         {
264             writeSiteDocument((SiteDocument) nodes.nextElement());
265         }
266     }
267     
268     private void writeSiteReferences(Enumeration nodes) throws SAXException
269     {
270         while(nodes.hasMoreElements())
271         {
272             writeSiteReference((SiteReference) nodes.nextElement());
273         }
274     }
275     
276     private void writeSiteContents(Enumeration nodes) throws SAXException
277     {
278         while(nodes.hasMoreElements())
279         {
280             writeSiteContent((SiteContent) nodes.nextElement());
281         }
282     }
283     
284     private void writeSiteContentIntegrations(Enumeration nodes) throws SAXException
285     {
286         while(nodes.hasMoreElements())
287         {
288             writeSiteContentIntegration((SiteContentIntegration) nodes.nextElement());
289         }
290     }
291     
292     private void writeStartElement(String JavaDoc name, Attributes attribs) throws SAXException
293     {
294         wr.startElement ("", name, "", attribs);
295     }
296     
297     private void writeEndElement(String JavaDoc name) throws SAXException
298     {
299         wr.endElement ("", name, "");
300     }
301 }
Popular Tags