001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools.sourceformatter;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.util.ArrayList;
022    import java.util.List;
023    import java.util.regex.Matcher;
024    import java.util.regex.Pattern;
025    
026    /**
027     * @author Hugo Huijser
028     */
029    public class JavaTerm {
030    
031            public JavaTerm(String name, int type, String content, int lineCount) {
032                    _name = name;
033                    _type = type;
034                    _content = content;
035                    _lineCount = lineCount;
036            }
037    
038            public String getContent() {
039                    return _content;
040            }
041    
042            public int getLineCount() {
043                    return _lineCount;
044            }
045    
046            public String getName() {
047                    return _name;
048            }
049    
050            public List<String> getParameterTypes() {
051                    if (_parameterTypes != null) {
052                            return _parameterTypes;
053                    }
054    
055                    _parameterTypes = new ArrayList<String>();
056    
057                    if (!JavaClass.isInJavaTermTypeGroup(
058                                    _type, JavaClass.TYPE_CONSTRUCTOR) &&
059                            !JavaClass.isInJavaTermTypeGroup(_type, JavaClass.TYPE_METHOD)) {
060    
061                            return _parameterTypes;
062                    }
063    
064                    Matcher matcher = _parameterTypesPattern.matcher(_content);
065    
066                    if (!matcher.find()) {
067                            return _parameterTypes;
068                    }
069    
070                    String parameters = matcher.group(3);
071    
072                    if (Validator.isNull(parameters)) {
073                            return _parameterTypes;
074                    }
075    
076                    parameters = StringUtil.replace(
077                            parameters, new String[] {StringPool.TAB, StringPool.NEW_LINE},
078                            new String[] {StringPool.BLANK, StringPool.SPACE});
079    
080                    for (int x = 0;;) {
081                            parameters = StringUtil.trim(parameters);
082    
083                            x = parameters.indexOf(StringPool.SPACE);
084    
085                            if (x == -1) {
086                                    return _parameterTypes;
087                            }
088    
089                            String parameterType = parameters.substring(0, x);
090    
091                            if (parameterType.equals("final")) {
092                                    int y = parameters.indexOf(StringPool.SPACE, x + 1);
093    
094                                    if (y == -1) {
095                                            return _parameterTypes;
096                                    }
097    
098                                    parameterType = parameters.substring(x + 1, y);
099                            }
100    
101                            _parameterTypes.add(parameterType);
102    
103                            int y = parameters.indexOf(StringPool.COMMA);
104    
105                            if (y == -1) {
106                                    return _parameterTypes;
107                            }
108    
109                            parameters = parameters.substring(y + 1);
110                    }
111            }
112    
113            public int getType() {
114                    return _type;
115            }
116    
117            public void setContent(String content) {
118                    _content = content;
119            }
120    
121            public void setLineCount(int lineCount) {
122                    _lineCount = lineCount;
123            }
124    
125            public void setName(String name) {
126                    _name = name;
127            }
128    
129            public void setParameterTypes(List<String> parameterTypes) {
130                    _parameterTypes = parameterTypes;
131            }
132    
133            public void setType(int type) {
134                    _type = type;
135            }
136    
137            private String _content;
138            private int _lineCount;
139            private String _name;
140            private List<String> _parameterTypes;
141            private final Pattern _parameterTypesPattern = Pattern.compile(
142                    "\t(private |protected |public )([\\s\\S]*?)\\(([\\s\\S]*?)\\)");
143            private int _type;
144    
145    }