|
1 /* |
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.codemodel.internal; |
|
27 |
|
28 import java.util.ArrayList; |
|
29 import java.util.Collection; |
|
30 import java.util.Iterator; |
|
31 |
|
32 /** |
|
33 * A part is a part of a javadoc comment, and it is a list of values. |
|
34 * |
|
35 * <p> |
|
36 * A part can contain a free-form text. This text is modeled as a collection of 'values' |
|
37 * in this class. A value can be a {@link JType} (which will be prinited with a @link tag), |
|
38 * anything that can be turned into a {@link String} via the {@link Object#toString()} method, |
|
39 * or a {@link Collection}/array of those objects. |
|
40 * |
|
41 * <p> |
|
42 * Values can be added through the various append methods one by one or in a bulk. |
|
43 * |
|
44 * @author Kohsuke Kawaguchi |
|
45 */ |
|
46 public class JCommentPart extends ArrayList<Object> { |
|
47 |
|
48 private static final long serialVersionUID = 1L; |
|
49 |
|
50 /** |
|
51 * Appends a new value. |
|
52 * |
|
53 * If the value is {@link JType} it will be printed as a @link tag. |
|
54 * Otherwise it will be converted to String via {@link Object#toString()}. |
|
55 */ |
|
56 public JCommentPart append(Object o) { |
|
57 add(o); |
|
58 return this; |
|
59 } |
|
60 |
|
61 public boolean add(Object o) { |
|
62 flattenAppend(o); |
|
63 return true; |
|
64 } |
|
65 |
|
66 private void flattenAppend(Object value) { |
|
67 if(value==null) return; |
|
68 if(value instanceof Object[]) { |
|
69 for( Object o : (Object[])value) |
|
70 flattenAppend(o); |
|
71 } else |
|
72 if(value instanceof Collection<?>) { |
|
73 for( Object o : (Collection<?>)value) |
|
74 flattenAppend(o); |
|
75 } else |
|
76 super.add(value); |
|
77 } |
|
78 |
|
79 /** |
|
80 * Writes this part into the formatter by using the specified indentation. |
|
81 */ |
|
82 protected void format( JFormatter f, String indent ) { |
|
83 if(!f.isPrinting()) { |
|
84 // quickly pass the types to JFormatter, as that's all we care. |
|
85 // we don't need to worry about the exact formatting of text. |
|
86 for( Object o : this ) |
|
87 if(o instanceof JClass) |
|
88 f.g((JClass)o); |
|
89 return; |
|
90 } |
|
91 |
|
92 if(!isEmpty()) |
|
93 f.p(indent); |
|
94 |
|
95 Iterator<Object> itr = iterator(); |
|
96 while(itr.hasNext()) { |
|
97 Object o = itr.next(); |
|
98 |
|
99 if(o instanceof String) { |
|
100 int idx; |
|
101 String s = (String)o; |
|
102 while( (idx=s.indexOf('\n'))!=-1 ) { |
|
103 String line = s.substring(0,idx); |
|
104 if(line.length()>0) |
|
105 f.p(escape(line)); |
|
106 s = s.substring(idx+1); |
|
107 f.nl().p(indent); |
|
108 } |
|
109 if(s.length()!=0) |
|
110 f.p(escape(s)); |
|
111 } else |
|
112 if(o instanceof JClass) { |
|
113 // TODO: this doesn't print the parameterized type properly |
|
114 ((JClass)o).printLink(f); |
|
115 } else |
|
116 if(o instanceof JType) { |
|
117 f.g((JType)o); |
|
118 } else |
|
119 throw new IllegalStateException(); |
|
120 } |
|
121 |
|
122 if(!isEmpty()) |
|
123 f.nl(); |
|
124 } |
|
125 |
|
126 /** |
|
127 * Escapes the appearance of the comment terminator. |
|
128 */ |
|
129 private String escape(String s) { |
|
130 while(true) { |
|
131 int idx = s.indexOf("*/"); |
|
132 if(idx <0) return s; |
|
133 |
|
134 s = s.substring(0,idx+1)+"<!---->"+s.substring(idx+1); |
|
135 } |
|
136 } |
|
137 } |