001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.squid.text; 021 022 public class MultiLinesCommentHandler extends LineContextHandler { 023 024 private StringBuilder currentLineComment; 025 private boolean isFirstLineOfComment = false; 026 027 private boolean isJavadoc = false; 028 private boolean isLicenseHeader = false; 029 030 private boolean commentStarted = false; 031 032 private final static String START_COMMENT_TAG = "/*"; 033 private final static String START_JAVADOC_TAG = "/**"; 034 private final static String START_GWT_NATIVE_CODE_TAG = "/*-{"; 035 private final static String END_COMMENT_TAG = "*/"; 036 037 @Override 038 boolean matchToEnd(Line line, StringBuilder pendingLine) { 039 if (!commentStarted) { 040 throw new IllegalStateException("Method doContextBegin(StringBuilder pendingLine) has not been called first (line = '" + pendingLine 041 + "')."); 042 } 043 currentLineComment.append((char) getLastCharacter(pendingLine)); 044 if (isJavaDoc()) { 045 isJavadoc = true; 046 } 047 if (isGwtNativeCode()) { 048 initProperties(); 049 return true; 050 } 051 boolean match = matchEndOfString(pendingLine, END_COMMENT_TAG); 052 if (match) { 053 endOfCommentLine(line); 054 initProperties(); 055 return true; 056 } 057 return false; 058 } 059 060 private boolean isGwtNativeCode() { 061 return isFirstLineOfComment && currentLineComment.length() == START_GWT_NATIVE_CODE_TAG.length() 062 && currentLineComment.toString().equals(START_GWT_NATIVE_CODE_TAG); 063 } 064 065 private boolean isJavaDoc() { 066 return isFirstLineOfComment && currentLineComment.length() == START_JAVADOC_TAG.length() 067 && currentLineComment.toString().equals(START_JAVADOC_TAG); 068 } 069 070 @Override 071 boolean matchToBegin(Line line, StringBuilder pendingLine) { 072 boolean match = matchEndOfString(pendingLine, START_COMMENT_TAG); 073 if (match) { 074 isFirstLineOfComment = true; 075 commentStarted = true; 076 currentLineComment = new StringBuilder(START_COMMENT_TAG); 077 if (line.getLineIndex() == 1) { 078 isLicenseHeader = true; 079 } else { 080 isLicenseHeader = false; 081 } 082 } 083 return match; 084 } 085 086 @Override 087 boolean matchWithEndOfLine(Line line, StringBuilder pendingLine) { 088 endOfCommentLine(line); 089 return false; 090 } 091 092 private void endOfCommentLine(Line line) { 093 line.setComment(currentLineComment.toString(), isJavadoc, isLicenseHeader); 094 currentLineComment = new StringBuilder(); 095 isFirstLineOfComment = false; 096 } 097 098 private void initProperties() { 099 commentStarted = false; 100 isJavadoc = false; 101 isLicenseHeader = false; 102 currentLineComment = new StringBuilder(); 103 isFirstLineOfComment = false; 104 } 105 }