1 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 DEFAULT_ENCODING = "utf-8"; 40 public static final String 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 encoding, String dtdPrefix) { 53 super(out); 54 55 appendHeader(encoding, dtdPrefix); 56 } 57 58 public XMLPrinter(TraversalStrategy strategy, PrintWriter out, String encoding, String dtdPrefix) { 59 super(strategy, out); 60 61 appendHeader(encoding, dtdPrefix); 62 } 63 64 private void appendHeader(String encoding, String 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 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 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 name) { 173 append("<name>"); 174 super.printNodeName(node, name); 175 append("</name>"); 176 177 return this; 178 } 179 } 180 | Popular Tags |