1 | /* |
2 | * Copyright (c) 2001-2009, Jean Tessier |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * * Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * * Neither the name of Jean Tessier nor the names of his contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
32 | |
33 | package com.jeantessier.dependency; |
34 | |
35 | import java.io.*; |
36 | import java.util.*; |
37 | import java.text.*; |
38 | |
39 | public class HTMLPrinter extends TextPrinter { |
40 | private static final String FROM = "<--"; |
41 | private static final String TO = "-->"; |
42 | private static final String BIDIRECTIONAL = "<->"; |
43 | |
44 | private MessageFormat urlFormat; |
45 | |
46 | public HTMLPrinter(PrintWriter out, MessageFormat format) { |
47 | super(out); |
48 | |
49 | this.urlFormat = format; |
50 | } |
51 | |
52 | public HTMLPrinter(TraversalStrategy strategy, PrintWriter out, MessageFormat format) { |
53 | super(strategy, out); |
54 | |
55 | this.urlFormat = format; |
56 | } |
57 | |
58 | protected Printer printScopeNodeName(Node node, String name) { |
59 | String fullName = node.getName(); |
60 | |
61 | Object[] urlArgument = new Object[1]; |
62 | urlArgument[0] = perlEscapeName(fullName); |
63 | String url = urlFormat.format(urlArgument); |
64 | |
65 | StringBuffer link = new StringBuffer("<a"); |
66 | link.append(" class=\"scope"); |
67 | if (isShowInferred() && !node.isConfirmed()) { |
68 | link.append(" inferred"); |
69 | } |
70 | link.append("\""); |
71 | link.append(" href=\"").append(url).append("\""); |
72 | link.append(" id=\"").append(fullName).append("\""); |
73 | link.append(">"); |
74 | link.append(name); |
75 | link.append("</a>"); |
76 | |
77 | openPotentialInferredSpan(node); |
78 | printNodeName(node, link.toString()); |
79 | closePotentialInferredSpan(node); |
80 | |
81 | return this; |
82 | } |
83 | |
84 | protected void printDependencies(Node node, Map<Node, Integer> dependencies) { |
85 | Object[] urlArgument = new Object[1]; |
86 | |
87 | String scopeNodeName = node.getName(); |
88 | |
89 | for (Map.Entry<Node, Integer> entry : dependencies.entrySet()) { |
90 | Node dependency = entry.getKey(); |
91 | |
92 | String rawName = dependency.getName(); |
93 | urlArgument[0] = perlEscapeName(rawName); |
94 | String url = urlFormat.format(urlArgument); |
95 | |
96 | String symbol; |
97 | String idConjunction; |
98 | if (entry.getValue() < 0) { |
99 | symbol = FROM; |
100 | idConjunction = "_from_"; |
101 | } else if (entry.getValue() > 0) { |
102 | symbol = TO; |
103 | idConjunction = "_to_"; |
104 | } else { |
105 | symbol = BIDIRECTIONAL; |
106 | idConjunction = "_bidirectional_"; |
107 | } |
108 | |
109 | StringBuffer link = new StringBuffer("<a"); |
110 | if (isShowInferred() && !dependency.isConfirmed()) { |
111 | link.append(" class=\"inferred\""); |
112 | } |
113 | link.append(" href=\"").append(url).append("\""); |
114 | link.append(" id=\"").append(scopeNodeName).append(idConjunction).append(rawName).append("\""); |
115 | link.append(">"); |
116 | link.append(rawName); |
117 | link.append("</a>"); |
118 | |
119 | indent(); |
120 | openPotentialInferredSpan(dependency); |
121 | append(symbol).append(" ").printDependencyNodeName(dependency, link.toString()); |
122 | closePotentialInferredSpan(dependency); |
123 | eol(); |
124 | } |
125 | } |
126 | |
127 | private void openPotentialInferredSpan(Node node) { |
128 | if (isShowInferred() && !node.isConfirmed()) { |
129 | append("<span class=\"inferred\">"); |
130 | } |
131 | } |
132 | |
133 | private void closePotentialInferredSpan(Node node) { |
134 | if (isShowInferred() && !node.isConfirmed()) { |
135 | append("</span>"); |
136 | } |
137 | } |
138 | |
139 | private String perlEscapeName(String name) { |
140 | String result = name; |
141 | |
142 | result = perl().substitute("s/\\(/\\\\(/g", result); |
143 | result = perl().substitute("s/\\)/\\\\)/g", result); |
144 | result = perl().substitute("s/\\$/\\\\\\$/g", result); |
145 | result = perl().substitute("s/\\[/\\\\[/g", result); |
146 | result = perl().substitute("s/\\]/\\\\]/g", result); |
147 | |
148 | return result; |
149 | } |
150 | } |