Skip to content

Commit

Permalink
Avoid illegal chars in dir names when creating package names
Browse files Browse the repository at this point in the history
Closes #383
  • Loading branch information
joelittlejohn committed Aug 30, 2015
1 parent b5d9ea6 commit 5f29610
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package org.jsonschema2pojo;

import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JCodeModel;
import static org.apache.commons.lang3.StringUtils.*;

import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -31,18 +29,19 @@
import java.util.Iterator;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isEmpty;

import org.apache.commons.io.FilenameUtils;
import org.jsonschema2pojo.exception.GenerationException;
import org.jsonschema2pojo.rules.RuleFactory;
import org.jsonschema2pojo.util.NameHelper;
import org.jsonschema2pojo.util.URLUtil;

import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JCodeModel;

public class Jsonschema2Pojo {
/**
* Reads the contents of the given source and initiates schema generation.
*
*
* @param config
* the configuration options (including source and target paths,
* and other behavioural options) that will control code
Expand Down Expand Up @@ -71,13 +70,7 @@ public static void generate(GenerationConfig config) throws IOException {
URL source = sources.next();

if (URLUtil.parseProtocol(source.toString()) == URLProtocol.FILE && URLUtil.getFileFromURL(source).isDirectory()) {
generateRecursive(
config,
mapper,
codeModel,
defaultString(config.getTargetPackage()),
Arrays.asList(URLUtil.getFileFromURL(source).listFiles(config.getFileFilter()))
);
generateRecursive(config, mapper, codeModel, defaultString(config.getTargetPackage()), Arrays.asList(URLUtil.getFileFromURL(source).listFiles(config.getFileFilter())));
} else {
mapper.generate(codeModel, getNodeName(source), defaultString(config.getTargetPackage()), source);
}
Expand Down Expand Up @@ -119,9 +112,10 @@ private static void generateRecursive(GenerationConfig config, SchemaMapper mapp
}
}
}

private static String childQualifiedName( String parentQualifiedName, String childSimpleName ) {
return isEmpty(parentQualifiedName) ? childSimpleName : parentQualifiedName + "." + childSimpleName;

private static String childQualifiedName(String parentQualifiedName, String childSimpleName) {
String safeChildName = childSimpleName.replaceAll(NameHelper.ILLEGAL_CHARACTER_REGEX, "_");
return isEmpty(parentQualifiedName) ? safeChildName : parentQualifiedName + "." + safeChildName;
}

private static void removeOldOutput(File targetDirectory) {
Expand All @@ -132,7 +126,7 @@ private static void removeOldOutput(File targetDirectory) {
}
}

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
private static void delete(File f) {
if (f.isDirectory()) {
for (File child : f.listFiles()) {
Expand All @@ -144,9 +138,7 @@ private static void delete(File f) {

private static Annotator getAnnotator(GenerationConfig config) {
AnnotatorFactory factory = new AnnotatorFactory();
return factory.getAnnotator(
factory.getAnnotator(config.getAnnotationStyle()),
factory.getAnnotator(config.getCustomAnnotator()));
return factory.getAnnotator(factory.getAnnotator(config.getAnnotationStyle()), factory.getAnnotator(config.getCustomAnnotator()));
}

private static String getNodeName(URL file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
package org.jsonschema2pojo.util;

import static java.lang.Character.*;
import static javax.lang.model.SourceVersion.isKeyword;
import static javax.lang.model.SourceVersion.*;
import static org.apache.commons.lang3.StringUtils.*;

import com.sun.codemodel.JType;
import org.apache.commons.lang3.text.WordUtils;

import org.jsonschema2pojo.GenerationConfig;

import com.sun.codemodel.JType;

public class NameHelper {

private static final String ILLEGAL_CHARACTER_REGEX = "[^0-9a-zA-Z_$]";
public static final String ILLEGAL_CHARACTER_REGEX = "[^0-9a-zA-Z_$]";

private final GenerationConfig generationConfig;

Expand Down Expand Up @@ -65,7 +65,9 @@ public String capitalizeTrailingWords(String name) {
}

/**
* Convert jsonFieldName into the equivalent Java fieldname by replacing illegal characters and normalizing it.
* Convert jsonFieldName into the equivalent Java fieldname by replacing
* illegal characters and normalizing it.
*
* @param jsonFieldName
* @return
*/
Expand All @@ -84,9 +86,9 @@ public String getPropertyName(String jsonFieldName) {
return jsonFieldName;
}


/**
* Generate setter method name for property.
* Generate setter method name for property.
*
* @param propertyName
* @return
*/
Expand All @@ -101,9 +103,9 @@ public String getSetterName(String propertyName) {
return setterName;
}


/**
* Generate getter method name for property.
*
* @param propertyName
* @param type
* @return
Expand Down

0 comments on commit 5f29610

Please sign in to comment.