001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependency;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: public class XMLPrinter extends Printer {
039: public static final String DEFAULT_ENCODING = "utf-8";
040: public static final String DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd";
041:
042: private boolean atTopLevel = false;
043:
044: public XMLPrinter(PrintWriter out) {
045: this (out, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX);
046: }
047:
048: public XMLPrinter(TraversalStrategy strategy, PrintWriter out) {
049: this (strategy, out, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX);
050: }
051:
052: public XMLPrinter(PrintWriter out, String encoding, String dtdPrefix) {
053: super (out);
054:
055: appendHeader(encoding, dtdPrefix);
056: }
057:
058: public XMLPrinter(TraversalStrategy strategy, PrintWriter out,
059: String encoding, String dtdPrefix) {
060: super (strategy, out);
061:
062: appendHeader(encoding, dtdPrefix);
063: }
064:
065: private void appendHeader(String encoding, String dtdPrefix) {
066: append("<?xml version=\"1.0\" encoding=\"").append(encoding)
067: .append("\" ?>").eol();
068: eol();
069: append("<!DOCTYPE dependencies SYSTEM \"").append(dtdPrefix)
070: .append("/dependencies.dtd\">").eol();
071: eol();
072: }
073:
074: public void traverseNodes(Collection<? extends Node> nodes) {
075: if (atTopLevel) {
076: super .traverseNodes(nodes);
077: } else {
078: atTopLevel = true;
079: indent().append("<dependencies>").eol();
080: raiseIndent();
081: super .traverseNodes(nodes);
082: lowerIndent();
083: indent().append("</dependencies>").eol();
084: atTopLevel = false;
085: }
086: }
087:
088: protected void preprocessPackageNode(PackageNode node) {
089: super .preprocessPackageNode(node);
090:
091: if (shouldShowPackageNode(node)) {
092: indent().append("<package confirmed=\"").append(
093: node.isConfirmed() ? "yes" : "no").append("\">")
094: .eol();
095: raiseIndent();
096: indent().printScopeNodeName(node).eol();
097: }
098: }
099:
100: protected void postprocessPackageNode(PackageNode node) {
101: if (shouldShowPackageNode(node)) {
102: lowerIndent();
103: indent().append("</package>").eol();
104: }
105: }
106:
107: public void visitInboundPackageNode(PackageNode node) {
108: printInboundNode(node, "package");
109: }
110:
111: public void visitOutboundPackageNode(PackageNode node) {
112: printOutboundNode(node, "package");
113: }
114:
115: protected void preprocessClassNode(ClassNode node) {
116: super .preprocessClassNode(node);
117:
118: if (shouldShowClassNode(node)) {
119: indent().append("<class confirmed=\"").append(
120: node.isConfirmed() ? "yes" : "no").append("\">")
121: .eol();
122: raiseIndent();
123: indent().printScopeNodeName(node).eol();
124: }
125: }
126:
127: protected void postprocessClassNode(ClassNode node) {
128: if (shouldShowClassNode(node)) {
129: lowerIndent();
130: indent().append("</class>").eol();
131: }
132: }
133:
134: public void visitInboundClassNode(ClassNode node) {
135: printInboundNode(node, "class");
136: }
137:
138: public void visitOutboundClassNode(ClassNode node) {
139: printOutboundNode(node, "class");
140: }
141:
142: protected void preprocessFeatureNode(FeatureNode node) {
143: super .preprocessFeatureNode(node);
144:
145: if (shouldShowFeatureNode(node)) {
146: indent().append("<feature confirmed=\"").append(
147: node.isConfirmed() ? "yes" : "no").append("\">")
148: .eol();
149: raiseIndent();
150: indent().printScopeNodeName(node).eol();
151: }
152: }
153:
154: protected void postprocessFeatureNode(FeatureNode node) {
155: if (shouldShowFeatureNode(node)) {
156: lowerIndent();
157: indent().append("</feature>").eol();
158: }
159: }
160:
161: public void visitInboundFeatureNode(FeatureNode node) {
162: printInboundNode(node, "feature");
163: }
164:
165: public void visitOutboundFeatureNode(FeatureNode node) {
166: printOutboundNode(node, "feature");
167: }
168:
169: public void printInboundNode(Node node, String type) {
170: if (isShowInbounds()) {
171: indent().append("<inbound type=\"").append(type).append(
172: "\" confirmed=\"").append(
173: node.isConfirmed() ? "yes" : "no").append("\">")
174: .printDependencyNodeName(node).append("</inbound>")
175: .eol();
176: }
177: }
178:
179: public void printOutboundNode(Node node, String type) {
180: if (isShowOutbounds()) {
181: indent().append("<outbound type=\"").append(type).append(
182: "\" confirmed=\"").append(
183: node.isConfirmed() ? "yes" : "no").append("\">")
184: .printDependencyNodeName(node)
185: .append("</outbound>").eol();
186: }
187: }
188:
189: protected Printer printScopeNodeName(Node node, String name) {
190: append("<name>");
191: super .printScopeNodeName(node, name);
192: append("</name>");
193:
194: return this;
195: }
196: }
|