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 XMLCyclePrinter implements CyclePrinter, Visitor {
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 PrintWriter out;
043:
044: private String indentText = " ";
045:
046: public XMLCyclePrinter(PrintWriter out) {
047: this (out, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX);
048: }
049:
050: public XMLCyclePrinter(PrintWriter out, String encoding,
051: String dtdPrefix) {
052: this .out = out;
053:
054: appendHeader(encoding, dtdPrefix);
055: }
056:
057: private void appendHeader(String encoding, String dtdPrefix) {
058: out.append("<?xml version=\"1.0\" encoding=\"")
059: .append(encoding).append("\" ?>").println();
060: out.println();
061: out.append("<!DOCTYPE dependencies SYSTEM \"")
062: .append(dtdPrefix).append("/cycles.dtd\">").println();
063: out.println();
064: }
065:
066: public void setIndentText(String indentText) {
067: this .indentText = indentText;
068: }
069:
070: public void visitCycles(Collection<Cycle> cycles) {
071: out.append("<cycles>").println();
072:
073: for (Cycle cycle : cycles) {
074: visitCycle(cycle);
075: }
076:
077: out.append("</cycles>").println();
078: }
079:
080: public void visitCycle(Cycle cycle) {
081: out.append(indentText).append("<cycle>").println();
082: traverseNodes(cycle.getPath());
083: out.append(indentText).append("</cycle>").println();
084: }
085:
086: public void traverseNodes(Collection<? extends Node> nodes) {
087: for (Node node : nodes) {
088: node.accept(this );
089: }
090: }
091:
092: public void visitPackageNode(PackageNode node) {
093: visitNode(node, "package");
094: }
095:
096: public void visitInboundPackageNode(PackageNode node) {
097: // Do nothing
098: }
099:
100: public void visitOutboundPackageNode(PackageNode node) {
101: // Do nothing
102: }
103:
104: public void visitClassNode(ClassNode node) {
105: visitNode(node, "class");
106: }
107:
108: public void visitInboundClassNode(ClassNode node) {
109: // Do nothing
110: }
111:
112: public void visitOutboundClassNode(ClassNode node) {
113: // Do nothing
114: }
115:
116: public void visitFeatureNode(FeatureNode node) {
117: visitNode(node, "feature");
118: }
119:
120: public void visitInboundFeatureNode(FeatureNode node) {
121: // Do nothing
122: }
123:
124: public void visitOutboundFeatureNode(FeatureNode node) {
125: // Do nothing
126: }
127:
128: private void visitNode(Node node, String type) {
129: out.append(indentText).append(indentText).append(
130: "<node type=\"").append(type).append("\">").append(
131: node.toString()).append("</node>").println();
132: }
133: }
|