Author: pottlinger
Date: Mon May 18 18:15:45 2015
New Revision: 1680062
URL: http://svn.apache.org/r1680062
Log:
Low Hanging Fruit (LHF)
-Extracting enum ReportFormat from Mojo (can go to api later).
-Fixed minor issues from PMD reports.
Added:
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/ReportFormat.java
creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/config/ReportFormatTest.java
Modified:
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/analysis/license/CopyrightHeader.java
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/header/CharFilter.java
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/IXmlWriter.java
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/impl/base/XmlWriter.java
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java
Modified:
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/analysis/license/CopyrightHeader.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/analysis/license/CopyrightHeader.java?rev=1680062&r1=1680061&r2=1680062&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/analysis/license/CopyrightHeader.java
(original)
+++
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/analysis/license/CopyrightHeader.java
Mon May 18 18:15:45 2015
@@ -18,30 +18,30 @@
*/
package org.apache.rat.analysis.license;
-import java.util.regex.Pattern;
-
import org.apache.rat.analysis.IHeaderMatcher;
import org.apache.rat.analysis.RatHeaderAnalysisException;
import org.apache.rat.api.Document;
import org.apache.rat.api.MetaData.Datum;
+import java.util.regex.Pattern;
+
/**
* Matches a typical Copyright header line only based on a regex pattern
* which allows for one (starting) year or year range, and a
* configurable copyright owner.
- *
+ * <p/>
* <p>The matching is done case insensitive</p>
- *
+ * <p/>
* Example supported Copyright header lines, using copyright owner
"FooBar"
* <ul>
- * <li>* Copyright 2010 FooBar. *</li>
- * <li>* Copyright 2010-2012 FooBar. *</li>
- * <li>*copyright 2012 foobar*</li>
+ * <li>* Copyright 2010 FooBar. *</li>
+ * <li>* Copyright 2010-2012 FooBar. *</li>
+ * <li>*copyright 2012 foobar*</li>
* </ul>
- *
+ * <p/>
* <p>Note also that the copyright owner is appended to the regex pattern, so
* can support additional regex but also requires escaping where needed,<br>
- * e.g. use "FooBar \(www\.foobar\.com\)" for matching "FooBar
(www.foobar.com)"</p>
+ * e.g. use "FooBar \(www\.foobar\.com\)" for matching "FooBar
(www.foobar.com)"</p>
*
* @since Rat 0.9
*/
@@ -53,7 +53,7 @@ public class CopyrightHeader extends Bas
private String copyrightOwner;
private boolean copyrightMatch = false;
- public CopyrightHeader(){
+ public CopyrightHeader() {
}
protected CopyrightHeader(Datum licenseFamilyCategory, Datum
licenseFamilyName, String notes) {
@@ -68,7 +68,7 @@ public class CopyrightHeader extends Bas
// Called by ctor, so must not be overridden
public final void setCopyrightOwner(String copyrightOwner) {
this.copyrightOwner = copyrightOwner;
- this.copyrightPattern =
Pattern.compile(COPYRIGHT_PREFIX_PATTERN_DEFN+copyrightOwner+".*",
Pattern.CASE_INSENSITIVE);
+ this.copyrightPattern = Pattern.compile(COPYRIGHT_PREFIX_PATTERN_DEFN
+ copyrightOwner + ".*", Pattern.CASE_INSENSITIVE);
}
public String getCopyRightOwner() {
@@ -91,10 +91,8 @@ public class CopyrightHeader extends Bas
}
public boolean match(Document subject, String s) throws
RatHeaderAnalysisException {
- if (!copyrightMatch) {
- if (matchCopyright(s)) {
- reportOnLicense(subject);
- }
+ if (!copyrightMatch && matchCopyright(s)) {
+ reportOnLicense(subject);
}
return copyrightMatch;
}
Added:
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/ReportFormat.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/ReportFormat.java?rev=1680062&view=auto
==============================================================================
---
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/ReportFormat.java
(added)
+++
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/ReportFormat.java
Mon May 18 18:15:45 2015
@@ -0,0 +1,37 @@
+package org.apache.rat.config;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @author pottlinger
+ * @version 2015-05-18, 19:49
+ */
+public enum ReportFormat {
+ PLAIN,
+
+ XML;
+
+ public boolean is(String optionGiven) {
+ if (optionGiven == null || optionGiven.length() == 0) {
+ return false;
+ }
+ return name().equalsIgnoreCase(optionGiven);
+ }
+}
Modified:
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/header/CharFilter.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/header/CharFilter.java?rev=1680062&r1=1680061&r2=1680062&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/header/CharFilter.java
(original)
+++
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/header/CharFilter.java
Mon May 18 18:15:45 2015
@@ -26,5 +26,5 @@ interface CharFilter {
* @return true if this character should be filtered out,
* false if this character should be filtered in
*/
- public boolean isFilteredOut(char character);
+ boolean isFilteredOut(char character);
}
Modified:
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/IXmlWriter.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/IXmlWriter.java?rev=1680062&r1=1680061&r2=1680062&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/IXmlWriter.java
(original)
+++
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/IXmlWriter.java
Mon May 18 18:15:45 2015
@@ -36,7 +36,7 @@ public interface IXmlWriter {
* if called after the first element has been written
* or once a prolog has already been written
*/
- public IXmlWriter startDocument() throws IOException;
+ IXmlWriter startDocument() throws IOException;
/**
* Writes the start of an element.
@@ -47,7 +47,7 @@ public interface IXmlWriter {
* @throws OperationNotAllowedException
* if called after the first element has been closed
*/
- public IXmlWriter openElement(CharSequence elementName) throws IOException;
+ IXmlWriter openElement(CharSequence elementName) throws IOException;
/**
* Writes an attribute of an element.
@@ -62,7 +62,7 @@ public interface IXmlWriter {
* @throws OperationNotAllowedException if called after {@link
#content(CharSequence)}
* or {@link #closeElement()} or before any call to {@link
#openElement(CharSequence)}
*/
- public IXmlWriter attribute(CharSequence name, CharSequence value) throws
IOException;
+ IXmlWriter attribute(CharSequence name, CharSequence value) throws
IOException;
/**
* Writes content.
@@ -75,7 +75,7 @@ public interface IXmlWriter {
* if called before any call to {@link #openElement}
* or after the first element has been closed
*/
- public IXmlWriter content(CharSequence content) throws IOException;
+ IXmlWriter content(CharSequence content) throws IOException;
/**
* Closes the last element written.
@@ -85,7 +85,7 @@ public interface IXmlWriter {
* if called before any call to {@link #openElement}
* or after the first element has been closed
*/
- public IXmlWriter closeElement() throws IOException;
+ IXmlWriter closeElement() throws IOException;
/**
* Closes all pending elements.
@@ -97,5 +97,5 @@ public interface IXmlWriter {
* @throws OperationNotAllowedException
* if called before any call to {@link #openElement}
*/
- public IXmlWriter closeDocument() throws IOException;
+ IXmlWriter closeDocument() throws IOException;
}
Modified:
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/impl/base/XmlWriter.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/impl/base/XmlWriter.java?rev=1680062&r1=1680061&r2=1680062&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/impl/base/XmlWriter.java
(original)
+++
creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/xml/writer/impl/base/XmlWriter.java
Mon May 18 18:15:45 2015
@@ -15,21 +15,20 @@
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
- */
+ */
package org.apache.rat.report.xml.writer.impl.base;
+import org.apache.commons.collections.ArrayStack;
+import org.apache.rat.report.xml.writer.IXmlWriter;
+import org.apache.rat.report.xml.writer.InvalidXmlException;
+import org.apache.rat.report.xml.writer.OperationNotAllowedException;
+
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
-import org.apache.commons.collections.ArrayStack;
-
-import org.apache.rat.report.xml.writer.IXmlWriter;
-import org.apache.rat.report.xml.writer.InvalidXmlException;
-import org.apache.rat.report.xml.writer.OperationNotAllowedException;
-
/**
* <p>Lightweight {@link IXmlWriter} implementation.</p>
* <p>
@@ -44,9 +43,9 @@ public final class XmlWriter implements
private static final byte NAME_MASK = 1 << 2;
private static final byte NAME_BODY_CHAR = NAME_MASK;
private static final byte NAME_START_OR_BODY_CHAR = NAME_MASK |
NAME_START_MASK;
-
- private final static boolean[] ALLOWED_CHARACTERS = new boolean[1 << 16];
-
+
+ private final static boolean[] ALLOWED_CHARACTERS = new boolean[1 << 16];
+
static {
Arrays.fill(ALLOWED_CHARACTERS, false);
ALLOWED_CHARACTERS[0x9] = true;
@@ -55,9 +54,9 @@ public final class XmlWriter implements
Arrays.fill(ALLOWED_CHARACTERS, 0x20, 0xD7FF, true);
Arrays.fill(ALLOWED_CHARACTERS, 0xE000, 0xFFFD, true);
}
-
- private final static byte[] CHARACTER_CODES = new byte[1 << 16];
-
+
+ private final static byte[] CHARACTER_CODES = new byte[1 << 16];
+
static {
// Name ::= (Letter | '_' | ':') (NameChar)*
CHARACTER_CODES['_'] = NAME_START_OR_BODY_CHAR;
@@ -399,28 +398,28 @@ public final class XmlWriter implements
Arrays.fill(CHARACTER_CODES, 0x30FC, 0x30FE, NAME_BODY_CHAR);
}
-
+
private final Writer writer;
private final ArrayStack elementNames;
private final Set<CharSequence> currentAttributes = new
HashSet<CharSequence>();
-
+
boolean elementsWritten = false;
boolean inElement = false;
boolean prologWritten = false;
-
+
public XmlWriter(final Writer writer) {
this.writer = writer;
this.elementNames = new ArrayStack();
}
-
+
/**
* Starts a document by writing a prolog.
* Calling this method is optional.
* When writing a document fragment, it should <em>not</em> be called.
+ *
* @return this object
- * @throws OperationNotAllowedException
- * if called after the first element has been written
- * or once a prolog has already been written
+ * @throws OperationNotAllowedException if called after the first element
has been written
+ * or once a prolog has already been
written
*/
public IXmlWriter startDocument() throws IOException {
if (elementsWritten) {
@@ -433,15 +432,14 @@ public final class XmlWriter implements
prologWritten = true;
return this;
}
-
+
/**
* Writes the start of an element.
- *
+ *
* @param elementName the name of the element, not null
- * @return this object
- * @throws InvalidXmlException if the name is not valid for an xml element
- * @throws OperationNotAllowedException
- * if called after the first element has been closed
+ * @return this object
+ * @throws InvalidXmlException if the name is not valid for an
xml element
+ * @throws OperationNotAllowedException if called after the first element
has been closed
*/
public IXmlWriter openElement(final CharSequence elementName) throws
IOException {
if (elementsWritten && elementNames.isEmpty()) {
@@ -461,26 +459,26 @@ public final class XmlWriter implements
currentAttributes.clear();
return this;
}
-
+
/**
* Writes an attribute of an element.
* Note that this is only allowed directly after {@link
#openElement(CharSequence)}
* or {@link #attribute}.
- *
- * @param name the attribute name, not null
+ *
+ * @param name the attribute name, not null
* @param value the attribute value, not null
* @return this object
- * @throws InvalidXmlException if the name is not valid for an xml
attribute
- * or if a value for the attribute has already been written
- * @throws OperationNotAllowedException if called after {@link
#content(CharSequence)}
- * or {@link #closeElement()} or before any call to {@link
#openElement(CharSequence)}
+ * @throws InvalidXmlException if the name is not valid for an
xml attribute
+ * or if a value for the attribute
has already been written
+ * @throws OperationNotAllowedException if called after {@link
#content(CharSequence)}
+ * or {@link #closeElement()} or
before any call to {@link #openElement(CharSequence)}
*/
public IXmlWriter attribute(CharSequence name, CharSequence value) throws
IOException {
if (elementNames.isEmpty()) {
if (elementsWritten) {
throw new OperationNotAllowedException("Root element has
already been closed.");
} else {
- throw new OperationNotAllowedException("Close called before an
element has been opened.");
+ throw new OperationNotAllowedException("Close called before an
element has been opened.");
}
}
if (!isValidName(name)) {
@@ -501,28 +499,27 @@ public final class XmlWriter implements
currentAttributes.add(name);
return this;
}
-
+
private void writeAttributeContent(CharSequence content) throws
IOException {
writeEscaped(content, true);
}
/**
* Writes content.
- * Calling this method will automatically
+ * Calling this method will automatically
* Note that this method does not use CDATA.
- *
+ *
* @param content the content to write
* @return this object
- * @throws OperationNotAllowedException
- * if called before any call to {@link #openElement}
- * or after the first element has been closed
+ * @throws OperationNotAllowedException if called before any call to
{@link #openElement}
+ * or after the first element has
been closed
*/
public IXmlWriter content(CharSequence content) throws IOException {
if (elementNames.isEmpty()) {
if (elementsWritten) {
throw new OperationNotAllowedException("Root element has
already been closed.");
} else {
- throw new OperationNotAllowedException("An element must be
opened before content can be written.");
+ throw new OperationNotAllowedException("An element must be
opened before content can be written.");
}
}
if (inElement) {
@@ -532,14 +529,14 @@ public final class XmlWriter implements
inElement = false;
return this;
}
-
+
private void writeBodyContent(final CharSequence content) throws
IOException {
writeEscaped(content, false);
}
private void writeEscaped(final CharSequence content, boolean
isAttributeContent) throws IOException {
final int length = content.length();
- for (int i=0;i<length;i++) {
+ for (int i = 0; i < length; i++) {
char character = content.charAt(i);
if (character == '&') {
writer.write("&");
@@ -558,7 +555,7 @@ public final class XmlWriter implements
}
}
}
-
+
private boolean isOutOfRange(final char character) {
final boolean result = !ALLOWED_CHARACTERS[character];
return result;
@@ -566,18 +563,17 @@ public final class XmlWriter implements
/**
* Closes the last element written.
- *
+ *
* @return this object
- * @throws OperationNotAllowedException
- * if called before any call to {@link #openElement}
- * or after the first element has been closed
+ * @throws OperationNotAllowedException if called before any call to
{@link #openElement}
+ * or after the first element has
been closed
*/
public IXmlWriter closeElement() throws IOException {
if (elementNames.isEmpty()) {
if (elementsWritten) {
throw new OperationNotAllowedException("Root element has
already been closed.");
} else {
- throw new OperationNotAllowedException("Close called before an
element has been opened.");
+ throw new OperationNotAllowedException("Close called before an
element has been opened.");
}
}
final CharSequence elementName = (CharSequence) elementNames.pop();
@@ -594,43 +590,41 @@ public final class XmlWriter implements
inElement = false;
return this;
}
-
-
+
+
/**
* Closes all pending elements.
* When appropriate, resources are also flushed and closed.
* No exception is raised when called upon a document whose
* root element has already been closed.
+ *
* @return this object
- * @throws OperationNotAllowedException
- * if called before any call to {@link #openElement}
+ * @throws OperationNotAllowedException if called before any call to
{@link #openElement}
*/
public IXmlWriter closeDocument() throws IOException {
- if (elementNames.isEmpty()) {
- if (!elementsWritten) {
- throw new OperationNotAllowedException("Close called before an
element has been opened.");
- }
+ if (elementNames.isEmpty() && !elementsWritten) {
+ throw new OperationNotAllowedException("Close called before an
element has been opened.");
}
- while(!elementNames.isEmpty()) {
+ while (!elementNames.isEmpty()) {
closeElement();
}
writer.flush();
return this;
}
-
+
private void rawWrite(final CharSequence sequence) throws IOException {
- for (int i=0;i<sequence.length();i++) {
+ for (int i = 0; i < sequence.length(); i++) {
final char charAt = sequence.charAt(i);
writer.write(charAt);
}
}
-
+
private boolean isValidName(final CharSequence sequence) {
boolean result = true;
final int length = sequence.length();
- for (int i=0;i<length;i++) {
+ for (int i = 0; i < length; i++) {
char character = sequence.charAt(i);
- if (i==0) {
+ if (i == 0) {
if (!isValidNameStart(character)) {
result = false;
break;
@@ -644,13 +638,13 @@ public final class XmlWriter implements
}
return result;
}
-
+
private boolean isValidNameStart(final char character) {
final byte code = CHARACTER_CODES[character];
final boolean result = (code & NAME_START_MASK) > 0;
return result;
}
-
+
private boolean isValidNameBody(final char character) {
final byte code = CHARACTER_CODES[character];
final boolean result = (code & NAME_MASK) > 0;
Added:
creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/config/ReportFormatTest.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/config/ReportFormatTest.java?rev=1680062&view=auto
==============================================================================
---
creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/config/ReportFormatTest.java
(added)
+++
creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/config/ReportFormatTest.java
Mon May 18 18:15:45 2015
@@ -0,0 +1,47 @@
+package org.apache.rat.config;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @author pottlinger
+ * @version 2015-05-18, 19:52
+ */
+public class ReportFormatTest {
+ @Test
+ public void isANullSafe() {
+ for (String optionType : Arrays.asList(null, "")) {
+ assertFalse("Must not equal PLAIN, was " + optionType,
ReportFormat.PLAIN.is(optionType));
+ }
+ }
+ @Test
+ public void isAConfigurationOption() {
+ for (String optionType : Arrays.asList("PLAIN", "pLain", "plain",
ReportFormat.PLAIN.name())) {
+ assertTrue("Must equal PLAIN, was " + optionType,
ReportFormat.PLAIN.is(optionType));
+ }
+ assertFalse(ReportFormat.PLAIN.is(ReportFormat.XML.name()));
+ }
+}
Modified:
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java?rev=1680062&r1=1680061&r2=1680062&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java
(original)
+++
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java
Mon May 18 18:15:45 2015
@@ -28,6 +28,7 @@ import org.apache.maven.plugins.annotati
import org.apache.rat.Defaults;
import org.apache.rat.ReportConfiguration;
import org.apache.rat.config.AddLicenseHeaders;
+import org.apache.rat.config.ReportFormat;
import org.apache.rat.report.claim.ClaimStatistic;
import java.io.File;
@@ -53,6 +54,7 @@ public class RatCheckMojo extends Abstra
* report or "xml" for the raw XML report. Alternatively you can give the
* path of an XSL transformation that will be applied on the raw XML to
* produce the report written to the output file.
+ *
*/
@Parameter(property = "rat.outputStyle", defaultValue = "plain")
private String reportStyle;
@@ -120,9 +122,9 @@ public class RatCheckMojo extends Abstra
* @see #reportStyle
*/
private InputStream getStyleSheet() throws MojoExecutionException {
- if (reportStyle == null || reportStyle.equals("plain")) {
+ if (reportStyle == null || ReportFormat.PLAIN.is(reportStyle)) {
return Defaults.getPlainStyleSheet();
- } else if (reportStyle.equals("xml")) {
+ } else if (ReportFormat.XML.is(reportStyle)) {
return null;
} else {
try {