com.googlecode.jsonschema2pojo.rules.FormatRule Maven / Gradle / Ivy
/**
* Copyright © 2010-2011 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 com.googlecode.jsonschema2pojo.rules;
import java.net.URI;
import java.util.Date;
import java.util.regex.Pattern;
import org.codehaus.jackson.JsonNode;
import com.googlecode.jsonschema2pojo.Schema;
import com.sun.codemodel.JType;
/**
* Applies the "format" schema rule.
*
* @see http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.23
*/
public class FormatRule implements SchemaRule {
protected FormatRule() {
}
/**
* Applies this schema rule to take the required code generation steps.
*
* This rule maps format values to Java types:
*
* - "format":"date-time" => {@link java.util.Date}
*
- "format":"date" => {@link String}
*
- "format":"time" => {@link String}
*
- "format":"utc-millisec" =>
long
* - "format":"regex" => {@link java.util.regex.Pattern}
*
- "format":"color" => {@link String}
*
- "format":"style" => {@link String}
*
- "format":"phone" => {@link String}
*
- "format":"uri" => {@link java.net.URI}
*
- "format":"email" => {@link String}
*
- "format":"ip-address" => {@link String}
*
- "format":"ipv6" => {@link String}
*
- "format":"host-name" => {@link String}
*
- other (unrecognised format) => baseType
*
*
* @param nodeName
* the name of the node to which this format is applied
* @param node
* the format node
* @param baseType
* the type which which is being formatted e.g. for
* { "type" : "string", "format" : "uri" }
the
* baseType would be java.lang.String
* @return the Java type that is appropriate for the format value
*/
@Override
public JType apply(String nodeName, JsonNode node, JType baseType, Schema schema) {
if (node.getTextValue().equals("date-time")) {
return baseType.owner().ref(Date.class);
} else if (node.getTextValue().equals("date")) {
return baseType.owner().ref(String.class);
} else if (node.getTextValue().equals("time")) {
return baseType.owner().ref(String.class);
} else if (node.getTextValue().equals("utc-millisec")) {
return baseType.owner().LONG;
} else if (node.getTextValue().equals("regex")) {
return baseType.owner().ref(Pattern.class);
} else if (node.getTextValue().equals("color")) {
return baseType.owner().ref(String.class);
} else if (node.getTextValue().equals("style")) {
return baseType.owner().ref(String.class);
} else if (node.getTextValue().equals("phone")) {
return baseType.owner().ref(String.class);
} else if (node.getTextValue().equals("uri")) {
return baseType.owner().ref(URI.class);
} else if (node.getTextValue().equals("email")) {
return baseType.owner().ref(String.class);
} else if (node.getTextValue().equals("ip-address")) {
return baseType.owner().ref(String.class);
} else if (node.getTextValue().equals("ipv6")) {
return baseType.owner().ref(String.class);
} else if (node.getTextValue().equals("host-name")) {
return baseType.owner().ref(String.class);
} else {
return baseType;
}
}
}