Skip to content

Commit

Permalink
Merge pull request #1 from flibbertigibbet/feature/keep-extras-as-ann…
Browse files Browse the repository at this point in the history
…otations

Feature/keep extras as annotations
  • Loading branch information
flibbertigibbet committed Nov 22, 2015
2 parents 16759f1 + ada0b91 commit 5d99c80
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
/**
* 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;

import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Logger;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JAnnotationArrayMember;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.*;
import org.jsonschema2pojo.annotations.*;


/**
* Created by kat on 11/21/15.
*/
public class JsonEditorAnnotator extends AbstractAnnotator {

// as used by json-editor
static int DEFAULT_PROPERTY_ORDER = 1000;

static Logger Log = Logger.getLogger("JsonEditorAnnotator");

@Override
public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) {
JAnnotationArrayMember annotationValue = clazz.annotate(JsonPropertyOrder.class).paramArray("value");

SortedMap<Integer, String> fieldOrders = new TreeMap();

for (Iterator<String> properties = propertiesNode.fieldNames(); properties.hasNext();) {
//annotationValue.param(properties.next());
String propertyName = properties.next();

if (propertyName.equals("_localId")) {
continue;
}

JsonNode element = propertiesNode.findValue(propertyName);
JsonNode propertyOrder = element.findValue("propertyOrder");

int order = DEFAULT_PROPERTY_ORDER;
if (propertyOrder != null) {
if (element != null && element.has("propertyOrder")) {
JsonNode propertyOrder = element.findValue("propertyOrder");
order = propertyOrder.asInt(DEFAULT_PROPERTY_ORDER);
}

Expand All @@ -42,6 +63,66 @@ public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) {
}
}

@Override
public void propertyField(JFieldVar field, JDefinedClass clazz,
String propertyName, JsonNode propertyNode) {

if (propertyNode.has("title")) {
String title = propertyNode.get("title").asText();

if (!title.isEmpty()) {
JAnnotationUse titleAnnotation = field.annotate(Title.class);
titleAnnotation.param("value", title);
}
}

if (propertyNode.has("plural_title")) {
String pluralTitle = propertyNode.get("plural_title").asText();

if (!pluralTitle.isEmpty()) {
JAnnotationUse pluralTitleAnnotation = field.annotate(PluralTitle.class);
pluralTitleAnnotation.param("value", pluralTitle);
}
}

if (propertyNode.has("multiple")) {
Boolean multiple = propertyNode.get("multiple").asBoolean();
JAnnotationUse multipleAnnotation = field.annotate(Multiple.class);
multipleAnnotation.param("value", multiple);
}

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);
} else {
Log.info("No field type found for " + fieldType);
}
}

if (propertyNode.has("options")) {
JsonNode options = propertyNode.get("options");
if (options.has("hidden")) {
Boolean isHidden = options.get("hidden").asBoolean();
JAnnotationUse hiddenAnnotation = field.annotate(IsHidden.class);
hiddenAnnotation.param("value", isHidden);
}
}

if (propertyNode.has("description")) {
String description = propertyNode.get("description").asText();

if (!description.isEmpty()) {
JAnnotationUse descriptionAnnotation = field.annotate(Description.class);
descriptionAnnotation.param("value", description);
}
}

}

@Override
public boolean isAdditionalPropertiesSupported() {
return true;
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 Description {
public String value();
}
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 FieldType {
public FieldTypes value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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;

/**
* Created by kat on 11/22/15.
*/
public enum FieldTypes {
text, image, selectlist, reference
}
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 IsHidden {
public boolean value();
}
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 Multiple {
public boolean value();
}
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 PluralTitle {
public String value();
}
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 Title {
public String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,18 @@ private void propertyAnnotations(String nodeName, JsonNode node, Schema schema,
}

private JsonNode resolveRefs(JsonNode node, Schema parent) {
JsonNode ref = null;
if (node.has("$ref")) {
Schema refSchema = ruleFactory.getSchemaStore().create(parent, node.get("$ref").asText());
ref = node.get("$ref");
} else if (node.has("items")) {
JsonNode itemsNode = node.get("items");
if (itemsNode.has("$ref")) {
ref = itemsNode.get("$ref");
}
}

if (ref != null) {
Schema refSchema = ruleFactory.getSchemaStore().create(parent, ref.asText());
JsonNode refNode = refSchema.getContent();
return resolveRefs(refNode, parent);
} else {
Expand Down

0 comments on commit 5d99c80

Please sign in to comment.