Skip to content

Commit

Permalink
Merge pull request #38 from tcmj/version_1_3_5
Browse files Browse the repository at this point in the history
Merge version 1.3.5 into master
  • Loading branch information
tcmj authored Mar 16, 2019
2 parents 72965c5 + 7f21685 commit e0ed556
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 101 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tcmj.pug</groupId>
<artifactId>tcmj-pug-enums</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<groupId>com.tcmj.pug</groupId>
Expand Down
2 changes: 1 addition & 1 deletion builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tcmj.pug</groupId>
<artifactId>tcmj-pug-enums</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<groupId>com.tcmj.pug</groupId>
Expand Down
2 changes: 1 addition & 1 deletion datasources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.tcmj.pug</groupId>
<artifactId>tcmj-pug-enums</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<groupId>com.tcmj.pug</groupId>
Expand Down
13 changes: 12 additions & 1 deletion examples-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tcmj.pug</groupId>
<artifactId>tcmj-pug-enums</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<groupId>com.tcmj.pug</groupId>
Expand Down Expand Up @@ -77,6 +77,17 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<filesets>
<fileset>
<directory>src/main/generated</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>com.tcmj.plugins</groupId>
<artifactId>pug-enum-maven-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion exporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.tcmj.pug</groupId>
<artifactId>tcmj-pug-enums</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<groupId>com.tcmj.pug</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,29 @@ public EnumResult export(EnumResult enumResult) {

Path exportDir = Paths.get(exportPathPrefix, directories);
Path exportPath = Paths.get(exportPathPrefix, directories, fileName);
LOG.info("Writing : {}", exportPath);

Files.createDirectories(exportDir);

String finalContent = Objects.requireNonNull(enumResult.getResultFormatted(), "EnumResult.ResultFormatted");

Files.write(exportPath, finalContent.getBytes(getEncoding(enumResult)));

Path existingFile = appendExistingFile(exportPath, enumResult);
if (Files.exists(exportPath)) {
LOG.warn("File: {}", exportPath);
LOG.warn("...already exists and will not be touched! Consider using maven-clean plugin!");
} else {
LOG.info("Writing : {}", exportPath);
Files.createDirectories(exportDir);
String finalContent = Objects.requireNonNull(enumResult.getResultFormatted(), "EnumResult.ResultFormatted");
Files.write(exportPath, finalContent.getBytes(getEncoding(enumResult)));
appendResultFile(exportPath, enumResult);
}
} catch (Exception e) {
LOG.error("Cannot write Enum '{}' to '{}'", fileName, directories, e);
throw new JavaFileHasNotBeenCreatedException(e);
}
return enumResult;
}

