KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > XMLPrinter


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.io.*;
36 import java.util.*;
37
38 public class XMLPrinter extends Printer {
39     public static final String JavaDoc DEFAULT_ENCODING = "utf-8";
40     public static final String JavaDoc DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd";
41
42     private boolean atTopLevel = false;
43
44     public XMLPrinter(PrintWriter out) {
45         this(out, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX);
46     }
47
48     public XMLPrinter(TraversalStrategy strategy, PrintWriter out) {
49         this(strategy, out, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX);
50     }
51     
52     public XMLPrinter(PrintWriter out, String JavaDoc encoding, String JavaDoc dtdPrefix) {
53         super(out);
54         
55         appendHeader(encoding, dtdPrefix);
56     }
57     
58     public XMLPrinter(TraversalStrategy strategy, PrintWriter out, String JavaDoc encoding, String JavaDoc dtdPrefix) {
59         super(strategy, out);
60
61         appendHeader(encoding, dtdPrefix);
62     }
63
64     private void appendHeader(String JavaDoc encoding, String JavaDoc dtdPrefix) {
65         append("<?xml version=\"1.0\" encoding=\"").append(encoding).append("\" ?>").eol();
66         eol();
67         append("<!DOCTYPE dependencies SYSTEM \"").append(dtdPrefix).append("/dependencies.dtd\">").eol();
68         eol();
69     }
70
71     public void traverseNodes(Collection nodes) {
72         if (atTopLevel) {
73             super.traverseNodes(nodes);
74         } else {
75             atTopLevel = true;
76             indent().append("<dependencies>").eol();
77             raiseIndent();
78             super.traverseNodes(nodes);
79             lowerIndent();
80             indent().append("</dependencies>").eol();
81             atTopLevel = false;
82         }
83     }
84
85     protected void preprocessPackageNode(PackageNode node) {
86         super.preprocessPackageNode(node);
87
88         if (shouldShowPackageNode(node)) {
89             indent().append("<package confirmed=\"").append(node.isConfirmed() ? "yes" : "no").append("\">").eol();
90             raiseIndent();
91             indent().printNodeName(node).eol();
92         }
93     }
94
95     protected void postprocessPackageNode(PackageNode node) {
96         if (shouldShowPackageNode(node)) {
97             lowerIndent();
98             indent().append("</package>").eol();
99         }
100     }
101
102     public void visitInboundPackageNode(PackageNode node) {
103         printInboundNode(node, "package");
104     }
105
106     public void visitOutboundPackageNode(PackageNode node) {
107         printOutboundNode(node, "package");
108     }
109
110     protected void preprocessClassNode(ClassNode node) {
111         super.preprocessClassNode(node);
112
113         if (shouldShowClassNode(node)) {
114             indent().append("<class confirmed=\"").append(node.isConfirmed() ? "yes" : "no").append("\">").eol();
115             raiseIndent();
116             indent().printNodeName(node).eol();
117         }
118     }
119
120     protected void postprocessClassNode(ClassNode node) {
121         if (shouldShowClassNode(node)) {
122             lowerIndent();
123             indent().append("</class>").eol();
124         }
125     }
126
127     public void visitInboundClassNode(ClassNode node) {
128         printInboundNode(node, "class");
129     }
130
131     public void visitOutboundClassNode(ClassNode node) {
132         printOutboundNode(node, "class");
133     }
134
135     protected void preprocessFeatureNode(FeatureNode node) {
136         super.preprocessFeatureNode(node);
137
138         if (shouldShowFeatureNode(node)) {
139             indent().append("<feature confirmed=\"").append(node.isConfirmed() ? "yes" : "no").append("\">").eol();
140             raiseIndent();
141             indent().printNodeName(node).eol();
142         }
143     }
144
145     protected void postprocessFeatureNode(FeatureNode node) {
146         if (shouldShowFeatureNode(node)) {
147             lowerIndent();
148             indent().append("</feature>").eol();
149         }
150     }
151
152     public void visitInboundFeatureNode(FeatureNode node) {
153         printInboundNode(node, "feature");
154     }
155
156     public void visitOutboundFeatureNode(FeatureNode node) {
157         printOutboundNode(node, "feature");
158     }
159
160     public void printInboundNode(Node node, String JavaDoc type) {
161         if (isShowInbounds()) {
162             indent().append("<inbound type=\"").append(type).append("\" confirmed=\"").append(node.isConfirmed() ? "yes" : "no").append("\">").append(node.getName()).append("</inbound>").eol();
163         }
164     }
165
166     public void printOutboundNode(Node node, String JavaDoc type) {
167         if (isShowOutbounds()) {
168             indent().append("<outbound type=\"").append(type).append("\" confirmed=\"").append(node.isConfirmed() ? "yes" : "no").append("\">").append(node.getName()).append("</outbound>").eol();
169         }
170     }
171
172     protected Printer printNodeName(Node node, String JavaDoc name) {
173         append("<name>");
174         super.printNodeName(node, name);
175         append("</name>");
176
177         return this;
178     }
179 }
180
Popular Tags