From a16fe9d1448070c61f8eb7d43369643b666cf71b Mon Sep 17 00:00:00 2001 From: bellingard Date: Thu, 21 Oct 2010 14:09:13 +0000 Subject: [SONAR-1875] Improve CodeReaderFilter with channel capabilities http://jira.codehaus.org/browse/SONAR-1875 --- .../org/sonar/channel/ChannelCodeReaderFilter.java | 4 +- .../java/org/sonar/channel/ChannelDispatcher.java | 6 +- .../main/java/org/sonar/channel/CodeBuffer.java | 27 ++++--- .../main/java/org/sonar/channel/CodeReader.java | 36 ++++++--- .../org/sonar/channel/CodeReaderConfiguration.java | 90 ++++++++++++++++++++++ .../java/org/sonar/channel/CodeReaderFilter.java | 21 +++++ 6 files changed, 154 insertions(+), 30 deletions(-) create mode 100644 sonar-channel/src/main/java/org/sonar/channel/CodeReaderConfiguration.java (limited to 'sonar-channel/src/main') diff --git a/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java b/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java index c68265a1d33..dabe5b7f4aa 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java +++ b/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java @@ -35,7 +35,7 @@ public final class ChannelCodeReaderFilter extends CodeReaderFilter extends CodeReaderFilter extends Channel { private static final Logger logger = LoggerFactory.getLogger(ChannelDispatcher.class); private final boolean failIfNoChannelToConsumeOneCharacter; + @SuppressWarnings("rawtypes") private final Channel[] channels; + @SuppressWarnings("rawtypes") public ChannelDispatcher(List tokenizers) { this(tokenizers, false); } + @SuppressWarnings("rawtypes") public ChannelDispatcher(List tokenizers, boolean failIfNoChannelToConsumeOneCharacter) { this.channels = tokenizers.toArray(new Channel[0]); // NOSONAR, lack of performance is not an issue here this.failIfNoChannelToConsumeOneCharacter = failIfNoChannelToConsumeOneCharacter; @@ -53,8 +56,7 @@ public class ChannelDispatcher extends Channel { } if ( !channelConsumed) { String message = "None of the channel has been able to handle character '" + (char) code.peek() + "' (decimal value " + code.peek() - + ") at line " - + code.getLinePosition() + ", column " + code.getColumnPosition(); + + ") at line " + code.getLinePosition() + ", column " + code.getColumnPosition(); if (failIfNoChannelToConsumeOneCharacter) { throw new IllegalStateException(message); } diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java b/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java index d308a48104a..a8433264b7a 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java +++ b/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java @@ -41,39 +41,35 @@ public class CodeBuffer implements CharSequence { private final Reader code; private int lastChar = -1; private Cursor cursor; - private final static int DEFAULT_BUFFER_CAPACITY = 8000; private int bufferCapacity; private char[] buffer; private int bufferPosition = 0; private int bufferSize = 0; private static final char LF = '\n'; private static final char CR = '\r'; + private int tabWidth; + private CodeReaderConfiguration configuration; + private boolean recordingMode = false; private StringBuilder recordedCharacters = new StringBuilder(); - public CodeBuffer(Reader code, CodeReaderFilter... codeReaderFilters) { - this(code, DEFAULT_BUFFER_CAPACITY, codeReaderFilters); - } - - private CodeBuffer(Reader initialCodeReader, int bufferCapacity, CodeReaderFilter... codeReaderFilters) { + protected CodeBuffer(Reader initialCodeReader, CodeReaderConfiguration configuration) { + this.configuration = configuration; lastChar = -1; cursor = new Cursor(); - this.bufferCapacity = bufferCapacity; + bufferCapacity = configuration.getBufferCapacity(); + tabWidth = configuration.getTabWidth(); buffer = new char[bufferCapacity]; Reader reader = initialCodeReader; - for (CodeReaderFilter codeReaderFilter : codeReaderFilters) { + for (CodeReaderFilter codeReaderFilter : configuration.getCodeReaderFilters()) { reader = new Filter(reader, codeReaderFilter); } this.code = reader; fillBuffer(); } - public CodeBuffer(String code, CodeReaderFilter... codeReaderFilters) { - this(new StringReader(code), codeReaderFilters); - } - - protected CodeBuffer(String code, int bufferCapacity, CodeReaderFilter... codeReaderFilters) { - this(new StringReader(code), bufferCapacity, codeReaderFilters); + protected CodeBuffer(String code, CodeReaderConfiguration configuration) { + this(new StringReader(code), configuration); } /** @@ -94,6 +90,8 @@ public class CodeBuffer implements CharSequence { cursor.line++; } cursor.column = 0; + } else if (character == '\t') { + cursor.column += tabWidth; } else { cursor.column++; } @@ -263,6 +261,7 @@ public class CodeBuffer implements CharSequence { public Filter(Reader in, CodeReaderFilter codeReaderFilter) { super(in); this.codeReaderFilter = codeReaderFilter; + this.codeReaderFilter.setConfiguration(CodeBuffer.this.configuration.cloneWithoutCodeReaderFilters()); this.codeReaderFilter.setReader(in); } diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java b/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java index af4ade884d0..73a2172fbeb 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java +++ b/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java @@ -37,26 +37,38 @@ public class CodeReader extends CodeBuffer { * Constructor needed to be backward compatible (before using CodeReaderFilter) */ public CodeReader(Reader code) { - super(code, new CodeReaderFilter[] {}); + super(code, new CodeReaderConfiguration()); } /* * Constructor needed to be backward compatible (before using CodeReaderFilter) */ public CodeReader(String code) { - super(code, new CodeReaderFilter[] {}); + super(code, new CodeReaderConfiguration()); } - public CodeReader(Reader code, CodeReaderFilter... codeReaderFilters) { - super(code, codeReaderFilters); - } - - public CodeReader(String code, CodeReaderFilter... codeReaderFilters) { - super(code, codeReaderFilters); + /** + * Creates a code reader with specific configuration parameters. + * + * @param code + * the Reader to read code from + * @param configuration + * the configuration parameters + */ + public CodeReader(Reader code, CodeReaderConfiguration configuration) { + super(code, configuration); } - protected CodeReader(String code, int bufferCapacity, CodeReaderFilter... codeReaderFilters) { - super(code, bufferCapacity, codeReaderFilters); + /** + * Creates a code reader with specific configuration parameters. + * + * @param code + * the code itself + * @param configuration + * the configuration parameters + */ + public CodeReader(String code, CodeReaderConfiguration configuration) { + super(code, configuration); } /** @@ -113,7 +125,7 @@ public class CodeReader extends CodeBuffer { } /** - * @deprecated see peekTo(EndMatcher matcher, Appendable appendable) + * @see peekTo(EndMatcher matcher, Appendable appendable) */ @Deprecated public final String peekTo(EndMatcher matcher) { @@ -123,7 +135,7 @@ public class CodeReader extends CodeBuffer { } /** - * @deprecated see popTo(Matcher matcher, Appendable appendable) + * @see popTo(Matcher matcher, Appendable appendable) */ @Deprecated public final void popTo(EndMatcher matcher, Appendable appendable) { diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeReaderConfiguration.java b/sonar-channel/src/main/java/org/sonar/channel/CodeReaderConfiguration.java new file mode 100644 index 00000000000..1f837aea4ba --- /dev/null +++ b/sonar-channel/src/main/java/org/sonar/channel/CodeReaderConfiguration.java @@ -0,0 +1,90 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.channel; + +/** + * Configuration parameters used by a CodeReader to handle some specificities. + */ +public class CodeReaderConfiguration { + + private final static int DEFAULT_BUFFER_CAPACITY = 8000; + + private final static int DEFAULT_TAB_WIDTH = 1; + + private int bufferCapacity = DEFAULT_BUFFER_CAPACITY; + + private int tabWidth = DEFAULT_TAB_WIDTH; + + private CodeReaderFilter[] codeReaderFilters = new CodeReaderFilter[0]; + + /** + * @return the bufferCapacity + */ + public int getBufferCapacity() { + return bufferCapacity; + } + + /** + * @param bufferCapacity + * the bufferCapacity to set + */ + public void setBufferCapacity(int bufferCapacity) { + this.bufferCapacity = bufferCapacity; + } + + /** + * @return the tabWidth + */ + public int getTabWidth() { + return tabWidth; + } + + /** + * @param tabWidth + * the tabWidth to set + */ + public void setTabWidth(int tabWidth) { + this.tabWidth = tabWidth; + } + + /** + * @return the codeReaderFilters + */ + @SuppressWarnings("rawtypes") + public CodeReaderFilter[] getCodeReaderFilters() { + return codeReaderFilters; + } + + /** + * @param codeReaderFilters + * the codeReaderFilters to set + */ + public void setCodeReaderFilters(CodeReaderFilter... codeReaderFilters) { + this.codeReaderFilters = codeReaderFilters; + } + + public CodeReaderConfiguration cloneWithoutCodeReaderFilters() { + CodeReaderConfiguration clone = new CodeReaderConfiguration(); + clone.setBufferCapacity(bufferCapacity); + clone.setTabWidth(tabWidth); + return clone; + } + +} diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeReaderFilter.java b/sonar-channel/src/main/java/org/sonar/channel/CodeReaderFilter.java index 7b5106b153f..d152783a17b 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/CodeReaderFilter.java +++ b/sonar-channel/src/main/java/org/sonar/channel/CodeReaderFilter.java @@ -39,6 +39,8 @@ public abstract class CodeReaderFilter { private OUTPUT output; + private CodeReaderConfiguration configuration; + public CodeReaderFilter() { } @@ -84,6 +86,25 @@ public abstract class CodeReaderFilter { this.output = output; } + /** + * Returns the configuration used for the CodeReader + * + * @return the configuration + */ + public CodeReaderConfiguration getConfiguration() { + return configuration; + } + + /** + * Sets the configuration that must be used by the CodeReader + * + * @param configuration + * the configuration to set + */ + public void setConfiguration(CodeReaderConfiguration configuration) { + this.configuration = configuration; + } + /** * This method implements the filtering logic, that is: *
    -- cgit v1.2.3