001    /**
002     * Copyright (c) 2000-2013 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.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.io.IOException;
024    
025    import java.util.List;
026    
027    /**
028     * @author Hugo Huijser
029     */
030    public class JavaTerm {
031    
032            public JavaTerm(
033                    String name, int type, List<String> parameterTypes, String content,
034                    int lineCount) {
035    
036                    _name = name;
037                    _type = type;
038                    _parameterTypes = parameterTypes;
039                    _content = content;
040                    _lineCount = lineCount;
041            }
042    
043            public String getContent() {
044                    return _content;
045            }
046    
047            public int getLineCount() {
048                    return _lineCount;
049            }
050    
051            public String getName() {
052                    return _name;
053            }
054    
055            public List<String> getParameterTypes() {
056                    return _parameterTypes;
057            }
058    
059            public int getType() {
060                    return _type;
061            }
062    
063            public void setContent(String content) {
064                    _content = content;
065            }
066    
067            public void setLineCount(int lineCount) {
068                    _lineCount = lineCount;
069            }
070    
071            public void setName(String name) {
072                    _name = name;
073            }
074    
075            public void setParameterTypes(List<String> parameterTypes) {
076                    _parameterTypes = parameterTypes;
077            }
078    
079            public void setType(int type) {
080                    _type = type;
081            }
082    
083            public void sortAnnotations() throws IOException {
084                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
085                            new UnsyncStringReader(_content));
086    
087                    String line = null;
088    
089                    String annotation = StringPool.BLANK;
090                    String previousAnnotation = StringPool.BLANK;
091    
092                    while ((line = unsyncBufferedReader.readLine()) != null) {
093                            if (line.equals(StringPool.TAB + StringPool.CLOSE_CURLY_BRACE)) {
094                                    return;
095                            }
096    
097                            if (StringUtil.count(line, StringPool.TAB) == 1) {
098                                    if (Validator.isNotNull(previousAnnotation) &&
099                                            (previousAnnotation.compareTo(annotation) > 0)) {
100    
101                                            _content = StringUtil.replaceFirst(
102                                                    _content, previousAnnotation, annotation);
103                                            _content = StringUtil.replaceLast(
104                                                    _content, annotation, previousAnnotation);
105    
106                                            return;
107                                    }
108    
109                                    if (line.startsWith(StringPool.TAB + StringPool.AT)) {
110                                            if (Validator.isNotNull(annotation)) {
111                                                    previousAnnotation = annotation;
112                                            }
113    
114                                            annotation = line + "\n";
115                                    }
116                                    else {
117                                            annotation = StringPool.BLANK;
118                                    }
119                            }
120                            else {
121                                    if (Validator.isNull(annotation)) {
122                                            return;
123                                    }
124    
125                                    annotation += line + "\n";
126                            }
127                    }
128            }
129    
130            private String _content;
131            private int _lineCount;
132            private String _name;
133            private List<String> _parameterTypes;
134            private int _type;
135    
136    }