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: import java.text.*;
038:
039: public class HTMLPrinter extends TextPrinter {
040: private MessageFormat urlFormat;
041:
042: public HTMLPrinter(PrintWriter out, MessageFormat format) {
043: super (out);
044:
045: this .urlFormat = format;
046: }
047:
048: public HTMLPrinter(TraversalStrategy strategy, PrintWriter out,
049: MessageFormat format) {
050: super (strategy, out);
051:
052: this .urlFormat = format;
053: }
054:
055: protected Printer printScopeNodeName(Node node, String name) {
056: String fullName = node.getName();
057:
058: String escapedName = fullName;
059: escapedName = perl().substitute("s/\\(/\\\\(/g", escapedName);
060: escapedName = perl().substitute("s/\\)/\\\\)/g", escapedName);
061: escapedName = perl().substitute("s/\\$/\\\\\\$/g", escapedName);
062:
063: Object[] urlArgument = new Object[1];
064: urlArgument[0] = escapedName;
065: String url = urlFormat.format(urlArgument);
066:
067: StringBuffer link = new StringBuffer("<a");
068: link.append(" class=\"scope");
069: if (isShowInferred() && !node.isConfirmed()) {
070: link.append(" inferred");
071: }
072: link.append("\"");
073: link.append(" href=\"").append(url).append("\"");
074: link.append(" id=\"").append(fullName).append("\"");
075: link.append(">");
076: link.append(name);
077: link.append("</a>");
078:
079: openPotentialInferredSpan(node);
080: printNodeName(node, link.toString());
081: closePotentialInferredSpan(node);
082:
083: return this ;
084: }
085:
086: protected void printDependencies(Node node,
087: Map<Node, Integer> dependencies) {
088: Object[] urlArgument = new Object[1];
089:
090: String scopeNodeName = node.getName();
091:
092: for (Map.Entry<Node, Integer> entry : dependencies.entrySet()) {
093: Node dependency = entry.getKey();
094:
095: String rawName = dependency.getName();
096: String escapedName = rawName;
097: escapedName = perl().substitute("s/\\(/\\\\(/g",
098: escapedName);
099: escapedName = perl().substitute("s/\\)/\\\\)/g",
100: escapedName);
101: escapedName = perl().substitute("s/\\$/\\\\\\$/g",
102: escapedName);
103: urlArgument[0] = escapedName;
104: String url = urlFormat.format(urlArgument);
105:
106: String symbol;
107: String idConjunction;
108: if (entry.getValue() < 0) {
109: symbol = "<--";
110: idConjunction = "_from_";
111: } else if (entry.getValue() > 0) {
112: symbol = "-->";
113: idConjunction = "_to_";
114: } else {
115: symbol = "<->";
116: idConjunction = "_bidirectional_";
117: }
118:
119: StringBuffer link = new StringBuffer("<a");
120: if (isShowInferred() && !dependency.isConfirmed()) {
121: link.append(" class=\"inferred\"");
122: }
123: link.append(" href=\"").append(url).append("\"");
124: link.append(" id=\"").append(scopeNodeName).append(
125: idConjunction).append(rawName).append("\"");
126: link.append(">");
127: link.append(rawName);
128: link.append("</a>");
129:
130: indent();
131: openPotentialInferredSpan(dependency);
132: append(symbol).append(" ").printDependencyNodeName(
133: dependency, link.toString());
134: closePotentialInferredSpan(dependency);
135: eol();
136: }
137: }
138:
139: private void openPotentialInferredSpan(Node node) {
140: if (isShowInferred() && !node.isConfirmed()) {
141: append("<span class=\"inferred\">");
142: }
143: }
144:
145: private void closePotentialInferredSpan(Node node) {
146: if (isShowInferred() && !node.isConfirmed()) {
147: append("</span>");
148: }
149: }
150: }
|