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 TextCyclePrinter implements CyclePrinter {
039: protected PrintWriter out;
040:
041: private String indentText = " ";
042: private int indentLevel;
043:
044: public TextCyclePrinter(PrintWriter out) {
045: this .out = out;
046: }
047:
048: public void setIndentText(String indentText) {
049: this .indentText = indentText;
050: }
051:
052: public void visitCycles(Collection<Cycle> cycles) {
053: for (Cycle cycle : cycles) {
054: visitCycle(cycle);
055: }
056: }
057:
058: public void visitCycle(Cycle cycle) {
059: Node currentNode;
060: Node previousNode;
061:
062: Iterator<Node> i = cycle.getPath().iterator();
063: currentNode = i.next();
064: visitFirstNode(currentNode);
065:
066: while (i.hasNext()) {
067: previousNode = currentNode;
068: currentNode = i.next();
069: visitNode(previousNode, currentNode);
070: }
071:
072: previousNode = currentNode;
073: currentNode = cycle.getPath().iterator().next();
074: visitNode(previousNode, currentNode);
075: }
076:
077: private void visitFirstNode(Node node) {
078: indentLevel = 0;
079: printFirstNode(node);
080: }
081:
082: private void visitNode(Node previousNode, Node currentNode) {
083: indentLevel++;
084: indent();
085: printNode(previousNode, currentNode);
086: }
087:
088: private void indent() {
089: for (int i = 0; i < indentLevel; i++) {
090: out.print(indentText);
091: }
092: }
093:
094: protected void printFirstNode(Node node) {
095: out.println(node);
096: }
097:
098: protected void printNode(Node previousNode, Node currentNode) {
099: out.print("--> ");
100: out.println(currentNode);
101: }
102: }
|