Skip to content

Commit

Permalink
Handled comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
lbellonda committed Feb 1, 2018
1 parent f9fb96c commit 90057a0
Show file tree
Hide file tree
Showing 69 changed files with 432 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,20 @@ public class CPMojo extends AbstractMojo {
*/
protected boolean errors = true;

/**
* Flag to skip comments rewriting, if true no comments will be generated
*
* @parameter skipComments, defaultValue = false
*/
protected boolean skipComments = false;

/**
* Flag to avoid output, if true no output will be generated
*
* @parameter errors, defaultValue = false
*/
protected boolean preventOutput = false;

protected boolean preventOutput = false;
/**
* Flag to allow only canonical format using '#' as separator
*
Expand Down Expand Up @@ -183,6 +190,7 @@ private void dumpParameters() {
getLog().debug(" fileNameContainsLocaleCode: " + fileNameContainsLocaleCode);
getLog().debug(" outputFolder: " + outputFolder);
getLog().debug(" baseLocale: " + baseLocale);
getLog().debug(" skipComments: " + skipComments);
getLog().debug(" targetBaseDir: " + targetBaseDir);
getLog().debug(" preventOutput: " + preventOutput);

Expand Down Expand Up @@ -212,6 +220,7 @@ private Configuration createConfiguration() throws MojoFailureException {
configuration.setBaseDir(cleanParam(projectBaseDir));
configuration.setErrors(errors);
configuration.setStrict(strict);
configuration.setSkipComments(skipComments);
configuration.setFileNameContainsLocaleCode(fileNameContainsLocaleCode);
checkEmptyParam(targetBaseDir, "Invalid build directory");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Configuration {
private boolean errors = true;
private boolean preventOutput = false;
private boolean strict = false;
private boolean skipComments = false;

public String getEncoding() {
return encoding;
Expand Down Expand Up @@ -113,4 +114,12 @@ public void setStrict(boolean strict) {
this.strict = strict;
}

public boolean isSkipComments() {
return skipComments;
}

public void setSkipComments(boolean skipComments) {
this.skipComments = skipComments;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ protected List<PError> checkDuplicateItems(DirInfo dirInfo) {
protected List<PError> checkDuplicateItems(FileInfo fileInfo) {
List<PError> errors = new ArrayList<PError>();
for (PropInfo propInfo : fileInfo.getProperties().values()) {
if (!propInfo.getRedefinitions().isEmpty()) {
DuplicateKeyError duplicateKeyError = new DuplicateKeyError(fileInfo.getFolderName(),
fileInfo.getName(), propInfo.getKey(), propInfo.getLineDefined());
for (PropInfo redefinition : propInfo.getRedefinitions()) {
duplicateKeyError.addRedefinition(redefinition.getLineDefined());
if (propInfo.isValue()) {
if (!propInfo.getRedefinitions().isEmpty()) {
DuplicateKeyError duplicateKeyError = new DuplicateKeyError(fileInfo.getFolderName(),
fileInfo.getName(), propInfo.getKey(), propInfo.getLineDefined());
for (PropInfo redefinition : propInfo.getRedefinitions()) {
duplicateKeyError.addRedefinition(redefinition.getLineDefined());
}
errors.add(duplicateKeyError);
}
errors.add(duplicateKeyError);
}
}
return errors;
Expand Down Expand Up @@ -296,15 +298,25 @@ private List<PError> readFile(Configuration configuration, FileInfo fileInfo, Fi
protected ReadInfoResult extractKey(final String input, final boolean strict) {
ReadInfoResult result = new ReadInfoResult();
result.setError(false);
if ((null == input) || input.isEmpty()) {
if (null == input) {
return result;
}
String rInput = input;
if (rInput.charAt(0) == '\ufeff') {
rInput = rInput.substring(1);
if (!rInput.isEmpty()) {
if (rInput.charAt(0) == '\ufeff') {
rInput = rInput.substring(1);
}
}
String input2 = rInput.trim();
if (input2.startsWith("#") || input2.startsWith("!") || input2.isEmpty()) {
if (input2.isEmpty()) {
PropInfoEmpty propInfo = new PropInfoEmpty();
result.setPropInfo(propInfo);
return result;
}
if (input2.startsWith("#") || input2.startsWith("!")) {
PropInfoComment propInfo = new PropInfoComment();
propInfo.setValue(input2.substring(1));
result.setPropInfo(propInfo);
return result;
}
int indexOfSep = input2.indexOf("=");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ public FileInfo(DirInfo newDirInfo) {
}

public void addInfo(PropInfo newModel) {
if (properties.containsKey(newModel.getKey())) {
PropInfo property = properties.get(newModel.getKey());
property.getRedefinitions().add(newModel);
if(newModel.isValue()) {
if (properties.containsKey(newModel.getKey())) {
PropInfo property = properties.get(newModel.getKey());
property.getRedefinitions().add(newModel);
} else {
properties.put(newModel.getKey(), newModel);
itemsSorted.add(newModel);
}
} else {
properties.put(newModel.getKey(), newModel);
itemsSorted.add(newModel);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ public void writeToString(StringBuilder sb) {
sb.append("=");
sb.append(getValue());
}

public boolean isValue() {
return true ;
}

public boolean isComment() {
return false ;
}

public boolean isEmpty() {
return false ;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2018 Luca Bellonda.
*
* Part of the checklocale project
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
* Licensed 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.
*/

package io.github.lbellonda.checklocale.mvnplugin.operation;

public class PropInfoComment extends PropInfo {

@Override
public void writeToString(StringBuilder sb) {
sb.append("#");
sb.append(getValue());
}

@Override
public boolean isValue() {
return false ;
}

@Override
public boolean isComment() {
return true ;
}

@Override
public boolean isEmpty() {
return false ;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2018 Luca Bellonda.
*
* Part of the checklocale project
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
* Licensed 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.
*/

package io.github.lbellonda.checklocale.mvnplugin.operation;

public class PropInfoEmpty extends PropInfo {

@Override
public void writeToString(StringBuilder sb) {
// nothing
}

@Override
public boolean isValue() {
return false ;
}

@Override
public boolean isComment() {
return false ;
}

@Override
public boolean isEmpty() {
return true ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ private void writeInfo(SingleExecution execution, File folder) {

private void writeSingleLocale(SingleExecution execution, File folder, FileInfo fileInfo, FileInfo baseLocale) {
StringBuilder sb = new StringBuilder();
writeSingleLocale(fileInfo, baseLocale, sb);
writeSingleLocale(execution.getConfiguration().getEncoding(), fileInfo, baseLocale, sb,
execution.getConfiguration().isSkipComments());
File outputFile = new File(folder, fileInfo.getName());
try {
FileUtils.writeStringToFile(outputFile, sb.toString(), execution.getConfiguration().getEncoding());
Expand All @@ -83,22 +84,28 @@ private void writeSingleLocale(SingleExecution execution, File folder, FileInfo
}
}

private void writeSingleLocale(FileInfo fileInfo, FileInfo baseLocale, StringBuilder sb) {
sb.append("# automatically written\n");
private void writeSingleLocale(final String encoding, FileInfo fileInfo, FileInfo baseLocale, StringBuilder sb,
final boolean isSkipComments) {
sb.append("# automatically written, encoding:" + encoding + ", baseLocale:"
+ (null == baseLocale ? "unspecified" : baseLocale.getDirInfo().getLocale()) + "\n");
sb.append("# ****existing items\n");
for (PropInfo propInfo : fileInfo.getItemsSorted()) {
writeSingleItem(propInfo, fileInfo, baseLocale, sb);
writeSingleItem(propInfo, fileInfo, baseLocale, sb, isSkipComments);
}
if (fileInfo.hasMissingItems()) {
sb.append("# ****missing items\n");
for (PropInfo missingInfo : fileInfo.getMissingItems().values()) {
writeSingleItem(missingInfo, fileInfo, null, sb);
writeSingleItem(missingInfo, fileInfo, null, sb, false);
}
}

} // writeSingleLocale()

private void writeSingleItem(PropInfo propInfo, FileInfo fileInfo, FileInfo baseLocale, StringBuilder sb) {
private void writeSingleItem(PropInfo propInfo, FileInfo fileInfo, FileInfo baseLocale, StringBuilder sb,
final boolean isSkipComments) {
if (!propInfo.isValue() && isSkipComments) {
return;
}

PropInfo baseItem = null;
if (null != baseLocale) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void testDefaultParameters() throws Exception {
assertEquals("UTF-8", configuration.getEncoding());
assertEquals(".", configuration.getBaseDir());
assertEquals(false, configuration.isFileNameContainsLocaleCode());
assertEquals(false, configuration.isSkipComments());
assertEquals("./target/" + CPMojo.DEFAULT_FOLDER, configuration.getOutputFolder());
assertEquals("", configuration.getBaseLocale());
assertEquals(false, configuration.isPreventOutput());
Expand All @@ -82,6 +83,7 @@ public void testEncoding() throws Exception {
assertEquals("ISO-8859-1", configuration.getEncoding());
assertEquals(".", configuration.getBaseDir());
assertEquals(false, configuration.isFileNameContainsLocaleCode());
assertEquals(false, configuration.isSkipComments());
assertEquals("./target/" + CPMojo.DEFAULT_FOLDER, configuration.getOutputFolder());
assertEquals("", configuration.getBaseLocale());
assertEquals(false, configuration.isPreventOutput());
Expand Down Expand Up @@ -111,6 +113,7 @@ public void testDirectories() throws Exception {
assertEquals("UTF-8", configuration.getEncoding());
assertEquals(".", configuration.getBaseDir());
assertEquals(false, configuration.isFileNameContainsLocaleCode());
assertEquals(false, configuration.isSkipComments());
assertEquals("./target/" + CPMojo.DEFAULT_FOLDER, configuration.getOutputFolder());
assertEquals("", configuration.getBaseLocale());
assertEquals(false, configuration.isPreventOutput());
Expand Down Expand Up @@ -140,6 +143,7 @@ public void testPreventOutput() throws Exception {
assertEquals("UTF-8", configuration.getEncoding());
assertEquals(".", configuration.getBaseDir());
assertEquals(false, configuration.isFileNameContainsLocaleCode());
assertEquals(false, configuration.isSkipComments());
assertEquals("./target/" + CPMojo.DEFAULT_FOLDER, configuration.getOutputFolder());
assertEquals("", configuration.getBaseLocale());
assertEquals(true, configuration.isPreventOutput());
Expand All @@ -155,6 +159,7 @@ public void testFileNameContainsLocaleCode() throws Exception {
assertEquals("UTF-8", configuration.getEncoding());
assertEquals(".", configuration.getBaseDir());
assertEquals(true, configuration.isFileNameContainsLocaleCode());
assertEquals(false, configuration.isSkipComments());
assertEquals("./target/" + CPMojo.DEFAULT_FOLDER, configuration.getOutputFolder());
assertEquals("", configuration.getBaseLocale());
assertEquals(false, configuration.isPreventOutput());
Expand Down Expand Up @@ -185,6 +190,7 @@ public void testErrors() throws Exception {
assertEquals("UTF-8", configuration.getEncoding());
assertEquals(".", configuration.getBaseDir());
assertEquals(false, configuration.isFileNameContainsLocaleCode());
assertEquals(false, configuration.isSkipComments());
assertEquals("./target/" + CPMojo.DEFAULT_FOLDER, configuration.getOutputFolder());
assertEquals("", configuration.getBaseLocale());
assertEquals(false, configuration.isPreventOutput());
Expand Down Expand Up @@ -213,11 +219,27 @@ public void testStrict() throws Exception {
assertEquals("UTF-8", configuration.getEncoding());
assertEquals(".", configuration.getBaseDir());
assertEquals(false, configuration.isFileNameContainsLocaleCode());
assertEquals(false, configuration.isSkipComments());
assertEquals("./target/" + CPMojo.DEFAULT_FOLDER, configuration.getOutputFolder());
assertEquals("", configuration.getBaseLocale());
assertEquals(false, configuration.isPreventOutput());
assertEquals(true, configuration.isStrict());
compareCollections(configuration.getDirectories(), makeList(new String[] { "locale" }));
}

public void testSkipComments() throws Exception {
TestCPMojoConfig mojo = doSetUp();
mojo.setupCfgSkipComments();
Configuration configuration = mojo.execSetup();
assertEquals(true, configuration.isErrors());
assertEquals("UTF-8", configuration.getEncoding());
assertEquals(".", configuration.getBaseDir());
assertEquals(false, configuration.isFileNameContainsLocaleCode());
assertEquals(true, configuration.isSkipComments());
assertEquals("./target/" + CPMojo.DEFAULT_FOLDER, configuration.getOutputFolder());
assertEquals("", configuration.getBaseLocale());
assertEquals(false, configuration.isPreventOutput());
assertEquals(false, configuration.isStrict());
compareCollections(configuration.getDirectories(), makeList(new String[] { "locale" }));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public class TestEndToEnd extends TestCase {
public final static String BASE_REFERENCE = "src/test/resources/endToEnd/reference/";

private void compareDirectoriesRecursive(File dirReference, File dirCandidate) throws IOException {
if (null == dirReference) { // git...

}
File[] filesReference = dirReference.listFiles();
File[] filesCandidate = dirCandidate.listFiles();
if (null == filesReference) { // git...
Expand Down Expand Up @@ -86,7 +83,7 @@ private void runCase(final String caseNo, boolean expectedErrors) throws Excepti
public void testCase0() throws Exception {
runCase("0", false);
}

// no output
public void testCase1() throws Exception {
runCase("1", false);
Expand Down Expand Up @@ -121,4 +118,19 @@ public void testCase6() throws Exception {
public void testCase7() throws Exception {
runCase("7", true);
}

// regular no base locale
public void testCase8() throws Exception {
runCase("8", false);
}

// regular comments
public void testCase9() throws Exception {
runCase("9", false);
}

// regular no comments
public void testCase10() throws Exception {
runCase("10", false);
}
}
Loading

0 comments on commit 90057a0

Please sign in to comment.