001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.codemodel.internal;
027:
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: /**
033: * A part is a part of a javadoc comment, and it is a list of values.
034: *
035: * <p>
036: * A part can contain a free-form text. This text is modeled as a collection of 'values'
037: * in this class. A value can be a {@link JType} (which will be prinited with a @link tag),
038: * anything that can be turned into a {@link String} via the {@link Object#toString()} method,
039: * or a {@link Collection}/array of those objects.
040: *
041: * <p>
042: * Values can be added through the various append methods one by one or in a bulk.
043: *
044: * @author Kohsuke Kawaguchi
045: */
046: public class JCommentPart extends ArrayList<Object> {
047: /**
048: * Appends a new value.
049: *
050: * If the value is {@link JType} it will be printed as a @link tag.
051: * Otherwise it will be converted to String via {@link Object#toString()}.
052: */
053: public JCommentPart append(Object o) {
054: add(o);
055: return this ;
056: }
057:
058: public boolean add(Object o) {
059: flattenAppend(o);
060: return true;
061: }
062:
063: private void flattenAppend(Object value) {
064: if (value == null)
065: return;
066: if (value instanceof Object[]) {
067: for (Object o : (Object[]) value)
068: flattenAppend(o);
069: } else if (value instanceof Collection) {
070: for (Object o : (Collection) value)
071: flattenAppend(o);
072: } else
073: super .add(value);
074: }
075:
076: /**
077: * Writes this part into the formatter by using the specified indentation.
078: */
079: protected void format(JFormatter f, String indent) {
080: if (!f.isPrinting()) {
081: // quickly pass the types to JFormatter
082: for (Object o : this )
083: if (o instanceof JClass)
084: f.g((JClass) o);
085: return;
086: }
087:
088: if (!isEmpty())
089: f.p(indent);
090:
091: Iterator itr = iterator();
092: while (itr.hasNext()) {
093: Object o = itr.next();
094:
095: if (o instanceof String) {
096: int idx;
097: String s = (String) o;
098: while ((idx = s.indexOf('\n')) != -1) {
099: String line = s.substring(0, idx);
100: if (line.length() > 0)
101: f.p(line);
102: s = s.substring(idx + 1);
103: f.nl().p(indent);
104: }
105: if (s.length() != 0)
106: f.p(s);
107: } else if (o instanceof JClass) {
108: // TODO: this doesn't print the parameterized type properly
109: ((JClass) o).printLink(f);
110: } else if (o instanceof JType) {
111: f.g((JType) o);
112: } else
113: throw new IllegalStateException();
114: }
115:
116: if (!isEmpty())
117: f.nl();
118: }
119: }
|