001// Copyright 2007, 2008, 2011, 2012 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.javadoc;
016
017import com.sun.javadoc.FieldDoc;
018import com.sun.javadoc.SeeTag;
019import com.sun.javadoc.Tag;
020
021import java.io.IOException;
022import java.util.regex.Pattern;
023
024import org.apache.commons.lang.StringEscapeUtils;
025
026public class ParameterDescription
027{
028    public final FieldDoc field;
029
030    public final String name;
031
032    public final String type;
033
034    public final String defaultValue;
035
036    public final String defaultPrefix;
037
038    public final boolean required;
039
040    public final boolean allowNull;
041
042    public final boolean cache;
043
044    public final String since;
045
046    public final boolean deprecated;
047
048    private static final Pattern STRIPPER = Pattern.compile("(<.*?>|&.*?;)", Pattern.DOTALL);
049
050    public ParameterDescription(FieldDoc fieldDoc, String name, String type, String defaultValue, String defaultPrefix,
051                                boolean required, boolean allowNull, boolean cache, String since, boolean deprecated)
052    {
053        this.field = fieldDoc;
054        this.name = name;
055        this.type = type;
056        this.defaultValue = defaultValue;
057        this.defaultPrefix = defaultPrefix;
058        this.required = required;
059        this.allowNull = allowNull;
060        this.cache = cache;
061        this.since = since;
062        this.deprecated = deprecated;
063    }
064
065    /**
066     * Extracts the description, converting Text and @link nodes as needed into markup text.
067     *
068     * @return markup text, ready for writing
069     * @throws IOException
070     */
071    public String extractDescription() throws IOException
072    {
073        StringBuilder builder = new StringBuilder();
074
075        for (Tag tag : field.inlineTags())
076        {
077            if (tag.name().equals("Text"))
078            {
079                builder.append(tag.text());
080                continue;
081            }
082
083            if (tag.name().equals("@link"))
084            {
085                SeeTag seeTag = (SeeTag) tag;
086
087                String label = seeTag.label();
088                if (label != null && !label.equals(""))
089                {
090                    builder.append(label);
091                    continue;
092                }
093
094                if (seeTag.referencedClassName() != null)
095                    builder.append(seeTag.referencedClassName());
096
097                if (seeTag.referencedMemberName() != null)
098                {
099                    builder.append("#");
100                    builder.append(seeTag.referencedMemberName());
101                }
102            }
103            else if (tag.name().equals("@code"))
104            {
105                builder.append("<code>");
106                builder.append(StringEscapeUtils.escapeHtml(tag.text()));
107                builder.append("</code>");
108            }
109        }
110
111        String text = builder.toString();
112
113        // Fix it up a little.
114
115        // Remove any simple open or close tags found in the text, as well as any XML entities.
116
117        return STRIPPER.matcher(text).replaceAll("").trim();
118    }
119}