Skip to content

Commit

Permalink
Merge pull request joelittlejohn#11 from flibbertigibbet/feature/anno…
Browse files Browse the repository at this point in the history
…tate-format

Feature/annotate format
  • Loading branch information
flibbertigibbet authored Jun 22, 2016
2 parents 668cb46 + 5ecc620 commit 79a2b54
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,54 +95,60 @@ public void propertyField(JFieldVar field, JDefinedClass clazz,
if (propertyNode.has("fieldType")) {
String fieldType = propertyNode.get("fieldType").asText();
FieldTypes foundFieldType = FieldTypes.valueOf(fieldType);

if (foundFieldType != null) {
JAnnotationUse fieldTypeAnnotation = field.annotate(FieldType.class);
fieldTypeAnnotation.param("value", foundFieldType);

/* Additional annotations for reference types. To see how they're built in DRIVER:
* https://github.com/WorldBank-Transport/DRIVER/blob/develop/schema_editor/app/scripts/schemas/schemas-service.js
* NB: in DRIVER usage of json-editor, referenced field is always watch.target,
* enumSource value is always _localId, and id is always the default 'item'.
*/
if (foundFieldType == FieldTypes.reference) {

// annotate with watch target (referenced field)
JsonNode watch = propertyNode.get("watch");
if (watch != null && watch.has("target")) {
String target = watch.get("target").asText();
if (target != null) {
JAnnotationUse watchTargetAnnotation = field.annotate(WatchTarget.class);
watchTargetAnnotation.param("value", target);
} else {
Log.warning("No watch target found for reference type field " + propertyName);
}

JAnnotationUse fieldTypeAnnotation = field.annotate(FieldType.class);
fieldTypeAnnotation.param("value", foundFieldType);

/* Additional annotations for reference types. To see how they're built in DRIVER:
* https://github.com/WorldBank-Transport/DRIVER/blob/develop/schema_editor/app/scripts/schemas/schemas-service.js
* NB: in DRIVER usage of json-editor, referenced field is always watch.target,
* enumSource value is always _localId, and id is always the default 'item'.
*/
if (foundFieldType == FieldTypes.reference) {

// annotate with watch target (referenced field)
JsonNode watch = propertyNode.get("watch");
if (watch != null && watch.has("target")) {
String target = watch.get("target").asText();
if (target != null) {
JAnnotationUse watchTargetAnnotation = field.annotate(WatchTarget.class);
watchTargetAnnotation.param("value", target);
} else {
Log.warning("No watch/target found on reference type field " + propertyName);
Log.warning("No watch target found for reference type field " + propertyName);
}
} else {
Log.warning("No watch/target found on reference type field " + propertyName);
}

// annotate with label template to use from referenced field
JsonNode enumSources = propertyNode.get("enumSource");
if (enumSources != null && enumSources.isArray()) {
// expect DRIVER enumSources to be a list with single element
JsonNode enumSource = enumSources.get(0);
if (enumSource != null && enumSource.has("title")) {
String titlePattern = enumSource.get("title").asText();
if (titlePattern != null) {
JAnnotationUse titlePatternAnnotation = field.annotate(ReferenceTitlePattern.class);
titlePatternAnnotation.param("value", titlePattern);
} else {
Log.info("No title pattern set for reference type field " + propertyName);
}
// annotate with label template to use from referenced field
JsonNode enumSources = propertyNode.get("enumSource");
if (enumSources != null && enumSources.isArray()) {
// expect DRIVER enumSources to be a list with single element
JsonNode enumSource = enumSources.get(0);
if (enumSource != null && enumSource.has("title")) {
String titlePattern = enumSource.get("title").asText();
if (titlePattern != null) {
JAnnotationUse titlePatternAnnotation = field.annotate(ReferenceTitlePattern.class);
titlePatternAnnotation.param("value", titlePattern);
} else {
Log.warning("No enumSource/title found for reference type field " + propertyName);
Log.info("No title pattern set for reference type field " + propertyName);
}
} else {
Log.warning("No enumSources found for reference type field " + propertyName);
Log.warning("No enumSource/title found for reference type field " + propertyName);
}
} else {
Log.warning("No enumSources found for reference type field " + propertyName);
}
} else {
Log.info("No field type found for " + fieldType);
}
}

if (propertyNode.has("format")) {
String format = propertyNode.get("format").asText();
FieldFormats foundFormat = FieldFormats.valueOf(format);

JAnnotationUse fieldFormatAnnotation = field.annotate(FieldFormat.class);
if (!format.isEmpty()) {
fieldFormatAnnotation.param("value", foundFormat);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright © 2010-2014 Nokia
*
* 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 org.jsonschema2pojo.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldFormat {
FieldFormats value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright © 2010-2014 Nokia
*
* 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 org.jsonschema2pojo.annotations;

/**
* For annotating with JSON schema field formats, as used by json-editor.
*
* Created by kat on 6/22/16.
*/
public enum FieldFormats {
// text type formats
color, date, datetime, datetimelocal, email, html, month, integer, number, range, tel, text, textarea, time, url, week,
// other type formats
checkbox, table, select, grid, location
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.jsonschema2pojo.annotations;

/**
* For annotating with JSON schema field types.
*
* Created by kat on 11/22/15.
*/
public enum FieldTypes {
text, image, selectlist, reference
text, image, selectlist, reference, integer, string, object, booleantype, arraytype
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package org.jsonschema2pojo.rules;

import java.math.BigDecimal;
import java.net.URI;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Pattern;

import com.sun.codemodel.JCodeModel;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
Expand Down Expand Up @@ -81,55 +83,83 @@ protected FormatRule(RuleFactory ruleFactory) {
*/
@Override
public JType apply(String nodeName, JsonNode node, JType baseType, Schema schema) {

String nodeText = node.asText();

if (node.asText().equals("date-time")) {
if (nodeText.equals("date-time")) {
return baseType.owner().ref(getDateTimeType());

} else if (node.asText().equals("date")) {
} else if (nodeText.equals("date")) {
return baseType.owner().ref(getDateOnlyType());

} else if (node.asText().equals("time")) {
} else if (nodeText.equals("time")) {
return baseType.owner().ref(getTimeOnlyType());

} else if (node.asText().equals("utc-millisec")) {
} else if (nodeText.equals("utc-millisec")) {
return unboxIfNecessary(baseType.owner().ref(Long.class), ruleFactory.getGenerationConfig());

} else if (node.asText().equals("regex")) {
} else if (nodeText.equals("regex")) {
return baseType.owner().ref(Pattern.class);

} else if (node.asText().equals("color")) {
} else if (nodeText.equals("color")) {
return baseType.owner().ref(String.class);

} else if (node.asText().equals("style")) {
} else if (nodeText.equals("style")) {
return baseType.owner().ref(String.class);

} else if (node.asText().equals("phone")) {
} else if (nodeText.equals("phone")) {
return baseType.owner().ref(String.class);

} else if (node.asText().equals("uri")) {
} else if (nodeText.equals("uri")) {
return baseType.owner().ref(URI.class);

} else if (node.asText().equals("email")) {
} else if (nodeText.equals("email")) {
return baseType.owner().ref(String.class);

} else if (node.asText().equals("ip-address")) {
} else if (nodeText.equals("ip-address")) {
return baseType.owner().ref(String.class);

} else if (node.asText().equals("ipv6")) {
} else if (nodeText.equals("ipv6")) {
return baseType.owner().ref(String.class);

} else if (node.asText().equals("host-name")) {
} else if (nodeText.equals("host-name")) {
return baseType.owner().ref(String.class);
}
else if (node.asText().equals("uuid")) {
return baseType.owner().ref(UUID.class);
} else if (nodeText.equals("uuid")) {
return baseType.owner().ref(UUID.class);
} else if (nodeText.equals("number")) {
return getNumberType(baseType.owner());
} else if (nodeText.equals("integer")) {
return getIntegerType(baseType.owner(), node);
}
else {
return baseType;
}

}

private JType getNumberType(JCodeModel owner) {
GenerationConfig config = ruleFactory.getGenerationConfig();
if (config.isUseBigDecimals()) {
return unboxIfNecessary(owner.ref(BigDecimal.class), config);
} else if (config.isUseDoubleNumbers()) {
return unboxIfNecessary(owner.ref(Double.class), config);
} else {
return unboxIfNecessary(owner.ref(Float.class), config);
}
}

private JType getIntegerType(JCodeModel owner, JsonNode node) {
GenerationConfig config = ruleFactory.getGenerationConfig();
if (config.isUseLongIntegers() ||
(node.has("minimum") && node.get("minimum").isLong()) ||
(node.has("maximum") && node.get("maximum").isLong())) {
return unboxIfNecessary(owner.ref(Long.class), config);
} else {
return unboxIfNecessary(owner.ref(Integer.class), config);
}

}

private Class<?> getDateTimeType() {
String type=ruleFactory.getGenerationConfig().getDateTimeType();
if (!isEmpty(type)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.lang3.text.WordUtils;
import org.jsonschema2pojo.GenerationConfig;

import static javax.lang.model.SourceVersion.isIdentifier;
import static javax.lang.model.SourceVersion.isKeyword;
import static org.apache.commons.lang3.StringUtils.capitalize;
import static org.apache.commons.lang3.StringUtils.containsAny;
Expand All @@ -36,6 +37,10 @@ public NameHelper(GenerationConfig generationConfig) {
}

public static String replaceIllegalCharacters(String name) {
if (isIdentifier(name)) {
return name;
}

for (char character: name.toCharArray()) {
if (!Character.isJavaIdentifierPart(character)) {
name = name.replace(String.valueOf(character), "_");
Expand Down

0 comments on commit 79a2b54

Please sign in to comment.