private Path appendExistingFile(Path path, EnumResult enumResult) {
private void appendResultFile(Path path, EnumResult enumResult) {
Path absolutePath = path.toAbsolutePath();
if (Files.isRegularFile(absolutePath)) {
enumResult.addOption(JavaSourceFileExporter.OPTION_RESULT_PATH, absolutePath);
return absolutePath;
}
return null;
}

private Charset getEncoding(EnumResult enumResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,38 @@
import com.tcmj.pug.enums.model.EnumData;

import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/** Extracts some data from the enum source needed by some exporter. */
public class MetaDataExtractor {

/**
* Remove all real javadoc comments (/**).
*/
public static String removeJavadocs(String enumSource) {
return enumSource.replaceAll("/\\*\\*(?s:(?!\\*/).)*\\*/", "");
}

/**
* Remove all slash-star-comments (/*).
*/
public static String removeDocs(String enumSource) {
return enumSource.replaceAll("/\\*(?s:(?!\\*/).)*\\*/", "");
}

/**
* Extract the java package name from a java source class.
* @return null if no package found
*/
public static String getPackageName(String source) {
//todo use regex
int idxPkg = Objects.requireNonNull(source, "source").indexOf("package");
int idxSemi = source.indexOf(';');
if (idxPkg >= 0) {
boolean invalid = !source.substring(0, idxPkg).trim().equals("");
if (idxSemi == -1 || idxPkg > idxSemi || invalid) {
return null;
} else {
return source.substring((idxPkg + 8), idxSemi);
}
}
return null;
Objects.requireNonNull(source, "Cannot get a package from a null input!");
String harmonized = removeDocs(removeJavadocs(source));
return Stream.of(harmonized.split(System.lineSeparator()))
.filter(line -> line.trim().startsWith("package"))
.map(pckg -> {
String tmp = pckg.trim().substring("package ".length()).trim();
return tmp.substring(0, tmp.indexOf(";")).trim();
}).collect(Collectors.joining());
}

/** Simple java class name. eg.: MyEnum */
Expand Down Expand Up @@ -88,13 +99,17 @@ public static String getFileNameSingle(EnumResult enumResult) {

public static String getPackageDirectories(String source) {
String packageName = getPackageName(source);
return getPackageDirectoriesIntern(packageName);
}

private static String getPackageDirectoriesIntern(String packageName) {
return packageName == null ? null : packageName.replace('.', '/').trim();
}

/** Package Directories eg.: com/tcmj/iso */
public static String getPackageDirectories(EnumResult enumResult) {
EnumData enumData = Objects.requireNonNull(enumResult.getData(), "Cannot get EnumData object from EnumResult!");
String javaPackageName = Objects.requireNonNull(enumData.getPackageName(), "EnumData.getPackageName() of EnumResult!");
return javaPackageName.replace('.', '/').trim();
return getPackageDirectoriesIntern(javaPackageName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import com.tcmj.pug.enums.api.EnumExporter;
import com.tcmj.pug.enums.model.EnumData;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.Random;

/**
* Creates java source enum test data in form of String objects. Used to test the {@link
* EnumExporter} implementations.
*/
public class TestDataProvider {

public static final String LINESEP = System.lineSeparator();
private final Random random = new Random();
String[] data = new String[]{
"package one.two.three; public enum SimpleEnum { ONE,TWO,THREE } ",
"public enum NoPckEnum { ONE,TWO,THREE } ",
" package com.tcmj.iso ; import java.util.Date; public enum UnFormat { A, B ,C }",
"package org; public enum Animal { DOG(\"Goldie\"); private final String name; Animal(String name) { this.name = name; } public String getName() { return name; } } "
};
"package one.two.three; public enum SimpleEnum { ONE,TWO,THREE } ",
"public enum NoPckEnum { ONE,TWO,THREE } ",
" package com.tcmj.iso ; import java.util.Date; public enum UnFormat { A, B ,C }",
"package org; public enum Animal { DOG(\"Goldie\"); private final String name; Animal(String name) { this.name = name; } public String getName() { return name; } } "
};

public EnumData getEnumData() {
return new EnumData();
Expand All @@ -36,11 +42,106 @@ public String getExtendedEnum() {
return data[3];
}

/**
* Create a enum text object with a given package.
*/
public String getEnumNamed(String packageName, String className) {
return "package "
+ packageName
+ "; import java.util.Date; public enum "
+ packageName
+ "; import java.util.Date; public enum "
+ className
+ "{ RED,BLUE,GREEN }";
}

/**
* Create a enum text object without the leading java package line.
*/
public String getEnumNamed(String className) {
return
"import java.util.Date; public enum "
+ className
+ "{ RED,BLUE,GREEN }";
}

public String getEnumSimple(String packageName, String classNameSimple) {
return join(System.lineSeparator(),
packageName + ";",
"", //sometimes an additional newline
"import java.util.Date; ",
"import java.util.Objects; ",
"",
"public enum " + classNameSimple + " { ",
" MARS, SNICKERS, TOBLERONE,",
"}");
}

/**
* Generates a complete enum object with a little bit of randomness.
* complete means with package, javadoc.
* randomness means sometimes more or less empty lines and sometimes with subfields
*/
public String getEnumComplete(String packageName, String classNameSimple) {
String enumSubs = random.nextBoolean() ? "(\"value\")" : "";
return join(System.lineSeparator(),
"package " + packageName + ";",
random.nextBoolean() ? null : "", //sometimes an additional newline
"import java.util.Date; ",
"import java.util.Objects; ",
random.nextBoolean() ? null : "",
"/** ",
" * This is a generated Enum object.",
" * @author me",
" */ ",
"public enum " + classNameSimple + " { ",
" RED" + enumSubs + ",",
" BLUE" + enumSubs + ",",
" GREEN" + enumSubs + ";",
"".equals(enumSubs) ? "" : " private final String myField;",
random.nextBoolean() ? null : "",
"".equals(enumSubs) ? "" : " " + classNameSimple + "(String name) {",
"".equals(enumSubs) ? "" : " this.myField = name;",
"".equals(enumSubs) ? "" : " }",
random.nextBoolean() ? null : "",
"".equals(enumSubs) ? "" : " public String getMyField() {",
"".equals(enumSubs) ? "" : " return this.myField;",
"".equals(enumSubs) ? "" : " }",
"}");
}

private String join(final String separator, final String... objects) {
Objects.requireNonNull(separator, "Separator cannot be null");
final StringBuilder result = new StringBuilder();
final Iterator<String> iterator = Arrays.asList(objects).iterator();
while (iterator.hasNext()) {
final String value = iterator.next();
if (value == null) {
continue;
} else {
result.append(value);
}
if (iterator.hasNext()) {
result.append(separator);
}
}
return result.toString();
}

public String getStaticEnumComplete() {
return "/* some licence */" + LINESEP +
"package a.b.c.d;" + LINESEP
+ "/** " + LINESEP + " * some class doc. " + LINESEP + " */" + LINESEP + "public enum MyEnum { " + LINESEP
+ " ONE,TWO,THREE;" + LINESEP +
" private final String myField;" + LINESEP +
" /** " + LINESEP + " * some constructor doc. " + LINESEP + " */" + LINESEP +
" MyEnum(String name) {" + LINESEP +
" //set the field name:" + LINESEP +
" this.myField = name;" + LINESEP +
" }" + LINESEP +
" /** " + LINESEP + " * some getter doc. " + LINESEP + " wo star " + LINESEP + " */" + LINESEP +
" public String getMyField() {" + LINESEP +
" /* here we return! */" + LINESEP +
" return this.myField;" + LINESEP +
" }" + LINESEP +
"}";
}
}
Loading

0 comments on commit e0ed556

Please sign in to comment.